-
Notifications
You must be signed in to change notification settings - Fork 23
/
server.js
199 lines (169 loc) · 4.37 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
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
var express = require('express');
var app = express();
var server = require('http').createServer(app)
var io = require('socket.io').listen(server);
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.compress());
app.use(app.router);
app.use('/assets', express.static(__dirname + '/public'));
app.use('/assets/js', express.static(__dirname + '/js'));
//TODO: matchmaking
//TODO: game syncing
var gameId = 0;
var games = {};
var pool = {};
var dudeToGame = {};
var scores = {"'green": 1000,
"'blue" : 1000}
function notMe(game, me) {
for(var i in game["players"]) {
if(i != me) return game["players"][i];
}
return false;
}
var colors = ["'green", "'blue"];
var lastColor = 0;
var gameNumberThreshold = 100;
function nextColor() {
lastColor++;
if(lastColor > (colors.length - 1)) {
lastColor = 0;
}
return colors[lastColor];
}
function oppositeColor(c) {
if(c == "'blue") {
return "'green";
}
return "'blue";
}
function findOpposite(dude) {
for(var id in pool) {
if(id == dude.socket.id) continue;
if(pool[id].color != dude.color) {
return pool[id];
}
}
return false;
}
function now() {
return (new Date()).getTime();
}
function pickLevel() {
return "first";
}
function makeGame(p1, p2) {
delete pool[p1.id];
delete pool[p2.id];
var game = gameId++;
dudeToGame[p1.id] = dudeToGame[p2.id] = game;
games[game] = {created: now(), players: {}};
games[game]["players"][p1.id] = p1;
games[game]["players"][p2.id] = p2;
var level = pickLevel();
p1.socket.emit('game', { "'color": p1.color, "'opponent-color": p2.color, "'level": level});
p2.socket.emit('game', { "'color": p2.color, "'opponent-color": p1.color, "'level": level});
}
var fiveMinutes = 5 * 60 * 1000;
function prune() {
var old = now() - fiveMinutes;
for(var i in games) {
if(games[i].created <= old) {
delete games[i];
}
}
}
function match() {
if(Object.keys(pool).length == 0
|| Object.keys(games).length >= gameNumberThreshold) { process.nextTick(match); return; }
prune();
for(var i in pool) {
var dude = pool[i];
var other = findOpposite(dude);
console.log
if(other) {
console.log("making a game");
makeGame(dude, other);
console.log("made a game");
}
return process.nextTick(match); //try matching all the time
}
}
function report() {
console.log("Games running: " + Object.keys(games).length);
console.log("Pool size: " + Object.keys(pool).length);
setTimeout(report, 5000);
}
report();
function queue(me) {
pool[me.id] = me;
}
function removeFromGame(g, me) {
try {
delete games[g]["players"][me.id];
delete dudeToGame[me.id];
//tell the other client player dropped
var other = notMe(games[g], me.id);
if(other && other.socket) {
other.socket.emit("opponent-dropped", {});
} else {
//you're both gone
console.log("Both dc'd removing game: " + g);
delete games[g];
}
} catch (e) {
console.log("Failed remove: " + e.stack);
}
}
io.sockets.on('connection', function (socket) {
var me = {id: socket.id, socket: socket, color: nextColor()};
socket.on("match", function() {
queue(me);
});
socket.on('action', function (data) {
var g = dudeToGame[me.id];
if(g) {
var other = notMe(games[g], me.id);
if(other && other.socket) {
other.socket.emit("action", data);
}
}
});
socket.on("end", function (data) {
var g = dudeToGame[me.id];
if(games[g] && !games[g]["won"]) {
me.socket.emit("win", {"'win": true});
games[g]["won"] = true;
var other = notMe(games[g], me.id);
if(other && other.socket) {
other.socket.emit("win", {"'win": false});
removeFromGame(g, other);
//add to global points
}
} else {
me.socket.emit("win", {"'win": false});
}
removeFromGame(g, me);
//add to global points
});
socket.on("disconnect", function() {
var g = dudeToGame[me.id];
console.log("disconnected: " + me.id);
if(g) {
console.log("disconnect from: " + g);
removeFromGame(g, me)
} else {
if(pool[me.id]) {
delete pool[me.id];
}
}
});
});
io.set("log level", 1);
server.listen(3000);
match();
console.log('Listening on port 3000');