diff --git a/src/lib/actions.ts b/src/lib/actions.ts index 09faed8..0022990 100644 --- a/src/lib/actions.ts +++ b/src/lib/actions.ts @@ -3,7 +3,7 @@ import {Action} from "./game" import { render } from "./template" export const ActionAnnounceNewGame: Action = { - Before(game) { + async Before(game) { const msg:MessageCreateOptions = { content: render( `new game. players ({{playercount}}) @@ -14,8 +14,6 @@ export const ActionAnnounceNewGame: Action = { players: Object.values(game.players), }), } - return [ - msg - ] + game.sink.SendMessage(msg) }, } diff --git a/src/lib/game.ts b/src/lib/game.ts index decc2a0..5c0a38f 100644 --- a/src/lib/game.ts +++ b/src/lib/game.ts @@ -29,22 +29,23 @@ export interface ActionFactory { // an action is in charge of the entire state transition export interface Action { - Before?(game:Game):MessageCreateOptions[] - Play?(game:Game):MessageCreateOptions[] - After?(game:Game):MessageCreateOptions[] + Before?(game:Game):Promise + Play?(game:Game):Promise + After?(game:Game):Promise } export const PlayActions = async (game:Game, actions:Action[]) => { for(let i = 0; i < actions.length; i++) { const a = actions[i]; + try { [a.Before,a.Play,a.After].forEach(async (x)=>{ if(x){ - const msgs = x(game) - msgs.forEach(async (msg)=>{ - await game.sink.SendMessage(msg).catch(console.log) - }) + await x(game) } }); + } catch(e) { + // we stop running on the first error, aka is play fails, after is not run + } } } @@ -75,11 +76,15 @@ export class Player { // chance you will win your next combat power: number + // custom fields for actions to use + stats: {[index:string]:any} + constructor(name:string) { this.health = 100 this.visibility = 50 this.power = 50 this.name = name + this.stats = {} } // bool is whether or not player is dead after damage