def pay_tax(self, game_id, user_id): player = PlayerProvider().get_player(game_id=game_id, user_id=user_id) if self.__player_exists(player): card = CardProvider().get_card_with_position(player.position) if card != None: if PropertyProvider().check_if_exist(game_id, card.id): property = PropertyProvider().get_property_with_card( game_id, card.id) if property.player.user_id != user_id: charge = ChargeProvider().get_charge(card.charge_id) player2 = PlayerProvider().get_player( game_id=game_id, user_id=property.player.user_id) tax_to_pay = charge.get_charge_for_amount_of_buildings( property.buildings) print('1p old balance: ' + str(player.balance)) player.update_balance(-tax_to_pay) print('1p new balance: ' + str(player.balance)) print('2p old balance: ' + str(player2.balance)) player2.update_balance(tax_to_pay) print('2p new balance: ' + str(player2.balance)) self.status = 1000 return [player, player2], self.status else: self.status = 2000 return None, self.status else: self.status = 2007 return None, self.status else: self.status = 2004 return None, self.status else: self.status = 2000 return None, self.status
def release_player_properties(self, game_id, user_id): player = PlayerProvider().get_player(game_id=game_id, user_id=user_id) if self.__player_exists(player): player_properties = PropertyProvider().get_player_properties( game_id=game_id, player_id=player.id) player_properties.delete() return None, self.status
def create_offer(self, game_id, user_id, card_id, price): player = PlayerProvider().get_player(game_id, user_id) user_property = PropertyProvider().get_property_with_card( game_id, card_id) self.__default_validations(player, user_property) if self.__is_valid(): self.__property_owner_validations(player, user_property) self.__player_has_move(player) if self.__is_valid(): user_property.for_sell(price) return user_property, self.status return None, self.status
def cancel_offer(self, game_id, user_id, card_id): player = PlayerProvider().get_player(game_id, user_id) user_property = PropertyProvider().get_property_with_card( game_id, card_id) self.__default_validations(player, user_property) self.__property_owner_validations(player, user_property) self.__property_for_sale(user_property) if self.__is_valid(): user_property.cancel_offer() return user_property, self.status else: return None, self.status
def get_player_properties(self, game_id, user_id): player = PlayerProvider().get_player(game_id=game_id, user_id=user_id) if self.__player_exists(player): self.status = 1000 return PropertyProvider().get_player_properties( game_id=game_id, player_id=player.id), self.status return [], self.status
def cancel_players_offers(self, game_id, user_id): player = PlayerProvider().get_player(game_id, user_id) user_properties = PropertyProvider().get_player_properties( game_id=game_id, player_id=player.id) for user_property in user_properties: self.__property_for_sale(user_property) if self.__is_valid(): user_property.cancel_offer()
def test_property_provider_returns_if_property_with_card_id_not_exist(): # GIVEN Proxy() game = GameFactory() property = PropertyFactory(game_id=game.id, card_id=1) Proxy().load() # WHEN if_exist = PropertyProvider().check_if_exist(game_id=game.id, card_id=2) # THEN assert if_exist == False
def test_is_property_taken_returns_false(): # GIVEN Proxy() game = GameFactory() card = CardFactory() Proxy().load() # WHEN is_taken = PropertyProvider().is_property_taken(game_id=game.id, card_id=card.id) # THEN assert is_taken == False
def test_property_provider_returns_valid_property(): # GIVEN Proxy() player = PlayerFactory() property = PropertyFactory(player=player, game_id=player.game_id) Proxy().load() # WHEN provided_properties = PropertyProvider().get_player_properties( game_id=player.game_id, player_id=player.id) # THEN assert provided_properties[0] == property
def deposit(self, game_id, user_id, card_id): player = PlayerProvider().get_player(game_id,user_id) user_property = PropertyProvider().get_property_with_card(game_id=game_id,card_id=card_id) self.check_default_validations(player, user_property) if self.__is_valid(): self.is_not_deposited(user_property) if self.__is_valid(): self.__deposit(user_property) return PlayerProvider().get_player(game_id,user_id), self.status return None, self.status
def get_available_card_to_buy(self, game_id, user_id): player = PlayerProvider().get_player(game_id, user_id) card = CardProvider().get_card_with_position(player.position) if card: if not PropertyProvider().check_if_exist(game_id, card.id): return card, self.status else: self.status = 2006 return None, self.status self.status = 2005 return None, self.status
def test_is_property_taken_returns_true(): # GIVEN Proxy() game = GameFactory() card = CardFactory() player = PlayerFactory(game_id=game.id) property = PropertyFactory(player=player, game_id=game.id, card_id=card.id) Proxy().load() # WHEN is_taken = PropertyProvider().is_property_taken(game_id=game.id, card_id=card.id) # THEN assert is_taken == True
def accept_offer(self, game_id, user_id, card_id): new_owner = PlayerProvider().get_player(game_id, user_id) user_property = PropertyProvider().get_property_with_card( game_id, card_id) old_owner = PlayerProvider().get_owner(property_id=user_property.id) self.__default_validations(new_owner, user_property) self.__property_for_sale(user_property) self.__player_can_afford(new_owner, user_property) if self.__is_valid(): self.__finish_exchange(new_owner, old_owner, user_property) return [new_owner, old_owner], self.status else: return None, self.status
def test_property_provider_returns_if_property_with_card_id_not_exist_in_specified_game( ): # GIVEN Proxy() game = GameFactory() game2 = GameFactory() card = CardFactory() property = PropertyFactory(game_id=game2.id, card_id=card.id) Proxy().load() # WHEN if_exist = PropertyProvider().check_if_exist(game_id=game.id, card_id=card.id) # THEN assert if_exist == False
def test_property_provider_returns_valid_property_when_there_are_more_players( ): # GIVEN Proxy() player = PlayerFactory() player2 = PlayerFactory(game_id=player.game_id, order=1) property = PropertyFactory(player=player, game_id=player.game_id) property2 = PropertyFactory(player=player, game_id=player2.game_id) Proxy().load() # WHEN provided_properties = PropertyProvider().get_player_properties( game_id=player.game_id, player_id=player.id) # THEN assert provided_properties[0] == property
def test_cancel_offer_when_user_is_not_owner(): # GIVEN Proxy() user = UserFactory() # WHEN game = GameFactory() player = PlayerFactory(user=user, game=game, move=1) card = CardFactory(position=5) user_property = PropertyFactory(card=card, game=game, selling_price=4000) Proxy().load(full=True) [prop, status] = TradingService().cancel_offer(game.id, user.id, card.id) # THEN property_price = PropertyProvider().get_property_with_id( user_property.id).selling_price assert property_price == 4000 assert prop == None assert status == 2004
def sell_building(self, game_id, user_id, card_id): player = PlayerProvider().get_player(game_id=game_id, user_id=user_id) self.__player_exists(player) if self.__is_valid(): user_property = PropertyProvider().get_property_with_card( game_id, card_id=card_id) self.__property_validations(user_property, player) if self.__is_valid(): self.__is_deposited(user_property) self.__player_has_move(player) self.__min_apartment_reach_limit(user_property) if self.__is_valid(): self.__sell_building(user_property) return PlayerProvider().get_player( game_id=game_id, user_id=user_id), self.status return None, self.status
def test_buy_building_when_valid_buying_hotel(): # GIVEN Proxy() player = PlayerFactory(balance=700, position=3, move=1) card = CardFactory(position=3) property = PropertyFactory(card=card, player=player, game=player.game, buildings=4, deposited=False) Proxy().load(full=True) [property, status] = BuildingService().buy_building(player.game_id, player.user_id) new_balance = PlayerProvider().get_player_with_id(player.id).balance updated_property = PropertyProvider().get_property_with_id(property.id) assert status == 1000 assert updated_property.buildings == 5 assert new_balance == 0
def buy_building(self, game_id, user_id): player = PlayerProvider().get_player(game_id=game_id, user_id=user_id) self.__player_exists(player) if self.__is_valid(): user_property = PropertyProvider().get_property_with_position( game_id, player.position) self.__property_validations(user_property, player) if self.__is_valid(): self.__is_deposited(user_property) #self.__player_can_afford(user_property, player) <- git gud self.__player_has_move(player) self.__max_apartment_reach_limit(user_property) if self.__is_valid(): self.__buy_building(user_property) return PlayerProvider().get_player( game_id=game_id, user_id=user_id), self.status return None, self.status
def buy_property(self, game_id, user_id): player = PlayerProvider().get_player(game_id=game_id, user_id=user_id) if self.__player_exists(player): card = CardProvider().get_card_with_position(player.position) if card != None: if not PropertyProvider().is_property_taken(game_id, card.id): self.status = 1000 property = Property(player_id=player.id, game_id=player.game_id, card_id=card.id) Proxy().propertys_dict[property.id] = property player.update_balance(-card.cost) return player, self.status else: self.status = 2006 else: self.status = 2005 else: self.status = 2002 return None, self.status
def test_repurchase_when_valid(): # GIVEN Proxy() game = GameFactory() card = CardFactory(position=1) player = PlayerFactory(game_id=game.id, move=1) user_property = PropertyFactory(player=player, card=card, game=game, deposited=True) Proxy().load(full=True) # WHEN deposit_property, status = DepositService().repurchase( user_id=player.user.id, game_id=game.id, card_id=card.id) # THEN new_balance = PlayerProvider().get_player_with_id(player.id).balance new_property = PropertyProvider().get_property_with_id(user_property.id) assert new_property == user_property assert status == 1000 assert new_property.deposited == False assert new_balance == (player.balance - card.deposit_value)