Beispiel #1
0
 def set_player_defeated(self, user_id, game_id):
     self.status = 2002
     player = PlayerProvider().get_player(game_id, user_id)
     if (self.__player_exists(player)):
         player.defeat()
         self.status = 1000
     return player, self.status
Beispiel #2
0
 def check_bankrupt(self, user_id, game_id):
     self.status = 2007
     player = PlayerProvider().get_player(game_id, user_id)
     if (self.__player_exists(player)):
         if (player.is_bankrupt()):
             player.defeat()
             self.status = 1000
     return player, self.status
Beispiel #3
0
    def join_player(self, user_id, game_id):
        self.status = 1001
        player = PlayerProvider().get_player(game_id, user_id)
        if (self.__free_slot(game_id) and player == None):
            player_order = len(PlayerProvider().get_game_players(
                game_id))  #Player.objects.filter(game_id=game_id).count()
            player = self.__add_player(user_id, game_id, player_order)

        return player, self.status
Beispiel #4
0
 def get_chance_card(self, game_id, user_id):
     player = PlayerProvider().get_player(game_id, user_id)
     position = player.position
     if position == 3 or position == 9 or position == 15 or position == 21:
         chance_card = ChanceProvider().get_chance()
         if self.__player_exists(player):
             player.update_balance(chance_card.value)
         return chance_card, self.status
     else:
         self.status = 2018
         return None, self.status
Beispiel #5
0
 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
Beispiel #6
0
    def skip_turn(self, user_id, game_id):
        self.status = 1003
        player = PlayerProvider().get_player(game_id, user_id)
        #Game.objects.get(pk=game_id)

        if (self.__player_exists(player) and self.__skip_constraint(player)
            ) or self.__player_exists(player):
            PlayerProvider().skip_turn(player.id)
        if len(PlayerProvider().get_active_game_players(game_id=game_id)) == 1:
            self.status = 1410

        return GameProvider().get_game(game_id), self.status
def test_tax_when_success():
  Proxy()
  game = GameFactory()
  init_balance = 1000
  cost = 100
  player = PlayerFactory(game=game, position=1, balance=init_balance)
  player2 = PlayerFactory(game=game, balance=init_balance)
  charge = ChargeFactory(zero_apartments=cost)
  card = CardFactory(position=1, charge=charge)
  property = PropertyFactory(player=player2, game=game, card=card, buildings=0)
  Proxy().load(full=True)
  response = WebsocketService().tax(player.game_id, player.user_id)
  assert PlayerProvider().get_player_with_id(player.id).balance == init_balance-cost
  assert PlayerProvider().get_player_with_id(player2.id).balance == init_balance+cost
 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
Beispiel #9
0
    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
Beispiel #10
0
 def __free_slot(self, game_id):
     players_amount = len(PlayerProvider().get_game_players(
         game_id))  #Player.objects.filter(game_id=game_id).count()
     allowed_players_amount = int(
         GameProvider().get_game(game_id).players_amount
     )  #Game.objects.filter(id=game_id).first().players_amount
     return players_amount < allowed_players_amount
 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 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
Beispiel #13
0
    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_player_provider_returns_valid_player():
    # GIVEN
    player = PlayerFactory()
    # WHEN
    provided_player = PlayerProvider().get_player(user_id=player.user_id,
                                                  game_id=player.game_id)
    # THEN
    assert provided_player == player
Beispiel #15
0
 def __buy_building(self, property):
     owner = PlayerProvider().get_owner(property.id)
     property.buildings += 1
     card = CardProvider().get_card(property.card_id)
     if property.buildings == 5:
         owner.balance -= card.hotel_cost
     elif property.buildings < 5 and property.buildings > 0:
         owner.balance -= card.apartment_cost
 def __move_player_to_jail(self, player):
     self.status = 1997
     if self.__player_exists(player):
         player.jailed = settings.DEFAULT_GAME_SETTINGS['jail_turns']
         player.position = settings.DEFAULT_GAME_SETTINGS['jail_position']
         player.move = 0
         PlayerProvider().skip_turn(player.id)
     return player, self.status
