@corsaircoalition/common
Advanced tools
Comparing version 1.3.1 to 1.3.2
@@ -5,12 +5,116 @@ import EventEmitter from "node:events"; | ||
export default class GameState extends EventEmitter { | ||
gameType = "custom"; | ||
gamePhase = "initializing"; | ||
replay_id = ""; | ||
playerIndex = null; | ||
turn = 0; | ||
usernames = null; | ||
scores = null; | ||
won = null; | ||
constructor(botId, enableTurnByTurnUpdates = false) { | ||
super(); | ||
Object.defineProperty(this, "gameType", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "custom" | ||
}); | ||
Object.defineProperty(this, "gamePhase", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "initializing" | ||
}); | ||
Object.defineProperty(this, "replay_id", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: "" | ||
}); | ||
Object.defineProperty(this, "playerIndex", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: null | ||
}); | ||
Object.defineProperty(this, "turn", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: 0 | ||
}); | ||
Object.defineProperty(this, "usernames", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: null | ||
}); | ||
Object.defineProperty(this, "scores", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: null | ||
}); | ||
Object.defineProperty(this, "won", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: null | ||
}); | ||
Object.defineProperty(this, "handleStateUpdates", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: async (data) => { | ||
Log.debugObject('handleStateUpdates', data); | ||
let type = Object.keys(data)[0]; | ||
switch (type) { | ||
case 'connected': | ||
this.gamePhase = "connected"; | ||
this.emit('connected'); | ||
break; | ||
case 'disconnected': | ||
this.gamePhase = "initializing"; | ||
this.emit('disconnected'); | ||
break; | ||
case 'joined': | ||
this.gamePhase = "joined_lobby"; | ||
this.emit('joined'); | ||
break; | ||
case 'left': | ||
this.gamePhase = "connected"; | ||
this.emit('left'); | ||
break; | ||
case 'playing': | ||
this.gamePhase = "playing"; | ||
this.emit('playing'); | ||
break; | ||
case 'game_lost': | ||
this.gamePhase = "connected"; | ||
this.won = false; | ||
this.emit('ended'); | ||
break; | ||
case 'game_won': | ||
this.gamePhase = "connected"; | ||
this.won = true; | ||
this.emit('ended'); | ||
break; | ||
case 'game_start': | ||
this.gamePhase = "playing"; | ||
this.gameType = data.game_start.game_type; | ||
this.replay_id = data.game_start.replay_id; | ||
this.playerIndex = data.game_start.playerIndex; | ||
this.turn = 0; | ||
this.usernames = data.game_start.usernames; | ||
this.scores = null; | ||
this.won = null; | ||
this.emit('game_start', data.game_start.replay_id); | ||
break; | ||
} | ||
this.emit('phase', this.gamePhase); | ||
this.emit('update', this); | ||
} | ||
}); | ||
Object.defineProperty(this, "handleGameUpdates", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: async (data) => { | ||
this.turn = data.turn; | ||
this.scores = data.scores; | ||
this.emit('update', this); | ||
} | ||
}); | ||
Redis.subscribe(botId, "state", this.handleStateUpdates); | ||
@@ -21,57 +125,3 @@ if (enableTurnByTurnUpdates) { | ||
} | ||
handleStateUpdates = async (data) => { | ||
Log.debugObject('handleStateUpdates', data); | ||
let type = Object.keys(data)[0]; | ||
switch (type) { | ||
case 'connected': | ||
this.gamePhase = "connected"; | ||
this.emit('connected'); | ||
break; | ||
case 'disconnected': | ||
this.gamePhase = "initializing"; | ||
this.emit('disconnected'); | ||
break; | ||
case 'joined': | ||
this.gamePhase = "joined_lobby"; | ||
this.emit('joined'); | ||
break; | ||
case 'left': | ||
this.gamePhase = "connected"; | ||
this.emit('left'); | ||
break; | ||
case 'playing': | ||
this.gamePhase = "playing"; | ||
this.emit('playing'); | ||
break; | ||
case 'game_lost': | ||
this.gamePhase = "connected"; | ||
this.won = false; | ||
this.emit('ended'); | ||
break; | ||
case 'game_won': | ||
this.gamePhase = "connected"; | ||
this.won = true; | ||
this.emit('ended'); | ||
break; | ||
case 'game_start': | ||
this.gamePhase = "playing"; | ||
this.gameType = data.game_start.game_type; | ||
this.replay_id = data.game_start.replay_id; | ||
this.playerIndex = data.game_start.playerIndex; | ||
this.turn = 0; | ||
this.usernames = data.game_start.usernames; | ||
this.scores = null; | ||
this.won = null; | ||
this.emit('game_start', data.game_start.replay_id); | ||
break; | ||
} | ||
this.emit('phase', this.gamePhase); | ||
this.emit('update', this); | ||
}; | ||
handleGameUpdates = async (data) => { | ||
this.turn = data.turn; | ||
this.scores = data.scores; | ||
this.emit('update', this); | ||
}; | ||
} | ||
//# sourceMappingURL=gameState.js.map |
@@ -1,5 +0,2 @@ | ||
export default class Log { | ||
static debugEnabled = false; | ||
static outStream = process.stdout; | ||
static errStream = process.stderr; | ||
class Log { | ||
static setOutStream(outStream, errStream) { | ||
@@ -33,2 +30,21 @@ Log.outStream = outStream; | ||
} | ||
Object.defineProperty(Log, "debugEnabled", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: false | ||
}); | ||
Object.defineProperty(Log, "outStream", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: process.stdout | ||
}); | ||
Object.defineProperty(Log, "errStream", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: process.stderr | ||
}); | ||
export default Log; | ||
//# sourceMappingURL=log.js.map |
@@ -5,7 +5,3 @@ import { createClient } from '@redis/client'; | ||
import { later } from './utils.js'; | ||
export default class Redis { | ||
static publisher; | ||
static subscriber; | ||
static EXPIRATION_TIME; | ||
static connectionEventEmitter = new EventEmitter(); | ||
class Redis { | ||
static async initilize(redisConfig) { | ||
@@ -66,3 +62,2 @@ Redis.EXPIRATION_TIME = redisConfig.EXPIRATION_TIME; | ||
} | ||
static redisEventHandler = (type) => (data) => Redis.connectionEventEmitter.emit('status', type, data); | ||
static async ping() { | ||
@@ -140,2 +135,15 @@ await Redis.publisher.ping(); | ||
} | ||
Object.defineProperty(Redis, "connectionEventEmitter", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: new EventEmitter() | ||
}); | ||
Object.defineProperty(Redis, "redisEventHandler", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: (type) => (data) => Redis.connectionEventEmitter.emit('status', type, data) | ||
}); | ||
export default Redis; | ||
//# sourceMappingURL=redis.js.map |
{ | ||
"name": "@corsaircoalition/common", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"description": "common helper modules for a modular generals.io bot framework", | ||
@@ -30,4 +30,4 @@ "main": "out/index.js", | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^3.0.1", | ||
"@types/node": "^20.4.6", | ||
"@sindresorhus/tsconfig": "^4.0.0", | ||
"@types/node": "^20.4.10", | ||
"typescript": "^5.1.6" | ||
@@ -34,0 +34,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41107
591