-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
141 lines (107 loc) · 2.82 KB
/
server.js
File metadata and controls
141 lines (107 loc) · 2.82 KB
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
var HOST = null;
var PORT = 8080;
var GRID_SIZE = 10;
//holds each user's position in dictionary for fast retrieval
// key is user's id, values are coordinates in form {row : '0', col: '2'}
var positions = new Array();
//the grid
var grid = new Array(GRID_SIZE);
for (i=0; i < grid.length; i++) {
grid[i]=new Array(GRID_SIZE);
}
var mud = new function() {
var callbacks = [];
this.flush = function() {
while(callbacks.length > 0) {
callbacks.shift().call();
}
}
this.addCallback = function(callback) {
callbacks.push(callback);
}
setInterval(function() {
while(callbacks.length > 0) {
callbacks.shift().call();
}
}, 30 * 1000);
}
function destroyID(id) {
var p = getPos(id);
//delete id from grid and positions dictionary
grid[p.row][p.col] = null;
delete positions[id];
}
function genCoordPoint() {
return Math.floor(Math.random() * GRID_SIZE);
}
function setPos(id, row, col) {
grid[row][col] = id;
positions[id] = {row : row, col : col};
}
function getPos(id) {
return positions[id];
}
function movePos(id, direction) {
var p = getPos(id);
//sys.puts('<' + id + '>: moving ' + direction + ' from (' + p.row + ', ' + p.col + ')');
//wipe out old position
grid[p.row][p.col] = null;
switch(direction) {
case 'down':
if(p.row + 1 <= GRID_SIZE - 1) {setPos(id, p.row + 1, p.col);}
else { grid[p.row][p.col] = id;}
break;
case 'up':
if(p.row - 1 >= 0) {setPos(id, p.row - 1, p.col);}
else { grid[p.row][p.col] = id;}
break;
case 'right':
if(p.col + 1 <= GRID_SIZE - 1) {setPos(id, p.row, p.col + 1);}
else { grid[p.row][p.col] = id;}
break;
case 'left':
if(p.col - 1 >= 0) {setPos(id, p.row, p.col - 1);}
else { grid[p.row][p.col] = id;}
break;
}
}
var fu = require("./fu"),
sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
qs = require("querystring");
fu.listen(PORT, HOST);
fu.get("/", fu.staticHandler("index.html"));
fu.get("/style.css", fu.staticHandler("style.css"));
fu.get("/register", function(request, response) {
var id = qs.parse(url.parse(request.url).query).id;
setPos(id, genCoordPoint() , genCoordPoint());
response.simpleJSON(200, {
grid : grid
});
sys.puts('<' + id + '>: has joined');
mud.flush();
});
fu.get("/move", function(request, response) {
var id = qs.parse(url.parse(request.url).query).id;
var direction = qs.parse(url.parse(request.url).query).direction;
movePos(id, direction);
response.simpleJSON(200, {
grid : grid
});
mud.flush();
});
fu.get("/recv", function(request, response) {
mud.addCallback(function() {
response.simpleJSON(200, {
grid : grid
});
});
});
fu.get("/part", function(request, response) {
var id = qs.parse(url.parse(request.url).query).id;
destroyID(id);
sys.puts('<' + id + '>: has parted');
mud.flush();
});