Exemple #1
0
def start_game(mess_hand):
    global data
    data = init_game(mess_hand)
    living_list = list(data["player_dict"].keys())
    data["living_list"] = living_list
    num_round = 0
    update_period = 1
    while len(living_list) != 1:
        if num_round % update_period == 0:
            operation.update_value(data)
        num_round += 1
        for gamer_id in living_list:
            gamer = data["player_dict"][gamer_id]
            operation.push2all("Now is %s turn" % gamer.name)
            operation.push2all("Gamer current cash %d" % gamer.cash)
            if gamer.cur_status == 0:
                # In jail
                operation.push2all("%s are in jail" % gamer.name)
                a, b = roll_dice()
                if a == b:
                    gamer.cur_status = 1
                    gamer._in_jail = 0
                    turn(gamer, data)
                else:
                    while True:
                        operation.push2all("1: Bail. Get out of prison.")
                        operation.push2all("2: Stay in prison.")
                        while True:
                            input_str = operation.wait_choice(
                                "Please enter the number of your decision:")
                            if (True):
                                input_data = data["msg"].get_json_data("input")
                                input_str = input_data[0]["request"]
                            try:
                                choice = int(input_str)
                                break
                            except ValueError:
                                operation.push2all("Please enter a number.")
                        operation.push2all()
                        if choice == 1:
                            if operation.bail(gamer, data):
                                gamer.cur_status = 1
                                gamer._in_jail = 0
                                turn(gamer, data)
                                break
                            else:
                                operation.push2all("Please stay in jail")
                                gamer.count_in_jail()
                                break
                        elif choice == 2:
                            gamer.count_in_jail()
                            break
                        else:
                            operation.push2all("Invalid choice")
            elif gamer.cur_status == 1:
                # Normal Status
                turn(gamer, data)
            else:
                pass
            operation.push2all()
def game_main(room_d, connection):
    global roomid, conn
    roomid = room_d
    conn = connection

    data = init_game()
    living_list = list(data["player_dict"].keys())
    data["living_list"] = living_list
    while len(living_list) != 1:
        for gamer_id in living_list:
            gamer = data["player_dict"][gamer_id]
            operation.push2all("Now is %s turn" % gamer.name)
            operation.push2all("Gamer current cash %d" % gamer.cash)
            if gamer.cur_status == 0:
                # In jail
                operation.push2all("%s are in jail" % gamer.name)
                a, b = roll_dice()
                if a == b:
                    gamer.cur_status = 1
                    gamer._in_jail = 0
                    turn(gamer)
                else:
                    while True:
                        operation.push2all("1: Bail. Get out of prison.")
                        operation.push2all("2: Stay in prison.")
                        while True:
                            operation.push2all(
                                "Please enter the number of your decision:")
                            input_str = operation.wait_choice()
                            try:
                                choice = int(input_str)
                                break
                            except ValueError:
                                operation.push2all("Please enter a number.")
                        operation.push2all(" ")
                        if choice == 1:
                            if operation.bail(gamer, data):
                                gamer.cur_status = 1
                                gamer._in_jail = 0
                                turn(gamer)
                                break
                            else:
                                operation.push2all("Please stay in jail")
                                gamer.count_in_jail()
                                break
                        elif choice == 2:
                            gamer.count_in_jail()
                            break
                        else:
                            operation.push2all("Invalid choice")
            elif gamer.cur_status == 1:
                # Normal Status
                turn(gamer)
            else:
                pass
            operation.push2all(" ")
def start_game(create_room_dict, child_conn):
    """
    start a game

    """
    global data
    print("*", create_room_dict)
    # Initialize messager
    roomid = create_room_dict["room"]["room_id"]
    mess_hand = messager.Messager(roomid, child_conn)
    # Initialize game setting
    data = init_game(mess_hand, create_room_dict)
    living_list = list(data["player_dict"].keys())
    data["living_list"] = living_list
    num_round = 0
    update_period = 1
    result = operation.gen_init_json(data)
    data["msg"].push2all(result)
    while len(living_list) != 1:
        # When only one player left, end the game
        if num_round % update_period == 0:
            # Dynamic change the value after every update period
            operation.update_value(data)
        num_round += 1
        for gamer_id in living_list:
            # Iterate every player
            gamer = data["player_dict"][gamer_id]

            if gamer.cur_status == 0:
                # In jail
                data["msg"].push2single(
                    gamer.id,
                    operation.gen_hint_json("%s are in jail" % gamer.name))
                a, b = roll_dice(gamer, data)
                if a == b:
                    # Out of jail
                    gamer.cur_status = 1
                    gamer._in_jail = 0
                    turn(gamer, data)
                else:
                    while True:
                        data["msg"].push2all(
                            gamer.id,
                            operation.gen_choice_json(
                                "You are in jail! Do you want to bail yourself out?"
                            ))
                        input_data = data["msg"].get_json_data("input")
                        while input_data is False:
                            input_data = data["msg"].get_json_data("input")
                        input_str = input_data[0]["request"]
                        choice = int(input_str)
                        if choice == 1:
                            # Bail out
                            if operation.bail(gamer, data):
                                gamer.cur_status = 1
                                gamer._in_jail = 0
                                turn(gamer, data)
                                break
                            else:
                                # Don't have enough money
                                data["msg"].push2single(
                                    gamer.id,
                                    operation.gen_hint_json(
                                        "Please stay in jail"))
                                gamer.count_in_jail()
                                break
                        elif choice == 2:
                            # Stay in jail
                            gamer.count_in_jail()
                            break
                        else:
                            # Invalid choice
                            data["msg"].push2single(
                                gamer.id,
                                operation.gen_hint_json("Invalid choice"))
            elif gamer.cur_status == 1:
                data["msg"].push2all(operation.gen_newturn_json(gamer))
                # Normal Status
                turn(gamer, data)
            else:
                pass