@@ -56,7 +56,7 @@ function simblockLocationToId(blockX, blockY) {
56
56
return blockX + blockY * span ;
57
57
} else {
58
58
util . log ( "ERROR: simblockLocationToId(" + blockX + "," + blockY + ") span=" + span ) ;
59
- return - 1 ;
59
+ return null ;
60
60
}
61
61
}
62
62
@@ -185,18 +185,17 @@ function moveCreature(creature, to) {
185
185
186
186
// Class to handle a single game client
187
187
function Client ( connectionId , log , sendMessage ) {
188
- this . id = connectionId ;
188
+ this . creature = { id : connectionId , name : '??' , sprite_id : null , loc : null } ;
189
189
this . messages = [ ] ;
190
- this . name = '??'
191
- this . spriteId = null ;
192
- this . loc = null ;
193
190
this . subscribedTo = [ ] ; // list of block ids
194
191
this . eventIdPointer = eventId ; // this event and newer remain to be processed
195
192
196
-
197
- if ( clients [ this . id ] ) log ( 'ERROR: client id already in clients map' ) ;
198
- clients [ this . id ] = this ;
193
+ if ( clients [ connectionId ] ) log ( 'ERROR: client id already in clients map' ) ;
194
+ clients [ connectionId ] = this ;
199
195
196
+ // Tell the client which of the creature ids is itself
197
+ sendMessage ( { type : 'server_identify' , id : connectionId } ) ;
198
+
200
199
function sendChatToAll ( chatMessage ) {
201
200
for ( var clientId in clients ) {
202
201
clients [ clientId ] . messages . push ( chatMessage ) ;
@@ -209,10 +208,6 @@ function Client(connectionId, log, sendMessage) {
209
208
sendMessage ( { type : 'item_ins' , obj : obj } ) ;
210
209
} ) ;
211
210
( creatures [ blockId ] || [ ] ) . forEach ( function ( obj ) {
212
- // TODO: we currently mix up the player's character object
213
- // and the connection object, so we're sending extra
214
- // fields here like messages, eventIdPointer,
215
- // etc. Separate the objects.
216
211
sendMessage ( { type : 'creature_ins' , obj : obj } ) ;
217
212
} ) ;
218
213
}
@@ -262,15 +257,15 @@ function Client(connectionId, log, sendMessage) {
262
257
263
258
264
259
this . handleMessage = function ( message , binaryMessage ) {
265
- if ( message . type == 'identify ' ) {
266
- this . name = message . name ;
267
- this . spriteId = message . sprite_id ;
268
- moveCreature ( this , clientDefaultLocation ) ;
269
- sendChatToAll ( { from : this . name , sprite_id : this . spriteId ,
260
+ if ( message . type == 'client_identify ' ) {
261
+ this . creature . name = message . name ;
262
+ this . creature . sprite_id = message . sprite_id ;
263
+ moveCreature ( this . creature , clientDefaultLocation ) ;
264
+ sendChatToAll ( { from : this . creature . name , sprite_id : this . creature . sprite_id ,
270
265
systemtext : " has connected." , usertext : "" } ) ;
271
266
} else if ( message . type == 'move' ) {
272
267
// TODO: make sure that the move is valid
273
- moveCreature ( this , message . to ) ;
268
+ moveCreature ( this . creature , message . to ) ;
274
269
275
270
// NOTE: we must flush all events before subscribing to
276
271
// new blocks, or we'll end up sending things twice. For
@@ -283,15 +278,15 @@ function Client(connectionId, log, sendMessage) {
283
278
this . sendAllEvents ( ) ;
284
279
285
280
// The list of simblocks that the client should be subscribed to
286
- var simblocks = simblocksSurroundingLocation ( this . loc ) ;
281
+ var simblocks = simblocksSurroundingLocation ( this . creature . loc ) ;
287
282
// Compute the difference between the new list and the old list
288
283
var inserted = setDifference ( simblocks , this . subscribedTo ) ;
289
284
var deleted = setDifference ( this . subscribedTo , simblocks ) ;
290
285
// Set the new list on the server side
291
286
this . subscribedTo = simblocks ;
292
287
293
288
// The reply will tell the client where the player is now
294
- var reply = { type : 'move_ok' , loc : this . loc } ;
289
+ var reply = { type : 'move_ok' , loc : this . creature . loc } ;
295
290
// Set the new list on the client side
296
291
if ( inserted . length > 0 ) reply . simblocks_ins = inserted ;
297
292
if ( deleted . length > 0 ) reply . simblocks_del = deleted ;
@@ -332,20 +327,20 @@ function Client(connectionId, log, sendMessage) {
332
327
} else if ( message . type == 'message' ) {
333
328
// TODO: handle special commands
334
329
// TODO: handle empty messages (after spaces stripped)
335
- sendChatToAll ( { from : this . name , sprite_id : this . spriteId ,
330
+ sendChatToAll ( { from : this . creature . name , sprite_id : this . creature . sprite_id ,
336
331
systemtext : " says: " , usertext : message . message } ) ;
337
332
} else {
338
333
log ( ' -- unknown message type' ) ;
339
334
}
340
335
}
341
336
342
337
this . handleDisconnect = function ( ) {
343
- if ( this . spriteId != null ) {
344
- sendChatToAll ( { from : this . name , sprite_id : this . spriteId ,
338
+ if ( this . creature . sprite_id != null ) {
339
+ sendChatToAll ( { from : this . creature . name , sprite_id : this . creature . sprite_id ,
345
340
systemtext : " has disconnected." , usertext : "" } ) ;
346
341
}
347
- moveCreature ( this , null ) ;
348
- delete clients [ this . id ] ;
342
+ moveCreature ( this . creature , null ) ;
343
+ delete clients [ connectionId ] ;
349
344
}
350
345
}
351
346
0 commit comments