Repository navigation
Expand file tree
/
Copy pathbotOutbound.ts
More file actions
274 lines (248 loc) · 7.84 KB
/
Copy pathbotOutbound.ts
File metadata and controls
274 lines (248 loc) · 7.84 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { GetCommand, UpdateCommand } from '@aws-sdk/lib-dynamodb';
import { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs';
import { GameFactory } from '@abstractplay/gameslib';
import { ddbDocClient } from './ddb.js';
import { hydrateGameState } from './gameState.js';
import { signBotPayload } from './botSigning.js';
import { BotRecord, getBotRecord } from './participants.js';
const REGION = 'us-east-1';
const BOT_HTTP_TIMEOUT_MS = 30_000;
const sqsClient = new SQSClient({ region: REGION });
export type BotOutboundChallengeMessage = {
type: 'challenge';
challengeId: string;
metaGame: string;
botId: string;
standing: boolean;
};
export type BotOutboundMoveMessage = {
type: 'move';
metaGame: string;
gameid: string;
botId: string;
};
export type BotOutboundMessage = BotOutboundChallengeMessage | BotOutboundMoveMessage;
type ChallengeRecord = {
metaGame: string;
variants: string[];
clockStart: number;
clockInc: number;
clockMax: number;
challenger: { id: string };
challengees?: { id: string }[];
players?: { id: string }[];
};
type GameRecord = {
id: string;
metaGame: string;
numPlayers: number;
variants?: string[];
players: { id: string; name: string; time?: number }[];
toMove: string | boolean[];
state: string;
};
export type BotPostResult = {
statusCode: number;
reachable: boolean;
};
export async function enqueueBotOutbound(message: BotOutboundMessage): Promise<void> {
const queueUrl = process.env.BOT_OUTBOUND_QUEUE_URL;
if (!queueUrl) {
throw new Error('BOT_OUTBOUND_QUEUE_URL environment variable is not set');
}
await sqsClient.send(
new SendMessageCommand({
QueueUrl: queueUrl,
MessageBody: JSON.stringify(message),
})
);
}
export function getToMovePlayerIds(
game: { toMove: string | boolean[] | null | undefined; players: { id: string }[] },
simultaneous: boolean
): string[] {
const ids: string[] = [];
if (game.toMove === '' || game.toMove === null || game.toMove === undefined) {
return ids;
}
if (simultaneous) {
const toMove = game.toMove as boolean[];
for (let i = 0; i < toMove.length; i++) {
if (toMove[i]) {
ids.push(game.players[i].id);
}
}
} else {
ids.push(game.players[parseInt(game.toMove as string, 10)].id);
}
return ids;
}
function buildChallengersList(challenge: ChallengeRecord, botId: string): string[] {
const ids = new Set<string>();
ids.add(challenge.challenger.id);
challenge.challengees?.forEach(c => {
if (c.id !== botId) {
ids.add(c.id);
}
});
challenge.players?.forEach(p => {
if (p.id !== botId) {
ids.add(p.id);
}
});
return [...ids];
}
export function buildOutChallengePayload(challenge: ChallengeRecord, botId: string) {
return {
verb: 'challenge' as const,
metaGame: challenge.metaGame,
variants: challenge.variants ?? [],
clockStart: challenge.clockStart,
clockInc: challenge.clockInc,
clockMax: challenge.clockMax,
challengers: buildChallengersList(challenge, botId),
};
}
export function buildOutMovePayload(game: GameRecord, botId: string) {
const engine = GameFactory(game.metaGame, game.state);
if (!engine) {
throw new Error(`Unknown metaGame ${game.metaGame}`);
}
const botIndex = game.players.findIndex(p => p.id === botId);
if (botIndex === -1) {
throw new Error(`Bot ${botId} is not a player in game ${game.id}`);
}
const botPlayer = game.players[botIndex];
const moves = engine.moveHistory();
const engineWithContext = engine as { botContext?: () => Record<string, unknown> | null };
const context = typeof engineWithContext.botContext === 'function'
? engineWithContext.botContext()
: null;
const payload: Record<string, unknown> = {
verb: 'move',
metaGame: game.metaGame,
variants: game.variants ?? [],
gameid: game.id,
clockCurr: Math.round((botPlayer.time ?? 0) / 3_600_000),
numPlayers: game.numPlayers,
moves,
};
if (context !== null && context !== undefined) {
payload.context = context;
}
return payload;
}
async function recordBotContact(botId: string, statusCode: number, reachable: boolean): Promise<void> {
await ddbDocClient.send(
new UpdateCommand({
TableName: process.env.ABSTRACT_PLAY_TABLE,
Key: { pk: 'BOT', sk: botId },
ExpressionAttributeValues: {
':ls': Date.now(),
':sc': statusCode,
':ok': reachable && statusCode >= 200 && statusCode < 300,
},
UpdateExpression: 'set lastseen = :ls, lastStatusCode = :sc, operational = :ok',
})
);
}
export async function postToBot(
bot: BotRecord,
body: Record<string, unknown>,
method: 'GET' | 'POST' = 'POST',
expectedStatus = 200
): Promise<BotPostResult> {
const rawBody = method === 'POST' ? JSON.stringify(body) : '';
const headers: Record<string, string> = {
...signBotPayload(rawBody),
};
if (method === 'POST') {
headers['Content-Type'] = 'application/json';
}
let statusCode = 0;
let reachable = false;
try {
const response = await fetch(bot.endpoint, {
method,
headers,
body: method === 'POST' ? rawBody : undefined,
signal: AbortSignal.timeout(BOT_HTTP_TIMEOUT_MS),
});
statusCode = response.status;
reachable = true;
} catch (error) {
console.error(`Failed to reach bot ${bot.sk} at ${bot.endpoint}:`, error);
statusCode = 0;
}
await recordBotContact(bot.sk, statusCode, reachable);
console.log(
`Bot ${bot.sk} ${method} -> status ${statusCode}, reachable=${reachable}, expected=${expectedStatus}`
);
return { statusCode, reachable };
}
export async function loadChallengeRecord(
challengeId: string,
metaGame: string,
standing: boolean
): Promise<ChallengeRecord | undefined> {
const data = await ddbDocClient.send(
new GetCommand({
TableName: process.env.ABSTRACT_PLAY_TABLE,
Key: {
pk: standing ? `STANDINGCHALLENGE#${metaGame}` : 'CHALLENGE',
sk: challengeId,
},
})
);
return data.Item as ChallengeRecord | undefined;
}
export async function loadGameRecord(metaGame: string, gameid: string): Promise<GameRecord | undefined> {
const data = await ddbDocClient.send(
new GetCommand({
TableName: process.env.ABSTRACT_PLAY_TABLE,
Key: {
pk: 'GAME',
sk: `${metaGame}#0#${gameid}`,
},
})
);
const item = data.Item as GameRecord | undefined;
return item !== undefined ? hydrateGameState(item) : undefined;
}
export async function processBotChallengeMessage(message: BotOutboundChallengeMessage): Promise<void> {
const bot = await getBotRecord(message.botId);
if (!bot) {
console.error(`Bot ${message.botId} not found for challenge ${message.challengeId}`);
return;
}
const challenge = await loadChallengeRecord(message.challengeId, message.metaGame, message.standing);
if (!challenge) {
console.log(`Challenge ${message.challengeId} no longer exists; skipping bot notification`);
return;
}
const payload = buildOutChallengePayload(challenge, message.botId);
const result = await postToBot(bot, payload, 'POST', 200);
const accepted = result.statusCode === 200;
const { botRespondToChallenge } = await import('./challenges/authHandlers.js');
await botRespondToChallenge(
message.botId,
message.challengeId,
message.metaGame,
message.standing,
accepted
);
}
export async function processBotMoveMessage(message: BotOutboundMoveMessage): Promise<void> {
const bot = await getBotRecord(message.botId);
if (!bot) {
console.error(`Bot ${message.botId} not found for move notification in game ${message.gameid}`);
return;
}
const game = await loadGameRecord(message.metaGame, message.gameid);
if (!game) {
console.log(`Game ${message.gameid} not found; skipping bot move notification`);
return;
}
const payload = buildOutMovePayload(game, message.botId);
await postToBot(bot, payload, 'POST', 202);
}