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 => {
wall.draw(ctx);
});
console.log(this.players)
this.players.forEach(player => {
player.draw(ctx);
});
@ -115,12 +114,11 @@ class Game
{
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)
return
this.players.slice(index, 1);
this.players.splice(index, 1);
}
_receive_paddle_position(data)
@ -143,6 +141,8 @@ class Game
this._init_game(data)
else if (data.detail === "player_join")
this._receive_player_join(data)
else if (data.detail === "player_leave")
this._receive_player_leave(data)
}
_init_game(data)

View File

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

View File

@ -98,7 +98,7 @@ class Game(AbstractRoom):
return 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)
def _spectator_join(self, user_id: int, socket: WebsocketConsumer):