Skip to content

Commit 6073249

Browse files
committed
p2e
1 parent b53d2d5 commit 6073249

File tree

7 files changed

+76
-19
lines changed

7 files changed

+76
-19
lines changed

deploy/src/stack/discord.ts

+27
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ export class DiscordStack extends cdk.Stack {
188188
rankNodesTable.grantReadData(leaderboardHandler);
189189
rankLeaderboardsTable.grantReadData(leaderboardHandler);
190190

191+
const leaderboardFetchHandler = new lambda.Function(
192+
this,
193+
"leaderboardFetchHandler",
194+
{
195+
runtime: lambda.Runtime.NODEJS_14_X,
196+
code: lambda.Code.fromAsset(
197+
path.join(__dirname, "../../../.layers/leaderboard-fetch")
198+
),
199+
handler: "index.handler",
200+
timeout: cdk.Duration.seconds(10),
201+
memorySize: 256,
202+
environment: {
203+
MINIMUM_LOG_LEVEL: "INFO",
204+
TABLE_NAME_RANKER_BOARDS: rankBoardTable.tableName,
205+
},
206+
}
207+
);
208+
209+
rankBoardTable.grantReadData(leaderboardFetchHandler);
210+
191211
const deferredMessageHandler = new lambda.Function(
192212
this,
193213
"deferredMessageHandler",
@@ -330,6 +350,13 @@ export class DiscordStack extends cdk.Stack {
330350
const discordResource = discordApi.root.addResource("discord");
331351
discordResource.addMethod("POST", discordIntegration);
332352
const experienceResource = leaderboardApi.root.addResource("{experience}");
353+
experienceResource.addMethod(
354+
"GET",
355+
new apigateway.LambdaIntegration(leaderboardFetchHandler),
356+
{
357+
apiKeyRequired: true,
358+
}
359+
);
333360
const leaderboardResource = experienceResource.addResource("leaderboard");
334361
leaderboardResource.addMethod(
335362
"GET",

fakeScores.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22
aws sns publish \
33
--message-group-id 'potato' \
4-
--message '{"boardName":"potato","scoreDeltas":[{"playerId":"dd3cb41b-6428-45b8-81e6-82d57f5a5508","score":[-3]},{"playerId":"7b53dd5f-33b3-4815-be6b-d40d831b365c","score":[31]}]}' \
4+
--message '{"boardName":"potato.v1","scoreDeltas":[{"playerId":"dd3cb41b-6428-45b8-81e6-82d57f5a5508","score": [-1]}]}' \
55
--topic-arn "arn:aws:sns:us-west-2:163871723185:score-fifo-topic-2.fifo"

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
"author": "0xFlick <cmdrnft@protonmail.com>",
66
"license": "MIT",
77
"scripts": {
8-
"build": "yarn discord:build && yarn leaderboard:build && yarn players:build && yarn ingest:build && yarn deferred:build",
8+
"build": "yarn discord:build && yarn leaderboard:build && yarn players:build && yarn ingest:build && yarn deferred:build && yarn leaderboard-fetch:build",
99
"discord:cli": "yarn discord:cli:build && node .layers/discord-cli/index.cjs",
1010
"discord:cli:build": "esbuild src/cli.ts --platform=node --target=node14.19 --bundle --outfile=.layers/discord-cli/index.cjs",
1111
"discord:build": "esbuild src/lambda/discord.ts --platform=node --target=node14.19 --minify --bundle --outfile=.layers/discord/index.js",
1212
"leaderboard:build": "esbuild src/lambda/leaderboard.ts --platform=node --target=node14.19 --minify --bundle --outfile=.layers/leaderboard/index.js",
13+
"leaderboard-fetch:build": "esbuild src/lambda/leaderboard-fetch.ts --platform=node --target=node14.19 --minify --bundle --outfile=.layers/leaderboard-fetch/index.js",
1314
"players:build": "esbuild src/lambda/players.ts --platform=node --target=node14.19 --minify --bundle --outfile=.layers/players/index.js",
1415
"ingest:build": "esbuild src/lambda/ingest.ts --platform=node --target=node14.19 --minify --bundle --outfile=.layers/ingest/index.js",
1516
"deferred:build": "esbuild src/lambda/deferred.ts --platform=node --target=node14.19 --minify --bundle --outfile=.layers/deferred/index.js",

src/lambda/intest.test.ts src/lambda/ingest.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { v4 as createUuid } from "uuid";
33
import { handler } from "./ingest";
44
import { SQSEvent, SNSEventRecord, Context } from "aws-lambda";
55
import { initRanker } from "../ranker";
6-
import { Experiences } from "../commands/leaderboard/common";
76

87
function scoringEvent(
98
records: {
10-
boardName: Experiences;
9+
boardName: string;
1110
scores?: ScoreInput[];
1211
scoreDeltas?: ScoreInput[];
1312
}[]

src/lambda/ingest.ts

-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { ScoreInput, config } from "@0xflicker/ranker";
2-
import { TableScores } from "@0xflicker/ranker/lib/db/dynamodb";
32
import { SQSHandler } from "aws-lambda";
43
import { initRanker } from "../ranker";
54
import { createLogger } from "../utils/logging";
65

7-
const KNOWN_LEADERBOARDS = [Experiences.POTATO];
86
const logger = createLogger();
97

108
export const handler: SQSHandler = async (event) => {
@@ -20,10 +18,6 @@ export const handler: SQSHandler = async (event) => {
2018
scores?: ScoreInput[];
2119
scoreDeltas?: ScoreInput[];
2220
} = JSON.parse(message);
23-
if (!KNOWN_LEADERBOARDS.includes(boardName)) {
24-
logger.info("Unknown leaderboard");
25-
continue;
26-
}
2721
try {
2822
const l = await initRanker(boardName);
2923
if (scores) {

src/lambda/leaderboard-fetch.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { APIGatewayProxyHandler } from "aws-lambda";
2+
import { fetchBoard } from "../ranker";
3+
import { createLogger } from "../utils/logging";
4+
import { TableBoard } from "@0xflicker/ranker/lib/db/dynamodb";
5+
6+
const logger = createLogger();
7+
8+
export const handler: APIGatewayProxyHandler = logger.handler(async (event) => {
9+
if (event.httpMethod !== "GET") {
10+
return {
11+
statusCode: 405,
12+
body: "Method Not Allowed",
13+
};
14+
}
15+
const experience = event.pathParameters?.experience;
16+
if (!experience) {
17+
return {
18+
statusCode: 400,
19+
body: "Bad Request",
20+
};
21+
}
22+
23+
const board = await fetchBoard(experience);
24+
if (!board) {
25+
return {
26+
statusCode: 404,
27+
body: "Not Found",
28+
};
29+
}
30+
return {
31+
statusCode: 200,
32+
headers: {
33+
"Content-Type": "application/json",
34+
},
35+
body: JSON.stringify(transformBoard(board)),
36+
};
37+
});
38+
39+
function transformBoard(board: TableBoard) {
40+
return {
41+
id: board.Name,
42+
displayName: board.Display_Name,
43+
description: board.Description,
44+
};
45+
}

src/ranker/index.ts

-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import Ranker, * as ranker from "@0xflicker/ranker";
22
import { getDb } from "../storage/db/dynamo";
33

4-
// const rankerOptions = {
5-
// [Experiences.POTATO]: {
6-
// rootKey: Experiences.POTATO,
7-
// branchingFactor: 100,
8-
// leaderboardSize: 100,
9-
// scoreRange: [0, 100000],
10-
// },
11-
// };
12-
134
export async function initRanker(name: string) {
145
return await Ranker({
156
db: getDb(),

0 commit comments

Comments
 (0)