Skip to content

Commit

Permalink
feat: better user ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
alanzhu0 committed Sep 25, 2024
1 parent 2e89800 commit d579204
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 31 deletions.
3 changes: 1 addition & 2 deletions othello/apps/auth/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib.auth.models import AbstractUser, AbstractBaseUser
from django.contrib.auth.models import AbstractBaseUser, AbstractUser
from django.db import models


Expand Down Expand Up @@ -29,4 +29,3 @@ def save(self, *args, **kwargs):

def __str__(self):
return self.username

11 changes: 5 additions & 6 deletions othello/apps/games/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.db.models import Q
from django.db.models.functions import Coalesce
from django.utils import timezone

from ...moderator.constants import Player
Expand All @@ -18,15 +19,15 @@


def _save_path(instance, filename: str) -> AnyStr:
return os.path.join(instance.user.short_name if instance.user else instance.name, f"{uuid.uuid4()}.py")
return os.path.join(instance.user.username if instance.user else instance.name, f"{uuid.uuid4()}.py")


class SubmissionQuerySet(models.QuerySet):
def latest(self, **kwargs: Any) -> "models.query.QuerySet[Submission]":
"""
Returns a set of all the latest submissions for all users
"""
return self.filter(**kwargs).distinct("user").order_by("user", "-created_at")
return self.filter(**kwargs).distinct("user")


class Submission(models.Model):
Expand All @@ -51,17 +52,15 @@ class Meta:

def get_user_name(self) -> str:
return self.user.short_name

def get_user_username(self) -> str:
return self.user.username

def get_game_name(self) -> str:
return (
f"T-{self.tournament_win_year} {self.get_user_name()}"
if self.tournament_win_year != -1
else self.get_user_name()
if self.user is not None
else self.name
else self.get_user_name() if self.user is not None else self.name
)

def get_submitted_time(self) -> str:
Expand Down
6 changes: 1 addition & 5 deletions othello/apps/tournaments/pairings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ def swiss_pairing(players: Players, bye_player: TournamentPlayer) -> Pairings:

logger.info(players)

played_games = set(
game
for tgame in tournament.games.all()
if bye_player.submission.id not in (game := (tgame.game.black.id, tgame.game.white.id))
)
played_games = set(game for tgame in tournament.games.all() if bye_player.submission.id not in (game := (tgame.game.black.id, tgame.game.white.id)))
players_this_round = set()

for i in range(0, len(players)):
Expand Down
32 changes: 16 additions & 16 deletions othello/apps/tournaments/tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
import random
from collections import deque
from typing import List, Tuple
from time import sleep
from typing import List, Tuple

from celery import shared_task

Expand Down Expand Up @@ -96,9 +96,7 @@ def run_tournament(tournament_id: int) -> None:

include_users = list(t.include_users.all())
random.shuffle(include_users)
submissions: List[TournamentPlayer] = TournamentPlayer.objects.bulk_create(
[TournamentPlayer(tournament=t, submission=s) for s in include_users]
)
submissions: List[TournamentPlayer] = TournamentPlayer.objects.bulk_create([TournamentPlayer(tournament=t, submission=s) for s in include_users])
bye_player = TournamentPlayer.objects.create(tournament=t, submission=t.bye_player)

for round_num in range(t.num_rounds):
Expand All @@ -114,19 +112,21 @@ def run_tournament(tournament_id: int) -> None:
for match in matches:
logger.warning(f"{match[0]}({match[0].ranking}) v. {match[1]}({match[1].ranking})")
logger.info("\n")
games = deque([
t.games.create(
game=Game.objects.create(
black=game[0].submission,
white=game[1].submission,
time_limit=t.game_time_limit,
playing=False,
is_tournament=True,
runoff=t.runoff_enabled,
games = deque(
[
t.games.create(
game=Game.objects.create(
black=game[0].submission,
white=game[1].submission,
time_limit=t.game_time_limit,
playing=False,
is_tournament=True,
runoff=t.runoff_enabled,
)
)
)
for game in matches
])
for game in matches
]
)

running_games = {}
while games or running_games:
Expand Down
35 changes: 33 additions & 2 deletions othello/static/js/games/design.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,56 @@ function tournament_winner(data, escape){
return `<div>${escape(data.text)}</div>`;
}

// Sorry for this terrible code
function playerSort(el) {
let username = el.text;
let winner = false;
if(el.text.includes("T-")){
winner = true;
username = username.replace(" ", "").substring(2);
}
if(el.text.includes("(")){
username = el.text.match(/\(([^)]+)\)/)[1];
}
// sort descending by year, if it is part of the username, and then alphabetically
// if username begins with year, sort by year, then alphabetically
if(username.match(/^\d{4}/)){
// first four numbers are year
let year = username.substring(0, 4);
year = 3000 - parseInt(year);
username = year + username.substring(4);
}
if (winner){
username = "0" + username;
}
return username;
}

window.onload = function () {
helps()
let players = $("#id_black").children().toArray();
players = players.map(function(el) {
return {text: el.text, username: playerSort(el)};
});
$("#id_black").selectize({
options: players,
maxItems: 1,
onChange: function (val) {
showYourselfHelp(val, "#black-help");
},
sortField: [{'field': 'text', 'direction': 'desc'}],
sortField: [{'field': 'username', 'direction': 'asc'}],
render:{
option: tournament_winner,
item: tournament_winner
}
});
$("#id_white").selectize({
maxItems: 1,
options: players,
onChange: function (val) {
showYourselfHelp(val, "#white-help");
},
sortField: [{'field': 'text', 'direction': 'desc'}],
sortField: [{'field': 'username', 'direction': 'asc'}],
render:{
option: tournament_winner,
item: tournament_winner,
Expand Down

0 comments on commit d579204

Please sign in to comment.