-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.coffee
333 lines (289 loc) · 8.91 KB
/
db.coffee
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
db_url = "127.0.0.1"
mongoose = require('mongoose')
bcrypt = require('bcrypt')
db = mongoose.connect(db_url)
#
# Database schema definition
#
GAME_LOBBY = 0
GAME_PREGAME = 1
GAME_PROPOSE = 2
GAME_VOTE = 3
GAME_PTRC_PROPOSE = 4
GAME_PTRC_VOTE = 5
GAME_QUEST = 6
GAME_LADY = 7
GAME_ASSASSIN = 8
GAME_FINISHED = 9
ObjectId = mongoose.Schema.Types.ObjectId
playerSchema = new mongoose.Schema
name : String
password : String
socket : String
currentGame : ObjectId
#playerSchema.methods.set_password = (password, cb) ->
# bcrypt.hash password, 8, (err, hash) ->
# this.password = hash
# cb()
#
#playerSchema.methods.check_password = (password, cb) ->
# bcrypt.compare password, this.password, (err, res) ->
# cb(err, res)
playerSchema.methods.leave_game = (cb) ->
player = this
Game.findById this.currentGame, (err, game) ->
return if err || not game
for p in game.players
if p.id.equals(player._id)
if game.state == GAME_LOBBY || game.state == GAME_PREGAME
index = game.players.indexOf(p)
game.players.splice(index, 1)
else
p.left = true
p.socket = undefined
break
if game.players.length == 0
game.remove()
game.save (err, game) ->
cb(err, game)
Player = mongoose.model('Player', playerSchema)
gameSchema = new mongoose.Schema
state : {type: Number, default: GAME_LOBBY}
gameOptions : {
mordred : Boolean
oberon : Boolean
showfails : Boolean
ladylake : Boolean
ptrc : Boolean
hidden_ptrc : Boolean
danmode : Boolean
}
roles : [
name : String
isEvil : Boolean
]
players : [
id : {type: ObjectId, ref: 'Player'}
name : String
socket : String
order : Number
role : String
isEvil : Boolean
left : {type: Boolean, default: false}
info : [
otherPlayer : String
information : String
]
]
missions : [
numReq : Number
failsReq : Number
players : [
id : ObjectId
success : Boolean
]
status : {type: Number, default: 0}
]
votes : [
mission : Number
team : [ObjectId]
accepted : [ObjectId]
rejected : [ObjectId]
votes : [
id : ObjectId
vote : Boolean
]
]
currentMission : Number
currentLeader : ObjectId
finalLeader : ObjectId
lady : ObjectId
pastLadies : [ObjectId]
evilWon : Boolean
assassinated : ObjectId
reconnect_vote : [Number]
reconnect_user : String
reconnect_sock : String
created : {type: Date, default: Date.now}
gameSchema.methods.name = () ->
names = @players.map (p) -> p.name
return names.join(', ')
gameSchema.methods.add_player = (p) ->
for gp in this.players
if gp.name == p.name
return false
this.players.push
id : p._id
name : p.name
socket : p.socket
role : undefined
isEvil : undefined
info : []
return true
gameSchema.methods.get_player = (id) ->
for p in this.players
if p.id.equals(id)
return p
return null
mission_reqs =
5 : [2, 3, 2, 3, 3]
6 : [2, 3, 4, 3, 4]
7 : [2, 3, 3, 4, 4]
8 : [3, 4, 4, 5, 5]
9 : [3, 4, 4, 5, 5]
10 : [3, 4, 4, 5, 5]
11 : [4, 5, 5, 6, 6]
12 : [4, 5, 5, 6, 6]
gameSchema.methods.setup_missions = () ->
np = this.players.length
for i in [0..4]
this.missions.push
numReq : mission_reqs[np][i]
failsReq : if i == 3 && np >= 7 then 2 else 1
players : []
this.currentMission = 0
gameSchema.methods.set_next_leader = (new_mission) ->
next = -1
for p in this.players
if p.id.equals(this.currentLeader)
next = (p.order + 1) % this.players.length
final = -1
for p in this.players
if p.order == next
this.currentLeader = p.id
final = (p.order + 4) % this.players.length
if new_mission
for p in this.players
if p.order == final
this.finalLeader = p.id
gameSchema.methods.check_for_game_end = () ->
succ = ((if m.status == 2 then 1 else 0) for m in this.missions)
succ = succ.sum()
fail = ((if m.status == 1 then 1 else 0) for m in this.missions)
fail = fail.sum()
if succ == 3
hasAssassin = false
for p in this.players
if p.role == "Assassin"
hasAssassin = true
this.currentLeader = p.id
this.state = GAME_ASSASSIN
if not hasAssassin
this.state = GAME_FINISHED
else if fail == 3
this.state = GAME_FINISHED
this.evilWon = true
else
this.currentMission += 1
if this.gameOptions.ladylake && this.currentMission >= 2
this.state = GAME_LADY
else
this.state = GAME_PROPOSE
shuffle = (a) ->
for i in [a.length-1..1]
j = Math.floor Math.random() * (i + 1)
[a[i], a[j]] = [a[j], a[i]]
return a
gameSchema.methods.start_game = (order) ->
this.state = GAME_PROPOSE
if this.gameOptions.danmode
for p, i in this.players
if p.name == "Alex"
p.role = "Servant"
p.isEvil = false
this.roles.push
name : "Servant"
isEvil : false
else
p.role = "Minion"
p.isEvil = true
this.roles.push
name : "Minion"
isEvil : true
if i == 0
p.order = 0
else
p.order = order[p.id]
else
this.roles.push
name : "Merlin"
isEvil : false
this.roles.push
name : "Assassin"
isEvil : true
this.roles.push
name : "Percival"
isEvil : false
this.roles.push
name : "Morgana"
isEvil : true
cur_evil = 2
num_evil = Math.ceil(this.players.length / 3)
if this.gameOptions.mordred && cur_evil < num_evil
this.roles.push
name : "Mordred"
isEvil : true
cur_evil += 1
if this.gameOptions.oberon && cur_evil < num_evil
this.roles.push
name : "Oberon"
isEvil : true
cur_evil += 1
#Fill evil
while (cur_evil < num_evil)
this.roles.push
name : "Minion"
isEvil : true
cur_evil += 1
#Fill good
while (this.roles.length < this.players.length)
this.roles.push
name : "Servant"
isEvil : false
#Assign roles
playerroles = shuffle(this.roles)
for p, i in this.players
r = playerroles.pop()
p.role = r.name
p.isEvil = r.isEvil
if i == 0
p.order = 0
else
p.order = order[p.id]
#Sort by order
this.players.sort((a, b) -> a.order - b.order)
#Give info
for p in this.players
switch p.role
when "Merlin", "Assassin", "Minion", "Morgana", "Mordred"
for o in this.players
if o.isEvil
if p.role == "Merlin" && o.role == "Mordred"
continue
if p.role != "Merlin" && o.role == "Oberon"
continue
p.info.push
otherPlayer : o.name
information : "evil"
when "Percival"
for o in this.players
if o.role == "Merlin" || o.role == "Morgana"
p.info.push
otherPlayer : o.name
information : "magic"
this.setup_missions()
leader = Math.floor Math.random() * this.players.length
this.currentLeader = this.players[leader].id
final = (this.players[leader].order + 4) % this.players.length
for p in this.players
if p.order == final
this.finalLeader = p.id
if this.gameOptions.ladylake
lady = this.players[leader].order - 1
if lady == -1
lady = this.players.length - 1
for p in this.players
if p.order == lady
this.lady = p.id
this.pastLadies = [this.lady]
Game = mongoose.model('Game', gameSchema)