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

feature: prevent any move completion if game is finished #568

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions game/src/__tests__/control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ describe('control', () => {
'COMMIT',
])
})
it('does not give any moves if game is over', () => {
const s1 = {
...s0,
status: GameStatusEnum.FINISHED,
} as GameStatePlaying
const c1 = control(s1, [], 0)
expect(c1.completion).toStrictEqual([])
})

it('handles a mistake in the flow without an error or overflow', () => {
const f1 = {
Expand Down
36 changes: 20 additions & 16 deletions game/src/control.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { match } from 'ts-pattern'
import { always, head, map, pipe, sum, without } from 'ramda'
import { always, head, map, pipe, sum } from 'ramda'
import { pickFrameFlow } from './board/frame'
import {
Flower,
Expand All @@ -10,8 +10,8 @@ import {
Tableau,
Score,
SettlementRound,
BuildingEnum,
RondelToken,
GameStatusEnum,
} from './types'
import {
completeBuild,
Expand Down Expand Up @@ -66,20 +66,24 @@ const computeFlow = (state: GameStatePlaying) => {

export const control = (state: GameStatePlaying, partial: string[], player?: number): Controls => {
const completion = match(head(partial))
.with(undefined, () => [
...completeUse(state)([]),
...completeBuild(state)([]),
...completeCutPeat(state)([]),
...completeFellTrees(state)([]),
...completeWorkContract(state)([]),
...completeBuyPlot(state)([]),
...completeBuyDistrict(state)([]),
...completeConvert(state)([]),
...completeSettle(state)([]),
...completeWithLaybrother(state)([]),
...completeWithPrior(state)([]),
...completeCommit(state)([]),
])
.with(undefined, () =>
state.status === GameStatusEnum.FINISHED
? []
: [
...completeUse(state)([]),
...completeBuild(state)([]),
...completeCutPeat(state)([]),
...completeFellTrees(state)([]),
...completeWorkContract(state)([]),
...completeBuyPlot(state)([]),
...completeBuyDistrict(state)([]),
...completeConvert(state)([]),
...completeSettle(state)([]),
...completeWithLaybrother(state)([]),
...completeWithPrior(state)([]),
...completeCommit(state)([]),
]
)
// this can be prettier with https://github.com/gvergnaud/ts-pattern/pull/139
// git blame this line and see how it used to be, but i really want something like
// .with([GameCommandEnum.Use, P.rest], complete.USE(state))
Expand Down