Example #1
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()
            }
        })
Example #2
0
    def answer(request):
        qid     = request.POST.get('question_id', None)
        aid     = request.POST.get('answer_id', None)
        gid     = request.POST.get('game_id', 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

        if game.total >= 9:
            game.status = settings.GA
            game.save()
            connection = APNS()
            connection.connect()
            for p in game.players.all():
                p.status = 'online'
                p.save()

                push_id = p.push_id[1:-1]
                push_id = re.sub(r'\s', '', push_id)

                connection.send(push_id, {'apn': {'action': 'show_result'}})

            return JsonResponse({'status': 'ok',
                'data': {
                }
            })

        if game.step >= 4:
            game.step = 0

            p1 = game.players.all()[0]
            p2 = game.players.all()[1]

            p1.status = 'enemy_turn'
            p2.status = 'ingame'

            p1.save()
            p2.save()

            game.turn = p2.id

            p1_push_id = p1.push_id[1:-1]
            p1_push_id = re.sub(r'\s', '', p1_push_id)
            p2_push_id = p2.push_id[1:-1]
            p2_push_id = re.sub(r'\s', '', p2_push_id)
            connection = APNS()
            connection.connect()
            print(p1_push_id, p2_push_id)
            connection.send(p1_push_id, {'apn': {'action': 'enemy_turn'}})
            connection.send(p2_push_id, {'apn': {'action': 'ingame'}})

        else:
            game.step += 1

        game.total += 1
        game.save()

        res = IngameResults()
        res.player_id = player.id
        res.game = game
        res.question_id = qid
        res.answer_id = aid
        res.save()

        return JsonResponse({'status': 'ok',
            'data': {
                # 'username': player.login,
                # 'status': player.status,
                'username': player.login,
                'userId': str(player.id),
                'status': player.status,
                'game': game.json()
            }
        })
Example #3
0
from skirmish.settings import *

import random

from time import sleep
import re

from player.models import Player
from game.models import Game
from round.models import Round

from apns import APNS

django.setup()

connection = APNS()
connection.connect()

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()
Example #4
0
 def setUp(self):
     self.device_token = TEST_DEVICE_TOKEN
     self.notif = APNSNotification(device_token=self.device_token, alert="foobar", badge=1, sound="default")
     self.apns = APNS(ROOT_DIR + "/config/apns_cert.development.pem")
     self.apns.start()
     self.assertTrue(self.apns.wait_status(30))
Example #5
0
class TestService(unittest.TestCase):
    def setUp(self):
        self.device_token = TEST_DEVICE_TOKEN
        self.notif = APNSNotification(device_token=self.device_token, alert="foobar", badge=1, sound="default")
        self.apns = APNS(ROOT_DIR + "/config/apns_cert.development.pem")
        self.apns.start()
        self.assertTrue(self.apns.wait_status(30))

    def test_basic(self):
        print "Connected", self.apns.get_status()
        self.apns.stop()

    def test_send_notification(self):
        self.apns.put_notification(self.notif)
        time.sleep(1)

    def test_send_error_notification(self):
        self.notif.token = "abcd"
        self.apns.put_notification(self.notif)
        import time

        time.sleep(1)
        self.assertTrue(self.apns.get_last_error())
        self.assertEqual(self.apns.get_last_error().status_code, 8)

    def test_feedback(self):
        item = self.apns.get_feedback(timeout=5)
        print item
Example #6
0
@route('/push/')
def index():
    return ('<h1>Registered devices</h1>',
            '<br/>'.join('<strong>%s</strong>' % x for x in devices))

@put('/push/device/')
def register_device():
    devices.add(request.forms.token)
    return "ok"

@post('/push/device/')
def send_notification():
    apns.put(request.forms.token, request.forms.msg)
    return 'ok'


# start apn service
CERTIFICATE = ROOT_DIR+'/config/apns_cert.development.pem' 

from apns import APNS
logger.info("Connecting to APNs ...")
apns = APNS(CERTIFICATE)
apns.start()
assert apns.wait_status(30)


if __name__ == '__main__':
    run(server='gevent', host='0.0.0.0', port=8080, debug=DEBUG, reloader=False)
else:
    application = default_app()
Example #7
0
import sys, os

import django
sys.path.append('/home/ado/projects/skirmish')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "skirmish.settings")
from django.conf import settings

from time import sleep
import re

from player.models import Player

from apns import APNS

connection = APNS()
connection.connect()

target = "e941d9b2f296788ed219b6eeebadbabc5043f7c3bbc64f7c2f98a38cfbe6fde4"

connection.send(target, {'apn': {'alert': '11111'}})