-
Notifications
You must be signed in to change notification settings - Fork 0
/
gserver.js
165 lines (142 loc) · 4.18 KB
/
gserver.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
var _ = require('underscore');
var Game = require('./game');
var GamesHandler = function(ObjSet, oldGameCallback) {
var gameCounter = 897;
var games = {}
var gameDB = new ObjSet('game');
function writeState(gameID) {
var state = games[gameID].totalState();
gameDB.add(gameID, state, function (err, reply) {
if (err)
console.log(err);
});
}
this.createGame = function(players) {
var gameID = gameCounter++;
games[gameID] = new Game(players.length);
writeState(gameID);
players.forEach(function(player, index) {
player.updateProp('num', index);
});
return gameID;
};
this.userMessages = function(gameID) {
var state = games[gameID];
return {
clue: function(player, group) {
return function(clue) {
clue = JSON.parse(clue)
try {
var matching = state.giveClue(clue.giver,
clue.recipient, clue.suitOrNumber);
group.emitClue({
giver: clue.giver,
recipient: clue.recipient,
suitOrNumber: clue.suitOrNumber,
matching: matching
});
group.updateUsers();
writeState(gameID);
}
catch(err) {
console.log(err.message);
player.emit('erra', err.message);
}
};
},
playCard: function(player, group) {
return function(play) {
play = JSON.parse(play)
try {
var result = state.playCard(play.player, play.cardIndex);
var action = result.valid ? 'played a card' : 'tried to play an invalid card';
group.emitMove({
player: player.num,
card: result.card,
cardIndex: play.cardIndex,
action: action
});
console.log(action);
group.updateUsers();
writeState(gameID);
}
catch(err) {
console.log(err.message)
player.emit('erra', err.message)
}
};
},
discard: function(player, group) {
return function(discard) {
discard = JSON.parse(discard)
try {
var discarded = state.discard(discard.player, discard.cardIndex);
group.emitMove({
player: player.num,
card: discarded,
cardIndex: discard.cardIndex,
action: 'discarded a card'
});
group.updateUsers();
writeState(gameID);
}
catch(err) {
console.log(err.message)
player.emit('erra', err.message)
}
};
}
}
};
this.groupMethods = function(gameID) {
var state = games[gameID];
return {
emitState: function(group) {
return function() {
group.users.forEach(function(player) {
console.log('emitting state to ' + player.name);
player.emit('gameState', state.stringify(player.num));
});
};
},
emitClue: function(group) {
return function(groupClue) {
group.sendAll('clue', JSON.stringify(groupClue));
};
},
emitMove: function(group) {
return function(groupMove) {
group.sendAll('move', JSON.stringify(groupMove));
};
},
updateUsers: function(group) {
return function() {
group.emitNames();
group.emitState();
}
},
userDead: function(group) {
return function() {
group.sendAll('killGame', '');
}
}
}
}
gameDB.getAll(function(foundGames) {
console.log('foundGames: ' + JSON.stringify(foundGames));
var gameKeys = _.keys(foundGames)
gameKeys.forEach(function(gameKey) {
if (foundGames[gameKey]) {
var intGameKey = parseInt(gameKey);
gameCounter = gameCounter > intGameKey + 1 ? gameCounter : intGameKey + 1;
games[gameKey] = Game.unfreezeGame( foundGames[gameKey] );
oldGameCallback(gameKey);
} else {
gameDB.delete(gameKey);
}
});
});
}
module.exports = function(ObjSet, oldGameCallback) {
return new GamesHandler(ObjSet, oldGameCallback);
};