Skip to content

Commit

Permalink
Merge pull request #54 from Nihongo-Jouzu/53-bug-change-score-schema
Browse files Browse the repository at this point in the history
[BUG] Change score schema
  • Loading branch information
ebisuG authored Sep 14, 2024
2 parents 6e92fd7 + 55d164b commit bf67c13
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 11 deletions.
17 changes: 17 additions & 0 deletions backend/api/migrations/0012_remove_score_game_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1 on 2024-09-14 00:59

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api', '0005_wordslearned_user_content_squashed_0011_remove_wordslearned_user_content'),
]

operations = [
migrations.RemoveField(
model_name='score',
name='game_id',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.1 on 2024-09-14 03:43

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("api", "0012_remove_score_game_id"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RenameField(
model_name="score",
old_name="player1",
new_name="winner",
),
migrations.RenameField(
model_name="score",
old_name="player2",
new_name="loser",
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.1 on 2024-09-14 03:56

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0013_remove_score_player1_remove_score_player2_and_more'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterField(
model_name='score',
name='loser',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='loser', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='score',
name='winner',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='winner', to=settings.AUTH_USER_MODEL),
),
]
11 changes: 5 additions & 6 deletions backend/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@


class Score(models.Model):
player1 = models.ForeignKey(
winner = models.ForeignKey(
User,
to_field="id",
on_delete=models.CASCADE,
related_name="player_1",
related_name="winner",
default=1,
)
player2 = models.ForeignKey(
loser = models.ForeignKey(
User,
to_field="id",
on_delete=models.CASCADE,
related_name="player_2",
related_name="loser",
default=1,
)
game_id = models.ForeignKey("lobby", to_field="game_id", on_delete=models.CASCADE)
score = models.IntegerField()
date = models.DateTimeField(auto_now_add=True)

def __str__(self):
return f"{self.player1.username} vs {self.player2.username} - {self.score}"
return f"{self.winner.username} vs {self.loser.username} - {self.score}"


class Lobby(models.Model):
Expand Down
23 changes: 20 additions & 3 deletions backend/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@ class Meta:


class ScoreSerializer(serializers.ModelSerializer):
player1 = serializers.CharField(source="player1.username")
player2 = serializers.CharField(source="player2.username")
winner = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
loser = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())

winner_username = serializers.SerializerMethodField()
loser_username = serializers.SerializerMethodField()

class Meta:
model = Score
fields = ["id", "score", "date", "player1", "player2"]
fields = ["id", "score", "date", "winner", "loser", "winner_username", "loser_username"]

def get_winner_username(self, obj):
return obj.winner.username

def get_loser_username(self, obj):
return obj.loser.username

def create(self, validated_data):
winner = validated_data.pop("winner")
loser = validated_data.pop("loser")
score = validated_data.get("score")

score_instance = Score.objects.create(winner=winner, loser=loser, score=score)
return score_instance


class LobbySerializer(serializers.ModelSerializer):
Expand Down
5 changes: 4 additions & 1 deletion backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from dj_rest_auth.registration.views import SocialLoginView
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from rest_framework.decorators import authentication_classes, permission_classes


# Google
Expand Down Expand Up @@ -100,12 +101,14 @@ def get_scores(request, user_id):
limit = 20
# if user_id is None:
# user_id = 0
scores = Score.objects.filter(player1=user_id) | Score.objects.filter(player2=user_id)
scores = Score.objects.filter(winner=user_id) | Score.objects.filter(loser=user_id)
scores.order_by("-date")
serializer = ScoreSerializer(scores, many=True)
return Response(serializer.data)


@authentication_classes([])
@permission_classes([])
@api_view(["POST"])
def create_score(request):
serializer = ScoreSerializer(data=request.data)
Expand Down
2 changes: 1 addition & 1 deletion backend/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ python manage.py loaddata api/fixtures/initial_data.json
python manage.py createsuperuser --noinput

# Start Django application with Gunicorn
gunicorn backend.wsgi:application --bind 0.0.0.0:8000
gunicorn backend.wsgi:application --bind 0.0.0.0:8000 -c gunicorn.conf.py
5 changes: 5 additions & 0 deletions backend/gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
workers = 3 # 2 * (CPU cores) + 1
loglevel = "info"
accesslog = "-"
capture_output = True
enable_stdio_inheritance = True

0 comments on commit bf67c13

Please sign in to comment.