-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
180 lines (151 loc) · 5.04 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Imports
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const path = require('path');
var cors = require('cors');
const port = process.env.PORT || 80;
const app = express();
app.use(cors());
var entries = {
}
const server = http.createServer(app, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
// Data
let board = [
{votes: 0, state: ''}, {votes: 0, state: ''}, {votes: 0, state: ''},
{votes: 0, state: ''}, {votes: 0, state: ''}, {votes: 0, state: ''},
{votes: 0, state: ''}, {votes: 0, state: ''}, {votes: 0, state: ''}
]
let time = 0
let dirty = false
let gameActive = false
let collectiveTurn = true
let end = false;
let ending
// Socket
let clients = []
const io = socketIo(server); // < Interesting!
io.on("connection", (socket) => {
console.log("Client connected")
clients.push(socket)
socket.send({type: "status", gameActive: gameActive})
clients.forEach(sock=>sock.send({type: "turn", collectiveTurn: collectiveTurn}))
if(end) {clients.forEach(sock=>sock.send({type: "ending", ending: ending}))}
dirty = true
clients.forEach(sock=>sock.send({type: "entries", entries: entries}))
socket.on("message", (m)=> {
var d = JSON.parse(m)
if(d.type == "vote") {
if(board[d.tile].state == "" && collectiveTurn) {
board[d.tile].votes++
dirty = true
}
}
if(d.type == "start") {
gameActive = true;
time = 10;
clients.forEach(sock=>sock.send({type: "status", gameActive: gameActive}))
}
if(d.type == "ending") {
end = true;
ending = d.ending
board.forEach(t=>t.votes = 0)
clients.forEach(sock=>sock.send({type: "board", board: board}))
clients.forEach(sock=>sock.send({type: "ending", ending: ending}))
clients.forEach(sock=>sock.send({type: "entries", entries: entries}))
}
if(d.type == "admin_vote") {
if(board[d.tile].state == "" && !collectiveTurn) {
board[d.tile].state = "o"
dirty = true
collectiveTurn = true;
time = 10;
clients.forEach(sock=>sock.send({type: "turn", collectiveTurn: collectiveTurn}))
}
}
if(d.type == "entry") {
console.log(JSON.stringify(d))
entries[d.ip] = d
}
if(d.type == "restart") {
board.forEach((t)=>{
t.votes = 0;
t.state = 0;
})
time = 0;
dirty = true;
gameActive = false;
end = false;
ending = "";
collectiveTurn = true;
clients.forEach(sock=>sock.send({type: "status", gameActive: gameActive}))
clients.forEach(sock=>sock.send({type: "turn", collectiveTurn: collectiveTurn}))
clients.forEach(sock=>sock.send({type: "ending", ending: ""}))
}
if(d.type == "reset_entries") {
entries = {}
clients.forEach(sock=>sock.send({type: "entries", entries: entries}))
}
})
socket.on("disconnect", () => {
clients = clients.filter(item => item !== socket)
console.log("Client disconnected");
});
});
const findHighestTile = () => {
var highestVotes = -1
var highestTile = board[0]
board.forEach((tile)=>{
if(tile.votes > highestVotes && tile.state == "") {
highestVotes = tile.votes
highestTile = tile
}
})
return highestTile
}
var previousTime = 0
setInterval(() => {
if(!end) {
if(dirty) {
dirty = false
clients.forEach(sock=>sock.send({type: "board", board: board}))
}
if(time > 0 && gameActive) time --
if(time != previousTime) clients.forEach(sock=>sock.send({type: "time", time: time}))
previousTime = time
if(time == 0 && collectiveTurn && gameActive) {
findHighestTile().state = "x"
board.forEach((x)=>{x.votes = 0})
clients.forEach(sock=>sock.send({type: "board", board: board}))
collectiveTurn = false;
clients.forEach(sock=>sock.send({type: "turn", collectiveTurn: collectiveTurn}))
}
}
}, 1000);
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, '/index.html'));
});
app.get('/build/bundle.js', function(req, res) {
res.sendFile(path.join(__dirname, '/build/bundle.js'));
});
app.get('/build/bundle.css', function(req, res) {
res.sendFile(path.join(__dirname, '/build/bundle.css'));
});
app.get('/global.css', function(req, res) {
res.sendFile(path.join(__dirname, '/global.css'));
});
server.listen(port, () => console.log(`Listening on port ${port}`));
Object.filter = function( obj, predicate) {
let result = {}, key;
for (key in obj) {
if (obj.hasOwnProperty(key) && !predicate(obj[key])) {
result[key] = obj[key];
}
}
return result;
};