-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrid-server.js
150 lines (130 loc) · 5.18 KB
/
grid-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
var sys = require('sys'),
app = require('http').createServer(),
sio = require('socket.io'),
fs = require('fs'),
json = JSON.stringify;
var sids = new Array();
var users = new Array();
var kicked = new Array();
var colors = ['#05FDFD', '#FA7005']
var accepted_actions = ['move', 'speak', 'conn', 'info', 'thekick', 'theban'];
var currentTime;
var WORLD_W = 1500,
WORLD_Y = 1500;
app.listen(8080);
var io = sio.listen(app);
io.configure('production', function(){
io.set('log level', 1);
io.set('transports', ['websocket']);
});
io.sockets.on('connection', function(socket){
socket.ip = socket.handshake.address.address;
socket.on('message', function(message){
if (kicked.indexOf(socket.ip) != -1) {
socket._onDisconnect();
return false;
}
try {
request = JSON.parse(message.replace('<', '<').replace('>', '>'));
} catch (SyntaxError) {
console.log('Invalid JSON:');
console.log(message);
return false;
}
request.id = socket.id;
if(accepted_actions.indexOf(request.action) == -1) {
console.log('Invalid request:' + "\n" + message);
return false;
}
if(request.action == 'conn') {
request.name = request.name.substring(0,15);
for(i in sids){
var s = sids[i];
socket.send(json({
action:'conn',
id: s,
name:users[s].name,
color:users[s].color,
x: users[s].x,
y: users[s].y
}));
}
request.color = colors[Math.round(Math.random())];
socket.send(json({
action:'me',
id: socket.id,
name: request.name,
color: request.color
}));
var access = fs.createWriteStream('/home/azlyth/thegrid/access.log', {flags:'a'});
currentTime = new Date();
access.write("[" + currentTime.toUTCString() + "] " + request.name + " (SID: " + socket.id + " IP: " + socket.ip +") connected.\n");
sids.push(socket.id);
users[socket.id] = {name:request.name, color:request.color, ip:socket.ip, x:request.x, y:request.y};
} else if (sids.indexOf(socket.id) == -1) {
return false;
}
if(request.action == 'move') {
if ( request.x < 0 || request.x > WORLD_W || request.y < 0 || request.y > WORLD_Y) return false;
if(users[socket.id])
{
users[socket.id].x = request.x;
users[socket.id].y = request.y;
}
}
if(request.action == 'speak') {
var chatlog = fs.createWriteStream('/home/azlyth/thegrid/chat.log', {flags:'a'});
currentTime = new Date();
chatlog.write("[" + currentTime.toUTCString() + "] " + users[socket.id].name + ": " + request.chat +"\n");
request.chat = request.chat.substring(0,140);
io.sockets.send(json(request));
return true;
}
if(request.action == 'info') {
console.log("\nINFO\n");
console.log("Total clients: " + sids.length);
console.log("Kicked: " + kicked);
console.log("");
for (i in sids){
s = sids[i];
console.log("sid: " + s);
console.log("ip: " + users[s].ip);
console.log("name: " + users[s].name);
console.log("color: " + users[s].color);
console.log("pos: " + users[s].x + ", " + users[s].y);
console.log("");
}
}
if(request.action == 'thekick' || request.action == 'theban') {
kicked.push(request.ip);
console.log(request.action + ": " + request.ip);
if (request.action == 'thekick') {
var ip = request.ip;
var duration = parseInt(request.duration);
var unkick = function(){
return (function(){
delete kicked[kicked.indexOf(ip)];
console.log(kicked);
console.log("unkicked: " + ip);
console.log("time served: " + duration+"ms");
});
};
setTimeout(unkick(), duration);
}
return false;
}
socket.broadcast.send(json(request));
});
socket.on('disconnect', function(){
io.sockets.send(json({'id': socket.id, 'action': 'close'}));
if (sids.indexOf(socket.id) != -1) {
currentTime = new Date();
var access = fs.createWriteStream('/home/azlyth/thegrid/access.log', {flags:'a'});
access.write("[" + currentTime.toUTCString() + "] " + users[socket.id].name + " (SID: " + socket.id + " IP: " + socket.ip +") disconnected.\n");
sids.splice(sids.indexOf(socket.id),1);
delete users[socket.id];
} else {
console.log("on dc, cannot find: " + socket.id);
}
});
});