From 2fc235ab588cc5ed40ae32400e8639434380a2b6 Mon Sep 17 00:00:00 2001 From: Dan Sahagian <45240763+dansahagian@users.noreply.github.com> Date: Tue, 19 Nov 2024 16:16:01 -0800 Subject: [PATCH] Updates for the new lottery --- dev/initialize-db | 4 +- fbsurvivor/core/admin.py | 18 +++++- .../commands/pick_lottery_winner.py | 56 ++++++++++--------- ...layerstatus_has_complete_picks_and_more.py | 22 ++++++++ fbsurvivor/core/models.py | 2 + 5 files changed, 73 insertions(+), 29 deletions(-) create mode 100644 fbsurvivor/core/migrations/0047_playerstatus_has_complete_picks_and_more.py diff --git a/dev/initialize-db b/dev/initialize-db index 1eb53df..ad24884 100755 --- a/dev/initialize-db +++ b/dev/initialize-db @@ -1,7 +1,7 @@ #!/usr/bin/env sh ssh dan@linode "pg_dump -U fbsurvivor2 fbsurvivor2 > /tmp/fbsurvivor_dump" -scp dan@linode:/tmp/fbsurvivor_dump ~/d/code/dansahagian/fbsurvivor/tmp/fbsurvivor_dump +scp dan@linode:/tmp/fbsurvivor_dump ~/d/code/fbsurvivor/tmp/fbsurvivor_dump ssh dan@linode "rm /tmp/fbsurvivor_dump" psql -U postgres -d fbsurvivor2 -c "DROP SCHEMA IF EXISTS public CASCADE;" @@ -14,4 +14,4 @@ psql -U postgres -d postgres -c "GRANT ALL ON DATABASE fbsurvivor2 to fbsurvivor psql -U postgres -d fbsurvivor2 -c "DROP SCHEMA IF EXISTS public;" psql -U postgres -d fbsurvivor2 -c "CREATE SCHEMA IF NOT EXISTS public AUTHORIZATION fbsurvivor2;" -psql fbsurvivor2 < ~/d/code/dansahagian/fbsurvivor/tmp/fbsurvivor_dump +psql fbsurvivor2 < ~/d/code/fbsurvivor/tmp/fbsurvivor_dump diff --git a/fbsurvivor/core/admin.py b/fbsurvivor/core/admin.py index ebdb536..50b601e 100644 --- a/fbsurvivor/core/admin.py +++ b/fbsurvivor/core/admin.py @@ -30,8 +30,22 @@ def get_queryset(self, request): @admin.register(PlayerStatus) class PlayerStatusAdmin(admin.ModelAdmin): - list_display = ["player", "season", "is_paid", "is_retired", "is_survivor"] - list_editable = ["is_paid", "is_retired", "is_survivor"] + list_display = [ + "player", + "season", + "is_paid", + "is_retired", + "is_survivor", + "has_complete_picks", + "has_won_gt_buy_in", + ] + list_editable = [ + "is_paid", + "is_retired", + "is_survivor", + "has_complete_picks", + "has_won_gt_buy_in", + ] list_filter = ["season"] def get_queryset(self, request): diff --git a/fbsurvivor/core/management/commands/pick_lottery_winner.py b/fbsurvivor/core/management/commands/pick_lottery_winner.py index dcc096b..8f36a81 100644 --- a/fbsurvivor/core/management/commands/pick_lottery_winner.py +++ b/fbsurvivor/core/management/commands/pick_lottery_winner.py @@ -1,35 +1,41 @@ from secrets import choice from django.core.management.base import BaseCommand +from django.db.models.functions import Lower -from fbsurvivor.core.models import Player, Season +from fbsurvivor.core.models import PlayerStatus, Season class Command(BaseCommand): help = "Pick the winner of free entry for next season" + def add_arguments(self, parser): + parser.add_argument("year", type=int) + def handle(self, *args, **options): - current_seasons = Season.objects.filter(is_current=True) - - for current_season in current_seasons: - # filter out players who retired, won survivor, and me - players = Player.objects.filter( - playerstatus__season=current_season, - playerstatus__is_retired=False, - playerstatus__is_survivor=False, - ).exclude(username="DanTheAutomator") - - # filter out players who missed picks during the season - ep = [ - p.username - for p in players - if not p.pick_set.filter(team__isnull=True, week__season=current_season) - and sum( - p.payout_set.filter(season=current_season).values_list("amount", flat=True) - ) - < 30 - ] - display = "\n".join(ep) - - print(f"\n\n{current_season.year} Eligible Players:\n\n{display}\n\n") - print(f"And the winner is... {choice(ep)}\n\n") + year = options["year"] + season = Season.objects.get(year=year) + + # filter out players who don't have full picks, have won money, and me + ps = ( + PlayerStatus.objects.filter( + season=season, + has_complete_picks=True, + has_won_gt_buy_in=False, + is_retired=False, + ) + .exclude(player__username="DanTheAutomator") + .annotate(lower=Lower("player__username")) + .prefetch_related("player") + .order_by("-win_count", "lower") + ) + + hat = [] + for p in ps: + hat.extend([p.player.username] * p.win_count) + + total = len(hat) + for p in ps: + print(f"{p.player}: {round(p.win_count / total * 100, 2)}%") + + print(f"And the winner is... {choice(hat)}\n\n") diff --git a/fbsurvivor/core/migrations/0047_playerstatus_has_complete_picks_and_more.py b/fbsurvivor/core/migrations/0047_playerstatus_has_complete_picks_and_more.py new file mode 100644 index 0000000..0f90af8 --- /dev/null +++ b/fbsurvivor/core/migrations/0047_playerstatus_has_complete_picks_and_more.py @@ -0,0 +1,22 @@ +# Generated by Django 5.1.3 on 2024-11-19 23:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0046_season_is_live"), + ] + + operations = [ + migrations.AddField( + model_name="playerstatus", + name="has_complete_picks", + field=models.BooleanField(default=True), + ), + migrations.AddField( + model_name="playerstatus", + name="has_won_gt_buy_in", + field=models.BooleanField(default=False), + ), + ] diff --git a/fbsurvivor/core/models.py b/fbsurvivor/core/models.py index 9851671..5a3ca74 100644 --- a/fbsurvivor/core/models.py +++ b/fbsurvivor/core/models.py @@ -97,6 +97,8 @@ class PlayerStatus(models.Model): is_survivor = models.BooleanField(default=True) win_count = models.SmallIntegerField(default=0) loss_count = models.SmallIntegerField(default=0) + has_complete_picks = models.BooleanField(default=True) + has_won_gt_buy_in = models.BooleanField(default=False) def __str__(self): return f"{self.player} - {self.season}"