def create_colony(request): settler_type = 1 unit = Unit.objects.get(pk=int(request.GET['pk'])) if not unit and unit.unit_type == settler_type: return HttpResponseBadRequest if not check_margins(get_game_map(request.user), unit.left, unit.top): return HttpResponseBadRequest colony_type = 1 settlement = create_settlement(unit.map, unit.left, unit.top, unit.player, colony_type, False) Unit.objects.filter(pk=unit.pk).delete() settlement = Settlement.objects.filter(pk=settlement.pk) data = serializers.serialize('json', settlement, use_natural_keys=True) return HttpResponse(data, content_type='application/json')
def buy_unit(request): settlement = Settlement.objects.get(pk=int(request.GET['settlement_pk'])) if not settlement.active: return HttpResponseBadRequest() player = Player.objects.filter(user=request.user).first() unit_type = int(request.GET['type']) money = player.money cost = UNIT_TYPE[unit_type]['cost'] if money < cost: return HttpResponseBadRequest() player.money = money - cost game_map = get_game_map(request.user) unit = create_unit(game_map, settlement.left, settlement.top, player, unit_type, False) settlement.active = False settlement.save() player.save() unit_set = Unit.objects.filter(pk=unit.pk) data = serializers.serialize('json', unit_set, use_natural_keys=True) return HttpResponse(data, content_type='application/json')
def check_settlements_margins(request): unit = Unit.objects.get(pk=int(request.GET['pk'])) return HttpResponse(json.dumps({'available': check_margins(get_game_map(request.user), unit.left, unit.top)}), mimetype="application/json")