예제 #1
0
    def test_participants(self):
        rules = self._get_default_rules()
        battle = Battle(rules)
        client = MockClient("123", "peter")
        battle.add_participant(client)

        # can't add any junk
        self.assertRaises(AttributeError, battle.add_participant, object())
        junk_client = MockClient(0, 0)
        self.assertRaises(AssertionError, battle.add_participant, junk_client)

        # self.assertTrue(battle.participants)
        self.assertFalse(battle.ready_to_play())
        self.assertTrue(battle.is_open())

        client2 = MockClient("222", "chris")
        battle.add_participant(client2)
        self.assertTrue(battle.ready_to_play())
        self.assertFalse(battle.is_open())

        client3 = MockClient("333", "frick")
        self.assertRaises(BattleError, battle.add_participant, client3)

        battle.remove_participant(client)
        self.assertFalse(battle.ready_to_play())
        self.assertFalse(battle.is_open())
예제 #2
0
    def _initiate(self, rules):
        """called when the client has connected successfully"""
        self.send(dict(your_name=self.user_name))
        battle = None

        _del_battles = set()
        for created_battle in self.battles:
            if created_battle.is_dead(10):
                _del_battles.add(created_battle)
                continue

            if created_battle.is_open(rules):
                if self in created_battle:
                    self.send(dict(error={'message':'Already in an open battle',
                                          'code': errors.ERROR_ALREADY_IN_OPEN_BATTLE}))
                    return
                battle = created_battle
                logging.debug("%r joining battle: %r" % (self.user_name, battle))
                break

        for bad_battle in _del_battles:
            self.battles.remove(bad_battle)

        if not battle:
            battle = Battle(rules)
            logging.debug("Creating new battle")
            self.battles.add(battle)
        battle.add_participant(self, rules)
        self.current_client_battles[self.user_id] = battle
        if battle.ready_to_play():
            battle.commence(self.db)
예제 #3
0
    def test_participants(self):
        rules = self._get_default_rules()
        battle = Battle(rules)
        client = MockClient('123', 'peter')
        battle.add_participant(client)

        # can't add any junk
        self.assertRaises(AttributeError, battle.add_participant, object())
        junk_client = MockClient(0, 0)
        self.assertRaises(AssertionError, battle.add_participant, junk_client)

        #self.assertTrue(battle.participants)
        self.assertFalse(battle.ready_to_play())
        self.assertTrue(battle.is_open())

        client2 = MockClient('222', 'chris')
        battle.add_participant(client2)
        self.assertTrue(battle.ready_to_play())
        self.assertFalse(battle.is_open())

        client3 = MockClient('333', 'frick')
        self.assertRaises(BattleError, battle.add_participant, client3)

        battle.remove_participant(client)
        self.assertFalse(battle.ready_to_play())
        self.assertFalse(battle.is_open())