-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
202 lines (186 loc) · 5.91 KB
/
app.ts
File metadata and controls
202 lines (186 loc) · 5.91 KB
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
const express = require('express')
const bodyParser = require('body-parser');
const cors = require('cors');
const md5 = require("md5");
const functions = require('./functions.js');
const app = express()
const port: number = 3000;
app.use(bodyParser.json());
app.use(cors());
expireLoop();
//Lets players generate unique gameid
app.post('/generategameid', (req, res) => {
let playerid: string = req.body.playerid;
let out: string = functions.generateGameID(playerid);
// console.log(out, "gameid")
res.json([{
data: out
}])
})
//Lets players generate unique playerid
app.post('/generateplayerid', (req, res) => {
let out: string = functions.generatePlayerID();
// console.log(out, "playerid")
res.json([{
data: out
}])
})
//Allows players to create/join game rooms
app.post('/createboard', (req, res) => {
let playerid: string = req.body.playerid;
let gameid: string = req.body.gameid;
let out: string = functions.joinGame(playerid, gameid);
// console.log(out, "createboard")
res.json([{
data: out
}])
})
//Allows players to check gamestate, like turn and game status
app.post('/checkturn', (req, res) => {
let playerid: string = req.body.playerid;
let gameid: string = req.body.gameid;
let out: number[] = functions.checkTurn(playerid, gameid);
if (out[0] == -2)
functions.deleteGame(gameid);
res.json([{
data: out
}])
})
//Player request to insert piece
app.post('/move', (req, res) => {
let playerid: string = req.body.playerid;
let gameid: string = req.body.gameid;
let col: number = req.body.col;
let out: [string, number, []];
//Game doesn't exist
if (functions.checkTurn(playerid, gameid) == -2)
out = ["Game doesn't exist", 0, []];
//Not your turn
else if (functions.checkTurn(playerid, gameid)[0] != 1)
out = ["Not your turn", 0, []];
//Your turn
else {
out = functions.insert(col, playerid, gameid);
if (out[0] == "Good insert") {
functions.changeTurn(gameid);
let row: number = functions.getTop(gameid, col);
if (functions.checkWin(gameid, out[1], col, row)) {
out[0] = "Game won";
functions.updatePlayerScore(playerid, "", false, 1);
let otherplayerid = functions.getOtherPlayer(gameid, playerid);
functions.updatePlayerScore(otherplayerid, "", false, 0);
functions.endGame(gameid, playerid)
}
}
else
out[0] = "Bad insert";
}
res.json([{
data: out
}])
})
//Ends game for given player
app.post('/quit', (req, res) => {
let gameid: string = req.body.gameid;
let playerid: string = req.body.playerid;
let intentional: number = req.body.col
if (intentional) {
functions.updatePlayerScore(playerid, "", false, 0);
let otherplayerid = functions.getOtherPlayer(gameid, playerid);
functions.updatePlayerScore(otherplayerid, "", false, 1);
}
functions.deleteGame(gameid);
res.json([{
data: "quit successful"
}])
})
//Allows logging and returning current game and player data (nothing sensitive). Not essential for operation
app.get('/debug', (req, res) => {
let out: string = functions.debug();
res.json([{
data: out
}])
})
//Allows simple get request to check if API is alive. Not essential for operation
app.get('/healthcheck', (req, res) => {
res.status(200).json([{
data: 'Still Alive. V2'
}])
})
//Submits request for matchmaking
app.post('/matchmake', (req, res) => {
let playerid: string = req.body.playerid;
let gameid: string = functions.generateGameID(playerid);
let otherplayerdata: string[] = functions.matchmake(playerid, gameid);
let startgame = otherplayerdata[0];
let out: string[] = [];
if (startgame == "0") {
let concurrentPlayerCount = otherplayerdata[1];
let message = functions.joinGame(playerid, gameid);
out = [startgame, message, gameid, concurrentPlayerCount];
}
else {
let newgameid = otherplayerdata[1];
functions.deleteGame(gameid)
let message = functions.joinGame(playerid, newgameid);
out = [startgame, message, newgameid];
}
res.json([{
data: out
}])
})
//Removes a player from the matchmaking queue
app.post('/dequeue', (req, res) => {
let playerid: string = req.body.playerid;
let gameid: string = req.body.gameid;
let out: string = functions.dequeue(playerid, gameid);
res.json([{
data: out
}])
})
//Pings are used to determine if players are still using the site
app.post('/ping', (req, res) => {
let playerid: string = req.body.playerid;
let out: string = functions.registerPing(playerid);
res.json([{
data: out
}])
})
//Inserts new player data into the mongo db
app.post('/signup', async (req, res) => {
let username: string = req.body.playerid;
let password: string = req.body.gameid;
let hash = md5(password);
let playerid = functions.generatePlayerID();
let success: string = await functions.signUpUser(username, hash, playerid);
let out: string[] = [success, playerid];
res.json([{
data: out
}])
})
//Inserts new player data into the mongo db
app.post('/login', async (req, res) => {
let username: string = req.body.playerid;
let password: string = req.body.gameid;
let hash = md5(password);
let playerid = functions.generatePlayerID();
let out: JSON = await functions.loginUser(username, hash, playerid);
console.log("login info", out);
res.json([{
data: out
}])
})
//Runs every 10s to check for unresponsive players
async function expireLoop() {
while (true) {
//Wait 10s
await new Promise(resolve => setTimeout(resolve, 10000));
let expiredPlayersInGames: changeplayerscorevals[] = functions.expirePlayers();
for (let i: number = 0; i < expiredPlayersInGames.length; i++) {
functions.updatePlayerScore(expiredPlayersInGames[i].playerid, expiredPlayersInGames[i].username, true, 0);
functions.updatePlayerScore(expiredPlayersInGames[i].otherplayerid, expiredPlayersInGames[i].otherplayerusername, true, 1);
}
}
}
app.listen(port, () => console.log(`Listening on port ${port}`));
module.exports = app;