Beispiel #17
0
 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_chance_add_minus_value():
    Proxy()
    chance = ChanceFactory(value=-1500)
    player = PlayerFactory(balance=2500, position=3)
    Proxy().load(full=True)
    chance, status = ChanceService().get_chance_card(game_id=player.game.id,
                                                     user_id=player.user.id)
    new_balance = PlayerProvider().get_player_with_id(player.id).balance
    assert new_balance == (player.balance + chance.value)
Beispiel #19
0
def test_skip_turn_doestn_set_move_to_zero_if_move_eq_two():
    # GIVEN
    Proxy()
    player = PlayerFactory(move=2)
    Proxy().load(full=True)
    # WHEN
    GameService().skip_turn(game_id=player.game_id, user_id=player.user_id)
    # THEN
    assert PlayerProvider().get_player_with_id(player.id).move == 2
Beispiel #20
0
def test_player_on_go_to_jail_position_sets_next_player_move_to_two():
    Proxy()
    player = PlayerFactory(position=16, move=2, order=0)
    player2 = PlayerFactory(move=0, order=1, game=player.game)
    Proxy().load()
    dice = MockDice(2)
    player, status = PositionService(dice).move_player(game_id=player.game_id,
                                                       user_id=player.user_id)
    assert PlayerProvider().next_player(player.id).move == 2
Beispiel #21
0
 def __sell_building(self, property):
     owner = PlayerProvider().get_owner(property.id)
     card = CardProvider().get_card(property.card_id)
     if property.buildings == 5:
         property.buildings -= 1
         owner.balance += card.hotel_cost
     elif property.buildings >= 0:
         property.buildings -= 1
         owner.balance += card.apartment_cost
Beispiel #22
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 start(self, game_id):
     game = GameProvider().get_game(game_id)
     if not game.active:
         PlayerProvider().get_active_game_players(
             game_id=game_id)[0].enable_move()
         game.set_active()
         self.__prepare_response(game, 1000)
         return self.response
     self.__prepare_response(game, 2000)
     return self.response
 def move_player(self, game_id, user_id):
     player = PlayerProvider().get_player(game_id, user_id)
     if self.__player_exists(player) and self.__players_turn(player):
         if self.__player_jailed(player):
             self.decrement_player_jail(player)
         else:
             self.change_player_position(player)
             if self.__player_on_jail_position(player):
                 self.__move_player_to_jail(player)
     return player, self.status
Beispiel #25
0
def test_skip_turn_sets_player_move_to_zero():
    # GIVEN
    Proxy()
    player = PlayerFactory(move=1)
    player = PlayerFactory(move=0, game_id=player.game_id, order=1)
    Proxy().load(full=True)
    # WHEN
    GameService().skip_turn(game_id=player.game_id, user_id=player.user_id)
    # THEN
    assert PlayerProvider().get_player_with_id(player.id).move == 0
Beispiel #26
0
def test_join_player_creates_new_player():
    # GIVEN
    Proxy()
    game = GameFactory()
    user = UserFactory()
    Proxy().load(full=True)
    # WHEN
    GameService().join_player(game_id=game.id, user_id=user.id)
    # THEN
    assert len(PlayerProvider().get_game_players(game.id)) == 1
def test_player_provider_returns_valid_player_when_there_are_more_players():
    # GIVEN
    player1 = PlayerFactory()
    player2 = PlayerFactory(game_id=player1.game_id, order=1)
    player3 = PlayerFactory(game_id=player1.game_id, order=2)
    # WHEN
    provided_player = PlayerProvider().get_player(user_id=player1.user_id,
                                                  game_id=player1.game_id)
    # THEN
    assert provided_player == player1
Beispiel #28
0
def test_set_player_defeated_changes_active_to_false():
    # GIVEN
    Proxy()
    user = UserFactory()
    game = GameFactory()
    Proxy().load(full=True)
    player, _ = GameService().join_player(game_id=game.id, user_id=user.id)
    # WHEN
    GameService().set_player_defeated(user_id=user.id, game_id=game.id)
    # THEN
    assert PlayerProvider().get_player_with_id(player.id).active == False
 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
Beispiel #30
0
 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