-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.ts
73 lines (55 loc) · 1.31 KB
/
model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { NonEmptyArray } from "@grancalavera/ttt-etc"
// aggregate root
export interface Match extends MatchDescription {
readonly state: MatchState
}
export interface MatchDescription {
readonly id: MatchId
readonly owner: Player
}
export type MatchState = New | Challenge | Game | GameOver
export type MatchStateName = MatchState["kind"]
// states
export interface New {
readonly kind: "New"
}
export interface Challenge {
readonly kind: "Challenge"
readonly move: Move
}
export interface Game extends GameBaseState {
readonly kind: "Game"
readonly next: Player
}
export type GameOver = Draw | Victory
export interface Draw extends GameBaseState {
readonly kind: "Draw"
}
export interface Victory extends GameBaseState {
readonly kind: "Victory"
readonly winner: Winner
}
// domain objects
export type Players = [Player, Player]
export type Move = [Player, Position]
export type Moves = NonEmptyArray<Move>
export type Winner = [Player, Position[]]
export interface Player {
readonly id: PlayerId
}
export type PlayerId = Id
export type MatchId = Id
export type Position = number
type Id = string
interface GameBaseState {
readonly players: Players
readonly moves: Moves
}
// support
export const extractMatchDescription = ({
id,
owner,
}: Match): MatchDescription => ({
id,
owner,
})