From 7730bd73154e264df61b22488cf1651785095b93 Mon Sep 17 00:00:00 2001 From: MoltenWolfCub Date: Sat, 11 Jan 2025 15:03:46 +0000 Subject: [PATCH] fixed players killing themselves the bullets had no player assosiated with them so upon shooting, they were immediately in the player's hitbox and killed themselves. Now fixed --- common/bullet.py | 13 +++++++------ server/game_data.py | 31 +++++++++++++++++++++++++------ server/main.py | 2 +- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/common/bullet.py b/common/bullet.py index bf45410..ddf2f20 100644 --- a/common/bullet.py +++ b/common/bullet.py @@ -1,15 +1,16 @@ +from typing import Optional from common.data_types import Vec2D +from common.player import CommonPlayer class CommonBullet: ENCODED_SIZE: int = 4 - def __init__(self, pos: Vec2D, shoot_angle: int) -> None: - - self.pos = pos - - self.shoot_angle = shoot_angle + def __init__(self, pos: Vec2D, shoot_angle: int, shooter: Optional[CommonPlayer] = None) -> None: + self.owner: Optional[CommonPlayer] = shooter + self.pos: Vec2D = pos + self.shoot_angle: int = shoot_angle def encode(self) -> bytes: b = self.pos.x.to_bytes(2) @@ -22,7 +23,7 @@ def decode(bytes: bytes) -> 'CommonBullet': x = int.from_bytes(bytes[:2]) y = int.from_bytes(bytes[2:4]) - return CommonBullet(pos=Vec2D(x, y), shoot_angle=-1) + return CommonBullet(pos=Vec2D(x, y), shoot_angle=-1, shooter=None) def __str__(self) -> str: return f"Bullet[pos= {self.pos}, shoot_angle= {self.shoot_angle}]" diff --git a/server/game_data.py b/server/game_data.py index 5459979..8f6dcb3 100644 --- a/server/game_data.py +++ b/server/game_data.py @@ -54,25 +54,36 @@ def update(self) -> None: self.bullets.remove(bullet) continue + dead_conns: list[Connection] = [] for player in self.players: player_rect: Rect = Rect( (player.pos-Vec2D(Settings.player_radius, Settings.player_radius)), (player.pos+Vec2D(Settings.player_radius, Settings.player_radius)), ) - conn: Connection = self.server.open_connections[player.id] + conn: Optional[Connection] = self.get_connection(player.id) + if conn is None: + print(f"Couldn't find connection assosiated with player id {player.id}") + raise LookupError() gone_bullets: list[CommonBullet] = [] - for bullet in self.bullets: - if player_rect.contains(bullet.pos): - self.server.send(conn, S2CDisconnectPlayer(S2CDisconnectPlayer.KILLED)) - self.server.close_connection(conn) + if bullet.owner is player: + continue + if player_rect.contains(bullet.pos): + dead_conns.append(conn) gone_bullets.append(bullet) + bullet_dirty = True players_dirty = True + break + + self.bullets = [b for b in self.bullets if b not in gone_bullets] + + for c in dead_conns: + self.server.send(c, S2CDisconnectPlayer(S2CDisconnectPlayer.KILLED)) + self.server.close_connection(c) - self.bullets = [b for b in self.bullets if b not in gone_bullets] if players_dirty: self.server.broadcast(S2CPlayers(self.players)) @@ -107,3 +118,11 @@ def get_player(self, player_id: int) -> Optional[CommonPlayer]: print(f"Couldn't find player with ID {player_id}") return None + + def get_connection(self, player_id: int) -> Optional[Connection]: + for conn in self.server.open_connections: + if conn.player_id == player_id: + return conn # if more than 1 connection with same ID something very wrong + + print(f"Couldn't find connection with player ID {player_id}") + return None diff --git a/server/main.py b/server/main.py index 3232a39..defba06 100644 --- a/server/main.py +++ b/server/main.py @@ -274,7 +274,7 @@ def handle_packet(self, raw_packet: RawPacket) -> Optional[Exception]: print(f"Error! No player assosiated with connection: {raw_packet.sender}") return LookupError() - self.game.bullets.append(CommonBullet(shooting_player.pos.clone(), bullet_packet.angle)) + self.game.bullets.append(CommonBullet(pos=shooting_player.pos.clone(), shoot_angle=bullet_packet.angle, shooter=shooting_player)) self.broadcast(S2CBullets(self.game.bullets))