-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
117 lines (102 loc) · 3.26 KB
/
handler.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
'use strict';
const axios = require('axios');
const AWS = require("aws-sdk");
const lambda = new AWS.Lambda({
region: "eu-central-1"
});
const { ROOM_ID, MAP_ID, API_KEY, DOOR_IMAGES, DOOR_POS, PASSWORD } = require("./config");
const getMap = async () => {
const response = await axios.get('https://gather.town/api/getMap', {
params: {
apiKey: API_KEY,
spaceId: ROOM_ID,
mapId: MAP_ID,
}
});
return JSON.parse(JSON.stringify(response.data));
};
const setMap = async (mapData) => {
const response = await axios.post('https://gather.town/api/setMap', {
apiKey: API_KEY,
spaceId: ROOM_ID,
mapId: MAP_ID,
mapContent: mapData
});
return response.data;
};
const replaceItem = (mapData, normal, highlighted) => {
let newMapData = mapData;
// Find the door object and change its image and store the old object
for (let idx = 0; idx < mapData.objects.length; idx++) {
const object = mapData.objects[idx];
if (object.x === DOOR_POS.x && object.y === DOOR_POS.y) {
newMapData.objects[idx].normal = normal;
newMapData.objects[idx].highlighted = highlighted;
}
}
return newMapData;
}
const setImpassableTile = (mapData, active) => {
let newMapData = mapData;
const buf = Uint8Array.from(Buffer.from(mapData.collisions, 'base64'));
buf[DOOR_POS.y * mapData.dimensions[0] + DOOR_POS.x] = active ? 0x01 : 0x00;
mapData.collisions = Buffer.from(buf).toString('base64');
return newMapData;
}
module.exports.submit_password = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const body = JSON.parse(event.body);
console.log(body.password);
const sendResponse = (message, status = 200) => {
const response = {
statusCode: status,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({
message,
}),
};
callback(null, response);
};
if (body.password !== PASSWORD) {
sendResponse('Wrong password!', 403);
return;
}
getMap().then((currentMap) => {
currentMap = replaceItem(currentMap, DOOR_IMAGES.open, DOOR_IMAGES.open);
currentMap = setImpassableTile(currentMap, false);
setMap(currentMap).then((response) => {
console.log(response);
//invoke other function to close door after 5 seconds
const params = {
FunctionName: "gather-api-dev-closeDoor",
InvocationType: "Event",
Payload: JSON.stringify({"change": true})
};
lambda.invoke(params, function(error, data) {
if (error) {
console.error(JSON.stringify(error));
return new Error(`Error printing messages: ${JSON.stringify(error)}`);
} else if (data) {
console.log(data);
}
});
sendResponse('Opened!');
});
});
};
module.exports.close_door = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
setTimeout(() => {
getMap().then((currentMap) => {
currentMap = replaceItem(currentMap, DOOR_IMAGES.closed, DOOR_IMAGES.closed_highlight);
currentMap = setImpassableTile(currentMap, true);
setMap(currentMap).then((response) => {
console.log(response);
callback(null, response);
});
});
}, 5000);
};