-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda_function.js
More file actions
151 lines (123 loc) · 4.94 KB
/
lambda_function.js
File metadata and controls
151 lines (123 loc) · 4.94 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
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const { DynamoDBDocumentClient, ScanCommand, GetCommand, PutCommand } = require('@aws-sdk/lib-dynamodb');
const client = new DynamoDBClient({});
const dynamoDB = DynamoDBDocumentClient.from(client);
const TABLE_NAME = "SwordFightScores";
async function getLeaderboard() {
const params = {
TableName: TABLE_NAME,
ProjectionExpression: "playerId, score, totalScore, gamesPlayed"
};
const command = new ScanCommand(params);
const result = await dynamoDB.send(command);
const sortedItems = result.Items.sort((a, b) => (b.totalScore || 0) - (a.totalScore || 0));
return sortedItems;
}
// add score to player's total score if playerID already exists
async function updatePlayerScore(playerId, newScore) {
const currentTime = Math.floor(Date.now() / 1000);
const timeLimit = 3600;
const getParams = {
TableName: TABLE_NAME,
Key: { playerId }
};
const getCommand = new GetCommand(getParams);
const existingRecord = await dynamoDB.send(getCommand);
let highestScore = newScore;
let totalScore = newScore;
let gamesPlayed = 1;
if (existingRecord.Item) {
highestScore = Math.max(existingRecord.Item.score || 0, newScore);
totalScore = (existingRecord.Item.totalScore || 0) + newScore;
gamesPlayed = (existingRecord.Item.gamesPlayed || 0) + 1;
}
const updateItem = {
playerId,
score: highestScore,
totalScore: totalScore,
gamesPlayed: gamesPlayed,
timestamp: new Date().toISOString()
};
// TTL if score is zero - expire 1 hour
if (totalScore == 0) {
updateItem.ttl = currentTime + timeLimit;
}
const putParams = {
TableName: TABLE_NAME,
Item: updateItem
};
const putCommand = new PutCommand(putParams);
return dynamoDB.send(putCommand);
}
exports.lambda_handler = async (event) => {
try {
console.log("Received event:", JSON.stringify(event));
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
};
if (event.httpMethod === 'OPTIONS') {
return {
statusCode: 200,
headers,
body: JSON.stringify({ message: "CORS preflight response" })
};
}
if (event.httpMethod === 'GET') {
console.log("Processing GET request");
if (event.queryStringParameters && event.queryStringParameters.action === 'leaderboard') {
console.log("Fetching leaderboard");
const leaderboard = await getLeaderboard();
console.log("Leaderboard fetched:", JSON.stringify(leaderboard));
return {
statusCode: 200,
headers,
body: JSON.stringify({ leaderboard })
};
}
console.log("Invalid GET request - missing action=leaderboard parameter");
return {
statusCode: 400,
headers,
body: JSON.stringify({ error: "Invalid GET request. Use ?action=leaderboard" })
};
}
if (event.httpMethod === 'POST') {
console.log("Processing POST request");
const requestBody = JSON.parse(event.body);
console.log("Request body:", JSON.stringify(requestBody));
if (!requestBody.playerId || requestBody.score === undefined) {
console.log("Invalid POST request - missing required fields");
return {
statusCode: 400,
headers,
body: JSON.stringify({ error: "Missing required fields: playerId and/or score" })
};
}
console.log(`Updating score for player ${requestBody.playerId}: ${requestBody.score}`);
await updatePlayerScore(requestBody.playerId, requestBody.score);
console.log("Score updated successfully");
return {
statusCode: 200,
headers,
body: JSON.stringify({ message: "Score submitted successfully" })
};
}
console.log(`Unsupported HTTP method: ${event.httpMethod}`);
return {
statusCode: 405,
headers,
body: JSON.stringify({ error: "Method not allowed" })
};
} catch (error) {
console.error("Error:", error);
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify({ error: "Internal server error", details: error.message })
};
}
};