-
Notifications
You must be signed in to change notification settings - Fork 8
/
playersAuth.js
196 lines (169 loc) · 5.06 KB
/
playersAuth.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
module.exports = {
addPlayer: function (playerName) {
return addPlayer(playerName);
},
playerExists: function (playerName) {
return playerExists(playerName);
},
authPlayer: function (playerId, token) {
return authPlayer(playerId, token);
},
auth: function (req, res, next) {
return auth(req, res, next);
},
players: function () {
return players;
},
getLobbyPlayers: function () {
return getLobbyPlayers();
},
getReadyPlayers: function () {
return getReadyPlayers();
},
getPlayerId: function (playerName) {
return getPlayerId(playerName);
},
getPlayerById: function (playerName) {
return getPlayerById(playerName);
},
setIngame: function (playerId, ingame) {
players[playerId].ingame = ingame;
},
setReady: function (playerId, ready) {
players[playerId].ready = ready;
},
setInLobby: function (playerId, inLobby) {
players[playerId].inLobby = inLobby;
},
setSpectating: function (playerId, spectating) {
players[playerId].spectating = spectating;
},
setLobbyCallback: function (cb) {
updateLobbyCallback = cb;
},
playerActive: function (playerId) {
return playerActive(playerId);
},
addPlayerToLobby: function (playerId) {
return addPlayerToLobby(playerId);
},
chatDOSCheck: function (playerId) {
return chatDOSCheck(playerId);
}
};
var jwt = require('jsonwebtoken');
var config = require('./config');
var playersIncrement = 0;
var players = [];
var playerToken = [];
var updateLobbyCallback;
var chatMessagesRecentNum = [];
setInterval(function () {
if (updateLobbyCallback) {
let changes = false;
for (let i = 0; i < players.length; i++) {
if (players[i].inLobby && new Date() - players[i].lastActiveLobby > config.lobbyTimeout) {
players[i].inLobby = false;
players[i].ready = false;
changes = true;
console.log("player: " + players[i].playerName + " is inactive. ");
}
}
if (changes) updateLobbyCallback(players);
}
}, config.lobbyTimeoutCheckInterval);
function auth(req, res, next) {
var token = req.body.token || req.query.token;
if (token) {
jwt.verify(token, config.secret, function (err, decoded) {
if (err) {
res.status(200).send({
redirect: config.baseUrl
});
} else {
req.decoded = decoded;
next();
}
});
} else {
res.status(200).send({
redirect: config.baseUrl
});
}
}
function playerActive(playerId) {
players[playerId].lastActiveLobby = new Date();
if (players[playerId].inLobby) return;
players[playerId].inLobby = true;
console.log("player: " + players[playerId].playerName + " is active. ");
updateLobbyCallback(players);
}
function playerExists(playerName) {
for (let i = 0; i < players.length; i++) {
if (players[i].playerName === playerName) return true;
}
return false;
}
function addPlayer(playerName) {
const payload = {
playerName: playerName,
playerId: playersIncrement
};
players.push({
playerId: playersIncrement,
playerName: playerName,
ingame: false,
ready: false,
spectating: false,
inLobby: true,
lastActiveLobby: new Date()
});
playersIncrement++;
var token = jwt.sign(payload, config.secret, {
expiresIn: "10 days"
});
playerToken.push(token);
return token;
}
function addPlayerToLobby(playerId) {
if (players[playerId].inLobby) return;
players[playerId].inLobby = true;
console.log("player: " + players[playerId].playerName + " is active. ");
updateLobbyCallback(players);
}
function getLobbyPlayers() {
let lobbyPlayers = [];
for (let i = 0; i < players.length; i++) {
if (players[i].inLobby) lobbyPlayers[lobbyPlayers.length] = players[i];
}
return lobbyPlayers;
}
function getReadyPlayers() {
let readyPlayers = [];
for (let i = 0; i < players.length; i++) {
if (players[i].ready) readyPlayers[readyPlayers.length] = players[i];
}
return readyPlayers;
}
function authPlayer(req, res) {
console.log(req.decoded);
players[playerId].lastActiveLobby = new Date();
return (playerToken[playerId] === token);
}
function getPlayerId(playerName) {
for (let i = 0; i < players.length; i++) if (playerName === players[i].playerName) return players[i].playerId;
return;
}
function getPlayerById(playerId) {
return players[playerId];
}
function chatDOSCheck(playerId) {
if (chatMessagesRecentNum[playerId] === undefined) chatMessagesRecentNum[playerId] = 0;
if (chatMessagesRecentNum[playerId] === 0) {
setTimeout(function () {
chatMessagesRecentNum[playerId] = 0;
}, 10000)
}
chatMessagesRecentNum[playerId]++;
return chatMessagesRecentNum[playerId] > 3;
}