ready up feature now launches the game frame

This commit is contained in:
2023-04-27 20:52:12 -07:00
parent 3897f9e862
commit c513a08b28
2 changed files with 187 additions and 115 deletions

View File

@@ -13,6 +13,7 @@ class ServerVCKO:
self.server_socket.bind((self.host, Constants.port))
self.game_list = []
self.lobby = []
self.gamers = []
def handle_client(self, conn, addr):
print(f"Connection from: {addr}")
@@ -44,18 +45,49 @@ class ServerVCKO:
temp_lobby.append(player)
self.lobby = temp_lobby
self.send_lobby_state(conn)
elif full_command[1] == "get_status" and len(full_command) == 2:
self.send_lobby_state(conn)
elif full_command[1] == "get_status" and len(full_command) == 3:
found = False
for player in self.lobby:
if full_command[2] == player.player_id:
self.send_lobby_state(conn)
found = True
for player in self.gamers:
if full_command[2] == player.player_id:
message = f"game joined {player.game_id}"
conn.send(message.encode(Constants.text_format))
found = True
# this only runs if we somehow receive an invalid player id
if not found:
message = f"Unable to find any player with player id: {full_command[2]}"
conn.send(message.encode(Constants.text_format))
elif full_command[1] == "ready" and len(full_command) > 2:
ready_check = 0
for player in self.lobby:
if player.player_id == full_command[2]:
player.is_ready = True
self.send_lobby_state(conn)
if player.is_ready:
ready_check += 1
print(f"ready check: {ready_check}")
if ready_check == len(self.lobby):
new_game_id = str(uuid.uuid4())
for player in self.lobby:
print(f"lobby player: {player.name}")
players_to_remove = []
for player in self.lobby:
if player.is_ready:
new_gamer = GameMember(player.player_id, player.name, new_game_id)
self.gamers.append(new_gamer)
players_to_remove.append(player)
for player in players_to_remove:
self.lobby.remove(player)
message = f"game joined {new_game_id}"
conn.send(message.encode(Constants.text_format))
else:
self.send_lobby_state(conn)
elif full_command[1] == "unready" and len(full_command) > 2:
for player in self.lobby:
if player.player_id == full_command[2]:
player.is_ready = False
self.send_lobby_state(conn)
else:
conn.send("invalid message".encode(Constants.text_format))
@@ -72,7 +104,7 @@ class ServerVCKO:
conn, addr = self.server_socket.accept()
thread = threading.Thread(target=self.handle_client, args=(conn, addr))
thread.start()
print(f"Active threads: {threading.active_count() - 1}")
print(f"\nActive threads: {threading.active_count() - 1}")
def send_lobby_state(self, conn):
lobby_data = []
@@ -94,6 +126,13 @@ class LobbyMember:
self.is_ready = False
class GameMember:
def __init__(self, player_id, player_name, game_id):
self.name = player_name
self.player_id = player_id
self.game_id = game_id
if __name__ == '__main__':
print("server starting")
server = ServerVCKO()