Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update EntityFactory types + compiling errors #42

Merged
merged 2 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11,284 changes: 5,343 additions & 5,941 deletions package-lock.json

Large diffs are not rendered by default.

98 changes: 49 additions & 49 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
{
"name": "ranvier",
"description": "MUD game engine",
"homepage": "https://ranviermud.com",
"bugs": "https://github.com/Ranvier-TS/core-ts/issues",
"license": "MIT",
"author": "Shawn Biddle (http://shawnbiddle.com), Sean O'Donohue, Brian Nelson",
"version": "3.1.1",
"repository": "github:ranvier-ts/core-ts",
"engines": {
"node": ">= 12.18.2"
},
"scripts": {
"test": "nyc --reporter=html --reporter=text --include=src --all mocha --recursive",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"prepare": "npm run build",
"build": "tsc",
"project": "npx tsc -p tsconfig.json",
"watch": "tsc --watch --project tsconfig.json",
"prepublish": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json"
},
"main": "./dist/cjs/index.js",
"types": "./dist/index.d.ts",
"module": "./dist/esm/index.js",
"directory": [
"./dist/esm"
],
"dependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/commander": "^2.12.2",
"@types/js-yaml": "^3.12.5",
"@types/uuid": "^8.3.0",
"@types/winston": "^2.4.4",
"@types/ws": "^7.4.5",
"bcryptjs": "^2.4.0",
"js-yaml": "^3.12.0",
"pretty-error": "^2.0.2",
"require-dir": "^1.1.0",
"sty": "",
"uuid": "^3.3.2",
"winston": "^2.4.5",
"wrap-ansi": "^2.0.0"
},
"devDependencies": {
"@types/node": "^14.14.2",
"coveralls": "^3.0.2",
"mocha": "^5.2.0",
"nyc": "^13.1.0",
"typescript": "^4.4.4"
}
"name": "ranvier",
"description": "MUD game engine",
"homepage": "https://ranviermud.com",
"bugs": "https://github.com/Ranvier-TS/core-ts/issues",
"license": "MIT",
"author": "Shawn Biddle (http://shawnbiddle.com), Sean O'Donohue, Brian Nelson",
"version": "3.1.1",
"repository": "github:ranvier-ts/core-ts",
"engines": {
"node": ">= 12.18.2"
},
"scripts": {
"test": "nyc --reporter=html --reporter=text --include=src --all mocha --recursive",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"prepare": "npm run build",
"build": "tsc",
"project": "npx tsc -p tsconfig.json",
"watch": "tsc --watch --project tsconfig.json",
"prepublish": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json"
},
"main": "./dist/cjs/index.js",
"types": "./dist/index.d.ts",
"module": "./dist/esm/index.js",
"directory": [
"./dist/esm"
],
"dependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/js-yaml": "^3.12.5",
"@types/uuid": "^8.3.0",
"@types/winston": "^2.4.4",
"@types/ws": "^7.4.5",
"bcryptjs": "^2.4.0",
"commander": "^4.1.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had a compile error from commander in the node_modules. We only use the 'types' in core-ts, so just upgraded to the real package (as the types are just a stub) until the error went away.

"js-yaml": "^3.12.0",
"pretty-error": "^2.0.2",
"require-dir": "^1.1.0",
"sty": "",
"uuid": "^3.3.2",
"winston": "^2.4.5",
"wrap-ansi": "^2.0.0"
},
"devDependencies": {
"@types/node": "^14.14.2",
"coveralls": "^3.0.2",
"mocha": "^5.2.0",
"nyc": "^13.1.0",
"typescript": "^4.4.4"
}
}
3 changes: 2 additions & 1 deletion src/Area.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { AreaFloor } from './AreaFloor';
import { ISerializedEffect } from './Effect';
import { SerializedAttributes } from './EffectableEntity';
import { EntityDefinitionBase } from './EntityFactory';
import { GameEntity } from './GameEntity';
import { IGameState } from './GameState';
import { Metadata } from './Metadatable';
import { Npc } from './Npc';
import { Player } from './Player';
import { Room } from './Room';

