-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (65 loc) · 2.33 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const robot = require("robotjs");
let app = express();
app.use(bodyParser.urlencoded({extended: false}));
let active = false; //the round (or voting) is open or not
// CONTROLS: To add more controls just change these arrays properly
let counter = [0,0,0,0] //up down left right keep track of the respective votes
let counterMap = ['UP','DOWN','LEFT','RIGHT'] //the text message hotword to trigger those actions
let keyPressMap = ['up','down','left','right'] //the value that is passed into the keyboard interfacer library check https://robotjs.io/docs/syntax#keyboard
//END CONTROLSc
let highest = -1 //highest value in counter
let highestIndex = -1 //the index in which (highest) is stored
var timeCount = 5;
var intervalObj = setInterval(() => intervalNode() , 1000);
function intervalNode(params) {
{
console.log(timeCount)
active = true
timeCount--
console.log("Count:" + timeCount)
if (timeCount <= 0)
{
active = false
for (let i = 0; i < counter.length; i++) { //loop to find the most voted (largest int) int array
const ele = counter[i];
if(ele > highest){
highest = ele
highestIndex = i
}
else if(ele == highest){ //handle ties
if (Math.random() >= 0.5)
highestIndex = i
}
}
console.log(`Most of y'all chose to move : ${counterMap[highestIndex]}`)
robot.keyTap(keyPressMap[highestIndex])
counter = [0,0,0,0]
active = true
timeCount = 5
}
}
}
app.post("/message", function (req, res) {
if(active){
console.log(req.body);
const msg = req.body.Body.toUpperCase()
const succMsg = () => { res.send(`<Response><Message>Your Command "${msg}" was successfully interpreted </Message></Response>`)}
if(counterMap.includes(msg)){
succMsg()
let index = counterMap.indexOf(msg)
counter[index]++
}else{
res.send(`<Response><Message>Your Command "${req.body.Body}" is not valid </Message></Response>`)
}
}else{
res.send(`<Response><Message>Sorry, the round didn't start yet.</Message></Response>`)
}
});
app.get("/", function (req, res) {
res.send("Server Working!");
});
let listener = app.listen(56451, function () {
console.log('Your app is listening on port ' + listener.address().port);
});