def turn(gamer, data):
    """
    Start gamer's turn

    Parameters
    ----------
    gamer: Player who current play
    data: The whole data dict
    """
    end_flag = False
    while True:

        input_data = data["msg"].get_json_data("input")
        while input_data is False:
            input_data = data["msg"].get_json_data("input")
            # print(input_data)
        input_str = input_data[0]["request"]
        print("input_str", input_str)
        choice = int(input_str)
        if choice == 1:
            # Trade with others
            trade_data = data["msg"].get_json_data("trade")
            while trade_data is False:
                trade_data = data["msg"].get_json_data("trade")
            operation.trade(data, trade_data)
        elif choice == 2 and end_flag is False:
            # Roll dice
            dice_a, dice_b, end_flag = roll(gamer, data)
        elif choice == 3:
            # Construct building
            operation.construct_building(gamer, data)
        elif choice == 4:
            # Remove building
            operation.remove_building(gamer, data)
        elif choice == 5:
            # Mortgage
            operation.mortgage_asset(gamer, data)
        elif choice == 6:
            if end_flag is True:
                # End turn
                break
            else:
                # haven't rolled dice
                data["msg"].push2single(
                    gamer.id, operation.gen_hint_json("Please roll a dice"))
                data["msg"].push2single(gamer.id,
                                        operation.gen_newturn_json(gamer))
        else:
            # Invalide choice
            data["msg"].push2single(gamer.id,
                                    operation.gen_hint_json("Invalid choice"))
        # Update all value to front end
        data["msg"].push2all(operation.gen_update_json(data))
Пример #2
0
 def play(self, gamer, data):
     """
     Move player on the map to certain block
     """
     # data['msg'].push2single(
     #     gamer.id, operation.gen_hint_json(self.description))
     data['msg'].push2all(
         operation.gen_hint_json("player %s get card: %s" %
                                 (gamer.name, self.description)))
     if isinstance(self._destination, list):
         tmp = self._destination[0]
         for dest in self._destination:
             if gamer.position < dest:
                 tmp = dest
                 data['msg'].push2single(
                     gamer.id,
                     operation.gen_hint_json(
                         "Do not pass Go, no cash collected."))
                 gamer.move(steps=None, position=tmp)
                 break
             else:
                 continue
         go_block = data["chess_board"][0]
         data['msg'].push2single(
             gamer.id,
             operation.gen_hint_json("Passing Go, Gain %d" %
                                     go_block.reward))
         operation.pay(data['epic_bank'], gamer, go_block.reward, data)
         gamer.move(steps=None, position=tmp)
         dest_block = data["chess_board"][tmp]
         dest_block.display(gamer, data, 0)
     else:
         if gamer.position < self._destination:
             data['msg'].push2single(
                 gamer.id,
                 operation.gen_hint_json(
                     "Do not pass Go, no cash collected."))
         else:
             go_block = data["chess_board"][0]
             data['msg'].push2single(
                 gamer.id,
                 operation.gen_hint_json("Passing Go, Gain %d" %
                                         go_block.reward))
             operation.pay(data['epic_bank'], gamer, go_block.reward, data)
         if self._destination == 10:
             # in jail
             gamer.cur_status = 0
         gamer.move(steps=None, position=self._destination)
         dest_block = data["chess_board"][self._destination]
         dest_block.display(gamer, data, 0)
Пример #3
0
 def play(self, gamer, data):
     """
     :param from_role: a player or bank or rest players
     :param data: global game data
     """
     data['msg'].push2all(
         operation.gen_hint_json("player %s get card: %s" %
                                 (gamer.name, self.description)))
     if self._card_type == 2:
         to_role = data['epic_bank']
         from_role = gamer
         operation.pay(from_role, to_role, self._amount, data)
     elif self._card_type == 8:  # need to check
         to_role = []
         all_role = data["player_dict"]
         for role_id in all_role.keys():
             if role_id != gamer.id:
                 to_role.append(all_role[role_id])
         from_role = gamer
         total_amount = self._amount * len(to_role)
         for role in to_role:
             operation.pay(from_role, role, self._amount, data)
     elif self._card_type == 3:
         import estate
         from_role = gamer
         total_amount = 0
         for e in from_role.assets:
             if isinstance(estate, estate.Estate):
                 house_repair_amount = int(
                     e.house_num % 6) * self._amount[0]
                 hotel_repair_amount = int(
                     e.house_num / 6) * self._amount[1]
                 total_amount += house_repair_amount + hotel_repair_amount
                 operation.pay(from_role, data['epic_bank'], self._amount,
                               data)
