Skip to content

Commit d25dfa3

Browse files
author
Batiste Bieler
committed
Multiplayer map save is working! Sweet
1 parent edd9cdc commit d25dfa3

File tree

5 files changed

+84
-40
lines changed

5 files changed

+84
-40
lines changed

chat/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ def remove_player(self, key):
5555
return p
5656
return None
5757

58+
def save_map(self, request):
59+
room_map = Map.objects.get(pk=self.pk)
60+
room_map.content = request.POST['content']
61+
room_map.save()
62+
self.new_room_event({'update_map':room_map.serialized()})
63+
return json_response([1])
64+
5865
def player_new(self, request):
5966
key = request.COOKIES.get('rpg_key', False)
6067
new_player = self.get_player(key)
@@ -177,6 +184,7 @@ def _method(request):
177184
player_update_position = room_dispacher('player_update_position')
178185
room_updates = room_dispacher('room_updates')
179186
change_room = room_dispacher('change_room')
187+
save_map = room_dispacher('save_map')
180188

181189
def create_message(from_, body):
182190
data = {'id': str(uuid.uuid4()), 'from': from_, 'body': body}

static/game.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ body {
2727
background:#666;
2828
}
2929

30-
#tileset {
30+
#tileset-container {
3131
float:right;
32+
color:#fff;
3233
}
3334
#grid-serialized {
3435
width:600px;

