Skip to content

Commit

Permalink
Updates for the new lottery
Browse files Browse the repository at this point in the history
  • Loading branch information
dansahagian committed Nov 20, 2024
1 parent 5c50785 commit 2fc235a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 29 deletions.
4 changes: 2 additions & 2 deletions dev/initialize-db
Original file line number Diff line number Diff line change
@@ -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;"
Expand All @@ -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
18 changes: 16 additions & 2 deletions fbsurvivor/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
56 changes: 31 additions & 25 deletions fbsurvivor/core/management/commands/pick_lottery_winner.py
Original file line number Diff line number Diff line change
@@ -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")
Original file line number Diff line number Diff line change
@@ -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),
),
]
2 changes: 2 additions & 0 deletions fbsurvivor/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 2fc235a

Please sign in to comment.