-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
196 lines (153 loc) · 5.26 KB
/
index.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
var path = require('path');
var express = require('express')
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
const Board = require("./board.js");
var htmlPath = path.join(__dirname, 'content');
app.use(express.static(htmlPath));
var userCounter = 0;
var groups = {};
var existsGroup = (groupName) => groups[groupName] ? true : false
io.on('connection', function (socket) {
console.log('a user connected');
const userID = userCounter++;
socket.emit("userID", userID);
socket.on('group', function (groupName, userName) {
if (existsGroup(groupName)) {
return;
}
groups[groupName] = true;
const nsp = io.of('/' + groupName);
var game = new Game(null, function (turnID, team, role) {
nsp.emit('turn', turnID, team, role);
if (role == MASTER) nsp.emit('master update', 0, "");
});
nsp.on('connection', socket => onUserJoinsGroup(nsp, socket, game, game.board));
console.log('established group: ' + groupName);
});
});
function onUserJoinsGroup(nsp, socket, game, board) {
let uname, player;
socket.on('username', name => {
uname = name;
if (Object.values(game.players).find(p => p.username == uname)) uname = uname + userCounter;
game.players[socket.id] = new Player(-1, uname, RED, MASTER);
});
socket.emit('players', Object.values(game.players));
socket.emit('board update', board);
socket.emit('turn', game.turnID, game.team(), game.role());
socket.emit('master update', game.numSquares, game.hint);
socket.on('chat message', function (msg) {
nsp.emit('chat message', `${uname}: ${msg}`);
});
socket.on('team role', function (uID, team, role) {
player = game.players[socket.id];
player.team = team;
player.role = role;
socket.emit('team role', player.turnID(), team, role);
socket.emit('turn', game.turnID, game.team(), game.role());
nsp.emit('players', Object.values(game.players));
socket.emit('master board', role == MASTER ? board : null);
});
socket.on('select square', function (uID, row, column) {
if (game.makePlayGuesser(game.players[socket.id], row, column)) {
nsp.emit('board update', board);
nsp.emit('master update', game.numSquares, game.hint);
}
else socket.emit('board update', board);
});
socket.on('master', function (uID, numSquares, hint) {
if (game.makePlayMaster(game.players[socket.id], numSquares, hint))
nsp.emit('master update', numSquares, hint);
});
socket.on('disconnect', () => {
console.log(uname + " disconnected");
delete game.players[socket.id];
nsp.emit('players', Object.values(game.players));
});
socket.on('connect', () => {
console.log("reconnect");
if (player) {
game.players[socket.id] = player;
nsp.emit('players', Object.values(game.players));
}
})
};
server.listen(3000, function () {
var host = 'localhost';
var port = server.address().port;
console.log('listening on http://' + host + ':' + port + '/');
});
const RED = "red", BLUE = "blue";
const GUESSER = "guesser", MASTER = "master";
var turns = {};
turns[RED + MASTER] = 0;
turns[RED + GUESSER] = 1;
turns[BLUE + MASTER] = 2;
turns[BLUE + GUESSER] = 3;
class Game {
constructor(players, onNewTurn) {
this.turnID = 0;
this.players = players ? players : {};
this.onNewTurn = onNewTurn;
this.board = new Board();
this.numSquares = 0;
this.hint = "";
}
makePlayMaster(player, numSquares, hint) {
if (!this.pTurn(player) || player.role != MASTER) {
return false;
}
else {
this.numSquares = numSquares;
this.hint = hint;
this.endTurn();
return true;
}
}
makePlayGuesser(player, row, column) {
if (!this.pTurn(player) || player.role != GUESSER) {
console.log("received play from invalid user");
return false;
}
else if (this.board.select(row, column)) {
let square = this.board.getSquare(row, column);
let color = square.team == Board.BLUE ? "blue" : square.team == Board.RED ? "red" : "yellow"
this.numSquares--;
if (color != player.team || this.numSquares == 0) {
this.endTurn();
}
return true;
}
}
endTurn() {
this.incrementTurn();
this.onNewTurn(this.turnID, this.team(), this.role());
}
pTurn(player) {
return player.turnID() == this.turnID;
}
incrementTurn() {
this.turnID = this.turnID == 3 ? 0 : ++this.turnID;
console.log("turn is now " + this.turnID);
}
team(){
return this.turnID < 2 ? RED : BLUE;
}
role(){
return this.turnID % 2 == 0 ? MASTER : GUESSER;
}
}
class Player {
constructor(userID, username, team, role) {
this.userID = userID;
this.username = username;
this.team = team;
this.role = role;
}
turnID() {
return turns[this.team + this.role];
}
}