forked from mcharters/hockeypool-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
176 lines (152 loc) · 4.29 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, io = require('socket.io')
, redis = require('redis');
/**
* data source setup
*/
var mongo = require('mongodb')
, mongoServer = new mongo.Server('localhost', 27017, {auto_reconnect: true})
, mongodb = new mongo.Db('hockeypool', mongoServer);
var redisClient = redis.createClient();
redisClient.flushdb();
var players = mongodb.collection('players');
players.find({}).each(function(err, player) {
if(player != null) redisClient.sadd('players', player.name);
});
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/admin', routes.admin);
app.get('/players', function(req, res) {
mongodb.collection('players', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send({
status: "success",
data: {
"players": items
}
});
});
});
});
app.get('/teams', function(req, res) {
mongodb.collection('teams', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send({
status: "success",
data: {
"teams": items
}
});
});
});
});
app.get('/bots', function(req, res) {
redisClient.smembers(function(err, replies) {
res.send({
status: "success",
data: {
"bots" : replies
}
});
});
});
var httpServer = http.createServer(app);
var socketServer = io.listen(httpServer);
httpServer.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var sockets = {};
var bots = [];
var numBots;
var turnIndex = 0;
var numRounds = 10;
var roundCount = 0;
var countUp = true;
socketServer.sockets.on('connection', function(socket) {
socket.on('register', function(botName) {
console.log("Registered " + botName);
socket.set('botName', botName);
redisClient.sadd('bots', botName);
sockets[botName] = socket;
});
socket.on('pick', function(playerName) {
console.log("Picked " + playerName);
socket.get('botName', function(err, botName) {
console.log(botName + " picked " + playerName);
redisClient.sismember('players', playerName, function(err, reply) {
if(reply) {
redisClient.sadd(botName, playerName);
redisClient.srem('players', playerName);
socketServer.sockets.emit('picked', {player: playerName, by: botName});
var endOfDraft = false;
// find out whose turn it is next
turnIndex = (countUp ? turnIndex + 1 : turnIndex - 1);
if(turnIndex == numBots || turnIndex < 0) {
// new round. time to snake!
roundCount++;
countUp = !countUp;
turnIndex = (countUp ? turnIndex + 1 : turnIndex - 1);
// is it the end?
if(roundCount == numRounds) {
endOfDraft = true;
}
}
if(endOfDraft) {
socketServer.sockets.emit('theEnd');
} else {
sockets[bots[turnIndex]].emit('yourTurn');
}
} else {
socket.emit('yourTurn');
}
});
});
});
socket.on('startDraft', function() {
console.log("Draft Started!");
redisClient.scard('bots', function(err, result) {
numBots = result;
// slot the bots into their draft order
redisClient.smembers('bots', function(err, replies) {
replies.forEach(function(reply, i) {
bots.push(reply);
});
// randomize draft order via fisher-yates shuffle
var i = bots.length, j, tempi, tempj;
if(i === 0) socketServer.sockets.emit('theEnd'); // no bots in the draft!
while(--i) {
j = Math.floor(Math.random() * (i+1));
tempi = bots[i];
tempj = bots[j];
bots[i] = tempj;
bots[j] = tempi;
}
// start the first round
sockets[bots[turnIndex]].emit('yourTurn');
});
});
});
});