game: add: player can leave and join game

This commit is contained in:
starnakin 2024-01-18 13:59:19 +01:00
parent fa38ba6eb8
commit 1589763197
3 changed files with 7 additions and 6 deletions

View File

@ -56,7 +56,6 @@ class Game
this.walls.forEach(wall => { this.walls.forEach(wall => {
wall.draw(ctx); wall.draw(ctx);
}); });
console.log(this.players)
this.players.forEach(player => { this.players.forEach(player => {
player.draw(ctx); player.draw(ctx);
}); });
@ -115,12 +114,11 @@ class Game
{ {
const user_id = player_data.user_id; const user_id = player_data.user_id;
const index = this.players.find(player => player.id === user_id); const index = this.players.indexOf(this.players.find(player => player.id === user_id));
if (index === -1) if (index === -1)
return return
this.players.splice(index, 1);
this.players.slice(index, 1);
} }
_receive_paddle_position(data) _receive_paddle_position(data)
@ -143,6 +141,8 @@ class Game
this._init_game(data) this._init_game(data)
else if (data.detail === "player_join") else if (data.detail === "player_join")
this._receive_player_join(data) this._receive_player_join(data)
else if (data.detail === "player_leave")
this._receive_player_leave(data)
} }
_init_game(data) _init_game(data)

View File

@ -52,4 +52,5 @@ class GameWebSocket(WebsocketConsumer):
self.member.receive(data) self.member.receive(data)
def disconnect(self, close_code): def disconnect(self, close_code):
if (self.user.pk in self.game.players_id):
self.game.remove(self.member) self.game.remove(self.member)

View File

@ -98,7 +98,7 @@ class Game(AbstractRoom):
return player return player
def _player_leave(self, player: Player): def _player_leave(self, player: Player):
# TODO send data to all players self.broadcast("player_leave", player.to_dict(), [player])
self.players.remove(player) self.players.remove(player)
def _spectator_join(self, user_id: int, socket: WebsocketConsumer): def _spectator_join(self, user_id: int, socket: WebsocketConsumer):