-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
199 lines (129 loc) · 4.9 KB
/
app.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
var express = require("express");
var http = require("http");
var websocket = require("ws");
var port = process.argv[2];
var app = express();
var cookies = require("cookie-parser"); //////////////////////
app.use(cookies("my_secret_abc_123"));
app.use(express.static(__dirname + "/public"));
var server = http.createServer(app).listen(process.env.PORT || 3000);
const wss = new websocket.Server({ server })
var Messages = require("./public/javascripts/messages.js");
var gameStats = {
// since : Date.now(), /* since we keep it simple and in-memory, keep track of when this object was created */
shotsfired : 0, /* number of games initialized */
playersonline : 0, /* number of games aborted */
gamesInitialized: 0 /* number of games successfully completed */
};
var Game = require("./gamestate");
var websockets = {};
var currentGame = new Game(gameStats.gamesInitialized++);
var connectionID = 0;
var messageAField;
wss.on("connection", function connection(ws) {
let con = ws;
con.id = connectionID++;
// console.log("cookie " + con.cookie);
// res.cookie("signed_chocolate", "monster", { signed: true });
// con.cookies("cookies for game", "sup bot", { signed: true});
let playerType = currentGame.addPlayer(con);
console.log(currentGame.gameState);
console.log("playertype " + playerType);
currentGame.setState(playerType);
websockets[con.id] = currentGame;
console.log("Player %s placed in game %s as %s", con.id, currentGame.id, playerType);
// console.log(currentGame.playerA);
let gameObj = websockets[con.id];
if (currentGame.hasTwoConnectedPlayers()) {
var Outmail = Messages.O_Shot;
Outmail.data = 50;
gameObj.playerB.send(JSON.stringify(Outmail));
console.log("sending shot to player 3");
currentGame = new Game(gameStats.gamesInitialized++);
}
con.on("message", function incoming(message) {
let oMsg = JSON.parse(message);
console.log("getting message " + message);
// con.send("supder");
gameStats.playersonline++;
let isPlayerA = (gameObj.playerA == con) ? true : false;
// console.log(gameObj.playerA == con);
if (isPlayerA) {
/*
* player A cannot do a lot, just send the target word;
* if player B is already available, send message to B
*/
if (oMsg.type === Messages.T_Set_Field) {
// console.log("this is the fucking problem");
gameObj.setplayerAfield(oMsg.data);
messageAField = message;
}
if(oMsg.type == Messages.T_Shot){
gameStats.shotsfired++;
gameObj.playerB.send(message);
}
if( oMsg.type == Messages.T_GAME_WON_BY){
gameObj.setStatus(oMsg.data);
//game was won by somebody, update statistics
gameStatus.gamesCompleted++;
}
}
else {
if (oMsg.type == Messages.T_Set_Field) {
gameObj.setplayerBfield(oMsg.data);
if(gameObj.hasTwoConnectedPlayers()){
gameObj.playerA.send(message);
gameObj.playerB.send(messageAField);
}
}
/*
* player B can make a guess;
* this guess is forwarded to A
*/
if(oMsg.type == Messages.T_Shot){
gameStats.shotsfired++;
// console.log("sending shot from B to A")
gameObj.playerA.send(message);
}
/*
* player B can state who won/lost
*/
if( oMsg.type == Messages.T_GAME_WON_BY){
gameObj.setStatus(oMsg.data);
//game was won by somebody, update statistics
gameStats.playersonline--;
}
}
});
con.on("close", function (code){
if(code == "1001"){
let gameOBJ = websockets[con.id];
try {
gameObj.playerA.close();
gameObj.playerA = null;
}
catch(e){
console.log("playerA closing" + e);
}
try{
gameObj.playerB.close();
gameObj.playerB = null;
}
catch(e){
console.log("player B closing" + e);
}
}
})
})
app.set('view engine', 'ejs')
app.get('/', (req, res) => {
//example of data to render; here gameStatus is an object holding this information
res.render('splash.ejs', { shotsfired: gameStats.shotsfired, players: gameStats.playersonline, games: (gameStats.gamesInitialized - 1) });
})
app.get("/setup", function (req, res) {
res.sendFile("setup.html", {root: "./public"});
});
app.get("/game", function (req, res) {
res.sendFile("game.html", {root: "./public"});
});
server.listen(port);