-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-server.js
269 lines (239 loc) · 8.44 KB
/
game-server.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
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
//game-server.js
if (Meteor.isServer) {
Meteor.startup(function () {
Teams.remove({});
Teams.insert({ name: "Villagers", team_proportionality: 1, victory: "survive" });
Teams.insert({ name: "Werewolves", team_proportionality: 2, victory: "outnumber" });
Roles.remove({});
Roles.insert({ name: "Villager", team: "Villagers", is_default_role: true });
Roles.insert({ name: "Werewolf", team: "Werewolves", is_default_role: true });
Events.remove({});
Meteor.methods({
resetGameState: function (roomId) {
online_users = Meteor.users.find({ 'profile.online': true, 'profile.roomId': roomId }).fetch();
console.log('resetting game state');
//fisher-yates shuffle
for (var i = 0; i < online_users.length - 1; i++) {
j = getRandomIntBetween(i, online_users.length - 1);
temp = online_users[j];
online_users[j] = online_users[i];
online_users[i] = temp;
}
//First third of users are werewolves, the rest are villagers
//Assign everyone as villagers first to avoid seeing wolves
for (var i = 0; i < online_users.length; i++) {
Meteor.users.update({
username: online_users[i].username
}, {
$set: {
'profile.alive': true,
'profile.role': "Villager",
'profile.team': "Villagers",
'profile.reveal_role': null,
'profile.reveal_team': null
}
})
}
var third = Math.floor(online_users.length / 3);
for (var i = 0; i < online_users.length; i++) {
if (i < third) {
Meteor.users.update({
username: online_users[i].username
}, {
$set: {
'profile.alive': true,
'profile.role': "Werewolf",
'profile.team': "Werewolves",
'profile.reveal_role': null,
'profile.reveal_team': null
}
});
}
}
Gamestate.update({ _id: roomId }, {
$set: {
daytime: true,
day: 1,
winning_team: null
}
})
//reset votes
Votes.remove({})
startGameCountdown(GLOBAL_GAME_DAY_LENGTH, roomId);
return "whatever";
},
nextGameState: function (roomId) {
console.log('moving to next game state user: ' + Meteor.user() + ' in room: ' + roomId)
state = Gamestate.findOne({ _id: roomId })
if (state.daytime) {
//DAY ENDING
Meteor.call('murder', playerIdWithMostVotes('village', roomId), 'Village', roomId);
Votes.remove({'villageType': 'village' })
var victoryHappened = checkTeamVictories(roomId);
Gamestate.update({ _id: roomId }, {
$set: {
'daytime': false
}
})
console.log('day ending reset countdown');
if(!victoryHappened)
{
startGameCountdown(GLOBAL_GAME_NIGHT_LENGTH, roomId);
}
}
else {
//Check if a team has fulfilled their victory conditions
/*var total_alive = Meteor.users.find({'profile.alive' : true, 'profile.online' : true}).count();
Teams.find({}).forEach(function (this_team){
var victory = this_team.victory;
var teamCount = Meteor.users.find({'profile.alive' : true, 'profile.online' : true, 'profile.team' : this_team.name}).count();
if ((victory === "outnumber" && teamCount / total_alive >= 0.5) || (victory === "survive" && teamCount === total_alive)) {
Gamestate.update({},
{$set:
{ 'winning_team': this_team.name }
}
)
}
});*/
var victoryHappened = checkTeamVictories(roomId);
if(!victoryHappened)
{
startGameCountdown(GLOBAL_GAME_DAY_LENGTH, roomId);
}
Meteor.call('murder', playerIdWithMostVotes('wolf', roomId), 'Werewolf', roomId);
Votes.remove({'villageType': 'wolf' })
Gamestate.update({ _id: roomId }, {
$inc: {
day: 1
}
})
Gamestate.update({ _id: roomId }, {
$set: {
'daytime': true
}
})
}
},
murder: function (id, type, roomId) {
console.log(id + ' is being killed by: ' + type + ' in room: ' + roomId)
victim = Meteor.users.findOne({ _id: id })
if (victim && victim.profile.alive) {
Events.insert({
text: "A murder happened bro",
createdAt: new Date()
})
Meteor.users.update({ _id: id }, { $set: { 'profile.alive': false, 'profile.death': getDeath(type), 'profile.death_location': getLocation(), 'profile.reveal_role': victim.profile.role, 'profile.reveal_team': victim.profile.team} })
}
Votes.remove({'voteFrom': id})
},
castVote: function (voteFrom, voteFor, type, roomId) {
Votes.upsert(
{
voteFrom: voteFrom,
voteType: type,
roomId: Meteor.user().profile.roomId
},
{
voteFrom: voteFrom,
votefor: voteFor,
voteType: type,
roomId: Meteor.user().profile.roomId
})
},
tallyVote: function (id, type) {
}
})
})
};
// Returns a random integer between min and max, inclusive
function getRandomIntBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getDeath(type) {
deaths = flavor_text[type]
return deaths[getRandomIntBetween(0, deaths.length - 1)]
}
function getLocation() {
location = flavor_text['Location']
return location[getRandomIntBetween(0, location.length - 1)]
}
/*
* returns true if there was a victory
*/
function checkTeamVictories(roomId) {
console.log('checking team victories for room ' + roomId)
//Check if a team has fulfilled their victory conditions
var total_alive = Meteor.users.find({ 'profile.alive': true, 'profile.online': true, 'profile.roomId': roomId }).count();
console.log('total_alive: ' + total_alive)
Teams.find({}).forEach(function (this_team) {
var victory = this_team.victory;
var teamCount = Meteor.users.find({ 'profile.alive': true, 'profile.online': true, 'profile.team': this_team.name, 'profile.roomId': roomId }).count();
console.log("checking the " + this_team.name + " victory condition of " + this_team.victory + " - teamCount: " + teamCount);
if ((victory === "outnumber" && teamCount / total_alive >= 0.5) || (victory === "survive" && teamCount === total_alive)) {
console.log(this_team.name + " has won!");
Gamestate.update({ _id: roomId },
{
$set:
{ 'winning_team': this_team.name }
}
)
return true;
}
});
return false;
}
function startGameCountdown(countdownTime, roomId) {
/*
console.log('starting countdown');
var oldHandle = Gamestate.findOne({ _id: roomId }).timeoutHandle
console.log('old handle is:' + oldHandle);
if (oldHandle)
{
console.log('clearing old timer');
Meteor.clearTimeout(oldHandle)
}
//set up the time for the first day end
var date = new Date();
date.setSeconds(date.getSeconds() + countdownTime);
var timeoutHandle = Meteor.setTimeout(function () {
Meteor.call('nextGameState', roomId);
}, countdownTime * 1000)
console.log('tiemout handle is ' + timeoutHandle.id);
Gamestate.update({ _id: roomId }, {
$set: {
nextEvent: date//,
//timeoutHandle: timeoutHandle
}
})
*/
}
function continueGame(roomId)
{
if(Gamestate.findOne({ _id: roomId }).winning_team == null)
{
Meteor.call('nextGameState', roomId);
var timeoutHandle = Meteor.setTimeout(function () {
}, countdownTime * 1000)
}
}
function playerIdWithMostVotes(type, roomId) {
console.log("Calculating votes of type: " + type + " in room: " + roomId);
var v = [];
Meteor.users.find().forEach(function (player) {
v[player._id.toString()] = Votes.find({
votefor: player._id,
voteType: type,
'roomId': roomId
}).count()
})
leader = null;
currentBest = 0;
for (var userId in v) {
//console.log(userId)
if ((leader == null && v[userId] > 0) || v[userId] > currentBest) {
leader = userId;
currentBest = v[userId]
}
}
return leader;
}