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() } })
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() } })
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() game.status = settings.GAME_STATUS_NEW