Skip to content

Commit

Permalink
Merge pull request #39 from barnslig/backend_post
Browse files Browse the repository at this point in the history
Feat(Backend): Adds User-Auth & Action Points
  • Loading branch information
Teufelchen1 authored Jul 22, 2021
2 parents ef2d127 + ff5cc25 commit 8b52d51
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 37 deletions.
19 changes: 19 additions & 0 deletions backend/dpt_app/trails/migrations/0006_player_bearer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.5 on 2021-07-21 08:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('trails', '0005_alter_parameter_unique_together'),
]

operations = [
migrations.AddField(
model_name='player',
name='bearer',
field=models.CharField(default='', max_length=255),
preserve_default=False,
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.5 on 2021-07-21 08:58

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('trails', '0006_player_bearer'),
]

operations = [
migrations.AlterUniqueTogether(
name='player',
unique_together={('bearer', 'game')},
),
]
18 changes: 18 additions & 0 deletions backend/dpt_app/trails/migrations/0008_player_action_points.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.5 on 2021-07-22 08:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('trails', '0007_alter_player_unique_together'),
]

operations = [
migrations.AddField(
model_name='player',
name='action_points',
field=models.IntegerField(default=0),
),
]
6 changes: 6 additions & 0 deletions backend/dpt_app/trails/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ def __str__(self):


class Player(models.Model):
class Meta:
unique_together = ('bearer', 'game',)
name = models.CharField(max_length=255)
bearer = models.CharField(max_length=255)
game = models.ForeignKey(
'Game',
on_delete=models.CASCADE,
Expand All @@ -91,6 +94,8 @@ class Player(models.Model):
on_delete=models.CASCADE,
)

action_points = models.IntegerField(default=0)

def __str__(self):
return "Player {0} from game {1}".format(
self.name, self.game
Expand Down Expand Up @@ -124,5 +129,6 @@ class Log(models.Model):
on_delete=models.CASCADE,
related_name="logs"
)

def __str__(self):
return "{0} - Game: {1}, Code: {2}".format(str(self.created_at), self.game, self.code)
145 changes: 108 additions & 37 deletions backend/dpt_app/trails/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from .models import Game, Log, Parameter
from .models import Game, Log, Parameter, Player, Character
from .qr_models import Code
from .enums import ClockType, ParameterType, ActionType, CharacterType

Expand All @@ -24,6 +24,12 @@ def calcTimepassingParameters(game):
parameter.value -= round(passed_tick * parameter.rate)
parameter.save()

for player in game.player.all():
player.action_points += round(passed_tick * 1)
if player.action_points > 15:
player.action_points = 15
player.save()

else:
# Clock is not runnig, take no action
pass
Expand All @@ -36,32 +42,51 @@ def buildJsonResponse(data):
}, json_dumps_params={'indent': 4})


def getGameOr404(request, gameId, get, post):
try:
game = Game.objects.get(name=gameId)
except Game.DoesNotExist:
def get_game_or_404(func):
def inner(request, gameId, *args, **kwargs):
try:
game = Game.objects.get(name=gameId)
except Game.DoesNotExist:
return JsonResponse({"Errors": [
{
"id": "not-found",
"status": 404,
"title": "Unknown Game ID"
}
]}, status=404)
return func(request, game, *args, **kwargs)
return inner


def has_bearer_or_403(func):
def inner(request, game, *args, **kwargs):
if "Authorization" in request.headers.keys():
bearer = request.headers['Authorization']
print(bearer)
if game.player.filter(bearer=bearer).exists():
return func(request, game, *args, **kwargs)
return JsonResponse({"Errors": [
{
"id": "not-found",
"status": 404,
"title": "Unknown Game ID"
"id": "not-authorised",
"status": 403,
"title": "Missing or malformed bearer"
}
]}, status=404)

calcTimepassingParameters(game)

if request.method == 'GET':
return get(game)
elif request.method == 'POST':
return post(game)
]}, status=403)
return inner


@has_bearer_or_403
def index(request):
return HttpResponse("Hello, world.")


def clock(request, gameId):
return getGameOr404(request, gameId, func_clock_get, func_clock_post)
@get_game_or_404
@has_bearer_or_403
def clock(request, game):
if request.method == 'GET':
return func_clock_get(game)
elif request.method == 'POST':
return func_clock_post(game)


def func_clock_get(game):
Expand All @@ -82,17 +107,19 @@ def func_clock_post(game):


@csrf_exempt
def code(request, gameId, codeId):
try:
game = Game.objects.get(name=gameId)
except Game.DoesNotExist:
return JsonResponse({"Errors": [
{
"id": "not-found",
"status": 404,
"title": "Unknown Game ID"
}
]}, status=404)
@get_game_or_404
def code(request, game, codeId):
def is_onboarding(code):
return code.actions.filter(action_type=ActionType.CHARACTER).exists()

def get_bearer(request):
if "Authorization" in request.headers.keys():
return request.headers['Authorization']
else:
return None

def has_valid_bearer(bearer, game):
return game.player.filter(bearer=bearer).exists()

calcTimepassingParameters(game)

Expand All @@ -107,10 +134,21 @@ def code(request, gameId, codeId):
}
]}, status=404)

bearer = get_bearer(request)

if request.method == 'GET':
return func_code_get(game, code)
if has_valid_bearer(bearer, game) or is_onboarding(code):
return func_code_get(game, code)
elif request.method == 'POST':
return func_code_post(game, code)
if has_valid_bearer(bearer, game) or is_onboarding(code):
return func_code_post(game, code, bearer)
return JsonResponse({"Errors": [
{
"id": "not-authorised",
"status": 403,
"title": "Missing or malformed bearer"
}
]}, status=403)


def func_code_get(game, code):
Expand Down Expand Up @@ -145,7 +183,7 @@ def func_code_get(game, code):
return buildJsonResponse(response)


def func_code_post(game, code):
def func_code_post(game, code, bearer):
if code.one_shot is True and game.logs.filter(id=code.id).exists():
return JsonResponse({"errors": [
{
Expand All @@ -164,20 +202,39 @@ def func_code_post(game, code):
except:
pass
elif action.action_type == ActionType.CHARACTER:
pass
if game.player.filter(bearer=bearer).exists():
""" Error """
pass
else:
Player(
name="Example Name",
bearer=bearer,
game=game,
character=Character.objects.get(character_class=action.character)
).save()
else:
pass

Log(game=game, code=code).save()
# Apply code here
return func_code_get(game, code)


def parameter(request, gameId):
return getGameOr404(request, gameId, func_parameter_get, func_parameter_post)
@get_game_or_404
@has_bearer_or_403
def parameter(request, game):
def get_bearer(request):
return request.headers['Authorization']

calcTimepassingParameters(game)
if request.method == 'GET':
bearer = get_bearer(request)
player = game.player.get(bearer=bearer)
return func_parameter_get(game, player)
elif request.method == 'POST':
return func_parameter_post(game)


def func_parameter_get(game):
def func_parameter_get(game, player):
response = []
for parameter in game.parameter.all():
response.append(
Expand All @@ -194,6 +251,20 @@ def func_parameter_get(game):
}
)

response.append(
{
"type": "parameter",
"id": "movements",
"attributes": {
"scope": "user",
"value": player.action_points,
"rate": 1,
"min": 0,
"max": 15
}
}
)

return buildJsonResponse(response)


Expand Down

0 comments on commit 8b52d51

Please sign in to comment.