-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmud.js
More file actions
executable file
·309 lines (137 loc) · 3.81 KB
/
mud.js
File metadata and controls
executable file
·309 lines (137 loc) · 3.81 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//TODO get state of other player from client WIP
//TODO display other players
//TODO make it scheduled
//TODO initially other plys not sent
//TODO test mode with automatic users
var http = require('http');
var url = require('url');
var fs= require('fs');
//only one room at the mo
var room1 ={
h:6,
w:6,
map:
[1,1,1,1,1,1,
1,1,1,1,1,1,
1,1,1,1,1,1,
1,1,1,1,1,1,
1,1,1,1,1,1,
1,1,1,1,1,1]
};
//the walls are not resent but this is resent on player action
var activeObjects=[
{name:"chest",x:4,y:4}
];
//all users in the room and their state/ coordinates,
//resent when something changes
var usersInRoom=[];
//to get state of other players
function getOtherUsers(key){
var ret =[];
for( var i =0;i<usersInRoom.length;i++){
if( usersInRoom[i].userid !== key){
ret.push(usersInRoom[i]);
}
}
return ret;
}
//the usr is created on this get
function enterroom (
// usr
){
//TMP generating unique key
var ukey = "usr"+usersInRoom.length;
console.log("usr "+ukey+" getting initial room " + room1 );
//TMP
var userdef ={userid:ukey,x:1,y:1};
usersInRoom.push(userdef);
return {user_data:userdef,
walls:room1,
active_objects:activeObjects
};
}
function updateServerdataFromPlyAction(data){
console.log("upd serv from ply ");
//we need to replace data player, the others will get it on next ping
var acting_player = data.ply;
for(var i=0;i<usersInRoom.length;i++){
if(usersInRoom[i].userid===acting_player.userid){
usersInRoom[i]=acting_player;
break;
}
}
console.log("ply list updated " + JSON.stringify(usersInRoom));
}
function serveFile(name, res){
var src = fs.createReadStream("./static/"+name);
console.log(src);
console.log(src.toString());
src.pipe(res);
// res.end(src);
}
var server = http.createServer(function (req, res) {
// request handling logic...
// console.log();
var myrequrl=url.parse(req.url);
console.log(myrequrl);
if( myrequrl.pathname === "/index.html" ){
serveFile("index.html",res);
}
else
if( myrequrl.pathname === "/jquery.js" ){
serveFile("jquery.js",res);
}
else
if( myrequrl.pathname === "/main.js" ){
serveFile("main.js",res);
}
else
//waiting for /api/getroom?usr=toto
if( myrequrl.pathname === "/api/enterroom" ){
// var usr = myrequrl.query.split("=")[1];
// console.log(usr)
res.end(JSON.stringify(
enterroom(
// usr
)
));
// res.
}
else
if( myrequrl.pathname === "/api/sendupdate" ){
console.log("sendupdate");
if(req.method=='POST'){
var jsonString='';
req.on('data',function(data){
jsonString += data;
});
req.on('end',function(){
// console.log(JSON.parse(jsonString));
console.log("data received" +JSON.parse(jsonString));
updateServerdataFromPlyAction(JSON.parse(jsonString));
res.end();
});
}
//res.end();
// res.
}
else // work around for the client to get notifications ( polled every nth second )
if( myrequrl.pathname === "/api/getupdate" ){
console.log("getupdate");
if(req.method=='POST'){// post with userkey to defeat caching
var jsonString='';
req.on('data',function(data){
jsonString += data;
});
req.on('end',function(){
console.log("data received" +jsonString);
var ret=JSON.stringify({ret:getOtherUsers(JSON.parse(jsonString).userkey)});
console.log("ret "+ret);
res.end(ret);
});
}
//res.end();
// res.
}
});
server.listen(process.argv[2]);