-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move InitializeGame and ContextEnhancer into separate files
- Loading branch information
1 parent
17f7bce
commit 6dc3856
Showing
16 changed files
with
182 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Random } from './random'; | ||
import { Events } from './events'; | ||
|
||
/** | ||
* Context API to allow writing custom logs in games. | ||
*/ | ||
export class GameLoggerCtxAPI { | ||
constructor() { | ||
this._payload = undefined; | ||
} | ||
|
||
_api() { | ||
return { | ||
setPayload: payload => { | ||
this._payload = payload; | ||
}, | ||
}; | ||
} | ||
|
||
attach(ctx) { | ||
return { ...ctx, log: this._api() }; | ||
} | ||
|
||
update(state) { | ||
if (this._payload === undefined) { | ||
return state; | ||
} | ||
|
||
// attach the payload to the last log event | ||
let deltalog = state.deltalog; | ||
deltalog[deltalog.length - 1] = { | ||
...deltalog[deltalog.length - 1], | ||
payload: this._payload, | ||
}; | ||
this._payload = undefined; | ||
|
||
return { ...state, deltalog }; | ||
} | ||
|
||
static detach(ctx) { | ||
const { log, ...ctxWithoutLog } = ctx; // eslint-disable-line no-unused-vars | ||
return ctxWithoutLog; | ||
} | ||
} | ||
|
||
/** | ||
* This class is used to attach/detach various utility objects | ||
* onto a ctx, without having to manually attach/detach them | ||
* all separately. | ||
*/ | ||
export class ContextEnhancer { | ||
constructor(ctx, game, player) { | ||
this.random = new Random(ctx); | ||
this.events = new Events(game.flow, player); | ||
this.log = new GameLoggerCtxAPI(); | ||
} | ||
|
||
attachToContext(ctx) { | ||
let ctxWithAPI = this.random.attach(ctx); | ||
ctxWithAPI = this.events.attach(ctxWithAPI); | ||
ctxWithAPI = this.log.attach(ctxWithAPI); | ||
return ctxWithAPI; | ||
} | ||
|
||
static detachAllFromContext(ctx) { | ||
let ctxWithoutAPI = Random.detach(ctx); | ||
ctxWithoutAPI = Events.detach(ctxWithoutAPI); | ||
ctxWithoutAPI = GameLoggerCtxAPI.detach(ctxWithoutAPI); | ||
return ctxWithoutAPI; | ||
} | ||
|
||
_update(state, updateEvents) { | ||
let newState = updateEvents ? this.events.update(state) : state; | ||
newState = this.random.update(newState); | ||
newState = this.log.update(newState); | ||
return newState; | ||
} | ||
|
||
updateAndDetach(state, updateEvents) { | ||
const newState = this._update(state, updateEvents); | ||
newState.ctx = ContextEnhancer.detachAllFromContext(newState.ctx); | ||
return newState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { parse, stringify } from 'flatted'; | ||
import { Random } from './random'; | ||
import { Game } from './game'; | ||
import * as plugins from '../plugins/main'; | ||
import { ContextEnhancer } from './context-enhancer'; | ||
|
||
/** | ||
* InitializeGame | ||
* | ||
* Creates the initial game state. | ||
* | ||
* @param {...object} game - Return value of Game(). | ||
* @param {...object} numPlayers - The number of players. | ||
* @param {...object} multiplayer - Set to true if we are in a multiplayer client. | ||
*/ | ||
export function InitializeGame({ game, numPlayers, setupData }) { | ||
game = Game(game); | ||
|
||
if (!numPlayers) { | ||
numPlayers = 2; | ||
} | ||
|
||
let ctx = game.flow.ctx(numPlayers); | ||
|
||
let seed = game.seed; | ||
if (seed === undefined) { | ||
seed = Random.seed(); | ||
} | ||
ctx._random = { seed }; | ||
|
||
// Pass ctx through all the plugins that want to modify it. | ||
ctx = plugins.ctx.setup(ctx, game); | ||
|
||
// Augment ctx with the enhancers (TODO: move these into plugins). | ||
const apiCtx = new ContextEnhancer(ctx, game, ctx.currentPlayer); | ||
let ctxWithAPI = apiCtx.attachToContext(ctx); | ||
|
||
let initialG = game.setup(ctxWithAPI, setupData); | ||
|
||
// Pass G through all the plugins that want to modify it. | ||
initialG = plugins.G.setup(initialG, ctxWithAPI, game); | ||
|
||
const initial = { | ||
// User managed state. | ||
G: initialG, | ||
// Framework managed state. | ||
ctx: ctx, | ||
// List of {G, ctx} pairs that can be undone. | ||
_undo: [], | ||
// List of {G, ctx} pairs that can be redone. | ||
_redo: [], | ||
// A monotonically non-decreasing ID to ensure that | ||
// state updates are only allowed from clients that | ||
// are at the same version that the server. | ||
_stateID: 0, | ||
// A snapshot of this object so that actions can be | ||
// replayed over it to view old snapshots. | ||
// TODO: This will no longer be necessary once the | ||
// log stops replaying actions (but reads the actual | ||
// game states instead). | ||
_initial: {}, | ||
}; | ||
|
||
let state = game.flow.init({ G: initial.G, ctx: ctxWithAPI }); | ||
|
||
initial.G = state.G; | ||
initial._undo = state._undo; | ||
state = apiCtx.updateAndDetach(state, true); | ||
initial.ctx = state.ctx; | ||
const deepCopy = obj => parse(stringify(obj)); | ||
initial._initial = deepCopy(initial); | ||
|
||
return initial; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.