game: add: class: point and segment, add: type docstring
This commit is contained in:
@ -23,6 +23,7 @@ class GameWebSocket(WebsocketConsumer):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.channel_name = "games"
|
||||
self.group_name = "games"
|
||||
self.member = None
|
||||
|
||||
def connect(self):
|
||||
|
||||
@ -30,20 +31,23 @@ class GameWebSocket(WebsocketConsumer):
|
||||
if (self.user.pk is None):
|
||||
self.user.pk = 0
|
||||
|
||||
self.channel_layer.group_add(self.group_name, self.channel_name)
|
||||
self.accept()
|
||||
|
||||
self.game_id = int(self.scope['url_route']['kwargs']['game_id'])
|
||||
|
||||
self.game: Game = game_manager.get(self.game_id)
|
||||
|
||||
|
||||
if (self.game is None):
|
||||
self.send("Game not found.")
|
||||
self.disconnect(1017)
|
||||
|
||||
self.send(text_data=json.dumps({"detail": "Game not found"}))
|
||||
self.disconnect(1404)
|
||||
return
|
||||
|
||||
self.member: Player | Spectator = self.game.join(self.user.pk, self)
|
||||
|
||||
def disconnect(self, code):
|
||||
self.member.disconnect()
|
||||
if (self.member is not None):
|
||||
self.member.disconnect()
|
||||
super().disconnect(code)
|
||||
|
||||
def receive(self, text_data: str = None, bytes_data: bytes = None):
|
||||
|
||||
|
@ -7,10 +7,12 @@ class Ball:
|
||||
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
|
||||
self.size: float = config.BALL_SIZE
|
||||
|
||||
def to_dict(self):
|
||||
|
||||
data: dict = {
|
||||
"size": self.size,
|
||||
"position_x": self.postion_x,
|
||||
"position_y": self.postion_y,
|
||||
"velocity_x": self.velocity_x,
|
||||
|
@ -9,6 +9,8 @@ from .Ball import Ball
|
||||
from .Player import Player
|
||||
from .Spectator import Spectator
|
||||
from .Wall import Wall
|
||||
from .Point import Point
|
||||
from .Segment import Segment
|
||||
|
||||
import math
|
||||
|
||||
@ -34,30 +36,34 @@ class Game(AbstractRoom):
|
||||
|
||||
players_id: list[int] = self.model.get_players_id()
|
||||
|
||||
self.nb_sides = len(players_id) * 2
|
||||
nb_sides = len(players_id) * 2
|
||||
|
||||
self.polygon: list[tuple[float, float]] = []
|
||||
polygon: list[Point] = []
|
||||
|
||||
for i in range(self.nb_sides):
|
||||
for i in range(nb_sides):
|
||||
|
||||
angle: float = (i * 2 * math.pi / self.nb_sides) + (math.pi * 3 / 4)
|
||||
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))
|
||||
polygon.append(Point(x, y))
|
||||
|
||||
segments: list[Point] = []
|
||||
for i in range(nb_sides):
|
||||
segments.append(Segment(polygon[i], polygon[(i + 1) % nb_sides]))
|
||||
|
||||
self.players: list[Player] = []
|
||||
for i, player_id in enumerate(players_id):
|
||||
player = Player(self, player_id, None, *self.polygon[i * 2], *self.polygon[(i * 2 + 1) % self.nb_sides])
|
||||
player = Player(self, player_id, None, segments[i * 2])
|
||||
self.players.append(player)
|
||||
|
||||
self.spectators: list[Spectator] = []
|
||||
|
||||
self.walls: list[Wall] = []
|
||||
|
||||
for i in range(1, self.nb_sides, 2):
|
||||
self.walls.append(Wall(*self.polygon[i], *self.polygon[(i + 1) % self.nb_sides]))
|
||||
for i in range(1, nb_sides, 2):
|
||||
self.walls.append(Wall(segments[i]))
|
||||
|
||||
self._updated_players: list[Player] = []
|
||||
|
||||
@ -86,12 +92,11 @@ class Game(AbstractRoom):
|
||||
|
||||
def get_player_by_user_id(self, user_id: int) -> Player:
|
||||
for player in self.players:
|
||||
player: Player
|
||||
if (player.user_id == user_id):
|
||||
return player
|
||||
return None
|
||||
|
||||
def _send_game_data(self, member: AbstractRoomMember):
|
||||
def _send_game_data(self, member: Spectator | Player):
|
||||
member.send("init_game", self.to_dict())
|
||||
|
||||
def everbody_is_here(self):
|
||||
@ -109,20 +114,24 @@ class Game(AbstractRoom):
|
||||
|
||||
if (player.is_connected()):
|
||||
player.disconnect(1001)
|
||||
|
||||
player.connect(socket)
|
||||
|
||||
player.socket = socket
|
||||
|
||||
if (self.everbody_is_here()):
|
||||
print("chie moi dessu")
|
||||
print("start")
|
||||
self.start()
|
||||
|
||||
|
||||
self._update_player(player)
|
||||
|
||||
return player
|
||||
|
||||
def _update_player(self, player: Player):
|
||||
self._updated_players.append(player)
|
||||
|
||||
def _player_leave(self, player: Player):
|
||||
self._updated_players.append(player)
|
||||
print(player.socket)
|
||||
self._update_player(player)
|
||||
|
||||
def _spectator_join(self, user_id: int, socket: WebsocketConsumer):
|
||||
|
||||
@ -130,8 +139,6 @@ class Game(AbstractRoom):
|
||||
|
||||
self.spectators.append(spectator)
|
||||
|
||||
spectator.accept()
|
||||
|
||||
return spectator
|
||||
|
||||
def _spectator_leave(self, spectator: Spectator):
|
||||
|
@ -9,7 +9,7 @@ class GameManager():
|
||||
|
||||
def get(self, game_id: int) -> Game:
|
||||
|
||||
if (not GameModel.objects.filter(pk = game_id, started = True, finished = False).exists()):
|
||||
if (not GameModel.objects.filter(pk = game_id, finished = False).exists()):
|
||||
return None
|
||||
|
||||
for game in self._game_list:
|
||||
|
@ -6,6 +6,8 @@ from channels.generic.websocket import WebsocketConsumer
|
||||
|
||||
from .Position import Position
|
||||
from .Spectator import Spectator
|
||||
from .Point import Point
|
||||
from .Segment import Segment
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
@ -15,7 +17,7 @@ if TYPE_CHECKING:
|
||||
|
||||
class Player(Spectator):
|
||||
|
||||
def __init__(self, game: Game, user_id: int, socket: WebsocketConsumer, rail_start_x: float, rail_start_y: float, rail_stop_x: float, rail_stop_y: float) -> None:
|
||||
def __init__(self, game: Game, user_id: int, socket: WebsocketConsumer, rail: Segment) -> None:
|
||||
|
||||
super().__init__(user_id, socket, game)
|
||||
|
||||
@ -23,10 +25,7 @@ class Player(Spectator):
|
||||
|
||||
self.nb_goal: int = 0
|
||||
|
||||
self.rail_start_x: float = rail_start_x
|
||||
self.rail_start_y: float = rail_start_y
|
||||
self.rail_stop_x: float = rail_stop_x
|
||||
self.rail_stop_y: float = rail_stop_y
|
||||
self.rail: Segment = rail
|
||||
|
||||
def receive(self, data: dict):
|
||||
|
||||
@ -106,19 +105,17 @@ class Player(Spectator):
|
||||
|
||||
def disconnect(self, code: int = 1000):
|
||||
self.socket = None
|
||||
print("bozoman")
|
||||
self.game.leave(self)
|
||||
|
||||
def to_dict(self):
|
||||
def to_dict(self) -> dict:
|
||||
|
||||
data = {
|
||||
"user_id": self.user_id,
|
||||
"position": self.position.to_dict(),
|
||||
"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,
|
||||
"rail": self.rail.to_dict(),
|
||||
|
||||
"is_connected": self.is_connected(),
|
||||
}
|
||||
|
16
games/objects/Point.py
Normal file
16
games/objects/Point.py
Normal file
@ -0,0 +1,16 @@
|
||||
|
||||
|
||||
class Point:
|
||||
|
||||
def __init__(self, x: float, y: float) -> None:
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def to_dict(self):
|
||||
|
||||
data: dict[str: float] = {
|
||||
"x": self.x,
|
||||
"y": self.y,
|
||||
}
|
||||
|
||||
return data
|
17
games/objects/Segment.py
Normal file
17
games/objects/Segment.py
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
from .Point import Point
|
||||
|
||||
class Segment:
|
||||
|
||||
def __init__(self, start: Point, stop: Point) -> None:
|
||||
self.start: Point = start
|
||||
self.stop: Point = stop
|
||||
|
||||
def to_dict(self):
|
||||
|
||||
data: dict[str: dict] = {
|
||||
"start": self.start.to_dict(),
|
||||
"stop": self.stop.to_dict(),
|
||||
}
|
||||
|
||||
return data
|
@ -1,20 +1,15 @@
|
||||
|
||||
|
||||
from .Segment import Segment
|
||||
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 __init__(self, rail: Segment) -> None:
|
||||
self.rail: Segment = rail
|
||||
|
||||
def to_dict(self):
|
||||
def to_dict(self) -> dict[str: dict]:
|
||||
|
||||
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,
|
||||
"rail": self.rail.to_dict(),
|
||||
}
|
||||
|
||||
return data
|
@ -5,16 +5,68 @@ from typing import TYPE_CHECKING
|
||||
if TYPE_CHECKING:
|
||||
from .objects.Spectator import Spectator
|
||||
from .objects.Player import Player
|
||||
from .objects.Game import Game
|
||||
from .objects.Game import Game
|
||||
from .objects.Ball import Ball
|
||||
|
||||
from .objects.Point import Point
|
||||
from .objects.Segment import Segment
|
||||
|
||||
from . import config
|
||||
|
||||
import math
|
||||
from time import sleep
|
||||
|
||||
#see the video to understand the algorithme
|
||||
#https://www.youtube.com/watch?v=KOYoMYWUTEo
|
||||
def determine_director_coefficient(segment: Segment):
|
||||
return ((segment.start.y - segment.stop.y) / (segment.start.x - segment.stop.x))
|
||||
|
||||
def determine_ordinate_at_origin(point: Point, director_cofficient: float):
|
||||
return point.y - point.x * director_cofficient
|
||||
|
||||
def determine_intersection(director_coefficient1: float, ordinate_at_origin1: float, director_coefficient2: float, ordinate_at_origin2: float):
|
||||
if (director_coefficient1 == director_coefficient2):
|
||||
return None
|
||||
return (ordinate_at_origin1 + ordinate_at_origin2) / (director_coefficient1 + director_coefficient2)
|
||||
|
||||
def determine_intersections(ball: Ball, segments: list[Segment]):
|
||||
|
||||
intersections: list[Point] = []
|
||||
|
||||
for segment in segments:
|
||||
|
||||
# form m * x + p
|
||||
m: float = determine_director_coefficient(segment)
|
||||
p: float = determine_ordinate_at_origin(segment.start, m)
|
||||
|
||||
x: float = determine_intersection(m, p, ball.velocity_y, 0)
|
||||
|
||||
if (x is None):
|
||||
continue
|
||||
|
||||
y: float = m * x + p
|
||||
|
||||
intersections.append(Point(x, y))
|
||||
|
||||
return intersections
|
||||
|
||||
def determine_distance_between_ball_and_wall(ball: Ball, segments: list[Segment]):
|
||||
|
||||
intersections: list[Point] = determine_intersections(ball, segments)
|
||||
|
||||
distances = list(map(math.dist, intersections))
|
||||
|
||||
return min(distances)
|
||||
|
||||
def render(ball: Ball, game: Game):
|
||||
|
||||
segments: list[Segment] = [player.rail for player in game.players]
|
||||
|
||||
print(determine_distance_between_ball_and_wall(ball))
|
||||
|
||||
def routine(game: Game):
|
||||
|
||||
while True:
|
||||
|
||||
for player in game._updated_players:
|
||||
game.broadcast("update_paddle", player.to_dict(), [player])
|
||||
|
||||
|
Reference in New Issue
Block a user