Skip to content

Commit

Permalink
removing objects on disconnection now
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsanul committed May 27, 2011
1 parent b15372d commit d4493b3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
11 changes: 11 additions & 0 deletions js/xarkon2.coffee
Expand Up @@ -79,6 +79,17 @@ processMessage =
$("##{id}")
.css(left: x, top: y)

d: (msg)->
until msg.length == 0
l = msg.length
obj = msg.slice(l-1, l)
msg = msg.slice(0, l-1)

id = obj.charCodeAt(0)

$("##{id}").remove()


# updating positions via velocities
j: (msg)->
for id, vel of JSON.parse(msg)
Expand Down
12 changes: 12 additions & 0 deletions js/xarkon2.js
Expand Up @@ -91,6 +91,18 @@ processMessage = {
}
return _results;
},
d: function(msg) {
var id, l, obj, _results;
_results = [];
while (msg.length !== 0) {
l = msg.length;
obj = msg.slice(l - 1, l);
msg = msg.slice(0, l - 1);
id = obj.charCodeAt(0);
_results.push($("#" + id).remove());
}
return _results;
},
j: function(msg) {
var id, l, t, vel, _ref, _ref2, _results;
_ref = JSON.parse(msg);
Expand Down
2 changes: 1 addition & 1 deletion lib/socket.io
25 changes: 18 additions & 7 deletions xarkon-server.coffee
Expand Up @@ -235,14 +235,24 @@ SerializeCreate = $G(
return output
sendCreate: (objects)->
@send @serializeCreate(objects)
serializeDestroy: (objects)->
output = 'd'
for obj in objects
output += @setShortId(obj.id)
return output
sendDestroy: (objects)->
@send @serializeDestroy(objects)
)

Players = []
Player = $G(Physics, Engine, ShipCommand, SerializeCreate
SerializePos, SocketIoClient
lookup: Players
init: (x, y) ->
init: (x, y)->
@createPos(x, y)
remove: ->
for p in Players
p.sendDestroy([this])
)

processCommands = ->
Expand Down Expand Up @@ -300,14 +310,14 @@ log 'Server running at http://localhost:8124/'
socket = io.listen(server)
socket.on('connection', (client)->
# new player connects, needs a spaceship
ss = new Player(0,0)
ss.setClient(client)
ss.sendCreate(Players) #TODO {} serialization, then s/Players/GameObjects/
player = new Player(0,0)
player.setClient(client)
player.sendCreate(Players) #TODO {} serialization, then s/Players/GameObjects/

#TODO: notify other players about this new player
for p in Players
continue if p.id == ss.id
p.sendCreate([ss])
continue if p.id == player.id
p.sendCreate([player])

# better tell the player what's around
#ss.send(ss.serializeCreate(hasPosition))
Expand All @@ -318,9 +328,10 @@ socket.on('connection', (client)->
###

client.on('message', (msg)->
ss.bitmask = Number(msg)
player.bitmask = Number(msg)
)
client.on('disconnect', (msg)->
player.remove()
#TODO remove player and notify others
)
)
39 changes: 31 additions & 8 deletions xarkon-server.js
Expand Up @@ -267,13 +267,34 @@ SerializeCreate = $G({
},
sendCreate: function(objects) {
return this.send(this.serializeCreate(objects));
},
serializeDestroy: function(objects) {
var obj, output, _i, _len;
output = 'd';
for (_i = 0, _len = objects.length; _i < _len; _i++) {
obj = objects[_i];
output += this.setShortId(obj.id);
}
return output;
},
sendDestroy: function(objects) {
return this.send(this.serializeDestroy(objects));
}
});
Players = [];
Player = $G(Physics, Engine, ShipCommand, SerializeCreate, SerializePos, SocketIoClient, {
lookup: Players,
init: function(x, y) {
return this.createPos(x, y);
},
remove: function() {
var p, _i, _len, _results;
_results = [];
for (_i = 0, _len = Players.length; _i < _len; _i++) {
p = Players[_i];
_results.push(p.sendDestroy([this]));
}
return _results;
}
});
processCommands = function() {
Expand Down Expand Up @@ -338,24 +359,26 @@ server.listen(8124);
log('Server running at http://localhost:8124/');
socket = io.listen(server);
socket.on('connection', function(client) {
var p, ss, _i, _len;
ss = new Player(0, 0);
ss.setClient(client);
ss.sendCreate(Players);
var p, player, _i, _len;
player = new Player(0, 0);
player.setClient(client);
player.sendCreate(Players);
for (_i = 0, _len = Players.length; _i < _len; _i++) {
p = Players[_i];
if (p.id === ss.id) {
if (p.id === player.id) {
continue;
}
p.sendCreate([ss]);
p.sendCreate([player]);
}
/*
setInterval((->
client.send(Math.random())
), 1000)
*/
client.on('message', function(msg) {
return ss.bitmask = Number(msg);
return player.bitmask = Number(msg);
});
return client.on('disconnect', function(msg) {
return player.remove();
});
return client.on('disconnect', function(msg) {});
});

0 comments on commit d4493b3

Please sign in to comment.