Exemple #1
0
def leave_game(request):
    opponent = get_opponent(request.user)
    if opponent is not None:
        game = finish_game(get_opponent(request.user).user, request.user)
    else:
        game = finish_game(None, request.user)
    game = Game.objects.filter(pk=game.pk)
    data = serializers.serialize('json', game, use_natural_keys=True)
    return HttpResponse(data, content_type='application/json')
Exemple #2
0
def move_unit(request):
    pk = int(request.GET['pk'])
    unit = Unit.objects.get(pk=pk)
    if not unit.active:
        return HttpResponseBadRequest()
    unit.active = False
    left = int(request.GET['left'])
    top = int(request.GET['top'])

    opponent = get_opponent(request.user)
    opponent_units = opponent.unit_set.all().filter(left=left, top=top)
    opponent_unit = opponent_units.first()
    opponent_unit_json = None
    settlement_json = None

    if opponent_units.exists():
        if fight(unit, opponent_unit):
            unit.save()
            opponent_unit_json = Unit.objects.filter(pk=opponent_unit.pk)
            opponent_unit_json = serializers.serialize('json', opponent_unit_json, use_natural_keys=True)
            opponent_unit.delete()
        else:
            unit.delete()
            return HttpResponse()
    else:
        opponent_settlement = Settlement.objects.all().filter(player=opponent, left=left, top=top).first()
        if opponent_settlement is not None:
            opponent_settlement.player = get_player(request.user)
            opponent_settlement.save()
            settlement_json = Settlement.objects.filter(pk=opponent_settlement.pk)
            settlement_json = serializers.serialize('json', settlement_json, use_natural_keys=True)
        unit.left = left
        unit.top = top
        unit.save()

    unit = Unit.objects.filter(pk=pk)
    opponent_units = get_opponent_units_for_unit(unit.first(), opponent)
    opponent_settlements = get_opponent_settlements_for_unit(unit.first(), opponent)

    unit = serializers.serialize('json', unit, use_natural_keys=True)

    data = {'unit': unit, 'opponent_unit': opponent_unit_json, 'settlement': settlement_json,
            'opponent_units': opponent_units, 'opponent_settlements': opponent_settlements}

    return HttpResponse(json.dumps(data), content_type='application/json')
Exemple #3
0
def finish_stroke(request):
    player = get_player(request.user)
    opponent = get_opponent(request.user)

    if player.is_lost():
        finish_game(opponent.user, player.user)
        return HttpResponse()

    if opponent.is_lost():
        finish_game(player.user, opponent.user)
        return HttpResponse()

    player.active = False
    player.save()
    Unit.objects.filter(player=player).update(active=False)
    Settlement.objects.filter(player=player).update(active=False)

    opponent.increase_money_for_day()
    opponent.active = True
    opponent.save()

    Unit.objects.filter(player=opponent).update(active=True)
    Settlement.objects.filter(player=opponent).update(active=True)
    return HttpResponse()