-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
140 lines (140 loc) · 4.38 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
(function() {
var IO;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
IO = 10;
module.exports = {
start: function(callback) {
var History, RandomGUID, SelectNext, express;
this.server = (express = require('express'))();
this.server.set('views', "views");
this.server.set('view engine', 'jade');
this.server.use(express.bodyParser());
this.server.use(express.static('public'));
this.server.set('view options', {
pretty: true
});
this.server.get('/', function(req, res) {
return res.render('points');
});
IO = require('socket.io').listen(this.server.listen(7777, callback));
IO.set('log level', 1);
History = [];
IO.sockets.on('connection', function(player) {
player.on('login', function(msg) {
var name, _ref;
if (name = (_ref = msg.match(/I am (.+)$/)) != null ? _ref[1] : void 0) {
this.set('name', name);
return this.emit('welcome', "Logged in as " + name);
} else {
this.emit('sorry', "Try again later");
return this.disconnect('unauthorized');
}
});
player.on('chat', function(text) {
return this.get('name', function(err, name) {
var msg;
if (name) {
msg = {
text: text,
sender: name
};
IO.sockets.emit('chat', msg);
return History.push({
what: 'chat',
msg: msg
});
}
});
});
player.on('host', function(match) {
return this.get('name', __bind(function(err, name) {
if (name) {
match.id = RandomGUID();
match.host = name;
this.join(match.id);
IO.sockets.emit('host', match);
return History.push({
what: 'host',
msg: match
});
}
}, this));
});
player.on('join', function(match) {
return this.get('name', __bind(function(err, name) {
if (name) {
this.join(match.id);
match.player = name;
IO.sockets.emit('join', match);
return History.push({
what: 'join',
msg: match
});
}
}, this));
});
player.on('msg', function(msg) {
return this.get('name', __bind(function(err, name) {
if (name) {
msg.sender = name;
msg.next = SelectNext(msg.match, name);
IO.sockets["in"](msg.match).emit('msg', msg);
return History.push({
what: 'msg',
msg: msg
});
}
}, this));
});
player.on('history', function(msg) {
var m, _i, _len, _results;
_results = [];
for (_i = 0, _len = History.length; _i < _len; _i++) {
m = History[_i];
_results.push(this.emit(m.what, m.msg));
}
return _results;
});
return player.on('logout', function(msg) {
return console.log("logout");
});
});
RandomGUID = function() {
return ((1 + Math.random()) * 0x1000000 | 0).toString(16).substring(1);
};
return SelectNext = function(match, activePlayer) {
var n, s;
return ((function() {
var _i, _len, _ref, _results;
_ref = (function() {
var _j, _len, _ref, _results2;
_ref = IO.sockets.clients(match);
_results2 = [];
for (_j = 0, _len = _ref.length; _j < _len; _j++) {
s = _ref[_j];
_results2.push(s.store.data.name);
}
return _results2;
})();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
n = _ref[_i];
if (n !== activePlayer) {
_results.push(n);
}
}
return _results;
})())[0];
};
},
stop: function(callback) {
IO.server.close();
return callback();
},
gcp: function() {
return require('socket.io-client').connect("http://localhost:7777", {
'force new connection': true
});
}
};
}).call(this);