Skip to content

Commit aee6967

Browse files
author
Batiste Bieler
committed
Use JSON for position
1 parent d25dfa3 commit aee6967

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

chat/views.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ def __init__(self, room_id):
2424
def main(self, request):
2525
room_map = Map.objects.get(pk=self.pk)
2626
content = room_map.content.replace("\n", "")
27+
key = request.COOKIES.get('rpg_key', False)
28+
player = self.get_player(key)
29+
print player
2730
return render_to_response(
2831
'index.html',
2932
{
33+
'player':player,
3034
'map':room_map,
3135
'map_content':content,
3236
'MEDIA_URL': settings.MEDIA_URL
@@ -82,9 +86,8 @@ def player_new(self, request):
8286
def player_update_position(self, request):
8387
key = request.COOKIES['rpg_key']
8488
player = self.get_player(key)
85-
position = request.POST['body']
86-
player['position'] = position
87-
self.new_room_event(['update_player_position', [key, position]])
89+
player['position'] = simplejson.loads(request.POST['body'])
90+
self.new_room_event(['update_player_position', [key, player['position']]])
8891
return json_response([1])
8992

9093
def message_new(self, request):
@@ -129,20 +132,18 @@ def change_room(self, request):
129132
key = request.COOKIES['rpg_key']
130133
direction = request.POST.get('direction')
131134
player = self.remove_player(key)
132-
px, py = player['position'].split(',')
133-
px = int(px); py = int(py)
134135
x = 0; y = 0
135136
if direction == 'left':
136-
player['position'] = str(34 * 16) + ',' + str(py)
137+
player['position'][0] = 34 * 16
137138
x = -1
138139
if direction == 'right':
139-
player['position'] = str(0) + ',' + str(py)
140+
player['position'][0] = 0
140141
x = +1
141142
if direction == 'top':
142-
player['position'] = str(px) + ',' + str(29 * 16)
143+
player['position'][1] = 28 * 16
143144
y = -1
144145
if direction == 'bottom':
145-
player['position'] = str(px) + ',' + str(0)
146+
player['position'] = 0
146147
y = +1
147148
old_map = Map.objects.get(pk=self.pk)
148149
room_map, created = Map.objects.get_or_create(x=old_map.x+x, y=old_map.y+y)

static/game.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,11 @@ $(function() {
3737
},
3838

3939
onSuccess: function(response) {
40-
//try {
41-
var mapdata = $.evalJSON(response);
42-
$.receive_data(mapdata)
43-
//var pos = mapdata['players'][0]['position'].split(',');
44-
//$.player_target_position[0] = parseInt(pos[0])
45-
//$.player_target_position[1] = parseInt(pos[1])
46-
47-
//} catch (e) {
48-
// updater.onError();
49-
// return;
50-
//}
40+
var json = $.evalJSON(response);
41+
for(var i=0; i <json.length; i++) {
42+
handle_event(json[i]);
43+
};
44+
5145
updater.errorSleepTime = 500;
5246
window.setTimeout(updater.poll, 0);
5347
},
@@ -237,7 +231,6 @@ $(function() {
237231

238232
map.click(function(e) {
239233
grid1.paint_bloc($(e.target));
240-
$('#grid-serialized').val($.toJSON(grid));
241234
});
242235

243236
map.mousedown(function(e) {
@@ -372,11 +365,13 @@ $(function() {
372365

373366
player.prototype.send_position = function () {
374367
if(this.last_sent_position[0] != this.target_position[0]
375-
|| this.last_sent_position[1] != this.target_position[1]) {
376-
this.last_sent_position[0] = this.target_position[0];
377-
this.last_sent_position[1] = this.target_position[1];
378-
$.postJSON("/a/player/update_position", {
379-
'body':this.target_position[0]+','+this.target_position[1]
368+
|| this.last_sent_position[1] != this.target_position[1])
369+
{
370+
var pos = $.toJSON(this.target_position)
371+
this.last_sent_position[0] = this.target_position[0];
372+
this.last_sent_position[1] = this.target_position[1];
373+
$.postJSON("/a/player/update_position", {
374+
'body':pos
380375
},
381376
function(response) {}
382377
);
@@ -449,20 +444,12 @@ $(function() {
449444
};
450445
setInterval(_player_send_position, 1000);
451446

452-
$.receive_data = function(json) {
453-
for(var i=0; i <json.length; i++) {
454-
handle_event(json[i]);
455-
};
456-
};
457-
458447
function handle_event(event) {
459448
if(event[0] == 'update_player_position') {
460449
var p = get_player(event[1][0]);
461450
if(event[1][0] == personnal_key)
462451
return;
463-
var _pos = event[1][1].split(',');
464-
var pos = [parseInt(_pos[0]), parseInt(_pos[1])];
465-
p.target_position = pos;
452+
p.target_position = event[1][1];
466453
}
467454
if(event[0] == 'new_player') {
468455
var item = event[1];

templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
</div>
2828
<script>
2929
var map_content = "{{ map_content }}";
30+
var player_position = "{{ player.position }}";
3031
</script>
3132
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
3233
<script src="http://jquery-json.googlecode.com/files/jquery.json-2.2.js"></script>

0 commit comments

Comments
 (0)