-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
131 lines (113 loc) · 4 KB
/
app.js
File metadata and controls
131 lines (113 loc) · 4 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
//ts-check
const express = require("express");
const http = require("http");
const websocket = require("ws");
const indexRouter = require("./routes/index");
const messages = require("./public/javascripts/messages");
const gameStatus = require("./statTracker");
const Game = require("./game");
if(process.argv.length < 3) {
console.log("Error: expected a port as argument (eg. 'node app.js 3000').");
process.exit(1);
}
const port = process.argv[2];
const app = express();
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.get("/play", indexRouter);
app.get("/", indexRouter);
const server = http.createServer(app);
const wss = new websocket.Server({ server });
const websockets = {};
setInterval(function() {
for (let i in websockets) {
if (Object.prototype.hasOwnProperty.call(websockets,i)) {
let gameObj = websockets[i];
//if the gameObj has a final status, the game is complete/aborted
if (gameObj.finalStatus != null) {
delete websockets[i];
}
}
}
}, 50000);
let currentGame = new Game(gameStatus.gamesInitialized++);
let connectionID = 0;
wss.on("connection", function connection(ws) {
const con = ws;
con["id"] = connectionID++;
const playerType = currentGame.addPlayer(con);
websockets[con["id"]] = currentGame;
console.log(
`Player ${con["id"]} placed in game ${currentGame.id + 1} as ${playerType}`
);
con.send(playerType == "A" ? messages.S_PLAYER_A : messages.S_PLAYER_B);
//con.send(messages.S_PLAYER_TURN_A);
if (currentGame.hasTwoConnectedPlayers()) {
currentGame = new Game(gameStatus.gamesInitialized++);
}
con.on("message", function incoming(message) {
const oMsg = JSON.parse(message.toString());
const gameObj = websockets[con["id"]];
const isPlayerA = gameObj.playerA == con ? true : false;
if (isPlayerA) {
if (oMsg.type == messages.T_MAKE_A_GUESS) {
gameObj.playerB.send(message.toString());
gameObj.setStatus("TURN");
}
}
else {
if(oMsg.type == messages.T_PLAYER_2) {
gameObj.playerA.send(message.toString());
}
if (oMsg.type == messages.T_MAKE_A_GUESS) {
gameObj.playerA.send(message.toString());
gameObj.setStatus("TURN");
}
}
if (oMsg.type == messages.T_GAME_WON_BY) {
gameObj.setStatus(oMsg.data);
//game was won by somebody, update statistics
gameStatus.gamesCompleted++;
}
if (oMsg.type == "GAME-TIED") {
gameObj.setStatus("TIED");
//game was tied by somebody, update statistics
gameStatus.gamesCompleted++;
}
});
con.on("close", function(code) {
/*
* code 1001 means almost always closing initiated by the client;
* source: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
*/
console.log(`${con["id"]} disconnected ...`);
if (code == 1001) {
/*
* if possible, abort the game; if not, the game is already completed
*/
const gameObj = websockets[con["id"]];
if (gameObj.isValidTransition(gameObj.gameState, "ABORTED")) {
gameObj.setStatus("ABORTED");
gameStatus.gamesAborted++;
/*
* determine whose connection remains open;
* close it
*/
try {
gameObj.playerA.close();
gameObj.playerA = null;
} catch (e) {
console.log("Player A closing: " + e);
}
try {
gameObj.playerB.close();
gameObj.playerB = null;
} catch (e) {
console.log("Player B closing: " + e);
}
}
currentGame = new Game(gameStatus.gamesInitialized - 1);
}
});
})
server.listen(port);