-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (149 loc) · 4.63 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
// Initialize packages
var Express = require('express');
var HTTP = require('http');
var IO = require('socket.io');
var Game = require('./game');
// Initialize package objects
var app = Express();
var http = HTTP.Server(app);
var io = IO(http);
let game = undefined;
// Statically serve files
app.use('/', Express.static('game'));
app.get('/', (req, res) => {
res.send('<h1>Hello world<h/1>');
});
// Check if PORT was predefined on system
let PORT = process.env.PORT|80;
// Listen to requests
http.listen(PORT, () => {
console.log("Listening on " + PORT + "...");
});
io.on('connection', (socket) => {
console.log('\n\nConnection initiated with socket' , socket.id);
/*
if(Object.keys(io.sockets.sockets).length === 1) {
socket.emit('host', { message: "You're the host!" });
}
*/
// Removes players who disconnect from the game
/*
socket.on('disconnect', () => {
if(game !== undefined) {
console.log('\n\nDisconnecting socket' , socket.id);
game.removePlayer(socket.id);
console.log('Players: ' , game.players);
}
});
*/
// Creates instance of the game and asks all players for their Handle
socket.on('lockPlayers', (data) => {
game = new Game();
io.sockets.emit('players', {});
});
// Creates the player based on their Socket ID and Handle
socket.on('addPlayer', (data) => {
if(game !== undefined) {
// Create the player
console.log('\nAdding player...');
game.addPlayer(socket.id, data.handle);
// Tell first added player they're host
if(Object.keys(game.players).length === 1) {
socket.emit('host', { isHost : true });
} else {
socket.emit('host', { isHost : false });
}
// Gets the player object and gives it to that user
let thisPlayer = game.getPlayer(socket.id);
// socket.emit('yourPlayer', { player: thisPlayer });
}
console.log('\nPlayers: ' , game.players);
});
// Randomly selects a Player to start the game
socket.on('gameStart', (data) => {
// Get all sockets connected
let sockets = Object.keys(io.sockets.sockets);
for(let i = 0; i < sockets.length; i++) {
// Draws a card for that player
game.draw(sockets[i]);
// Gets the player object and gives it to that user
let thisPlayer = game.getPlayer(sockets[i]);
// Get socket and emit
let currSocket = io.sockets.sockets[sockets[i]];
currSocket.emit('yourPlayer', { player: thisPlayer });
}
// Get the Starting Player
let startingPlayer = game.switchTurns();
// Tell all Players the Starting Player
io.sockets.emit('newTurn', { currPlayer: startingPlayer.getHidden() });
});
socket.on('getPlayers', (data) => {
let playerList = [];
for(let key in game.players) {
playerList.push(game.players[key].getHidden());
}
socket.emit('playerList', { players: playerList });
});
// When that player starts their turn they draw a card
// TODO optimize with game start
socket.on('turnStart', (data) => {
if(game !== undefined) {
game.draw(socket.id);
}
let sockets = Object.keys(io.sockets.sockets);
// Refresh all player's states
for(let i = 0; i < sockets.length; i++) {
// Gets the player object and gives it to that user
let thisPlayer = game.getPlayer(sockets[i]);
// Get socket and emit
let currSocket = io.sockets.sockets[sockets[i]];
currSocket.emit('yourPlayer', { player: thisPlayer });
}
});
socket.on('cardPlayed', (data) => {
let players = game.players;
let targetSocket = '';
let count = 0
for (var key in players) {
console.log('\n\nChecking' , count , 'to' , data.target);
if(count == data.target){
targetSocket = key;
break;
}
}
console.log('Target:' , data.target);
let target = undefined;
if(data.target !== undefined) {
target = game.getSocket(data.target).socket
}
let ret = game.playCard(socket.id, target, data.card, data.param);
// Check for error
if(ret['error'] !== undefined) {
socket.emit('cardPlayError', {});
return;
}
// Check for info
if(ret['info'] !== undefined) {
socket.emit('info', { info: ret['info'] });
}
// Check for outcome
if(ret['outcome'] !== undefined) {
io.sockets.emit('outcome', { outcome: ret['outcome'] });
}
var x = game.checkEnd();
console.log(x);
if(x != null){
let winner = x.winner;
x.score.then(function(data){
let scoreboard = data[0];
console.log("Game Over");
console.log(data);
io.sockets.emit('gameOver', { "winner":winner, "scoreboard":scoreboard });
}).catch(err => console.log(err));
} else {
let discardPile = game.discard;
io.sockets.emit('discardUpdate', { discardPile: discardPile });
let nextPlayer = game.switchTurns();
io.sockets.emit('newTurn', { currPlayer: nextPlayer.getHidden() });}
});
});