core: fix: matchmaking and game

This commit is contained in:
2024-04-22 11:37:08 +02:00
parent d31d3e053e
commit e125eb16c7
16 changed files with 135 additions and 134 deletions

View File

@ -22,7 +22,7 @@ import threading
from typing import TYPE_CHECKING
if TYPE_CHECKING:
pass
from profiles.models import ProfileModel
class PongGame(AGame):
@ -36,7 +36,7 @@ class PongGame(AGame):
radius: float = min(config.MAP_SIZE_X, config.MAP_SIZE_Y) / 2 - 10
players_id: list[int] = self.model.get_players_id()
players: list[ProfileModel] = self.model.get_players()
nb_sides = 4
@ -58,16 +58,16 @@ class PongGame(AGame):
self.walls: list[Wall]
self.players: list[PongPlayer]
nb_players: int = len(players_id)
nb_players: int = len(players)
if (nb_players == 2):
self.players = [PongPlayer(self, players_id[0], None, segments[0]), PongPlayer(self, players_id[1], None, segments[2])]
self.players = [PongPlayer(self, players[0], None, segments[0]), PongPlayer(self, players[1], None, segments[2])]
self.walls = [Wall(segments[1].start, segments[1].stop), Wall(segments[3].start, segments[3].stop)]
else:
self.players = []
self.walls = []
for i in range(4):
if (i < nb_players):
self.players.append(PongPlayer(self, players_id[i], None, segments[i]))
self.players.append(PongPlayer(self, players[i], None, segments[i]))
else:
self.walls.append(Wall(segments[i]))

View File

@ -17,9 +17,9 @@ if TYPE_CHECKING:
class PongPlayer(APlayer):
def __init__(self, game: PongGame, user_id: int, socket: WebsocketConsumer, rail: Segment) -> None:
def __init__(self, game: PongGame, user: User, socket: WebsocketConsumer, rail: Segment) -> None:
super().__init__(user_id, socket, game)
super().__init__(user, socket, game)
self.position: Position = Position(0.5, 0)
@ -29,8 +29,6 @@ class PongPlayer(APlayer):
self.game: PongGame
self.username: str = User.objects.get(pk = self.user_id).username
def eliminate(self):
self.disconnect(1000)
@ -43,7 +41,7 @@ class PongPlayer(APlayer):
if (detail is None):
return
if (detail == "update_my_paddle_pos"):
self.update_position(data)
@ -119,8 +117,8 @@ class PongPlayer(APlayer):
def to_dict(self) -> dict:
data = {
"username": self.username,
"id": self.user_id,
"username": self.user.username,
"id": self.user.pk,
"position": self.position.to_dict(),
"score": self.score,