feat: improve exit game event on connection close
This commit is contained in:
31
main.py
31
main.py
@@ -24,39 +24,46 @@ async def enter_game(game_id, player_web_socket):
|
|||||||
""" TODO fix asssumption that there will only be two players per game """
|
""" TODO fix asssumption that there will only be two players per game """
|
||||||
# if game does not exist, create it
|
# if game does not exist, create it
|
||||||
if game_id not in GAMES:
|
if game_id not in GAMES:
|
||||||
GAMES[game_id] = {'players': [player_web_socket], 'game_id': game_id}
|
GAMES[game_id] = {"players": [player_web_socket], "game_id": game_id}
|
||||||
message = wait_for_opponent_response()
|
message = wait_for_opponent_response()
|
||||||
await player_web_socket.send(message)
|
await player_web_socket.send(message)
|
||||||
# if game exists, add player to game if they are not already in it
|
# if game exists, add player to game if they are not already in it
|
||||||
elif player_web_socket.remote_address[0] not in map(lambda p: p.remote_address[0], GAMES[game_id]['players']):
|
elif player_web_socket.remote_address[0] not in map(lambda p: p.remote_address[0], GAMES[game_id]["players"]):
|
||||||
GAMES[game_id]['players'].append(player_web_socket)
|
GAMES[game_id]["players"].append(player_web_socket)
|
||||||
message = start_game_response()
|
message = start_game_response()
|
||||||
await asyncio.wait([player.send(message) for player in GAMES[game_id]['players']])
|
await asyncio.wait([player.send(message) for player in GAMES[game_id]["players"]])
|
||||||
else:
|
else:
|
||||||
print("Already in game...")
|
print("Already in game...")
|
||||||
|
|
||||||
async def exit_game(game_id):
|
async def exit_game(player_remote_address):
|
||||||
players = GAMES[game_id]['players']
|
# get game with player that is disconnecting
|
||||||
del GAMES[game_id]
|
result = list(filter(lambda g: player_remote_address in map(lambda p: p.remote_address[0], g["players"]), GAMES.values()))
|
||||||
|
|
||||||
|
# if game exists, delete game and remove opponent player from game
|
||||||
|
if len(result):
|
||||||
|
game_with_player = result[0]
|
||||||
|
del GAMES[game_with_player["game_id"]]
|
||||||
|
|
||||||
|
# remove opponent player from game
|
||||||
|
result = list(filter(lambda p: p.remote_address[0] != player_remote_address, game_with_player["players"]))
|
||||||
|
if len(result):
|
||||||
|
opponent_player = result[0]
|
||||||
message = exit_game_response()
|
message = exit_game_response()
|
||||||
await asyncio.wait([player.send(message) for player in players])
|
await opponent_player.send(message)
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|
||||||
async def init(websocket, path):
|
async def init(websocket, path):
|
||||||
data = ""
|
|
||||||
try:
|
try:
|
||||||
async for message in websocket:
|
async for message in websocket:
|
||||||
print(websocket.remote_address)
|
|
||||||
data = json.loads(message)
|
data = json.loads(message)
|
||||||
if data["action"] == "enter_game":
|
if data["action"] == "enter_game":
|
||||||
await enter_game(data["gameId"], websocket)
|
await enter_game(data["gameId"], websocket)
|
||||||
else:
|
else:
|
||||||
print("Unsupported action...")
|
print("Unsupported action...")
|
||||||
finally:
|
finally:
|
||||||
if data and "gameId" in data:
|
|
||||||
print("Exiting game...")
|
print("Exiting game...")
|
||||||
await exit_game(data["gameId"])
|
await exit_game(websocket.remote_address[0])
|
||||||
print("Connection closed...")
|
print("Connection closed...")
|
||||||
|
|
||||||
start_server = websockets.serve(init, "", 5001)
|
start_server = websockets.serve(init, "", 5001)
|
||||||
|
|||||||
Reference in New Issue
Block a user