-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from TheSecretOrganization/54-implement-tourn…
…ament 54 implement tournament
- Loading branch information
Showing
19 changed files
with
886 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
from django.contrib import admin | ||
from .models import Pong | ||
from .models import Pong, PongTournament | ||
|
||
|
||
@admin.register(Pong) | ||
class PongAdmin(admin.ModelAdmin): | ||
list_display = ('user1', 'user2', 'score1', 'score2', 'created_at') | ||
search_fields = ('user1__username', 'user2__username') | ||
list_filter = ('created_at',) | ||
class PongGameAdmin(admin.ModelAdmin): | ||
list_display = ("user1", "user2", "score1", "score2", "created_at", "uuid") | ||
search_fields = ("user1__username", "user2__username") | ||
list_filter = ("created_at",) | ||
|
||
|
||
@admin.register(PongTournament) | ||
class PongTournamentAdmin(admin.ModelAdmin): | ||
list_display = ("name", "winner", "created_at", "updated_at") | ||
search_fields = ("name", "winner__username") | ||
list_filter = ("created_at", "updated_at", "winner") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,79 @@ | ||
from django.db import models | ||
from ft_auth.models import User | ||
from django.contrib.auth import get_user_model | ||
|
||
|
||
class Pong(models.Model): | ||
user1 = models.ForeignKey( | ||
User, on_delete=models.SET_NULL, null=True, related_name="user1" | ||
get_user_model(), | ||
on_delete=models.SET_NULL, | ||
null=True, | ||
related_name="user1", | ||
) | ||
user2 = models.ForeignKey( | ||
User, on_delete=models.SET_NULL, null=True, related_name="user2" | ||
get_user_model(), | ||
on_delete=models.SET_NULL, | ||
null=True, | ||
related_name="user2", | ||
) | ||
score1 = models.PositiveSmallIntegerField() | ||
score2 = models.PositiveSmallIntegerField() | ||
uuid = models.CharField(max_length=255) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
|
||
def __str__(self): | ||
return f"{self.user1.username} vs {self.user2.username} - {self.score1}:{self.score2}" | ||
|
||
def serialize(self): | ||
return { | ||
"user1": ( | ||
{"id": self.user1.id, "username": self.user1.username} | ||
if self.user1 | ||
else None | ||
), | ||
"user2": ( | ||
{"id": self.user2.id, "username": self.user2.username} | ||
if self.user2 | ||
else None | ||
), | ||
"score1": self.score1, | ||
"score2": self.score2, | ||
"uuid": self.uuid, | ||
"created_at": self.created_at.isoformat(), | ||
"updated_at": self.updated_at.isoformat(), | ||
} | ||
|
||
class PongTournament(models.Model): | ||
name = models.CharField(max_length=255, unique=True) | ||
winner = models.ForeignKey( | ||
get_user_model(), | ||
on_delete=models.SET_NULL, | ||
null=True, | ||
related_name="winner", | ||
) | ||
participants = models.ManyToManyField( | ||
get_user_model(), related_name="tournaments" | ||
) | ||
games = models.ManyToManyField(Pong, related_name="tournaments") | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
|
||
def __str__(self): | ||
return f"Tournament: {self.name} - Winner: {self.winner}" | ||
|
||
def serialize(self): | ||
return { | ||
"name": self.name, | ||
"winner": ( | ||
{"id": self.winner.id, "username": self.winner.username} | ||
if self.winner | ||
else None | ||
), | ||
"participants": [ | ||
{"id": participant.id, "username": participant.username} | ||
for participant in self.participants.all() | ||
], | ||
"games": [game.serialize() for game in self.games.all()], | ||
"created_at": self.created_at.isoformat(), | ||
"updated_at": self.updated_at.isoformat(), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.