-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridgeEnums.ts
205 lines (181 loc) · 6.01 KB
/
bridgeEnums.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {
BoardResult,
BoardResultC,
DirectionLetter,
Doubling,
Game,
Player,
PlayerAssignment,
Rank,
Strain,
Suit,
TableAssignment,
TableAssignmentCvt,
} from "./graphql/appsync";
// We cannot take the types from these enumerations, because they are generated
// from the graphql
export const allSuits = ["C", "D", "H", "S"] as const;
export const allStrains = [...allSuits, "NT"] as const;
export const allDoublings = ["NONE", "DOUBLE", "REDOUBLE"] as const;
export const allRanks = [
"TWO",
"THREE",
"FOUR",
"FIVE",
"SIX",
"SEVEN",
"EIGHT",
"NINE",
"TEN",
"JACK",
"QUEEN",
"KING",
"ACE",
] as const;
export const allDirections = ["N", "W", "E", "S"] as const;
export const allMovements = ["MITCHELL", "HOWELL", "RAINBOW"] as const;
// These cannot be made into GraphQL enums, because they are subsets of the integers
// and so must be validated in the resolvers instead
export const allLevels = [1, 2, 3, 4, 5, 6, 7] as const;
export const allWonTrickCounts = [
13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
] as const;
export type Level = (typeof allLevels)[number];
export type WonTrickCount = (typeof allWonTrickCounts)[number];
export const allMadeResults = [1, 2, 3, 4, 5, 6, 7] as const;
export const allDownResults = [
-13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1,
] as const;
export const allResults = [...allMadeResults, ...allDownResults] as const;
export type MadeResult = (typeof allMadeResults)[number];
export type DownResult = (typeof allDownResults)[number];
export type Result = MadeResult | DownResult;
export const wonTrickCountToResult = ({
wonTrickCount,
level,
}: {
wonTrickCount: WonTrickCount;
level: Level;
}): Result =>
(wonTrickCount >= level + 6
? wonTrickCount - 6
: wonTrickCount - level - 6) as Result;
export const resultToWonTrickCount = ({
result,
level,
}: {
result: Result;
level: Level;
}) => {
if (result > 0 && result < level) {
throw new Error(`result ${result} is not possible for level ${level}`);
}
return (result > 0 ? result + 6 : result + level + 6) as WonTrickCount;
};
export const possibleResults = (level: Level): Result[] =>
allWonTrickCounts.map((wonTrickCount) =>
wonTrickCountToResult({ wonTrickCount, level }),
);
// these are the values whereas keys are stored as part of the sortKey
export type UnkeyedPlayerAssignment = Omit<PlayerAssignment, "directionLetter">;
export type UnkeyedPlayer = Omit<Player, "playerId">;
// the rules for board and round integers relative to movement, roundCount, and boardsPerRound
// are too complicated to be put into the type system, so they are validated in the resolvers
// https://docs.google.com/spreadsheets/d/1fotK1vf9V7WYQfU_0_dcIdM79r2DtKcGE9QwbEcJ9sw/edit?usp=sharing
interface BoardAndRound {
board: number;
round: number;
}
interface CurrentAsOf {
currentAsOf: string;
}
interface TableAssignmentVectors {
playerAssignments: PlayerAssignment[];
results: BoardResultC[];
}
interface StrictPlayed {
type: "PLAYED";
level: Level;
strain: Strain;
doubling: Doubling;
declarer: DirectionLetter;
leadRank: Rank;
leadSuit: Suit;
wonTrickCount: WonTrickCount;
}
type LoosePlayed = Partial<StrictPlayed> & { type: "PLAYED" };
export type BoardResultUt =
| { type: "NOT_BID_NOT_PLAYED" }
| { type: "PASSED_OUT" }
| StrictPlayed;
export type BoardResultUl =
| { type: "NOT_BID_NOT_PLAYED" }
| { type: "PASSED_OUT" }
| LoosePlayed;
export type BoardResultUct = BoardResultUt & CurrentAsOf;
// type BoardResultUCL = BoardResultUL & CurrentAsOf;
// type BoardResultL = BoardResultUL & BoardAndRound;
// type BoardResultCL = BoardResultUL & BoardAndRound & CurrentAsOf;
export type BoardResultT = BoardResultUt & BoardAndRound;
export type BoardResultCt = BoardResultUt & BoardAndRound & CurrentAsOf;
export type BoardResultUc = Omit<BoardResultC, "board" | "round">;
export type BoardResultU = Omit<BoardResult, "board" | "round">;
// TableAssignmentCvt is schema-defined
// remove single: tableNumber, currentAsOf, vectors:
export type TableAssignmentUvct = Omit<TableAssignmentCvt, "tableNumber">;
export type TableAssignmentVt = Omit<TableAssignmentCvt, "currentAsOf">;
export type TableAssignmentCt = Omit<
TableAssignmentCvt,
"results" | "playerAssignments"
>;
// remove doubles: tableNumber, currentAsOf, vectors:
export type TableAssignmentUvt = Omit<
TableAssignmentCvt,
"tableNumber" | "currentAsOf"
>;
export type TableAssignmentUct = Omit<
TableAssignmentCvt,
"tableNumber" | "results" | "playerAssignments"
>;
export type TableAssignmentT = Omit<
TableAssignmentCvt,
"currentAsOf" | "results" | "playerAssignments"
>;
// remove all three: tableNumber, currentAsOf, vectors:
export type TableAssignmentUt = Omit<
TableAssignmentCvt,
"tableNumber" | "currentAsOf" | "results" | "playerAssignments"
>;
// tableAssignment is schema-defined
// modify singles: tableNumber, currentAsOf, vectors:
export type TableAssignmentU = Omit<TableAssignment, "tableNumber">;
export type TableAssignmentC = TableAssignment & CurrentAsOf;
export type TableAssignmentV = TableAssignment & TableAssignmentVectors;
// modify doubles: tableNumber, currentAsOf, vectors:
export type TableAssignmentUc = Omit<TableAssignment, "tableNumber"> &
CurrentAsOf;
export type TableAssignmentUv = Omit<TableAssignment, "tableNumber"> &
TableAssignmentVectors;
export type TableAssignmentCv = TableAssignment &
CurrentAsOf &
TableAssignmentVectors;
// modify triples: tableNumber, currentAsOf, vectors:
export type TableAssignmentUcv = Omit<TableAssignment, "tableNumber"> &
CurrentAsOf &
TableAssignmentVectors;
// lambda does not apply currentAsOf; response mapping template does:
export type CreateGameLambdaReturnType = Omit<Game, "tableAssignments"> & {
tableAssignments: (Omit<TableAssignmentVt, "results"> & {
results: BoardResultT[];
})[];
};
export const playedBoardRequiredFields = [
"level",
"strain",
"doubling",
"declarer",
"leadRank",
"leadSuit",
"wonTrickCount",
] as const;
export const anyGameItemLifetimeSeconds = 60 * 60 * 24 * 30; // 30 days