-
Notifications
You must be signed in to change notification settings - Fork 0
/
rommeyServer.js
215 lines (176 loc) · 5.73 KB
/
rommeyServer.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
// the rommey game server
var rommeyServer = (function() {
// return a random integer in range 0..L-1
function random(L) {
return Math.floor(Math.random()*L);
}
// randomizes the order of the elements of the array by swapping N random elements
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// the Rommey Game class
class Rommey {
constructor() {
this.colors = ["black", "blue", "red", "yellow"]
this.NUM_PIECES = 13; // we have pieces from 1 to 13
this.STONES_FOR_EACH_PLAYER = 14; // each player gets 14 pieces
this.stones = []; // here goes the stones that have been drawn
this.drawBag = []; // rest of the stones are in the draw bag.
this.names = {}; // names of the players (socket id -> name)
this.linepos = 14; // y=14 is where the player area starts
this.lines = {}; // lines for marking ... stuff
}
// return an array of all possible stones (1-13, all colors)
getAllPieces()
{
var pieces = [];
var y = 1;
for( var c in this.colors ) {
for( var i = 1; i <= this.NUM_PIECES; i++) {
pieces.push({x:i, y:y+0, playerarea:0, nummer:i, color:this.colors[c]});
pieces.push({x:i, y:y+1, playerarea:0, nummer:i, color:this.colors[c]});
}
y+=2;
}
return pieces;
}
// log current state of drawn stones and stones in drawbag.
logStones() {
console.log("Stones at players: " + this.stones.length + ", stones in drawBag: " + this.drawBag.length
+ ", sum = " + (this.stones.length + this.drawBag.length) + " (should be 104)");
}
// draw one random stone out of the draw bag
drawStoneFromBag() {
return this.drawBag.shift();
}
// draw 14 stones for one player
drawStonesForPlayer(id) {
for(var i=0; i < this.STONES_FOR_EACH_PLAYER; i++) {
let stone = this.drawStoneFromBag();
if( stone === undefined ) break;
stone.x = i; // x position
stone.y = this.linepos; // y position
stone.playerarea = id; // this marks stones as only visible by this player
this.stones.push(stone);
}
this.logStones();
}
// restart the game
restart()
{
this.stones = []; // empty out drawn stones
this.drawBag = this.getAllPieces(); // regenerate all stones in draw bag
shuffleArray(this.drawBag);
// now draw stones for each connected player
for (var id in this.names) {
this.drawStonesForPlayer(id);
}
}
// return true if there's already a stone at this position
alreadyStoneHere(gridpos, id) {
return this.stones.some( s => {
if( gridpos.y >= this.linepos && s.playerarea != id )
return false;
return s.x == gridpos.x && s.y == gridpos.y;
} )
}
// draw one stone for a player
drawOneStone(id)
{
let stone = this.drawStoneFromBag();
if(stone === undefined) return;
stone.playerarea = id;
for(var y=this.linepos; y<20; y++) {
for(var x=0; x<19; x++)
if( this.stones.every( s => !this.alreadyStoneHere({x:x, y:y}, id) ) )
{
stone.x = x;
stone.y = y;
this.stones.push(stone);
return;
}
}
}
// new player: add to names list, draw stones.
addPlayer(id) {
this.names[id] = "Player " + (Object.keys(this.names).length+1)
console.log('new connection: ' + id + " = " + this.names[id]); // log the connection
this.drawStonesForPlayer(id);
}
// player quit game, put back players' stones
removePlayer(id) {
console.log('disconnect ' + id + " = " + this.names[id]);
// delete from name array
delete this.names[id];
var playersStones = this.stones.filter( s => s.playerarea == id );
this.stones = this.stones.filter( s => s.playerarea != id );
this.drawBag = this.drawBag.concat(playersStones);
console.log("put back " + playersStones.map(s => s.color + "." + s.nummer ).join(", ") );
shuffleArray(this.drawBag);
this.logStones();
}
}
game = new Rommey();
game.restart();
function newConnection(socket){
socket.on('stones', stonesMsg);
socket.on('mouse', mouseMsg);
//socket.on('selected', selectedMsg);
socket.on('name', nameMsg);
socket.on('game', gameMsg);
socket.on('lines', linesMsg);
socket.on('disconnect', function() {
game.removePlayer(socket.id);
socket.broadcast.emit('names', game.names);
});
game.addPlayer(socket.id);
// send player's name to all other players
socket.emit('names', game.names);
socket.emit('lines', game.lines);
socket.broadcast.emit('names', game.names);
// send updated stones to all players
socket.emit('stones', game.stones);
socket.broadcast.emit('stones', game.stones);
function nameMsg(name) {
game.names[socket.id] = name
console.log(game.names);
socket.emit('names', game.names);
socket.broadcast.emit('names', game.names);
}
function mouseMsg(pos) {
pos.id = socket.id;
socket.broadcast.emit('mouse', pos);
}
function stonesMsg(s){
console.log("stonesMsg (" + s + ")")
game.stones = s;
socket.broadcast.emit('stones', game.stones);
}
function linesMsg(l) {
console.log(l);
// todo: only send those that get updated
game.lines[socket.id] = l;
socket.broadcast.emit('lines', game.lines);
}
function gameMsg(s){
console.log("gameMsg (" + s + ")")
if( s === "allPieces" )
stones = game.getAllPieces();
else if( s === "restart" )
game.restart();
else if( s === "draw" ) {
game.drawOneStone(socket.id);
}
socket.broadcast.emit('stones', game.stones);
socket.emit('stones', game.stones);
}
}
return {
newConnection: newConnection
}
})();
if(typeof module !== 'undefined')
module.exports.newConnection = rommeyServer.newConnection;