game: core: use server game calulation form

This commit is contained in:
2024-01-17 14:23:23 +01:00
parent b2dc43c1d8
commit 2bd0624100
15 changed files with 274 additions and 110 deletions

View File

@ -3,7 +3,19 @@ from .. import config
class Ball:
def __init__(self) -> None:
self.x: float = config.BALL_SPAWN_POS_X
self.y: float = config.BALL_SPAWN_POS_Y
self.speed: float = config.BALL_SPEED_START
self.postion_x: float = config.BALL_SPAWN_POS_X
self.postion_y: float = config.BALL_SPAWN_POS_Y
self.velocity_x: float = config.BALL_SPEED_START
self.velocity_y: float = config.BALL_SPEED_START
def to_dict(self):
data: dict = {
"position_x": self.postion_x,
"position_y": self.postion_y,
"velocity_x": self.velocity_x,
"velocity_y": self.velocity_y,
}
return data

View File

@ -1,8 +1,53 @@
from .Ball import Ball
from .Paddle import Paddle
from .Player import Player
from .Wall import Wall
import math
from .. import config
from ..models import GameModel
class Game:
def __init__(self, paddles: [Paddle], ball: Ball) -> None:
self.ball: Ball = ball
self.paddles: [paddles] = paddles
def __init__(self, game_id: int) -> None:
self.ball: Ball = Ball()
game: GameModel = GameModel.objects.get(pk = game_id)
players_id: [int] = game.get_players_id()
radius: float = min(config.MAP_SIZE_X, config.MAP_SIZE_Y) / 2 - 10
nb_sides = len(players_id) * 2
self.polygon = []
for i in range(nb_sides):
angle: float = (i * 2 * math.pi / nb_sides) + (math.pi * 3 / 4)
x: float = config.MAP_CENTER_X + radius * math.cos(angle)
y: float = config.MAP_CENTER_Y + radius * math.sin(angle)
self.polygon.append((x, y))
self.players: list(Player) = []
self.walls: list(Wall) = []
for i in range(nb_sides):
if (i % 2 == 0):
self.players.append(Player(players_id[ i // 2], *self.polygon[i], *self.polygon[(i + 1) % nb_sides]))
else:
self.walls.append(Wall(*self.polygon[i], *self.polygon[(i + 1) % nb_sides]))
self.game_id: int = game_id
def to_dict(self):
data: dict = {"ball": self.ball.to_dict(),
"players": [player.to_dict() for player in self.players],
"walls": [wall.to_dict() for wall in self.walls],
}
print(data)
return data

View File

@ -0,0 +1,22 @@
from .Game import Game
from ..models import GameModel
class GameManager():
def __init__(self) -> None:
self._game_list: [Game] = []
def get(self, game_id: int):
if (not GameModel.objects.filter(pk = game_id, started = True, finished = False).exists()):
return None
for game in self._game_list:
game: Game
if (game.game_id == game_id):
return game
return Game(game_id)

View File

@ -44,7 +44,7 @@ class GameMember(AbstractRoomMember):
new_position.position = data.get("position")
if (new_position is None):
if (new_position.position is None):
self.send_error("missing new_position")
return

View File

@ -2,12 +2,27 @@
class Player:
def __init__(self, user_id, pos, nb_goal, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y) -> None:
def __init__(self, user_id, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y) -> None:
self.user_id = user_id
self.pos = pos
self.nb_goal = nb_goal
self.position = 0.5
self.nb_goal = 0
self.rail_start_x = rail_start_x
self.rail_start_y = rail_start_y
self.rail_stop_x = rail_stop_x
self.rail_stop_y = rail_stop_y
self.rail_stop_y = rail_stop_y
def to_dict(self):
data = {
"user_id": self.user_id,
"position": self.position,
"nb_goal": self.nb_goal,
"rail_start_x": self.rail_start_x,
"rail_start_y": self.rail_start_y,
"rail_stop_x": self.rail_stop_x,
"rail_stop_y": self.rail_stop_y,
}
return data

20
games/objects/Wall.py Normal file
View File

@ -0,0 +1,20 @@
class Wall:
def __init__(self, rail_start_x, rail_start_y, rail_stop_x, rail_stop_y) -> None:
self.rail_start_x = rail_start_x
self.rail_start_y = rail_start_y
self.rail_stop_x = rail_stop_x
self.rail_stop_y = rail_stop_y
def to_dict(self):
data = {
"rail_start_x": self.rail_start_x,
"rail_start_y": self.rail_start_y,
"rail_stop_x": self.rail_stop_x,
"rail_stop_y": self.rail_stop_y,
}
return data