-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
165 lines (151 loc) · 4.5 KB
/
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
var express = require("express");
var app = express();
var port = 3700;
var _ = require('underscore');
app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");
app.engine('jade', require('jade').__express);
app.use(express.bodyParser())
app.use(express.logger('dev'))
app.use(express.static(__dirname + '/public'));
app.get("/", function(req, res){
res.render("index");
});
var io = require('socket.io').listen(app.listen(port));
/* game logistics */
var game = {
clients: {},
requiredNumOfPlayers: 3, //for testing ********
currLeader: null,
players: [],
currTeam: {},
teamMemberCount: 0,
teamMemberByRound: [3,4,4,5,5],
numVotesToLose: [1,1,1,2,1],
round: 0,
votes: [],
score: [],
missionVote: [[],[],[],[],[]]
};
var distributeIdentities = function() {
var arr = [];
for (var i = 0; i < game.requiredNumOfPlayers; i++) {
arr.push(i);
}
var shuffled = _.shuffle(arr);
for (var i = 0; i < game.players.length; i++) {
if (shuffled.pop() < 4) {
game.players[i][0].emit('identity', {'identity': 'spy'});
} else {
game.players[i][0].emit('identity', {'identity': 'spy'});
}
}
};
var startGame = function() {
distributeIdentities();
game.currLeader = 0;
game.players[game.currLeader][0].emit('leader', {});
};
var tallyVotes = function() {
var thisEvent = 'rejected';
var voteResult = _.reduce(_.pluck(game.votes, 'vote'), function(memo, num){ return memo + num; }, 0);
if (voteResult > (game.requiredNumOfPlayers / 2)) {
thisEvent = 'approved';
//TODO
//set timeout for 10 seconds
//send mission modal to the teamates
setTimeout(function() {
_.each(game.currTeam, function(isOnTeam, socketId) {
game.clients[socketId].emit('mission');
});
}, 1000);
} else { //failed
//TODO clear team members
//set leader to next person
//clear current leader
}
io.sockets.emit(thisEvent, {
votes: game.votes
});
};
/* socket events */
io.sockets.on('connection', function (socket) {
//new player created
game.clients[socket.id] = socket;
socket.on('newPlayer', function (data) {
_.each(game.players, function(player) {
socket.emit('newPlayerJoined', {
socketId: player[0].id,
playerName: player[1].playerName
});
});
game.players.push([socket, data]);
socket.emit('socketId', {socketId: socket.id});
socket.broadcast.emit('newPlayerJoined', {
socketId: socket.id,
playerName: data.playerName
});
if (game.players.length === game.requiredNumOfPlayers) {
startGame();
}
});
socket.on('select', function(data) {
if (game.currTeam[data.socketId]) {
delete game.currTeam[data.socketId];
game.teamMemberCount--;
io.sockets.emit('removeMember', {
socketId: data.socketId
});
} else {
game.currTeam[data.socketId] = true;
game.teamMemberCount++;
io.sockets.emit('nominateMember', {
socketId: data.socketId
});
}
});
socket.on('submitTeam', function() {
if (game.teamMemberCount === game.teamMemberByRound[game.round]) {
io.sockets.emit('voteOnTeam', {});
} else {
socket.emit('invalidTeam', {'message': "Need to pick " + game.teamMemberByRound[game.round] + " team members!"});
}
});
socket.on('approve', function(){
game.votes.push({socketId: socket.id, vote: 1});
if (game.votes.length === game.requiredNumOfPlayers) {
tallyVotes();
}
});
socket.on('disapprove', function(){
game.votes.push({socketId: socket.id, vote: 0});
if (game.votes.length === game.requiredNumOfPlayers) {
tallyVotes();
}
});
socket.on('missionCompleted', function(data) {
if (game.currTeam[socket.id]) { //if this is a team member
if (data.result === 'success') {
game.missionVote[game.round].push(0);
console.log('success');
} else {
game.missionVote[game.round].push(1);
console.log('fail');
}
if (game.missionVote[game.round].length === game.teamMemberByRound[game.round]) {
var score = _.reduce(game.missionVote[game.round], function(memo, num){ return memo + num; }, 0);
console.log(score);
if (score >= game.numVotesToLose[game.round]) {
game.score.push('fail');
} else {
game.score.push('success');
}
io.sockets.emit('missionResult', {
result: score,
numVotesToLose: game.numVotesToLose[game.round]
});
}
}
});
});
console.log("Listening on port " + port);