Пример #4
0
    def display(self, gamer, data, dice_result):
        """
        Start go to jail function

        Parameters
        ----------
        gamer: Player who currently play
        data: All the data in game. Dict format
        dice_result: The result of dice

        """
        data["msg"].push2single(
            gamer.id, operation.gen_hint_json("Move to jail"))
        gamer.move(steps=0, position=10)
        gamer.add_in_jail_time()
        gamer.cur_status = 0
def roll(gamer, data):
    """
    Roll dice

    Parameters
    ----------
    gamer: 
    data: 

    Returns
    -------
    result of two dice, end_flag determind whether obtain same result

    """
    # Whether pass Go
    a, b = roll_dice(gamer, data)
    if a == b:
        end_flag = False
    else:
        end_flag = True
    step = a + b
    current_gamer_position = gamer.position
    if current_gamer_position + step > 40:
        go_block = data["chess_board"][0]
        operation.pay(data['epic_bank'], gamer, go_block.reward, data)
        data["msg"].push2single(
            gamer.uid,
            operation.gen_hint_json("Passing Go, Gain %d" % go_block.reward))
        data["msg"].push2all("%s passing GO, Gain %d" %
                             (gamer.name, go_block.reward))
    gamer.move(step)
    end_position = gamer.position
    current_block = data['chess_board'][end_position]
    if isinstance(current_block, block.Go_To_Jail):
        end_flag = True
    data["msg"].push2all(
        operation.gen_record_json("%s at %s" %
                                  (gamer.name, current_block.name)))
    current_block.display(gamer, data, step)
    return a, b, end_flag
Пример #6
0
 def play(self, gamer, data):
     """
     :param from_role: a player or bank or rest players
     :param from_role: player or bank or rest players
     """
     # data['msg'].push2single(
     #     gamer.id, operation.gen_hint_json(self.description))
     data['msg'].push2all(
         operation.gen_hint_json("player %s get card: %s" %
                                 (gamer.name, self.description)))
     if self._card_type == 0:
         to_role = gamer
         operation.pay(data['epic_bank'], to_role, self._amount, data)
     elif self._card_type == 1:
         to_role = gamer
         from_role = []
         all_role = data["player_dict"]
         for role_id in all_role.keys():
             if role_id != gamer.id:
                 from_role.append(role_id)
         for role_id in from_role:
             payer = all_role[role_id]
             operation.pay(payer, gamer, self._amount, data)
Пример #7
0
    def display(self, gamer, data, dice_result):
        """display message when entering the utility

        Parameters
        ----------
        self: class itself
        gamer: gamer object
        data: data dict
        dice_result: result of dice

        """
        # import operation
        player_dict = data['player_dict']
        bank = data['epic_bank']
        if self._status == 1:
            # Some body own it
            owner_id = self.owner
            owner = player_dict[owner_id]
            if owner_id == gamer.id:
                # Owner pass this station
                data["msg"].push2all(
                    operation.gen_record_json("%s own %s" %
                                              (gamer.name, self.name)))
            else:
                # Other pass this station
                passing_time = 0
                if gamer.id in self.enter_log:
                    passing_time = self.enter_log[gamer.id]
                    self.enter_log[gamer.id] += 1
                else:
                    self.enter_log[gamer.id] = 1
                data["msg"].push2all(
                    operation.gen_record_json("%s own %s" %
                                              (gamer.name, self.name)))
                payment = self.payment(gamer.utility_num,
                                       dice_result) * (0.9**passing_time)
                if gamer.alliance == owner.alliance:
                    # Make discount to alliance
                    payment = payment * 0.9
                    data['msg'].push2single(
                        gamer.id,
                        operation.gen_hint_json(
                            "%s and %s are alliances, make discount" %
                            (owner.name, gamer.name)))
                operation.pay(gamer, owner, payment, data)
        elif self._status == -1:
            # Nobody own
            while True:
                data["msg"].push2single(
                    gamer.id,
                    operation.gen_choice_json(
                        "Nobody own %s do you want to buy it?" % self.name))
                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:
                    price = self.value
                    if price > gamer.cash:
                        data["msg"].push2single(
                            gamer.id,
                            operation.gen_hint_json(
                                "You do not have enough money"))
                        break
                    else:
                        operation.pay(gamer, bank, price, data)
                        operation.trade_asset(self, bank, gamer)
                        data["msg"].push2all(
                            operation.gen_record_json(
                                "%s buy %s for %d" %
                                (gamer.name, self.name, price)))
                        break
                elif choice == 2:
                    break
                else:
                    data["msg"].push2single(
                        gamer.id, operation.gen_hint_json("Invalid operation"))
        elif self._status == 0:
            # In mortgage
            data["msg"].push2single(
                gamer.id,
                operation.gen_hint_json("%s is in mortgaged" % self.name))
        else:
            raise ValueError("Invalid estate status")
Пример #8
0
 def display(self, gamer, data, dice_result):
     """
     Display description
     """
     player_dict = data['player_dict']
     epic_bank = data['epic_bank']
     if self._status == 1:
         # Some body own it
         owner_id = self.owner
         owner = player_dict[owner_id]
         print(owner_id)
         print(player_dict)
         if owner_id == gamer.id:
             # Owner pass this estate
             data['msg'].push2single(gamer.id, operation.gen_hint_json(
                 "%s own %s" % (gamer.name, self.name)))
         else:
             # Other pass this estate
             passing_time = 0
             if gamer.id in self.enter_log:
                 passing_time = self.enter_log[gamer.id]
                 self.enter_log[gamer.id] += 1
             else:
                 self.enter_log[gamer.id] = 1
             payment = self.payment * (0.9 ** passing_time)
             data['msg'].push2single(gamer.id, operation.gen_hint_json(
                 "%s own %s" % (owner.name, self.name)))
             if gamer.alliance == owner.alliance:
                 # Make discount to alliance
                 payment = payment * 0.9
                 data['msg'].push2single(gamer.id, operation.gen_hint_json(
                     "%s and %s are alliances, make discount" % (owner.name, gamer.name)))
             operation.pay(gamer, owner, payment, data)
     elif self._status == -1:
         # Nobody own
         while True:
             # data['msg'].push2singe(gamer.id, operation.gen_hint_json("Nobody own %s do you want to buy it?" % self.name))
             data["msg"].push2single(gamer.id, operation.gen_choice_json(
                 "Nobody own %s do you want to buy it?" % self.name))
             # data['msg'].push2single(gamer.id, operation.gen_hint_json("Price: %d" % self.value))
             # data['msg'].push2single(gamer.id, operation.gen_hint_json("1: Buy it"))
             # data['msg'].push2single(gamer.id, operation.gen_hint_json("2: Do not buy it"))
             input_str = data['msg'].get_json_data("input")
             while not input_str:
                 input_str = data['msg'].get_json_data("input")
             input_str = input_str[0]["request"]
             choice = int(input_str)
             if choice == 1:
                 price = self.value
                 cur_cash = gamer.cash
                 if price > cur_cash:
                     data['msg'].push2single(gamer.id, operation.gen_hint_json(
                         "You do not have enough money"))
                     break
                 else:
                     operation.pay(gamer, epic_bank, price, data)
                     operation.trade_asset(self, epic_bank, gamer)
                     data['msg'].push2all(operation.gen_hint_json(
                         "%s buy %s for %d" % (gamer.name, self.name, price)))
                     break
             elif choice == 2:
                 break
             else:
                 data['msg'].push2single(
                     gamer.id, operation.gen_hint_json("Invalid operation"))
     elif self._status == 0:
         # In mortgage
         data['msg'].push2single(gamer.id, operation.gen_hint_json(
             "%s is in mortgaged" % self.name))
     else:
         raise ValueError("Invalid estate status")
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