-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
189 lines (171 loc) · 5.41 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
/*
FAILLES CONNUES :
[x toléré] si un joueur ferme l'app dans le lobby ses mots restent dans le salad-bowl
[x faille comblée] si un joueur ouvre un autre onglet de l'app il y a conflit entre les deux instance (session)
[x]les entrée avec apostrophes et specials chars ne sont pas gérées
*/
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({extended : true});
var session = require('express-session');
class room {
constructor(name) {
this.name = name;
this.teamNumber = 0;
this.wordsNumber = 0;
this.tour = 0;
this.teamPlaying = 0;
this.scores = [];
this.words = [];
this.tmpWords = [];
this.players = [];
this.teams = [];
this.ready = false;
this.emptybowl = false;
}
}
class player {
constructor(name) {
this.name = name;
this.team = 1;
this.ready = false;
this.atEnd = false;
}
}
class team {
constructor(){
this.playersId = [];
this.playerPlaying = 0;
}
}
var games = [];
var sockets = require('./lib/sockets.js')(server, games);
app.use(express.static('public'));
app.use(session({secret: 'vander'}));
app.set('view engine','ejs');
app.get('/',(req,res)=>{
if(req.query.errorCode){
res.render('home', {errorCode: req.query.errorCode});
}
else{
res.render('home', {errorCode: 0});
}
})
.post('/host', urlencodedParser, (req,res)=>{
if(req.body.roomName in games){
res.redirect('/?errorCode=3');
}
else{
games[req.body.roomName] = new room(req.body.roomName);
games[req.body.roomName].players.push(new player(req.body.playerName));
req.session.playerId = games[req.body.roomName].players.length - 1;
req.session.roomName = req.body.roomName;
}
res.render('config', {roomName:req.session.roomName});
})
.post('/join', urlencodedParser,(req,res)=>{
var error = false;
if(req.body.roomName in games){
games[req.body.roomName].players.forEach(plr=>{
if(req.body.playerName == plr.name){
error = true;
res.redirect('/?errorCode=1');
}
});
if(!error){
if(games[req.body.roomName].ready == true){
games[req.body.roomName].players.push(new player(req.body.playerName));
req.session.playerId = games[req.body.roomName].players.length - 1;
req.session.roomName = req.body.roomName;
}
else{
res.redirect('/?errorCode=4');
}
}
}
else{
res.redirect('/?errorCode=2');
}
res.render('submit-words',{wordsNumber:games[req.session.roomName].wordsNumber});
})
.post('/config',urlencodedParser, (req,res)=>{
games[req.session.roomName].wordsNumber = parseInt(req.body.wordsNumber);
games[req.session.roomName].teamNumber = parseInt(req.body.teamNumber);
for(var i = 0; i<req.body.teamNumber; i++){
games[req.session.roomName].teams.push(new team());
games[req.session.roomName].scores.push(0);
}
games[req.session.roomName].ready = true;
res.render('submit-words',{wordsNumber:games[req.session.roomName].wordsNumber});
})
.post('/submit-words',urlencodedParser,(req,res)=>{
req.body.words.forEach(word => {
games[req.session.roomName].words.push(word);
});
res.redirect('/lobby');
})
.get('/lobby',(req,res)=>{
res.render('lobby',{
teamNumber : games[req.session.roomName].teamNumber,
myself : req.session.playerId,
players : games[req.session.roomName].players,
roomName : req.session.roomName,
});
})
.get('/game',(req,res)=>{
if(!games[req.session.roomName].emptybowl){
var teamPlaying = games[req.session.roomName].teams[games[req.session.roomName].teamPlaying];
var playerPlayingId = teamPlaying.playersId[teamPlaying.playerPlaying];
var playerPlayingName = games[req.session.roomName].players[playerPlayingId].name;
if(req.session.playerId == playerPlayingId){
res.render('pregame');
}
else{
res.render('message', {
myself : req.session.playerId,
roomName : req.session.roomName,
msg : playerPlayingName+' est en train de jouer !',
});
}
}
else{
res.redirect('/scores');
}
})
.get('/play',(req,res)=>{
res.render('game', {
myself : req.session.playerId,
roomName : req.session.roomName,
firstWord : games[req.session.roomName].tmpWords[0],
});
})
.get('/msg',(req,res)=>{
res.render('message', {msg: "L'equipe 1 joue"});
})
.get('/scores',(req,res)=>{
var title ="Fin de la manche";
var end = 0;
var scores = games[req.session.roomName].scores;
if(games[req.session.roomName].tour > 3){
title = "Partie terminée";
end = 1;
}
res.render('scores', {
title: title,
end:end,
scores: scores,
myself : req.session.playerId,
roomName : req.session.roomName,
});
})
.get('/MTerror', (req,res)=>{
res.render('MTerror');
})
.use(function(req, res, next){
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('FORBIDDEN');
});
server.listen(3000);