Skip to content

Commit

Permalink
Add event data
Browse files Browse the repository at this point in the history
Optional players to game engine constructor
[ci minor]
  • Loading branch information
ChromeQ committed Oct 7, 2023
1 parent 9da81f3 commit 7c8a00f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<T>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

Expand Down
3 changes: 2 additions & 1 deletion src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export interface Achievement {
achieved: Date;
}

export interface Event {
export interface Event<T = unknown> {
name: string | number;
data?: T;
}

export type HistoryItem = {
Expand Down
14 changes: 12 additions & 2 deletions src/GameEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<T = unknown>(player: Player, eventName: Event['name'], data?: T): void {
const event: Event<T> = {
name: eventName,
data,
};

this.datastore.recordEvent(player, event);
Expand Down

0 comments on commit 7c8a00f

Please sign in to comment.