|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { S3Client, GetObjectCommand, ListObjectsV2Command, PutObjectCommand, type _Object } from "@aws-sdk/client-s3"; |
| 4 | +import { Handler } from "aws-lambda"; |
| 5 | +import { GameFactory } from '@abstractplay/gameslib'; |
| 6 | +import { type APGameRecord } from '@abstractplay/recranks'; |
| 7 | +import { gunzipSync, strFromU8 } from "fflate"; |
| 8 | +import { load } from "ion-js"; |
| 9 | + |
| 10 | +const REGION = "us-east-1"; |
| 11 | +const s3 = new S3Client({region: REGION}); |
| 12 | +const DUMP_BUCKET = "abstractplay-db-dump"; |
| 13 | +const REC_BUCKET = "records.abstractplay.com"; |
| 14 | + |
| 15 | +type BasicRec = { |
| 16 | + Item: { |
| 17 | + pk: string; |
| 18 | + sk: string; |
| 19 | + [key: string]: any; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +type GameRec = { |
| 24 | + pk: string; |
| 25 | + sk: string; |
| 26 | + id: string; |
| 27 | + metaGame: string; |
| 28 | + state: string; |
| 29 | + pieInvoked?: boolean; |
| 30 | + players: { |
| 31 | + name: string; |
| 32 | + id: string; |
| 33 | + time: number; |
| 34 | + }[]; |
| 35 | + tournament?: string; |
| 36 | + event?: string; |
| 37 | + [key: string]: any; |
| 38 | +} |
| 39 | + |
| 40 | +type Tournament = { |
| 41 | + pk: string; |
| 42 | + sk: string; |
| 43 | + id: string; |
| 44 | + metaGame: string; |
| 45 | + variants: string[]; |
| 46 | + number: number; |
| 47 | + started: boolean; |
| 48 | + dateCreated: number; |
| 49 | + datePreviousEnded: number; // 0 means either the first tournament or a restart of the series (after it stopped because not enough participants), 3000000000000 means previous tournament still running. |
| 50 | + [key: string]: any; |
| 51 | +}; |
| 52 | + |
| 53 | +type OrgEvent = { |
| 54 | + pk: "ORGEVENT"; |
| 55 | + sk: string; // <eventid> |
| 56 | + name: string; |
| 57 | + description: string; |
| 58 | + organizer: string; |
| 59 | + dateStart: number; |
| 60 | + dateEnd?: number; |
| 61 | + winner?: string[]; |
| 62 | + visible: boolean; |
| 63 | +} |
| 64 | + |
| 65 | +type OrgEventGame = { |
| 66 | + pk: "ORGEVENTGAME"; |
| 67 | + sk: string; // <eventid>#<gameid> |
| 68 | + metaGame: string; |
| 69 | + variants?: string[]; |
| 70 | + round: number; |
| 71 | + gameid: string; |
| 72 | + player1: string; |
| 73 | + player2: string; |
| 74 | + winner?: number[]; |
| 75 | + arbitrated?: boolean; |
| 76 | +}; |
| 77 | + |
| 78 | +export const handler: Handler = async (event: any, context?: any) => { |
| 79 | + // generate file listing |
| 80 | + const recListCmd = new ListObjectsV2Command({ |
| 81 | + Bucket: REC_BUCKET, |
| 82 | + }); |
| 83 | + |
| 84 | + const recList: _Object[] = []; |
| 85 | + try { |
| 86 | + let isTruncatedOuter = true; |
| 87 | + |
| 88 | + while (isTruncatedOuter) { |
| 89 | + const { Contents, IsTruncated: IsTruncatedInner, NextContinuationToken } = |
| 90 | + await s3.send(recListCmd); |
| 91 | + if (Contents === undefined) { |
| 92 | + throw new Error(`Could not list the bucket contents`); |
| 93 | + } |
| 94 | + recList.push(...Contents); |
| 95 | + isTruncatedOuter = IsTruncatedInner || false; |
| 96 | + recListCmd.input.ContinuationToken = NextContinuationToken; |
| 97 | + } |
| 98 | + } catch (err) { |
| 99 | + console.error(err); |
| 100 | + } |
| 101 | + const cmd = new PutObjectCommand({ |
| 102 | + Bucket: REC_BUCKET, |
| 103 | + Key: `_manifest.json`, |
| 104 | + Body: JSON.stringify(recList), |
| 105 | + }); |
| 106 | + const response = await s3.send(cmd); |
| 107 | + if (response["$metadata"].httpStatusCode !== 200) { |
| 108 | + console.log(response); |
| 109 | + } |
| 110 | + console.log("Manifest generated"); |
| 111 | + |
| 112 | + console.log("ALL DONE"); |
| 113 | +}; |
0 commit comments