Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(phases): Support setting next phase with a function #972

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/core/flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,21 @@ test('init', () => {
expect(state.G).toMatchObject({ done: true });
});

test('next', () => {
const flow = Flow({
phases: {
A: { start: true, next: () => 'C' },
B: {},
C: {},
},
});

let state = { ctx: flow.ctx(3) } as State;
state = flow.processEvent(state, gameEvent('endPhase'));

expect(state.ctx.phase).toEqual('C');
});

describe('endIf', () => {
test('basic', () => {
const flow = Flow({ endIf: (G) => G.win });
Expand Down
9 changes: 8 additions & 1 deletion src/core/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ export function Flow({
onEnd: HookWrapper(phaseConfig.turn.onEnd),
endIf: TriggerWrapper(phaseConfig.turn.endIf),
};

if (typeof phaseConfig.next === 'function') {
phaseConfig.wrapped.next = HookWrapper(phaseConfig.next);
}
delucis marked this conversation as resolved.
Show resolved Hide resolved
}

function GetPhase(ctx: { phase: string }): PhaseConfig {
Expand Down Expand Up @@ -315,7 +319,10 @@ export function Flow({
return state;
}
} else if (phaseConfig.next !== undefined) {
delucis marked this conversation as resolved.
Show resolved Hide resolved
ctx = { ...ctx, phase: phaseConfig.next };
ctx =
typeof phaseConfig.next === 'function'
? { ...ctx, phase: phaseConfig.wrapped.next(state) }
: { ...ctx, phase: phaseConfig.next };
delucis marked this conversation as resolved.
Show resolved Hide resolved
} else {
ctx = { ...ctx, phase: null };
}
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export interface PhaseConfig<
CtxWithPlugins extends Ctx = Ctx
> {
start?: boolean;
next?: string;
next?: ((G: G, ctx: CtxWithPlugins) => any) | string;
delucis marked this conversation as resolved.
Show resolved Hide resolved
onBegin?: (G: G, ctx: CtxWithPlugins) => any;
onEnd?: (G: G, ctx: CtxWithPlugins) => any;
endIf?: (G: G, ctx: CtxWithPlugins) => boolean | void | { next: string };
Expand All @@ -219,6 +219,7 @@ export interface PhaseConfig<
) => boolean | void | { next: string };
onBegin?: (state: State<G, CtxWithPlugins>) => any;
onEnd?: (state: State<G, CtxWithPlugins>) => any;
next?: (state: State<G, CtxWithPlugins>) => any;
delucis marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand Down