export interface IAreaDef {
export interface IAreaDef extends EntityDefinitionBase {
bundle: string;
manifest: IAreaManifest;
quests: string[];
Expand Down
4 changes: 2 additions & 2 deletions src/AreaFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EntityReference } from './EntityReference';
/**
* Stores definitions of items to allow for easy creation/cloning of objects
*/
export class AreaFactory extends EntityFactory<IAreaDef> {
export class AreaFactory extends EntityFactory<Area, IAreaDef> {
/**
* Create a new instance of an area by name. Resulting area will not have
* any of its contained entities (items, npcs, rooms) hydrated. You will
Expand All @@ -27,7 +27,7 @@ export class AreaFactory extends EntityFactory<IAreaDef> {
const area = new Area(definition.bundle, entityRef, definition.manifest);

if (this.scripts.has(entityRef)) {
this.scripts?.get(entityRef)?.attach(area as any);
this.scripts?.get(entityRef)?.attach(area);
}

return area;
Expand Down
1 change: 1 addition & 0 deletions src/AreaManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class AreaManager {
title: 'Placeholder',
description:
'You are not in a valid room. Please contact an administrator.',
entityReference: 'placeholder',
});

this.placeholder.addRoom(placeholderRoom);
Expand Down
22 changes: 15 additions & 7 deletions src/BundleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import { Command, ICommandDef } from './Command';
import { Config } from './Config';
import { Data } from './Data';
import { IEffectDef } from './Effect';
import { EntityFactoryType } from './EntityFactory';
import { EntityDefinitionBase, EntityFactory } from './EntityFactory';
import { EntityLoaderKeys, EntityLoaderRegistry } from './EntityLoaderRegistry';
import { EntityReference } from './EntityReference';
import { EventListeners } from './EventManager';
import { GameEntities, GameEntityDefinition } from './GameEntity';
import { IGameState } from './GameState';
import { Helpfile } from './Helpfile';
import { Logger } from './Logger';
Expand All @@ -21,6 +20,7 @@ import { QuestGoal } from './QuestGoal';
import { QuestReward } from './QuestReward';
import { ISkillOptions, Skill } from './Skill';
import { SkillType } from './SkillType';
import { EffectableEntity } from './EffectableEntity';

export interface IListenersLoader {
listeners: EventListeners;
Expand Down Expand Up @@ -301,6 +301,8 @@ export class BundleManager {
items: [],
npcs: [],
rooms: [],
entityReference: areaName,
id: areaName,
};

const scriptPath = this._getAreaScriptPath(bundle, 'area');
Expand Down Expand Up @@ -376,11 +378,14 @@ export class BundleManager {
* @param {EntityFactory} factory
* @return {Array<string>}
*/
async loadEntities(
async loadEntities<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good use of generics here

TEntity extends EffectableEntity,
TDef extends EntityDefinitionBase
>(
bundle: string,
areaName: string,
type: EntityLoaderKeys,
factory: EntityFactoryType
factory: EntityFactory<TEntity, TDef>
): Promise<string[]> {
const loader = this.loaderRegistry.get(type);
loader.setBundle(bundle);
Expand All @@ -390,7 +395,7 @@ export class BundleManager {
return [];
}

const entities = (await loader.fetchAll()) as GameEntityDefinition[];
const entities: TDef[] = await loader.fetchAll();
if (!entities) {
Logger.warn(`\t\t\t${type} has an invalid value [${entities}]`);
return [];
Expand Down Expand Up @@ -438,8 +443,11 @@ export class BundleManager {
* @param {string} entityRef
* @param {string} scriptPath
*/
loadEntityScript(
factory: EntityFactoryType,
loadEntityScript<
TEntity extends EffectableEntity,
TDef extends EntityDefinitionBase
>(
factory: EntityFactory<TEntity, TDef>,
entityRef: EntityReference,
scriptPath: string
) {
Expand Down
15 changes: 7 additions & 8 deletions src/Character.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {
EffectableEntity,
ISerializedEffectableEntity,
} from './EffectableEntity';
import { ISerializedEffectableEntity } from './EffectableEntity';
import { IItemDef, Item } from './Item';
import { IInventoryDef, Inventory, InventoryFullError } from './Inventory';
import { Metadatable } from './Metadatable';
import { Room } from './Room';
import { Config } from './Config';
import { Party } from './Party';
import { EquipSlotTakenError, EquipAlreadyEquippedError } from './EquipErrors';
import { EntityReference } from './EntityReference';
import { Equipment } from './Equipment';
import { AnyCharacter } from './GameEntity';
import { GameEntity } from './GameEntity';
import { EntityDefinitionBase } from './EntityFactory';

export interface ICharacterConfig extends ISerializedEffectableEntity {
export interface ICharacterConfig
extends ISerializedEffectableEntity,
EntityDefinitionBase {
/** @property {string} name Name shown on look/who/login */
name: string;
/** @property {Inventory} inventory */
Expand Down Expand Up @@ -47,7 +46,7 @@ export interface ISerializedCharacter extends ISerializedEffectableEntity {
* @extends EffectableEntity
* @mixes Metadatable
*/
export class Character extends Metadatable(EffectableEntity) {
export class Character extends GameEntity {
/** @property {string} name Name shown on look/who/login */
name: string;
/** @property {Inventory} inventory */
Expand Down
7 changes: 3 additions & 4 deletions src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { EffectableEntity } from './EffectableEntity';
import { IGameState } from './GameState';
import { Skill } from './Skill';
import { EventListeners } from './EventManager';
import { PlayerOrNpc } from './GameEntity';
import { Character } from './Character';
import { AnyCharacter, PlayerOrNpc } from './GameEntity';

export type AttributesModifier =
| Record<string, (this: Effect, current: number, ...args: any[]) => any>
| ((this: Effect, attrName: string, current: number) => any)
| ((this: Effect, attrName: string, current: number) => any);

/** @typedef EffectModifiers {{attributes: !Object<string,function>}} */
export type EffectModifiers = {
Expand Down Expand Up @@ -377,7 +376,7 @@ export class Effect extends EventEmitter {
modifyOutgoingDamage(
damage: Damage,
currentAmount: number,
target: Character
target: AnyCharacter
) {
const modifier = this.modifiers.outgoingDamage.bind(this);
return modifier(damage, currentAmount, target as PlayerOrNpc);
Expand Down
12 changes: 4 additions & 8 deletions src/EffectList.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Attribute } from './Attribute';
import { Character } from './Character';
import { Damage } from './Damage';
import { Effect, ISerializedEffect } from './Effect';
import { EffectableEntity } from './EffectableEntity';
import { PlayerOrNpc } from './GameEntity';
import { AnyCharacter } from './GameEntity';
import { IGameState } from './GameState';

/**
Expand Down Expand Up @@ -92,10 +91,7 @@ export class EffectList {
) {
const now = Date.now();
const sinceLastTick = now - effect.state.lastTick;
if (
sinceLastTick <
effect.config.tickInterval * 1000
) {
if (sinceLastTick < effect.config.tickInterval * 1000) {
continue;
}
effect.state.lastTick = now;
Expand Down Expand Up @@ -258,7 +254,7 @@ export class EffectList {
evaluateIncomingDamage(
damage: Damage,
currentAmount: number,
_attacker?: Character
_attacker?: AnyCharacter
) {
this.validateEffects();

Expand All @@ -280,7 +276,7 @@ export class EffectList {
evaluateOutgoingDamage(
damage: Damage,
currentAmount: number,
target: Character
target: AnyCharacter
) {
this.validateEffects();

Expand Down
2 changes: 1 addition & 1 deletion src/EffectableEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class EffectableEntity extends EventEmitter {
*/
getBaseAttribute(attrName: string): number {
const attr = this.attributes.get(attrName);
return attr && attr.base || 0;
return (attr && attr.base) || 0;
}

/**
Expand Down
Loading