Example #1
0
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 playing(socket):
    while True:
        msg = comm.recv_msg(socket)
        if msg:
            if msg == "MSG":
                msg = comm.recv_msg(socket)
                print(msg)
            elif msg == "TABLE":
                print("TABLE")
            else:
                print(
                    f"ERROR, in SERVER MESSAGE. MESSAGE IS {msg} EXPECTED MSG OR TABLE"
                )
        else:
            break
Example #3
0
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")
Example #4
0
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 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
        if game_name_join(socket):
            if player_name(socket):
                playing(socket)

    except Exception as e:
        print(f"Error occurred ude to exception: {e}")


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

ip = input("Type Host IP: ")
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: