def play(self, gamer, data): """ Baild card, can be collected by players """ to_role = gamer operation.push2all(self.description) if to_role.bail_card_num == 0: operation.push2all("1. Keep it yourself.") operation.push2all("2. Sell to others.") 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) if choice == 1 or choice == 2: break elif choice == -1: return False else: operation.push2all( "Invaild choice, please input again.") except ValueError: operation.push2all( "Please enter a number. Enter -1 to quit") if choice == 1: to_role.bail_card_num = to_role.bail_card_num + 1 elif choice == 2: while True: # TODO: need checking operation.push2all("Players list:") for p in data['player_dict']: operation.push2all(p['name']) input_str = operation.wait_choice( "Please enter the player of you want to sell the card to or enter 'q' to quit:") if(True): input_data = data["msg"].get_json_data("input") input_str = input_data[0]["request"] try: choice = str(input_str) if choice in data['player_dict'].keys() and choice != gamer.name: break elif choice == 'q': return False else: operation.push2all( "Invaild choice, please input again.") except ValueError: operation.push2all( "Please enter a player name. Enter q to quit") to_role = data['player_dict'][choice] from_role = gamer # TODO: need to implement trade pass
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 display(self, gamer, data, dice_result): """ Display description :type data: dict :return: """ 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 operation.push2all("%s own %s" % (gamer.name, self.name)) else: # Other pass this station operation.push2all("%s own %s" % (owner.name, self.name)) fee = self.payment(gamer.station_num) operation.pay(gamer, owner, fee, data) elif self._status == -1: # Nobody own while True: operation.push2all( "Nobody own %s do you want to buy it?" % self.name) operation.push2all("1: Buy it") operation.push2all("2: Do not buy it") 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: price = self.value if price > gamer.cash: operation.push2all("You do not have enough money") break else: operation.pay(gamer, bank, price, data) operation.trade_asset(self, bank, gamer) operation.push2all("%s buy %s for %d" % (gamer.name, self.name, price)) break elif choice == 2: break else: operation.push2all("Invalid operation") elif self._status == 0: # In mortgage operation.push2all("%s is in mortgaged" % self.name) else: raise ValueError("Invalid estate status")
def display(self, gamer, data, dice_result): """ Display description :param dice_result: :return: """ 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] if owner_id == gamer.id: # Owner pass this estate operation.push2all("%s own %s" % (gamer.name, self.name)) else: # Other pass this estate operation.push2all("%s own %s" % (owner.name, self.name)) payment = self.payment operation.pay(gamer, owner, payment, data) elif self._status == -1: # Nobody own while True: operation.push2all("Nobody own %s do you want to buy it?" % self.name) operation.push2all("Price: %d" % self.value) operation.push2all("1: Buy it") operation.push2all("2: Do not buy it") 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: price = self.value cur_cash = gamer.cash if price > cur_cash: operation.push2all("You do not have enough money") break else: operation.pay(gamer, epic_bank, price, data) operation.trade_asset(self, epic_bank, gamer) operation.push2all("%s buy %s for %d" % (gamer.name, self.name, price)) break elif choice == 2: break else: operation.push2all("Invalid operation") elif self._status == 0: # In mortgage operation.push2all("%s is in mortgaged" % self.name) else: raise ValueError("Invalid estate status")
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 turn(gamer, data): """ :param gamer: players :return: """ end_flag = False while True: operation.push2all("1: Trade with others") operation.push2all("2: Roll dices") operation.push2all("3: Construct building") operation.push2all("4: Remove building") operation.push2all("5: Mortgage asset") operation.push2all("6: End turn") 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"] print("input_str", input_str) try: choice = int(input_str) break except ValueError: operation.push2all("Please enter a number.") operation.push2all() if choice == 1: # operation.trade(data, trade_data) pass elif choice == 2 and end_flag is False: dice_a, dice_b, end_flag = roll(gamer, data) elif choice == 3: operation.construct_building(gamer, data) elif choice == 4: operation.remove_building(gamer, data) elif choice == 5: operation.mortgage_asset(gamer, data) elif choice == 6: if end_flag is True: break else: operation.push2all("Please roll a dice") else: operation.push2all("Invalid choice")
def turn(gamer): """ :param gamer: players :return: """ global data end_flag = False while True: operation.push2all("1: Trade with others") operation.push2all("2: Roll dices") operation.push2all("3: Construct building") operation.push2all("4: Mortgage asset") operation.push2all("5: End turn") 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: operation.trade() elif choice == 2 and end_flag is False: dice_a, dice_b, end_flag = roll(gamer) elif choice == 3: operation.construct_building(gamer, data) elif choice == 4: operation.mortgage_asset(gamer, data) elif choice == 5: if end_flag is True: break else: operation.push2all("Please roll a dice") else: operation.push2all("Invalid choice")