static/game.js

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
$(function() {
22

3+
// evil
4+
Array.prototype.remove = function(from, to) {
5+
var rest = this.slice((to || from) + 1 || this.length);
6+
this.length = from < 0 ? this.length + from : from;
7+
return this.push.apply(this, rest);
8+
};
9+
310
function getCookie(name) {
411
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
512
return r ? r[1] : undefined;
@@ -8,7 +15,6 @@ $(function() {
815
if (!window.console) window.console = {};
916
if (!window.console.log) window.console.log = function() {};
1017

11-
1218
var event_cursor = false;
1319
// polling update
1420
var updater = {
@@ -80,6 +86,7 @@ $(function() {
8086
this.width = w;
8187
this.height = h;
8288
this.blocs = [];
89+
this.dom_blocs = [];
8390
this.choosen_bloc = [0, 0];
8491
this.is_loading_room = false;
8592
this.forbidden = [
@@ -93,32 +100,35 @@ $(function() {
93100
var top_offest = 0;
94101
var left_offset = 0;
95102
for(var i=0; i<this.height; i++) {
103+
var dom_line = [];
96104
var line = [];
97105
var top_offest = i * 16;
98106
for(var j=0; j<this.width; j++) {
99107
left_offset = j * 16;
100-
var bloc = $('<div class="bloc" id="bloc-'+ i
108+
var dom_bloc = $('<div class="bloc" id="bloc-'+ i
101109
+'-'+ j +'" style="top:' + top_offest
102110
+ 'px;left:'+ left_offset + 'px;"></div>');
103-
map.append(bloc);
104-
line.push([bloc, false]);
111+
map.append(dom_bloc);
112+
dom_line.push(dom_bloc);
113+
line.push(false);
105114
};
115+
this.dom_blocs.push(dom_line);
106116
this.blocs.push(line);
107117
};
108118
this.load_new_map(blocs_init);
109119
};
110120

111121
grid.prototype.get_bloc = function(indexes) {
112-
return this.blocs[indexes[1]][indexes[0]][1];
122+
return this.blocs[indexes[1]][indexes[0]];
113123
};
114124

115-
grid.prototype.paint_bloc = function(bloc) {
125+
grid.prototype.paint_bloc = function(dom_bloc) {
116126
//var bloc = $(e.target);
117-
if(bloc.hasClass('bloc')) {
118-
var b_string = bloc.attr('id').split('-');
127+
if(dom_bloc.hasClass('bloc')) {
128+
var b_string = dom_bloc.attr('id').split('-');
119129
var tile_pos_css = (-this.choosen_bloc[0]*17-1)+'px ' + (-this.choosen_bloc[1]*17-1)+'px';
120-
this.blocs[parseInt(b_string[1])][parseInt(b_string[2])][1] = this.choosen_bloc;
121-
bloc.css('background-position', tile_pos_css);
130+
this.blocs[parseInt(b_string[1])][parseInt(b_string[2])] = this.choosen_bloc;
131+
dom_bloc.css('background-position', tile_pos_css);
122132
};
123133
};
124134

@@ -153,17 +163,18 @@ $(function() {
153163
}
154164
if(direction) {
155165
this.is_loading_room = true;
156-
$.postJSON("/a/change_room", {'direction':direction}, function(response) {
157-
for(var i=0; i < other_players.length; i++) {
158-
if(other_players[i]) {
159-
other_players[i].remove();
160-
delete other_players[i];
161-
}
166+
$.postJSON("/a/change_room", {'direction':direction}, function change_room(response) {
167+
var others_number = other_players.length - 1;
168+
while(others_number >= 0) {
169+
if(other_players[0]) {
170+
other_players[0].remove();
171+
};
172+
other_players.remove(0);
173+
others_number = others_number - 1;
162174
};
163175
other_players = [];
164176
json = $.evalJSON(response);
165177
var new_room = json["change_room"];
166-
other_players = [];
167178
var map_content = new_room["content"];
168179
if(map_content)
169180
map_content = $.evalJSON(map_content);
@@ -181,7 +192,9 @@ $(function() {
181192
for(var i=0; i <json["events"].length; i++) {
182193
handle_event(json["events"][i]);
183194
};
184-
$('#room_data').text(new_room['x']+', '+new_room['y']);
195+
$('#room_x').text(new_room['x']);
196+
$('#room_y').text(new_room['y']);
197+
$('#room_name').text(new_room['name']);
185198
grid1.is_loading_room = false;
186199
});
187200
return;
@@ -192,13 +205,14 @@ $(function() {
192205
grid.prototype.load_new_map = function(blocs_init) {
193206
for(var i=0; i < this.height; i++) {
194207
for(var j=0; j < this.width; j++) {
195-
var bloc = this.blocs[i][j];
208+
var dom_bloc = this.dom_blocs[i][j];
196209
if(blocs_init)
197-
bloc[1] = blocs_init[i][j];
210+
var bloc = blocs_init[i][j];
198211
else
199-
bloc[1] = [1, 1];
200-
var tile_pos_css = (-bloc[1][0]*17-1)+'px ' + (-bloc[1][1]*17-1)+'px';
201-
bloc[0].css('background-position', tile_pos_css);
212+
var bloc = [1, 1];
213+
this.blocs[i][j] = bloc;
214+
var tile_pos_css = (-bloc[0]*17-1)+'px ' + (-bloc[1]*17-1)+'px';
215+
dom_bloc.css('background-position', tile_pos_css);
202216
};
203217
};
204218
};
@@ -232,7 +246,7 @@ $(function() {
232246

233247
map.mouseup(function(e) {
234248
mouseDown = false;
235-
$('#grid-serialized').val($.toJSON(grid));
249+
//$('#grid-serialized').val($.toJSON(grid));
236250
});
237251

238252
map.mousemove(function(e) {
@@ -307,6 +321,7 @@ $(function() {
307321
this.target_position[0] + parseInt(this.speed * vect[0]),
308322
this.target_position[1] + parseInt(this.speed * vect[1])
309323
];
324+
310325
var bloc = grid1.bloc_from_player_pos(next_pos);
311326
if(bloc && grid1.is_bloc_walkable(bloc)) {
312327
this.target_position = next_pos;
@@ -416,18 +431,23 @@ $(function() {
416431
};
417432
setInterval(_players_move, 25);
418433

419-
var _anim = function(){
420-
me.anim();
421-
for(var i=0; i <other_players.length; i++) {
422-
if(other_players[i])
423-
other_players[i].anim();
434+
var _anim = function() {
435+
if(!grid1.is_loading_room) {
436+
me.anim();
437+
for(var i=0; i <other_players.length; i++) {
438+
if(other_players[i])
439+
other_players[i].anim();
440+
};
424441
};
425442
};
426443
me.anim_interval = setInterval(_anim, 120);
427444

428445
// send the new position to the server
429-
var _player_send = function(){ me.send_position(); }
430-
setInterval(_player_send, 1000);
446+
var _player_send_position = function() {
447+
if(!grid1.is_loading_room)
448+
me.send_position();
449+
};
450+
setInterval(_player_send_position, 1000);
431451

432452
$.receive_data = function(json) {
433453
for(var i=0; i <json.length; i++) {
@@ -471,7 +491,7 @@ $(function() {
471491
for(var i=0; i < other_players.length; i++) {
472492
if(other_players[i] && other_players[i].key == item['key']) {
473493
other_players[i].remove();
474-
delete other_players[i];
494+
other_players.remove(i);
475495
};
476496
};
477497
var p = get_player(item['key']);
@@ -547,22 +567,30 @@ $(function() {
547567
};
548568

549569
var keyboard_events = $('#message')
550-
/*$('body').click(function(){
570+
$('body').click(function(){
551571
keyboard_events.focus();
552-
});*/
572+
});
553573
keyboard_events.focus();
554574

555575
keyboard_events.keydown(function(e) {
556576
update_keyboard(e, 1);
557577
//reset_cycle();
558578
player.walking = true;
559579
});
560-
keyboard_events.keyup(function(e){
580+
keyboard_events.keyup(function(e) {
561581
update_keyboard(e, 0);
562-
//reset_cycle();
563582
if( !keyboard["up"] && !keyboard["left"] && !keyboard["right"] && !keyboard["down"] )
564583
player.walking=false;
565584
});
585+
586+
$('#save-map').click(function() {
587+
var content = $.toJSON(grid1.blocs);
588+
$.postJSON("/a/save_map", {'content':content}, function change_room(response) {
589+
$('#save-message').show().fadeOut("slow");
590+
});
591+
})
592+
593+
566594
/*keyboard_events.blur(function(e) {
567595
keyboard = {'up':0, 'left':0, 'right':0, 'down':0};
568596
keyboard_events.focus();

templates/index.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
</head>
88
<body>
99
<div id="main">
10-
<p>(<span id="room_data">{{ map.x }}, {{ map.y }}</span>)</p>
11-
<img src="{{MEDIA_URL}}tileset.png" id="tileset">
10+
<p id="room_data"><span id="room_name">{{ map.name }}</span> (<span id="room_x">{{ map.x }}</span>, <span id="room_y">{{ map.y }}</span></span>)</p>
11+
<div id="tileset-container">
12+
<img src="{{MEDIA_URL}}tileset.png" id="tileset">
13+
<br>
14+
<input id="save-map" type="submit" value="Save map">
15+
<span id="save-message" style="display:none">Map saved.</span>
16+
</div>
17+
1218
<div id="map"></div>
1319
<div id="input">
1420
<form action="/a/message/new" method="post" id="messageform">

urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
('^a/room/updates$', 'room_updates'),
88
('^a/player/new$', 'player_new'),
99
('^a/player/update_position$', 'player_update_position'),
10-
('^a/change_room$', 'change_room')
10+
('^a/change_room$', 'change_room'),
11+
('^a/save_map', 'save_map'),
1112
)
1213

1314
urlpatterns += patterns('django.views.static',

0 commit comments

Comments
 (0)