Ejemplo n.º 1
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}],
        })
Ejemplo n.º 2
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()
            }
        })