-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
47 lines (43 loc) · 1.52 KB
/
run.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
const {WebSocket} = require("ws");
const socket = new WebSocket(`wss://4yyity02md.execute-api.us-east-1.amazonaws.com/ws?token=${token}`);
socket.on('open', function open() {
socket.on('message', async function catchMessage(data) {
try{
let message = JSON.parse(data);
switch (message.event) {
case "challenge":
break;
case "your_turn":
let responseWebSocket = "";
if(Math.floor(Math.random() * 6) >= 2 ){
responseWebSocket = process_action(message, "MOVE")
}else{
responseWebSocket = process_action(message, "SHOOT")
}
socket.send(responseWebSocket);
break;
case "list_users":
break;
case "game_over":
break;
default:
break;
}
}catch(e){
console.log("Error : ",e)
}
});
});
const process_action = (data, action) => {
const directions = ["NOTH","SOUTH","EAST","WEST"]
return JSON.stringify({
action: action,
data: {
"game_id": data.data.game_id,
"turn_token": data.data.turn_token,
"from_row": Math.floor(Math.random() * 18),
"from_col": Math.floor(Math.random() * 18),
"direction":directions[Math.floor(Math.random() * directions.length)]
}
})
}