-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
231 lines (205 loc) · 6.72 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const express = require('express')
const session = require('express-session');
const { v4: uuidv4 } = require('uuid');
const PORT = process.env.PORT || 5000;
const sslRedirect = require('heroku-ssl-redirect');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
io.on('connection', (client) => {
client.on('join', function (gameID, playerID, callback) {
const gameExists = Object.keys(gameState).includes(gameID);
const room = io.sockets.adapter.rooms[gameID];
let tabOpen = false;
if(room !== undefined){
const clients = room.sockets
for(const id in clients){
const c = io.sockets.connected[id].playerID;
if(playerID === c){
tabOpen = true;
break;
};
}
}
if(gameExists){
if(tabOpen){
callback({error: 'anotherTabOpen'});
}else{
client.playerID = playerID;
client.join(gameID);
const opponentID = Object.keys(gameState[gameID]['players']).find(k => k !== playerID);
const opponent = gameState[gameID]['players'][opponentID];
if(opponent && opponent['ready'])
client.emit('opponentReady', playerID);
}
}else{
callback({error: 'gameDoesNotExist'});
}
});
client.on('ready', function (data, callback) {
const gameID = data.gameID;
const playerID = data.playerID;
const gameExists = gameState[gameID] !== undefined;
if(gameExists){
if(gameState[gameID]['players'][playerID] === undefined){
const totalHits = data.totalHits;
const playerBoard = data.playerBoard;
gameState[gameID]['players'][playerID] = {};
gameState[gameID]['players'][playerID]['hits'] = totalHits;
gameState[gameID]['players'][playerID]['result'] = -1;
gameState[gameID]['players'][playerID]['ready'] = true;
gameState[gameID]['players'][playerID]['attacks'] = [];
gameState[gameID]['players'][playerID]['playerBoard'] = playerBoard;
gameState['changed'] = true;
client.to(gameID).emit('opponentReady');
}else{
callback({error: 'playersFull'});
}
}else{
callback({error: 'gameDoesNotExist'});
}
});
client.on('attack', function (data, callback) {
const gameID = data.gameID;
const playerID = data.playerID;
const attack = data.attack;
const gameExists = gameState[gameID] !== undefined;
if(gameExists){
const opponentID = Object.keys(gameState[gameID]['players']).find(k => k !== playerID);
const opponent = gameState[gameID]['players'][opponentID];
const player = gameState[gameID]['players'][playerID];
if (opponent && (gameState[gameID]['lastAttack'] === null || gameState[gameID]['lastAttack']['playerID'] !== playerID)) {
const hit = opponent['playerBoard'][attack[0]][attack[1]] > 0;
opponent['playerBoard'][attack[0]][attack[1]] = hit ? -opponent['playerBoard'][attack[0]][attack[1]] : 'miss';
opponent['hits'] -= hit;
if(opponent['hits'] === 0){
player['result'] = 1;
opponent['result'] = 0;
}
attack[2] = hit;
attack[3] = opponentID;
gameState[gameID]['lastAttack'] = { playerID: playerID, attack: attack };
gameState['changed'] = true;
player['attacks'].push(attack);
client.to(gameID).emit('incomingAttack', attack);
callback({hit: hit, error: false});
}
}else{
callback({hit: null, error: 'gameDoesNotExist'});
}
});
client.on('playAgain', function(gameID){
if (gameState[gameID]['lastAttack'] !== null) {
gameState[gameID] = {};
gameState[gameID]['time'] = Date.now();
gameState[gameID]['players'] = {};
gameState[gameID]['lastAttack'] = null;
gameState['changed'] = true;
}
});
});
app.use(sslRedirect());
const fs = require('fs');
const gameState = require('./game.json');
gameState['changed'] = false;
const maxAge = (1000 * 1) *//s
(60 * 1) *
(60 * 0.5) //h remove game from gameState after 30 mins of initialization
async function write() {
const currentTime = Date.now()
const items = Object.keys(gameState);
for (let i of items) {
if (gameState[i]['time'] !== undefined) {
let timestamp = gameState[i]['time'];
if ((currentTime - timestamp) > maxAge) {
delete gameState[i];
}
}
}
if (gameState['changed']) {
console.log("writing...");
fs.writeFile("./game.json", JSON.stringify(gameState), function (err) {
if (err) {
return console.log(err);
}
});
gameState['changed'] = false;
}
}
setInterval(write, 15000);
const maxSessions = 3;
app.use(express.json());
app.use(session({ secret: 'XASDASDA' }));
app.use(express.static(__dirname))
.set('view engine', 'ejs')
.get('/', (req, res) => {
const ssn = req.session;
if (ssn.gamesID !== undefined) {
for (let i = 0; i < ssn.gamesID.length; i++) {
if (gameState[ssn.gamesID[i]] === undefined) {
ssn.gamesID.splice(i, 1);
i--;
}
}
}
res.render('pages/index', {gamesID: ssn.gamesID ? ssn.gamesID : [], maxSessions: maxSessions});
})
.get('/index', (req, res) => res.redirect('/'))
http.listen(PORT, () => {
console.log('listening on *:' + PORT);
});
app.post('/init', (req, res, next) => {
const ssn = req.session;
if (ssn.gamesID === undefined || ssn.gamesID && ssn.gamesID.length < maxSessions) {
const gameID = uuidv4();
if (ssn.gamesID === undefined) {
ssn.gamesID = [gameID];
} else {
ssn.gamesID.push(gameID);
}
gameState[gameID] = {};
gameState[gameID]['time'] = Date.now();
gameState[gameID]['players'] = {};
gameState[gameID]['active'] = {};
gameState[gameID]['lastAttack'] = null;
gameState['changed'] = true;
}
res.send({gamesID: ssn.gamesID });
})
app.get('/play/:gameID', (req, res, next) => {
const { gameID } = req.params;
const gameExists = gameState[gameID] !== undefined;
const ssn = req.session;
if (gameExists) {
if (ssn.playerID) {
if(gameState[gameID]['players'][ssn.playerID] !== undefined){
//Game exists and player has been given a unique id
const player = gameState[gameID]['players'][ssn.playerID];
res.render('pages/index_multiplayer', {
gameID: gameID,
playerID: ssn.playerID,
data: player ? {
playerBoard: player.playerBoard,
ready: player.ready,
started: gameState[gameID]['lastAttack'] !== null,
turn: gameState[gameID]['lastAttack'] ? gameState[gameID]['lastAttack']['playerID'] !== ssn.playerID : true,
result: player.result,
attacks: player.attacks
} : null
});
}else{
res.render('pages/index_multiplayer', { gameID: gameID, playerID: ssn.playerID, data: null });
}
} else if(Object.keys(gameState[gameID]['players']).length < 2){
//Another player joins the game, and has not been given a unique id
const playerID = uuidv4();
ssn.playerID = playerID;
res.render('pages/index_multiplayer', { gameID: gameID, playerID: ssn.playerID, data: null });
} else{
res.redirect('/')
}
} else {
//Link to game does not exist
res.redirect('/')
}
});