-
Notifications
You must be signed in to change notification settings - Fork 4
/
messaging.js
125 lines (103 loc) · 4.11 KB
/
messaging.js
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
const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient();
require('./messaging-patch.js');
let send = undefined;
const TABLE_NAME = "game-session";
const REQUEST_START_OP = "1";
const THROW_OP = "5";
const BLOCK_HIT_OP = "9";
const YOU_WON = "91";
const YOU_LOST = "92";
const PLAYING_OP = "11";
function init(event) {
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint: event.requestContext.domainName + '/' + event.requestContext.stage
});
send = async (connectionId, data) => {
await apigwManagementApi.postToConnection({
ConnectionId: connectionId,
Data: `${data}`
}).promise();
}
}
function getConnections() {
return ddb.scan({
TableName: TABLE_NAME,
}).promise();
}
function getGameSession(playerId) {
return ddb.scan({
TableName: TABLE_NAME,
FilterExpression: "#p1 = :playerId or #p2 = :playerId",
ExpressionAttributeNames: {
"#p1": "player1",
"#p2": "player2"
},
ExpressionAttributeValues: {
":playerId": playerId
}
}).promise();
}
exports.handler = (event, context, callback) => {
console.log("Event received: %j", event);
init(event);
let message = JSON.parse(event.body);
console.log("message: %j", message);
let connectionIdForCurrentRequest = event.requestContext.connectionId;
console.log("Current connection id: " + connectionIdForCurrentRequest);
if (message && message.opcode) {
switch (message.opcode) {
case REQUEST_START_OP:
console.log("opcode 1 hit");
getGameSession(connectionIdForCurrentRequest).then((data) => {
console.log("getGameSession: " + data.Items[0].uuid);
// we check for closed to handle an edge case where if player1 joins and immediately quits,
// we mark closed to make sure a player2 can't join an abandoned game session
var opcodeStart = "0";
if (data.Items[0].gameStatus != "closed" && data.Items[0].player2 != "empty") {
opcodeStart = PLAYING_OP;
}
send(connectionIdForCurrentRequest, '{ "uuid": ' + data.Items[0].uuid + ', "opcode": ' +
opcodeStart + ' }');
});
break;
case THROW_OP:
console.log("opcode 5 hit");
getGameSession(connectionIdForCurrentRequest).then((data) => {
console.log("getGameSession: %j", data.Items[0]);
var sendToConnectionId = connectionIdForCurrentRequest;
if (data.Items[0].player1 == connectionIdForCurrentRequest) {
// request came from player1, just send out to player2
sendToConnectionId = data.Items[0].player2;
} else {
// request came from player2, just send out to player1
sendToConnectionId = data.Items[0].player1;
}
console.log("sending throw message to: " + sendToConnectionId);
send(sendToConnectionId, '{ "uuid": ' + data.Items[0].uuid + ', "opcode": ' +
THROW_OP + ', "message": "other player threw ball" }');
});
break;
case BLOCK_HIT_OP: // block hit, send game over
console.log("opcode 9 hit");
getGameSession(connectionIdForCurrentRequest).then((data) => {
console.log("getGameSession: %j", data.Items[0]);
if (data.Items[0].player1 == connectionIdForCurrentRequest) {
// player1 was the winner
send(data.Items[0].player1, '{ "opcode": ' + YOU_WON + ' }');
send(data.Items[0].player2, '{ "opcode": ' + YOU_LOST + ' }');
} else {
// player2 was the winner
send(data.Items[0].player1, '{ "opcode": ' + YOU_LOST + ' }');
send(data.Items[0].player2, '{ "opcode": ' + YOU_WON + ' }');
}
});
default:
// no default case
}
}
return callback(null, {
statusCode: 200,
});
};