Skip to content

Commit

Permalink
Fix(kick): Fixed server termination and kicking
Browse files Browse the repository at this point in the history
Co-authored-by: Hauke Platte <[email protected]>
  • Loading branch information
Petzys and HOOK-Hawkins committed Mar 19, 2024
1 parent fabe04b commit b5f87e7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions Client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ async def chat_message(self, message:str):

async def close(self):
await self._websocket.close()
exit()

async def terminate(self):
msg = {
Expand Down
2 changes: 0 additions & 2 deletions Client/ui_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ async def await_commands(self):

if message:
match message["message_type"]:
case "lobby/join":
pass
case "lobby/ready":
await self.lobby_ready(**message["args"])
case "lobby/kick":
Expand Down
15 changes: 9 additions & 6 deletions Server/websocket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class Lobby:
def __init__(self, admin:Player, port: int = 8765) -> None:
self._players = {}
#self._players = {admin.uuid : admin}
self._admin = admin
self._game = None
self._inprogress = False
self._port = port
Expand Down Expand Up @@ -73,7 +73,6 @@ async def handler(self, websocket):
"message_type": "lobby/kick",
"kick_player_uuid": message_json["kick_player_uuid"],
}))


case "lobby/ready":

Expand Down Expand Up @@ -137,9 +136,10 @@ async def handler(self, websocket):


case "server/terminate":
logger.info("Server Termination Requested")
logger.info("Server Termination Requested. Checking if game is in progress.")

if self._inprogress:
logger.info("Game in progress. Terminating game.")
if self._game.players.index(self._players[message_json["player_uuid"]]) == 1:
self._game.state.set_winner(2)
elif self._game.players.index(self._players[message_json["player_uuid"]]) == 2:
Expand All @@ -149,15 +149,18 @@ async def handler(self, websocket):

await self._end_game()

else:
elif message_json["player_uuid"] == str(self._admin.uuid):
# still in lobby, can terminate without game end.
logger.info("Not in game. Sender was host. Terminating server.")
websockets.broadcast(self._connections, json.dumps({
"message_type": "game/error",
"error_message": "Server terminated.",
}))

exit()


else:
logger.info("Not in game. Sender was not host. Ignoring termination request.")

case _:
await websocket.send(json.dumps({"message_type": "error", "error": "Unknown message type"}))
Expand All @@ -172,10 +175,10 @@ async def handler(self, websocket):

else:
# request a ping from everyone and delete player list to wait for join messages.
self._players = {}
websockets.broadcast(self._connections, json.dumps({
"message_type": "lobby/ping",
}))
self._players = {}

# End this connection loop
break
Expand Down
15 changes: 14 additions & 1 deletion UI/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, master, *args, opponent=player_type.unknown, local_players, q
self.master.network_events['lobby/status'] = self._update_lobby
self.master.network_events['game/start'] = self._start_game
self.master.network_events['lobby/kick'] = self._lobby_kick
self.master.network_events['lobby/connect_error'] = lambda *args: self._connect_error() #error is thrown when the server is not reachable anymore
self.bind('<Destroy>', lambda *args: self.on_destroy())
self.ready = False
if opponent not in [player_type.unknown, player_type.local]:
Expand Down Expand Up @@ -66,11 +67,21 @@ def _display_widgets(self):
self.btnExit.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=5, row=1)
self.btnStatistics.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=5, row=2)

def _connect_error(self):
self.master.show_menu()
msg = messages(type='info', message=f'Connection to the server was lost!')
msg.display()

def _menu(self):
list(self.master.out_queue.values())[0].put({'message_type': 'server/terminate', 'args' :{} })
self.master.show_menu()

def _update_lobby(self, queue):
if(len(self.playerlist) > 0):
for player in self.playerlist:
for object in player:
object.grid_forget()
object.destroy()
self.playerlist = []
for player in queue['player']:
rdy = '\u2611' if player.ready else ''
Expand All @@ -97,11 +108,13 @@ def _start_game(self, queue):
def on_destroy(self):
del self.master.network_events['lobby/status']
del self.master.network_events['game/start']
del self.master.network_events['lobby/kick']
del self.master.network_events['lobby/connect_error']

def _lobby_kick(self, queue):
self._menu()
msg = messages(type='info', message=f'You have been kicked from the lobby by the host')
msg.display()
self._menu()

def _show_statistics(self):
self.master.cache_current_frame()
Expand Down

0 comments on commit b5f87e7

Please sign in to comment.