Permalink
Browse files

Use a cursor system to send only the revelvant events to the players.

  • Loading branch information...
1 parent e955714 commit e270f80c59fdbec51e7a29ccd33744df8d9b268d Batiste Bieler committed Nov 28, 2009
Showing with 99 additions and 41 deletions.
  1. +1 −0 .gitignore
  2. +47 −19 chat/views.py
  3. +51 −22 static/game.js
View
@@ -1 +1,2 @@
*.pyc
+dev.db
View
@@ -13,14 +13,26 @@ def __init__(self):
self.last_message = []
self.players = []
self.cache = []
- self.new_room_event = Event()
+ self.room_event = Event()
+ self.event_cursor = 0
+ self.event_buffer = []
+ for v in range(0, 9):
+ self.event_buffer.append(None)
def main(self, request):
return render_to_response(
'index.html',
{'MEDIA_URL': settings.MEDIA_URL}
)
+ def new_room_event(self, value):
+ self.event_cursor += 1
+ if self.event_cursor >= len(self.event_buffer):
+ self.event_cursor = 0
+ self.event_buffer[self.event_cursor] = value
+ self.room_event.set()
+ self.room_event.clear()
+
def get_player(self, key):
for p in self.players:
if p['key'] == key:
@@ -30,39 +42,55 @@ def get_player(self, key):
def player_new(self, request):
key = str(uuid.uuid4())
name = request.POST['body']
- player = {'name':name, 'key':key}
- self.players.append(player)
- self.new_room_event.set()
- self.new_room_event.clear()
- response = json_response({'new_player':player})
+ new_player = {'name':name, 'key':key}
+ event_list = []
+ # send all the other player
+ for player in self.players:
+ event_list.append(['new_player', player])
+ self.players.append(new_player)
+ self.new_room_event(['new_player', new_player])
+ response = json_response({'you':new_player, 'events':event_list})
response.set_cookie('rpg_key', key)
- return response
+ return response
def player_update_position(self, request):
key = request.COOKIES['rpg_key']
player = self.get_player(key)
position = request.POST['body']
player['position'] = position
- self.new_room_event.set()
- self.new_room_event.clear()
- print "player update pos"
- return json_response({'update_player_position':[key, position]})
+ self.new_room_event(['update_player_position', [key, position]])
+ return json_response([1])
def message_new(self, request):
key = request.COOKIES['rpg_key']
msg = request.POST['body']
player = self.get_player(key)
player['last_message'] = msg
- self.new_room_event.set()
- self.new_room_event.clear()
- print "player update pos"
- return json_response({'last_message':[key, msg]})
+ self.new_room_event(['last_message', [key, msg]])
+ return json_response([1])
def room_updates(self, request):
- value = self.new_room_event.wait()
- return json_response({
- 'players': self.players
- })
+
+ value = self.room_event.wait()
+
+ cursor = int(request.POST.get('cursor', self.event_cursor+1))
+ if cursor == self.event_cursor:
+ return json_response([1])
+ event_list = []
+ if cursor >= len(self.event_buffer):
+ cursor = 0
+ while(cursor != self.event_cursor):
+ event = self.event_buffer[cursor]
+ if event:
+ event_list.append(self.event_buffer[cursor])
+ cursor += 1
+ if cursor >= len(self.event_buffer):
+ cursor = 0
+
+ event_list.append(self.event_buffer[self.event_cursor])
+ event_list.append(['update_cursor', self.event_cursor])
+
+ return json_response(event_list)
room = ChatRoom()
View
@@ -8,13 +8,17 @@ $(function() {
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function() {};
+
+ var event_cursor = false;
// polling update
var updater = {
errorSleepTime: 500,
cursor: null,
poll: function() {
var args = {"_xsrf": getCookie("_xsrf")};
+ if(event_cursor)
+ args["cursor"] = event_cursor
if (updater.cursor) args.cursor = updater.cursor;
$.ajax({
url: "/a/room/updates",
@@ -271,11 +275,14 @@ $(function() {
};
};
- var personnal_key = false
+ var personnal_key = false;
var player_name = prompt("Choose your hero name");
$.postJSON("/a/player/new", {'body':player_name}, function(response) {
response = $.evalJSON(response);
- personnal_key = response["new_player"]["key"];
+ personnal_key = response["you"]["key"];
+ for(var i=0; i < response["events"].length; i++) {
+ handle_event(response["events"][i]);
+ };
});
var me = new player([30, 100], personnal_key, player_name);
@@ -326,29 +333,51 @@ $(function() {
setInterval(_player_send, 1000);
$.receive_data = function(json) {
- for(var i=0; i <json["players"].length; i++) {
- var item = json["players"][i];
- var key = item['key'];
- if(key != personnal_key) {
- if(item['position']){
- var _pos = item['position'].split(',');
- var pos = [parseInt(_pos[0]), parseInt(_pos[1])];
- } else {
- var pos = [50, 60];
- }
- var p = get_player(key);
- if(p === false) {
- p = new player([pos[0], pos[1]], key, item['name']);
- other_players.push(p);
- }
- p.target_position = [pos[0], pos[1]];
- if(item['last_message']){
- p.say(item['last_message'])
- }
- }
+ for(var i=0; i <json.length; i++) {
+ handle_event(json[i]);
};
};
+ function handle_event(event) {
+ if(event[0] == 'update_player_position') {
+ var p = get_player(event[1][0]);
+ if(event[1][0] == personnal_key)
+ return;
+ var _pos = event[1][1].split(',');
+ var pos = [parseInt(_pos[0]), parseInt(_pos[1])];
+ p.target_position = pos;
+ }
+ if(event[0] == 'new_player') {
+ var item = event[1];
+ if(item['key'] == personnal_key)
+ return;
+ if(item['position']){
+ var _pos = item['position'].split(',');
+ var pos = [parseInt(_pos[0]), parseInt(_pos[1])];
+ } else {
+ var pos = [50, 60];
+ }
+ var p = get_player(item['key']);
+ if(p === false) {
+ p = new player([pos[0], pos[1]], item['key'], item['name']);
+ other_players.push(p);
+ }
+ p.target_position = [pos[0], pos[1]];
+ if(item['new_message']) {
+ p.say(item['new_message'])
+ }
+ };
+ if(event[0] == 'last_message') {
+ if(event[1][0] == personnal_key)
+ return;
+ var p = get_player(event[1][0]);
+ p.say(event[1][1]);
+ }
+ if(event[0] == 'update_cursor') {
+ event_cursor = event[1];
+ };
+ }
+
// keyboard stuff
var keyboard = {'up':0, 'left':0, 'right':0, 'down':0};

0 comments on commit e270f80

Please sign in to comment.