diff --git a/README.md b/README.md index 1baf0be..5de961c 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ This is a rewrite of [rodw/age](https://github.com/rodw/age) from coffeescript t 2. Achievement Rules are evaluated on event received rather than on `getPlayerAchievements` 3. Achieved Achievements are also added to the `player.history` 4. Transient rules are not yet implemented (TODO coming soon) -5. History entries are objects with a `timestamp` Date, and Achievements have an `achieved` Date -6. Methods return values rather than replying in Node style callbacks with errors +5. Events are now an object with a `name` and optional `data` +6. History entries are objects with a `timestamp` Date, and Achievements are also added to `player.history` and have an `achieved` Date +7. Methods return values rather than replying in Node style callbacks with errors ## Installation diff --git a/src/@types/index.ts b/src/@types/index.ts index e9338c0..2bf63b2 100644 --- a/src/@types/index.ts +++ b/src/@types/index.ts @@ -14,8 +14,9 @@ export interface Achievement { achieved: Date; } -export interface Event { +export interface Event { name: string | number; + data?: T; } export type HistoryItem = { diff --git a/src/GameEngine.ts b/src/GameEngine.ts index f10adde..28504b2 100644 --- a/src/GameEngine.ts +++ b/src/GameEngine.ts @@ -26,6 +26,10 @@ interface GameEngineProps { * **achievementRules** -- An array of achievement rules (default to empty array) */ achievementRules: AchievementRule[]; + /** + * **players** -- An array of players (default to empty array) + */ + players: Player[]; } type GAME_ENGINE_ACHIEVEMENT_ACHIEVED = 'achievement-achieved'; @@ -72,6 +76,11 @@ export class GameEngine extends EventEmitter { this.datastore = props?.datastore || new MemoryDataStore(); this.achievementRules = props?.achievementRules || this.achievementRules; + // Players aren't stored in GameEngine so loop to the `addPlayer` method + props?.players?.forEach((player) => { + this.addPlayer(player); + }); + this._on = super.on; } @@ -91,9 +100,10 @@ export class GameEngine extends EventEmitter { * @param player Player * @param event Event */ - addEvent(player: Player, eventName: Event['name']): void { - const event: Event = { + addEvent(player: Player, eventName: Event['name'], data?: T): void { + const event: Event = { name: eventName, + data, }; this.datastore.recordEvent(player, event);