feat: remove depend on ip address for game start

This commit is contained in:
2021-07-06 01:58:19 -04:00
parent f212d6e2f7
commit 25801eba4d

43
main.py
View File

@@ -19,36 +19,36 @@ def start_game_response():
def exit_game_response(): def exit_game_response():
return json.dumps({"type": "exit_game"}) return json.dumps({"type": "exit_game"})
def receive_opponent_piece(piece): def receive_piece(piece):
return json.dumps({"type": "receive_opponent_piece", "piece": piece}) return json.dumps({"type": "receive_piece", "piece": piece})
# EVENTS # EVENTS
async def enter_game(game_id, player): async def enter_game(game_id, client_id, player):
""" 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] = create_game(game_id, player) GAMES[game_id] = create_game(game_id, client_id, player)
message = wait_for_opponent_response() await player.send(wait_for_opponent_response())
await player.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.remote_address[0] not in map(lambda p: p.remote_address[0], GAMES[game_id]["players"]): elif client_id not in GAMES[game_id]["clients"]:
GAMES[game_id]["players"].append(player) GAMES[game_id]["players"].append(player)
message = start_game_response() GAMES[game_id]["clients"].append(client_id)
await asyncio.wait([player.send(message) for player in GAMES[game_id]["players"]]) await asyncio.wait([player.send(start_game_response()) for player in GAMES[game_id]["players"]])
else: else:
print("Already in game...") print("Already in game...")
async def send_piece(game_id, player, piece): async def send_piece(game_id, client_id, player, piece):
# make sure game exists # make sure game exists
if game_id not in GAMES: if game_id not in GAMES:
print("Game does not exist...") print("Game does not exist...")
return return
# get opponent player and send piece to opponent player # get opponent player and send piece to opponent player
await get_opponent_player(game_id, player.remote_address[0])\ await get_opponent_player(game_id, player)\
.send(receive_opponent_piece(piece)) .send(receive_piece(piece))
# TODO refactor function
async def exit_game(player_remote_address): async def exit_game(player_remote_address):
# get game with player that is disconnecting # get game with player that is disconnecting
result = list(filter(lambda g: player_remote_address in map(lambda p: p.remote_address[0], g["players"]), GAMES.values())) result = list(filter(lambda g: player_remote_address in map(lambda p: p.remote_address[0], g["players"]), GAMES.values()))
@@ -69,28 +69,25 @@ async def exit_game(player_remote_address):
# HELPER FUNCTIONS # HELPER FUNCTIONS
def get_opponent_player(game_id, player_remote_address): def get_opponent_player(game_id, player):
opponent_player = list(filter(lambda p: p.remote_address[0] != player_remote_address, GAMES[game_id]["players"]))[0]; opponent_player = list(filter(lambda p: p != player, GAMES[game_id]["players"]))[0];
return opponent_player return opponent_player
def create_game(game_id, player=None): def create_game(game_id, client_id=None, player=None):
print("Creating game...") return {"players": ([player] if player else []),\
return {"players": ([player] if player else []), "game_id": game_id} "clients": ([client_id] if client_id else []),\
"game_id": game_id}
# -------------------------- # --------------------------
async def init(websocket, path): async def init(websocket, path):
try: try:
async for message in websocket: async for message in websocket:
if message == "ping":
print(message)
await websocket.send("pong")
else:
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"], data["clientId"], websocket)
elif data["action"] == "send_piece": elif data["action"] == "send_piece":
await send_piece(data["gameId"], websocket, data["piece"]) await send_piece(data["gameId"], data["clientId"], websocket, data["piece"])
else: else:
print("Unsupported action...") print("Unsupported action...")
finally: finally: