예제 #1
0
파일: tests.py 프로젝트: Cephlin/PokeBattle
    def test_fight(self):
        self.do_setup(red_attack=3, red_speed=10, blue_attack=3, blue_speed=7)

        round1 = Round(battle=self.battle, red=self.red, blue=self.blue)
        round1.save()

        red_team = PlayerPokemon.objects.filter(player=round1.red)
        blue_team = PlayerPokemon.objects.filter(player=round1.blue)

        current_red_pokemon = None
        current_blue_pokemon = None

        for red_pokemon, blue_pokemon in izip(red_team, blue_team):
            if red_pokemon.lead:
                current_red_pokemon = red_pokemon

            if blue_pokemon.lead:
                current_blue_pokemon = blue_pokemon

            if current_red_pokemon and current_blue_pokemon:
                break

        self.assertEqual(self.battle.winner, None)
        self.assertEqual(current_red_pokemon.hp, 18)
        self.assertEqual(current_blue_pokemon.hp, 19)

        round1.red_selected_move = current_red_pokemon.move1
        round1.blue_selected_move = current_blue_pokemon.move1
        round1.save()

        round2 = round1.advance_round()
        round2.save()

        """
예제 #2
0
def match(first, second):
    first_push_id = first.push_id[1:-1]
    first_push_id = re.sub(r'\s', '', first_push_id)
    second_push_id = second.push_id[1:-1]
    second_push_id = re.sub(r'\s', '', second_push_id)

    first.status = PLAYER_STATUS_ONLINE
    second.status = PLAYER_STATUS_ONLINE

    first.save()
    second.save()

    game = Game()
    game.status = settings.GAME_STATUS_NEW
    game.save()

    game.players.add(first)
    game.players.add(second)

    r       = Round()
    r.game  = game
    r.owner = random.choice((first, second))
    r.turn  = r.owner
    r.current = 1
    r.save()

    if len(first_push_id) == 64:
        print ('--- sending notifications for ', first.login)
        connection.send(first_push_id, {
            'changed': [{'entity': 'user', 'entity_id': first.id}],
            'new': [{'entity': 'game', 'entity_id': game.id}],
        })
    if len(second_push_id) == 64:
        print ('--- sending notifications for ', second.login)
        connection.send(second_push_id, {
            'changed': [{'entity': 'user', 'entity_id': second.id}],
            'new': [{'entity': 'game', 'entity_id': game.id}],
        })
예제 #3
0
    def answer(request):
        gid     = request.POST.get('game_id', None)
        ch      = request.POST.get('choice', None)
        push_id = request.POST.get('push_id', None)

        game    = Model.objects.get(pk=gid)
        player  = Player.objects.filter(push_id=push_id)[0]

        if not Game:
            return

        cr = None
        for r in game.rounds.all():
            if r.current == 1:
                cr = r

        if cr.step == 5:
            if cr.turn != cr.owner:
                r       = Round()
                r.game  = game
                r.current = 1
                if game.players.all()[0].id == player.id:
                    r.owner = game.players.all()[0]
                else:
                    r.owner = game.players.all()[1]
                r.turn = r.owner
                r.step = 1

                r.save()

                cr.current = 0
                cr.save()

                connection = APNS()
                connection.connect()

                address = cr.turn.push_id[1:-1]
                address = re.sub(r'\s', '', address)
                connection.send(address, {
                    'changed': [{'entity': 'game', 'entity_id': game.id}]
                })

                return JsonResponse({
                    'status': 'ok',
                    'result': {},
                    'payload': {
                        'game': game.json()
                    }
                })

            if game.players.all()[0].id == player.id:
                cr.turn = game.players.all()[1]
            else:
                cr.turn = game.players.all()[0]
            cr.step = 1

            connection = APNS()
            connection.connect()

            address = cr.turn.push_id[1:-1]
            address = re.sub(r'\s', '', address)
            connection.send(address, {
                'changed': [{'entity': 'game', 'entity_id': game.id}]
            })

            connection.disconnect()
        else:
            cr.step = cr.step + 1

        cr.save()
        game.current_round = cr

        return JsonResponse({
            'status': 'ok',
            'result': {},
            'payload': {
                'game': game.json()
            }
        })