Skip to content

Commit f06b51d

Browse files
committed
On the server, separate the network connection from the underlying creature it controls. Send an identify message to the client with the client's own creature id, so that the client knows which one to display differently.
1 parent 7bf2c14 commit f06b51d

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

gameclient.as

+9-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ package {
6767
// Map objects:
6868
public var items:Object = {}; // {loc.toString(): {sprite: obj:}}
6969
public var creatures:Object = {}; // {obj id: clientId: {sprite: bitmap: obj:}}
70-
70+
public var myCreatureId:String = "";
71+
7172
public var colorMap:Array = [];
7273
public var client:Client = new Client();
7374
public var pingTime:TextField = new TextField();
@@ -280,7 +281,7 @@ package {
280281

281282
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
282283

283-
client.sendMessage({type: 'identify', name: playerName, sprite_id: spriteId});
284+
client.sendMessage({type: 'client_identify', name: playerName, sprite_id: spriteId});
284285
client.sendMessage({type: 'move', from: location, to: location});
285286
onEnterFrame(null);
286287
}
@@ -382,8 +383,10 @@ package {
382383
private var mapBlocks:Object = {};
383384
public function handleMessage(message:Object, binaryPayload:ByteArray):void {
384385
var bitmap:Bitmap;
385-
386-
if (message.type == 'move_ok') {
386+
387+
if (message.type == 'server_identify') {
388+
myCreatureId = message.id;
389+
} else if (message.type == 'move_ok') {
387390
moving = false;
388391
location = message.loc;
389392

@@ -442,12 +445,13 @@ package {
442445
} else if (message.type == 'creature_ins') {
443446
// HACK: until we separate server's player representation from connection
444447
message.obj.sprite_id = message.obj.sprite_id || message.obj.spriteId;
445-
448+
446449
bitmap = new Bitmap(new BitmapData(playerBitmap.width, playerBitmap.height, true, 0x00000000));
447450
char_spritesheet.drawToBitmap(message.obj.sprite_id, bitmap.bitmapData, playerStyle);
448451
bitmap.x = mapScale * message.obj.loc[0] - playerStyle.padding;
449452
bitmap.y = mapScale * message.obj.loc[1] - playerStyle.padding;
450453
characterLayer.addChild(bitmap);
454+
if (message.obj.id == myCreatureId) bitmap.visible = false; // it's me!
451455
if (creatures[message.obj.id] != null) Debug.trace("ERROR: ins creature, already exists at ", message.obj.id);
452456
creatures[message.obj.id] = {sprite: bitmap, obj: message.obj};
453457
} else if (message.type == 'creature_del') {

gameserver.js

+20-25
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function simblockLocationToId(blockX, blockY) {
5656
return blockX + blockY * span;
5757
} else {
5858
util.log("ERROR: simblockLocationToId(" + blockX + "," + blockY + ") span=" + span);
59-
return -1;
59+
return null;
6060
}
6161
}
6262

@@ -185,18 +185,17 @@ function moveCreature(creature, to) {
185185

186186
// Class to handle a single game client
187187
function Client(connectionId, log, sendMessage) {
188-
this.id = connectionId;
188+
this.creature = {id: connectionId, name: '??', sprite_id: null, loc: null};
189189
this.messages = [];
190-
this.name = '??'
191-
this.spriteId = null;
192-
this.loc = null;
193190
this.subscribedTo = []; // list of block ids
194191
this.eventIdPointer = eventId; // this event and newer remain to be processed
195192

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;
199195

196+
// Tell the client which of the creature ids is itself
197+
sendMessage({type: 'server_identify', id: connectionId});
198+
200199
function sendChatToAll(chatMessage) {
201200
for (var clientId in clients) {
202201
clients[clientId].messages.push(chatMessage);
@@ -209,10 +208,6 @@ function Client(connectionId, log, sendMessage) {
209208
sendMessage({type: 'item_ins', obj: obj});
210209
});
211210
(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.
216211
sendMessage({type: 'creature_ins', obj: obj});
217212
});
218213
}
@@ -262,15 +257,15 @@ function Client(connectionId, log, sendMessage) {
262257

263258

264259
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,
270265
systemtext: " has connected.", usertext: ""});
271266
} else if (message.type == 'move') {
272267
// TODO: make sure that the move is valid
273-
moveCreature(this, message.to);
268+
moveCreature(this.creature, message.to);
274269

275270
// NOTE: we must flush all events before subscribing to
276271
// new blocks, or we'll end up sending things twice. For
@@ -283,15 +278,15 @@ function Client(connectionId, log, sendMessage) {
283278
this.sendAllEvents();
284279

285280
// The list of simblocks that the client should be subscribed to
286-
var simblocks = simblocksSurroundingLocation(this.loc);
281+
var simblocks = simblocksSurroundingLocation(this.creature.loc);
287282
// Compute the difference between the new list and the old list
288283
var inserted = setDifference(simblocks, this.subscribedTo);
289284
var deleted = setDifference(this.subscribedTo, simblocks);
290285
// Set the new list on the server side
291286
this.subscribedTo = simblocks;
292287

293288
// 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};
295290
// Set the new list on the client side
296291
if (inserted.length > 0) reply.simblocks_ins = inserted;
297292
if (deleted.length > 0) reply.simblocks_del = deleted;
@@ -332,20 +327,20 @@ function Client(connectionId, log, sendMessage) {
332327
} else if (message.type == 'message') {
333328
// TODO: handle special commands
334329
// 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,
336331
systemtext: " says: ", usertext: message.message});
337332
} else {
338333
log(' -- unknown message type');
339334
}
340335
}
341336

342337
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,
345340
systemtext: " has disconnected.", usertext: ""});
346341
}
347-
moveCreature(this, null);
348-
delete clients[this.id];
342+
moveCreature(this.creature, null);
343+
delete clients[connectionId];
349344
}
350345
}
351346

0 commit comments

Comments
 (0)