def num_players(socket): while True: try: num = int(input("Enter the number of players (2-4): ")) if 1 < num < 5: comm.send_msg(socket, str(num)) return True else: print("Invalid number of players") except: print("Invalid input")
def join_setup(socket): while True: game_name = comm.recv_msg(socket) if game_name: game_lock.acquire() if game_name in games: comm.send_msg(socket,"TRUE") while True: player_name = comm.recv_msg(socket) if player_name: if player_name in games[game_name].list_of_player_names(): comm.send_msg(socket,"FALSE") else: comm.send_msg(socket,"TRUE") games[game_name].add_player(socket,player_name) game_lock.release() print("waiting on players...") return True else: game_lock.release() return False else: comm.send_msg(socket,"FALSE") game_lock.release() continue else: return False
def server_control(client_socket,address): intro_msg = "Welcome to e-poker!" comm.send_msg(client_socket,intro_msg) msg = comm.recv_msg(client_socket) game_setup = False if msg == "HOST": game_setup = host_setup(client_socket) else: game_setup = join_setup(client_socket) if not game_setup: client_socket.close() print(f"Connection from {address} closed") else: print(f"Game Established")
def player_name(socket): player_name = input("Enter player name: ") comm.send_msg(socket, player_name) while True: player_added = comm.recv_msg(socket) if player_added: if player_added == "TRUE": print(f"Player \"{player_name}\" added to game") return True elif player_added == "FALSE": print(f"Player \"{player_name}\" already exists") player_name = input("Enter other player name: ") comm.send_msg(socket, player_name) continue else: print("ERROR IN TRANSMISSION") return False else: return False
def game_name_join(socket): game_name = input("Input name of the game to join: ") comm.send_msg(socket, game_name) while True: game_est = comm.recv_msg(socket) if game_est: if game_est == "TRUE": print("Joined game") return True elif game_est == "FALSE": print("Game \"{game_name}\" does not exist") game_name = input("Input other game name: ") comm.send_msg(socket, game_name) continue else: print("ERROR IN TRANSMISSION") return False else: return False
def game_name_host(socket): game_name = input("Input name of your game: ") comm.send_msg(socket, game_name) while True: game_est = comm.recv_msg(socket) if game_est: if game_est == "TRUE": print(f"Game {game_name} has been established") return True elif game_est == "FALSE": print(f"Sorry game {game_name} already exists") game_name = input("Enter other game name: ") comm.send_msg(socket, game_name) continue else: print("ERROR IN TRANSMISSION") return False else: return False
def host_setup(socket): while True: game_name = comm.recv_msg(socket) if game_name: game_lock.acquire() if game_name in games: comm.send_msg(socket,"FALSE") game_lock.release() continue else: games[game_name] = game.Game(game_name,0) comm.send_msg(socket,"TRUE") while True: num_players = comm.recv_msg(socket) if num_players: while True: player_name = comm.recv_msg(socket) if player_name: if player_name in games[game_name].list_of_player_names(): comm.send_msg(socket,"FALSE") continue else: comm.send_msg(socket, "TRUE") games[game_name].add_player(socket,player_name) rg = threading.Thread(target=run_game, args=(game_name, num_players,)) rg.start() game_lock.release() return True else: game_lock.release() return False else: game_lock.release() return False else: return False
def run_game(game_name,expected_players): game_lock.acquire() running_game = games[game_name] game_lock.release() current_num_players = len(running_game.list_of_player_names()) old_num_players = current_num_players while True: current_num_players = len(running_game.list_of_player_names()) if current_num_players != old_num_players: wait_players = int(expected_players) - current_num_players for player in running_game.list_of_players(): comm.send_msg(player.socket,"MSG") comm.send_msg(player.socket,f"Waiting, on {wait_players} player(s)") old_num_players = current_num_players if current_num_players == expected_players: for player in running_game.list_of_players(): comm.send_msg(player.socket,f"Game, beginning") break time.sleep(2)
port = int(input("Type Port: ")) s.connect((ip, port)) msg = comm.recv_msg(s) print(msg) pref = '' while True: try: pref = int( input( "Would you like to Host (0) or Join (1) a game? (Please Type in Number) " )) if pref == 0: comm.send_msg(s, "HOST") host(s) break elif pref == 1: comm.send_msg(s, "JOIN") join(s) break else: print("Wrong input, Please try again") except: print("Wrong input, Please try again") print("Disconnected from server") exit(0)