-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.js
221 lines (160 loc) · 5.57 KB
/
server.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
var express = require('express');
var app = express();
var http = require('http').createServer(app);
var session = require('express-session');
var io = require('socket.io')(http);
var Board = require('./board');
var Player = require('./player');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 3000;
app.engine('html', require('ejs').renderFile);
app.set('views', __dirname+"/GUI");
app.use(express.static(__dirname+'/GUI'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(session({
secret: '$#0x--♠☻☺./\\',
saveUninitialized: true,
resave: false
}));
var board = new Board;
Player = new Player;
app.get('/', function(req, res){
let user = req.session.user;
if (!user)
return res.sendFile(__dirname +'/GUI/main.html');
let player = Player.get(user.id);
if (!player || board.matchEnded(player.matchId) || board.isMatchDraw(player.matchId)) {
delete req.session.user;
if (player) // if match ended and player existed
Player.delete(player.id);
return res.sendFile(__dirname +'/GUI/main.html');
}
let name = user.name;
let searching_htm = `Waiting`;
let opponent_sym = "";
if(player.symbol) opponent_sym= player.symbol=='X'?'O':'X';
let d = {name, opposition: searching_htm, uid: player.id, match_moves: null, symbol: player.symbol, opponent_sym, lock: null, "avatar_id":player.avatar_id, "opp_avatar_id":null};
if (player.started) {
let opposition_user = Player.opposition(player.id);
d.opposition = opposition_user.name;
d.lock = player.lock;
d.opp_avatar_id = opposition_user.avatar_id;
}
let match_moves = board.getMatchMoves(player.matchId);
d.match_moves = match_moves;
console.log(match_moves)
res.render('game.html', d);
});
app.post('/start', function(req, res){
if (req.session.user)
return res.redirect('/');
let new_player = Player.new({name: req.body.name, opposition: '', "avatar_id": Math.floor(Math.random() * 15)+1, started: false, symbol: '', lock: -1, matchId: null});
let user = {id : new_player.id, name: new_player.name};
req.session.user = user;
io.emit('started', user.name);
console.log(`User ${user.name} started the game`);
if (Player.count() >0) {
for (let player of Player.all()) {
if (player.started == false && player.id != new_player.id) {
console.log(`challenger found ${player.name}`)
new_player.started = player.started = true;
new_player.symbol = 'O';
new_player.opposition = player.id;
new_player.lock = 1;
player.symbol = 'X';
player.opposition = new_player.id;
player.lock = 0;
let match_id = board.matchCount();
new_player.matchId = player.matchId = match_id;
board.newMatch({match_id, moves: new Array(9), ended:false, draw:false});
Player.update(player.id, player);
Player.update(new_player.id, new_player);
break;
}
}
}
if (new_player.started == true) {
let cur_player = Player.get(req.session.user.id);
let opponent = Player.get(cur_player.opposition);
let d = {challenger_name: user.name, symbol: opponent.symbol, opponent_sym: cur_player.symbol, avatar_id:cur_player.avatar_id};
io.emit('found_challenger', JSON.stringify(d));
}
res.redirect('/');
});
app.put('/move', (req, res) => {
let user = req.session.user;
let player = Player.get(user.id);
if (board.matchEnded(player.matchId) || player.lock >0 || !Player.exist(player.id) || player.started == false) {
res.writeHead(401);
return res.end();
}
let cell = req.body.box;
let symbol = player.symbol;
if (!board.setMatchMove(player.matchId, cell, symbol)) {
res.writeHead(401);
return res.end();
}
io.emit('user_move', JSON.stringify({id: player.id, cell, symbol}));
console.log(`User ${player.name} made a move at ${cell}`);
player.lock = 1;
Player.opposition(player.id).lock = 0;
let winner = board.checkForWinner(player, Player);
if (winner)
io.emit('match_ended', JSON.stringify(winner));
let is_draw = board.matchDraw(player.matchId);
if (is_draw)
io.emit('match_draw', JSON.stringify({msg: "Match draw!"}));
res.writeHead(204);
res.end();
});
app.get('/check', (req, res) => {
let user = req.session.user;
let player = Player.get(user.id);
if (!Player.exist(user.id) || (player&&player.started == false)) {
res.writeHead(400);
return res.end();
}
res.send(player.lock==0);
res.end();
});
app.get('/end', (req, res) => {
let user = req.session.user;
if (!user || !Player.exist(user.id)) {
res.writeHead(400);
return res.end();
}
let player = Player.get(user.id);
if (board.hasMatch(player.matchId))
board.endMatch(player.matchId);
Player.delete(player.id);
let winner = Player.get(player.opposition);
io.emit('match_left', JSON.stringify(winner));
return res.redirect('/');
});
io.on('connection', function(socket){
console.log('connected');
});
app.post('/msg', function(req, res){
if (!req.session.user)
return res.redirect('/');
let user = req.session.user;
let msg = req.body.msg;
let player = Player.get(user.id);
if (player || !board.matchEnded(player.matchId)) {
let opposition_user = Player.opposition(player.id);
if (!opposition_user) {
res.writeHead(400);
return res.end()
}
let d = {msg: msg, user_id: player.opposition};
io.emit("msg_rec", JSON.stringify(d));
res.writeHead(204);
return res.end();
}
});
http.listen(PORT, function(){
console.log(`listening on http://localhost:${PORT}`);
});