-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddbSortkey.ts
167 lines (159 loc) · 4.7 KB
/
ddbSortkey.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
import { allDirections } from "./bridgeEnums";
import { DirectionLetter } from "./graphql/appsync";
export const clubSortKeyPrefix0 = "C";
export const clubDeviceSortKeyPrefix0 = "D";
export const gameSortKeyPrefix0 = "G";
export const userSortKeyPrefix0 = "U";
// shares slot with PlayerAssignment.directionLetter, so must not be N, S, E, or W:
export const resultSortKeyPrefix3 = "R";
export const playerSortKeyPrefix0 = "P";
export const clubDeviceIdFromSortKey = (sortKey: string) => {
if (!sortKey.startsWith(clubDeviceSortKeyPrefix0)) {
return {
error: `Not a clubDeviceItem sortKey; ${sortKey} did not start with ${clubDeviceSortKeyPrefix0}`,
result: "",
};
}
const theSplit = sortKey.split("#");
// G, gameId, tableNumber, direction
if (theSplit.length < 2) {
return {
error: `Not a correct sortKey; ${sortKey} has no hashes.`,
result: "",
};
}
return { result: theSplit[1], error: "" };
};
export const directionLetterFromSortKey = (
sortKey: string,
): { error: string; result: DirectionLetter } => {
if (!sortKey.startsWith(gameSortKeyPrefix0)) {
return {
error: `Not a correct sortKey; ${sortKey} did not start with ${gameSortKeyPrefix0}`,
result: "N" as const,
};
}
const theSplit = sortKey.split("#");
// G, gameId, tableNumber, direction
if (theSplit.length < 4) {
return {
error: `Not a correct sortKey; ${sortKey} has under three hashes.`,
result: "N" as const,
};
}
const directionString = theSplit[3];
if (!allDirections.includes(directionString as DirectionLetter)) {
return {
error: `Not a correct sortKey; ${directionString} is not a direction.`,
result: "N" as const,
};
}
return {
result: directionString as DirectionLetter,
error: "",
};
};
export const gameIdFromSortKey = (sortKey: string) => {
if (!sortKey.startsWith(gameSortKeyPrefix0)) {
return {
result: "",
error: `Not a correct sortKey; ${sortKey} did not start with ${gameSortKeyPrefix0}`,
};
}
const theSplit = sortKey.split("#");
// G, gameId
if (theSplit.length < 2) {
return {
result: "",
error: `Not a correct sortKey; ${sortKey} has no hashes.`,
};
}
return { result: theSplit[1], error: "" };
};
export const playerIdFromSortKey = (sortKey: string) => {
if (!sortKey.startsWith(playerSortKeyPrefix0)) {
return {
result: "",
error: `Not a correct sortKey; ${sortKey} did not start with ${playerSortKeyPrefix0}`,
};
}
const theSplit = sortKey.split("#");
// P, playerId
if (theSplit.length < 2) {
return {
result: "",
error: `Not a correct sortKey; ${sortKey} has no hashes.`,
};
}
// playerIds can (and do) contain hash (#), so must rejoin them:
return { result: theSplit.slice(1, theSplit.length).join("#"), error: "" };
};
export const tableNumberFromSortKey = (sortKey: string) => {
if (!sortKey.startsWith(gameSortKeyPrefix0)) {
return {
result: 0,
error: `Not a correct sortKey; ${sortKey} did not start with ${gameSortKeyPrefix0}`,
};
}
const theSplit = sortKey.split("#");
// G, gameId, tableNumber
if (theSplit.length < 3) {
return {
result: 0,
error: `Not a correct sortKey; ${sortKey} has < 2 hashes.`,
};
}
return { result: +theSplit[2], error: "" };
};
export const boardFromSortKey = (sortKey: string) => {
if (!sortKey.startsWith(gameSortKeyPrefix0)) {
return {
error: `Not a correct sortKey; ${sortKey} did not start with ${gameSortKeyPrefix0}`,
result: 0,
};
}
const theSplit = sortKey.split("#");
// G, gameId, tableNumber, R, boardNumber
if (theSplit.length < 5) {
return {
error: `Not a correct sortKey; ${sortKey} has under four hashes.`,
result: 0,
};
}
if (theSplit[3] !== resultSortKeyPrefix3) {
return {
error: `Not a correct sortKey; ${sortKey[3]} is not resultSortKeyPrefix3: ${resultSortKeyPrefix3}`,
result: 0,
};
}
return {
result: +theSplit[4],
error: "",
};
};
export const roundFromSortKey = (sortKey: string) => {
if (!sortKey.startsWith(gameSortKeyPrefix0)) {
return {
error: `Not a correct sortKey; ${sortKey} did not start with ${gameSortKeyPrefix0}`,
result: 0,
};
}
const theSplit = sortKey.split("#");
// G, gameId, tableNumber, R, boardNumber, roundNumber
if (theSplit.length < 6) {
return {
error: `Not a correct sortKey; ${sortKey} has under five hashes.`,
result: 0,
};
}
if (theSplit[3] !== resultSortKeyPrefix3) {
return {
error: `Not a correct sortKey; ${sortKey[3]} is not resultSortKeyPrefix3: ${resultSortKeyPrefix3}`,
result: 0,
};
}
return {
result: +theSplit[5],
error: "",
};
};