Compare commits

..

72 Commits

Author SHA1 Message Date
b485791d25 Bug: Update method signature in single-user raceguess page
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
2024-12-08 20:01:30 +01:00
e8c8e35e05 Disable info cards regarding season picks
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 11s
2024-12-08 19:57:24 +01:00
15305b2f3e Bug: Fix statistics issues caused by driver substitutions
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 16s
2024-12-08 19:52:02 +01:00
0dc4b22c72 Commented out leaderboard diagram extension for season points
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 16s
2024-12-08 19:38:31 +01:00
cef40a9e8b Implement season guess evaluation
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
2024-12-08 19:25:06 +01:00
c509746688 Template: Mark wrong season guesses with red border
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 27s
2024-12-08 17:22:35 +01:00
9cdd7267db Bug: Fix missing inactive drivers in season guesses 2024-12-08 17:22:14 +01:00
61d247508f Bug: Fix duplicated place numbering in points model
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-10-19 14:01:46 +02:00
f38a5f2e6d Bug: Call delete_memoized with function references
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 33s
2024-10-19 13:41:14 +02:00
95760baebf Flake: Add sqlitebrowser 2024-10-19 13:40:51 +02:00
34434cc7cc Replace memoize with cached for teams_sorted_by_points
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 2m21s
2024-09-24 00:04:36 +02:00
6da5919f86 Add a shitload of caching
Some checks are pending
Build Formula10 Docker Image / build-docker (push) Waiting to run
2024-09-24 00:01:41 +02:00
852f0a04ca Add profiler (currently disabled) 2024-09-24 00:00:41 +02:00
aa38d0138f Update FastF1 frontend button description 2024-09-23 21:23:38 +02:00
a9ab892c2a Add functions to obtain specific sessions to EventDataHelper 2024-09-23 21:23:20 +02:00
e6157893ae Remove deprecated import 2024-09-23 17:13:05 +02:00
b2a01ce453 Use column name string when sorting by column 2024-09-23 17:12:55 +02:00
5234e8a06e Add helper classes for FastF1 dataframes 2024-09-23 16:52:23 +02:00
a965c0007b Prefix OpenF1 functions 2024-09-23 16:52:11 +02:00
9acced3899 Add werkzeug to requirements 2024-09-23 16:52:00 +02:00
ed0c5f51b4 Add __repr__ to Race class 2024-09-23 16:51:18 +02:00
ebb862c9d7 Add Pandas to requirements 2024-09-23 16:50:48 +02:00
4a0eb5329c Set FastF1 cache location in dockerfile 2024-09-23 16:50:35 +02:00
30bf475000 Prefix OpenF1 classes + functions with "openf1" 2024-09-22 23:55:54 +02:00
d66087142d Add fastf1 module 2024-09-22 23:34:16 +02:00
8bdb3b5207 Add fastf1 python dependency to requirements 2024-09-22 23:01:22 +02:00
2e685ed646 Add fastf1 python dependency 2024-09-22 22:00:38 +02:00
b4c459ffe7 Allow specifying drivers as "inactive" and replace Logan Sargeant with Franco Colapinto
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 1m27s
2024-08-30 20:47:06 +02:00
3340b77efe Slight reformatting 2024-08-30 18:48:40 +02:00
0cff53cf42 Fix driver standing points string for NC
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 30s
2024-05-25 14:42:49 +02:00
f15bbb4b96 Update requirements.txt
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 28s
2024-03-10 20:10:49 +01:00
d88ae9568f Add frontend button to fetch from OpenF1
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 24s
2024-03-10 20:08:41 +01:00
1ab92eff9a Implement fetching of certain data from OpenF1 2024-03-10 19:54:28 +01:00
c2838c3332 Update flake with commands 2024-03-10 18:50:29 +01:00
e6a70365f3 Add OpenF1 model classes 2024-03-10 18:50:24 +01:00
6e20275dcd Remove obsolete notes
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 13s
2024-03-10 00:48:42 +01:00
10cf690ee2 Track sprint stats + fastest laps + add to stats
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
2024-03-10 00:32:07 +01:00
628e2cd99a Update frontend for extended stats (sprint)
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-09 23:22:18 +01:00
e57109caf4 Add quali_date + has_sprint fields to race
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
2024-03-09 20:56:58 +01:00
8b2920f886 Add dummy values to race result
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 20s
These columns are not marked nullable, so ignoring them prevents entering of race results
2024-03-09 19:48:07 +01:00
73273bc5cd Small database migration fixes
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-03 16:02:20 +01:00
d3097038a5 Large database migration
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
2024-03-03 15:38:35 +01:00
96cb8ca891 Fix sorting + standing bug for wcc/wdc
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 23s
2024-03-03 11:24:40 +01:00
1ad558171d Decrease diagram height on large screens
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-03 03:38:25 +01:00
873df8bd8e Only apply horizontal diagram width on mobile
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
2024-03-03 03:34:42 +01:00
b250c47cb3 Update diagrams for landscape mode
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-03 03:31:22 +01:00
6ed5b914e4 Use driver abbr in diagram
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 16s
2024-03-03 03:24:51 +01:00
feb6d27e24 Increase diagram point size
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-03 03:22:56 +01:00
0d598e75a2 Add diagrams to stats page
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 16s
2024-03-03 03:19:51 +01:00
a3d234a754 Add standing diff to stats page
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-03 02:33:21 +01:00
8fcb8c5704 Improve styling consistency
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-03 02:22:34 +01:00
20d177192f Update diagram
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-03 02:15:19 +01:00
27e0231a25 Update diagram
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
2024-03-03 02:10:58 +01:00
325f753d31 Add points history diagram
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 15s
2024-03-03 02:09:06 +01:00
b4794ca42f Add chartjs 2024-03-03 01:01:07 +01:00
481492868b Fix grid layout on PC screen
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-03 00:25:36 +01:00
64d37acc23 Many frontend improvements
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-03 00:06:52 +01:00
c2c71a32e8 Update note
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 22:40:03 +01:00
e7d2e960ad Add points to race table
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 22:31:40 +01:00
1bd174e73f Add notes to statistics/leaderboard pages 2024-03-02 22:27:25 +01:00
0ea6568082 Add statistics page
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 22:18:28 +01:00
09203d9390 Update season guess calculation
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 21:49:47 +01:00
5dcebab545 Allow enabling debug endpoints with env var 2024-03-02 21:49:39 +01:00
dfb9360125 Display current season guess state
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 20:31:34 +01:00
a0051aacc3 Implement domain version of season_guess_result 2024-03-02 20:31:14 +01:00
d3ee08df36 Add tooltip on leaderboard 2024-03-02 18:03:31 +01:00
0a94222cb2 Color first place(s) red in leaderboard
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 13s
2024-03-02 18:00:56 +01:00
162423ec84 Combine places with equal points in leaderboard
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 17:58:06 +01:00
fcc29d2372 Add place to leaderboard
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 17:49:04 +01:00
131965e61f Fix date mistakes in static race data
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 13:30:24 +01:00
d5d128379d Sort leaderboard table by total points
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 13:25:03 +01:00
49939a38b4 Revert race table styling
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
2024-03-02 13:15:20 +01:00
58 changed files with 3002 additions and 1139 deletions

View File

@ -1,10 +1,17 @@
# syntax=docker/dockerfile:1
FROM public.ecr.aws/docker/library/python:3.11.8-slim-bookworm
RUN apt-get update -y
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
EXPOSE 5000
ENV FASTF1_CACHE="/cache"
CMD ["python3", "-u", "-m", "flask", "--app", "formula10", "run", "--host", "0.0.0.0"]

View File

@ -1,22 +0,0 @@
name,abbr,team_name,country_code
None,None,None,NO
Alexander Albon,ALB,Williams,TH
Fernando Alonso,ALO,Aston Martin,ES
Valteri Bottas,BOT,Sauber,FL
Pierre Gasly,GAS,Alpine,FR
Lewis Hamilton,HAM,Mercedes,UK
Nico Hulkenberg,HUL,Haas,DE
Charles Leclerc,LEC,Ferrari,MC
Kevin Magnussen,MAG,Haas,DK
Lando Norris,NOR,McLaren,UK
Esteban Ocon,OCO,Alpine,FR
Sergio Perez,PER,Red Bull,MX
Oscar Piastri,PIA,McLaren,AU
Daniel Ricciardo,RIC,VCARB,AU
George Russel,RUS,Mercedes,UK
Carlos Sainz,SAI,Ferrari,ES
Logan Sargeant,SAR,Williams,US
Lance Stroll,STR,Aston Martin,CA
Yuki Tsunoda,TSU,VCARB,JP
Max Verstappen,VER,Red Bull,NL
Zhou Guanyu,ZHO,Sauber,CN
1 name abbr team_name country_code
2 None None None NO
3 Alexander Albon ALB Williams TH
4 Fernando Alonso ALO Aston Martin ES
5 Valteri Bottas BOT Sauber FL
6 Pierre Gasly GAS Alpine FR
7 Lewis Hamilton HAM Mercedes UK
8 Nico Hulkenberg HUL Haas DE
9 Charles Leclerc LEC Ferrari MC
10 Kevin Magnussen MAG Haas DK
11 Lando Norris NOR McLaren UK
12 Esteban Ocon OCO Alpine FR
13 Sergio Perez PER Red Bull MX
14 Oscar Piastri PIA McLaren AU
15 Daniel Ricciardo RIC VCARB AU
16 George Russel RUS Mercedes UK
17 Carlos Sainz SAI Ferrari ES
18 Logan Sargeant SAR Williams US
19 Lance Stroll STR Aston Martin CA
20 Yuki Tsunoda TSU VCARB JP
21 Max Verstappen VER Red Bull NL
22 Zhou Guanyu ZHO Sauber CN

View File

@ -1,25 +0,0 @@
name,number,date,pxx
Bahrain,1,2024-03-02-16-00,10
Saudi Arabia,2,2024-03-09-18-00,6
Australia,3,2024-03-24-05-00,15
Japan,4,2024-04-07-07-00,9
China,5,2024-04-21-09-00,7
Miami,6,2024-05-05-22-00,13
Emilia-Romagna,7,2024-05-21-15-00,17
Monaco,8,2024-05-26-15-00,5
Canada,9,2023-06-09-20-00,12
Spain,10,2024-06-23-15-00,8
Austria,11,2024-06-30-15-00,11
Great Britain,12,2024-07-07-16-00,4
Hungary,12,2023-07-23-15-00,17
Belgium,13,2024-07-28-15-00,13
Netherlands,14,2024-08-25-15-00,7
Monza,15,2024-09-01-15-00,16
Azerbaijan,16,2024-09-15-13-00,8
Singapore,17,2024-09-22-14-00,11
Austin,18,2024-10-20-21-00,5
Mexico,19,2024-10-27-21-00,14
Brazil,20,2024-11-03-18-00,4
Las Vegas,21,2024-11-23-07-00,12
Qatar,22,2024-12-01-18-00,6
Abu Dhabi,23,2024-12-08-14-00,10
1 name number date pxx
2 Bahrain 1 2024-03-02-16-00 10
3 Saudi Arabia 2 2024-03-09-18-00 6
4 Australia 3 2024-03-24-05-00 15
5 Japan 4 2024-04-07-07-00 9
6 China 5 2024-04-21-09-00 7
7 Miami 6 2024-05-05-22-00 13
8 Emilia-Romagna 7 2024-05-21-15-00 17
9 Monaco 8 2024-05-26-15-00 5
10 Canada 9 2023-06-09-20-00 12
11 Spain 10 2024-06-23-15-00 8
12 Austria 11 2024-06-30-15-00 11
13 Great Britain 12 2024-07-07-16-00 4
14 Hungary 12 2023-07-23-15-00 17
15 Belgium 13 2024-07-28-15-00 13
16 Netherlands 14 2024-08-25-15-00 7
17 Monza 15 2024-09-01-15-00 16
18 Azerbaijan 16 2024-09-15-13-00 8
19 Singapore 17 2024-09-22-14-00 11
20 Austin 18 2024-10-20-21-00 5
21 Mexico 19 2024-10-27-21-00 14
22 Brazil 20 2024-11-03-18-00 4
23 Las Vegas 21 2024-11-23-07-00 12
24 Qatar 22 2024-12-01-18-00 6
25 Abu Dhabi 23 2024-12-08-14-00 10

View File

@ -1 +0,0 @@
user_name,hot_take_correct,overtakes_correct
1 user_name hot_take_correct overtakes_correct

View File

@ -1,12 +0,0 @@
name
None
Alpine
Aston Martin
Ferrari
Haas
McLaren
Mercedes
Red Bull
Sauber
VCARB
Williams
1 name
2 None
3 Alpine
4 Aston Martin
5 Ferrari
6 Haas
7 McLaren
8 Mercedes
9 Red Bull
10 Sauber
11 VCARB
12 Williams

117
flake.nix
View File

@ -5,26 +5,104 @@
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.devshell.url = "github:numtide/devshell";
outputs = { self, nixpkgs, flake-utils, devshell }:
flake-utils.lib.eachDefaultSystem (system:
let
outputs = {
self,
nixpkgs,
flake-utils,
devshell,
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
inherit system;
config.allowUnfree = true;
overlays = [ devshell.overlays.default ];
overlays = [devshell.overlays.default];
};
myPython = pkgs.python311.withPackages (p: with p; [
timple = pkgs.python311Packages.buildPythonPackage rec {
pname = "timple";
version = "0.1.8";
src = pkgs.python311Packages.fetchPypi {
inherit pname version;
hash = "sha256-u8EgMA8BA6OpPlSg0ASRxLcIcv5psRIEcBpIicagXw8=";
};
doCheck = false;
pyproject = false;
# Build time deps
nativeBuildInputs = with pkgs.python311Packages; [
setuptools
];
# Run time deps
dependencies = with pkgs.python311Packages; [
matplotlib
numpy
];
};
fastf1 = pkgs.python311Packages.buildPythonPackage rec {
pname = "fastf1";
version = "3.4.0";
src = pkgs.python311Packages.fetchPypi {
inherit pname version;
hash = "sha256-rCjJaM0W2m9Yk3dhHkMOdIqPiKtVqoXuELBasmA9ybA=";
};
doCheck = false;
pyproject = true;
# Build time deps
nativeBuildInputs = with pkgs.python311Packages; [
hatchling
hatch-vcs
];
# Run time deps
dependencies = with pkgs.python311Packages; [
matplotlib
numpy
pandas
python-dateutil
requests
requests-cache
scipy
rapidfuzz
websockets
timple
];
};
myPython = pkgs.python311.withPackages (p:
with p; [
# Basic
rich
numpy
# Web
flask
flask-sqlalchemy
flask-caching
sqlalchemy
requests
# Test
pytest
fastf1
# TODO: For some reason, listing those under fastf1.dependencies doesn't work???
matplotlib
numpy
pandas
python-dateutil
requests-cache
scipy
rapidfuzz
websockets
timple
]);
in {
devShell = pkgs.devshell.mkShell {
@ -33,6 +111,8 @@
packages = with pkgs; [
myPython
sqlitebrowser
nodejs_21
nodePackages.sass
nodePackages.postcss-cli
@ -41,11 +121,26 @@
# Use $1 for positional args
commands = [
# {
# name = "";
# help = "";
# command = "";
# }
{
name = "vscode";
help = "Launch VSCode";
command = "code . &>/dev/null &";
}
{
name = "pycharm";
help = "Launch PyCharm Professional";
command = "pycharm-professional . &>/dev/null &";
}
{
name = "db";
help = "Launch SQLiteBrowser";
command = "sqlitebrowser ./instance/formula10.db &>/dev/null &";
}
{
name = "api";
help = "Launch Hoppscotch in Google Chrome";
command = "google-chrome-stable https://hoppscotch.io &>/dev/null &";
}
];
};
});

View File

@ -1,6 +1,8 @@
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_caching import Cache
from werkzeug.middleware.profiler import ProfilerMiddleware
# Load local ENV variables (can be set when calling the executable)
ENABLE_TIMING: bool = False if os.getenv("DISABLE_TIMING") == "True" else True
@ -9,21 +11,27 @@ if not ENABLE_TIMING:
print("- Disabled timing constraints")
app: Flask = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///formula10.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///formula10.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# Session cookie is used to propagate message to error page
app.config['SESSION_TYPE'] = 'memcached'
app.config['SECRET_KEY'] = 'ich stinke nach maggi'
app.config["SESSION_TYPE"] = "memcached"
app.config["SECRET_KEY"] = "ich stinke nach maggi"
app.url_map.strict_slashes = False
db: SQLAlchemy = SQLAlchemy()
db.init_app(app)
cache: Cache = Cache(config={"CACHE_TYPE": "SimpleCache"})
cache.init_app(app)
# app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=("/formula10/*",), sort_by=("cumtime",))
# NOTE: These imports are required to register the routes. They need to be imported after "app" is declared
import formula10.controller.race_controller # type: ignore
import formula10.controller.race_controller
import formula10.controller.season_controller
import formula10.controller.leaderboard_controller
import formula10.controller.statistics_controller
import formula10.controller.rules_controller
import formula10.controller.admin_controller
@ -31,17 +39,21 @@ import formula10.controller.error_controller
# TODO
# Statistics
# - Auto calculate points
# - Display points somewhere in race table? Below the name in the table header.
# - Highlight currently correct values for some season guesses (e.g. current most dnfs, team winners, podiums)
# - Generate static diagram using chart.js + templating the js (funny yikes)
# - Which driver was voted most for dnf?
# Large DB Update
# - Don't use names for frontend post requests, use IDs
# - For season guess calc there is missing: Fastest laps + sprint points + sprint DNFs (in race result)
# - Mask to allow changing usernames (easy if name is not used as ID)
# - Maybe even masks for races + drivers + teams?
# - DB fields for links to F1 site - NO: just hardcode them in with a dictionary
# Leaderboards/Points
# - Auto calculate season points (display season points in table + season guess card title?)
# Optimizations
# - Optimize PointsModel + TemplateModel. In case something is calculated often, cache it.
# - NEVER do manual DB queries, except in the DomainModel!
# General
# - Decouple names from IDs + Fix Valtteri/Russel spelling errors
# - Adapt diagram colors to team colors
# - Add links to the official F1 stats page (for quali/result), probably best to store entire link in DB (because they are not entirely regular)?
# - Unit testing (as much as possible, but especially points calculation)
# Possible but probably not
# - Show cards of previous race results, like with season guesses?
# - Make user order changeable using drag'n'drop?

View File

@ -3,41 +3,15 @@ from urllib.parse import unquote
from flask import redirect, render_template, request
from werkzeug import Response
from formula10.controller.error_controller import error_redirect
from formula10.database.update_queries import update_race_result, update_user
from formula10.database.import_export import export_dynamic_data, reload_season_guess_result_data, reload_static_data
from formula10.domain.cache_invalidator import cache_invalidate_user_updated, cache_invalidate_race_result_updated
from formula10.domain.domain_model import Model
from formula10.domain.template_model import TemplateModel
from formula10 import app
@app.route("/save/all")
def save() -> Response:
export_dynamic_data()
return redirect("/")
# @app.route("/load/all")
# def load() -> Response:
# reload_static_data()
# reload_dynamic_data()
# return redirect("/")
@app.route("/load/static")
def load_static() -> Response:
reload_static_data()
return redirect("/")
@app.route("/load/seasonresults")
def load_season_results() -> Response:
reload_season_guess_result_data()
return redirect("/")
# @app.route("/load/dynamic")
# def load_dynamic() -> Response:
# reload_dynamic_data()
# return redirect("/")
from formula10.openf1.model.openf1_session import OpenF1Session
from formula10.openf1.openf1_definitions import OPENF1_SESSION_NAME_RACE
from formula10.openf1.openf1_fetcher import openf1_fetch_driver, openf1_fetch_position, openf1_fetch_session
@app.route("/result")
@ -51,7 +25,7 @@ def result_active_race(race_name: str) -> str:
model = TemplateModel(active_user_name=None,
active_result_race_name=race_name)
return render_template("enter.jinja", model=model)
return render_template("result.jinja", model=model)
@app.route("/result-enter/<race_name>", methods=["POST"])
@ -62,7 +36,29 @@ def result_enter_post(race_name: str) -> Response:
dnfs: List[str] = request.form.getlist("dnf-drivers")
excluded: List[str] = request.form.getlist("excluded-drivers")
return update_race_result(race_name, pxxs, first_dnfs, dnfs, excluded)
# Extra stats for points calculation
fastest_lap: str | None = request.form.get("fastest-lap")
sprint_pxxs: List[str] = request.form.getlist("sprint-pxx-drivers")
sprint_dnf_drivers: List[str] = request.form.getlist("sprint-dnf-drivers")
if fastest_lap is None:
return error_redirect("Data was not saved, because fastest lap was not set.")
cache_invalidate_race_result_updated()
race_id: int = Model().race_by(race_name=race_name).id
return update_race_result(race_id, pxxs, first_dnfs, dnfs, excluded, int(fastest_lap), sprint_pxxs, sprint_dnf_drivers)
@app.route("/result-fetch/<race_name>", methods=["POST"])
def result_fetch_post(race_name: str) -> Response:
session: OpenF1Session = openf1_fetch_session(OPENF1_SESSION_NAME_RACE, "KSA")
openf1_fetch_driver(session.session_key, "VER")
openf1_fetch_position(session.session_key, 1)
# @todo Fetch stuff and build the race_result using update_race_result(...)
cache_invalidate_race_result_updated()
return redirect("/result")
@app.route("/user")
@ -75,11 +71,13 @@ def user_root() -> str:
@app.route("/user-add", methods=["POST"])
def user_add_post() -> Response:
cache_invalidate_user_updated()
username: str | None = request.form.get("select-add-user")
return update_user(username, add=True)
@app.route("/user-delete", methods=["POST"])
def user_delete_post() -> Response:
cache_invalidate_user_updated()
username: str | None = request.form.get("select-delete-user")
return update_user(username, delete=True)

View File

@ -0,0 +1,11 @@
from flask import render_template
from formula10 import app
from formula10.domain.points_model import PointsModel
from formula10.domain.template_model import TemplateModel
@app.route("/graphs")
def graphs_root() -> str:
model = TemplateModel(active_user_name=None, active_result_race_name=None)
points = PointsModel()
return render_template("leaderboard.jinja", model=model, points=points)

View File

@ -1,8 +1,12 @@
from typing import List
from urllib.parse import unquote
from flask import redirect, render_template, request
from werkzeug import Response
from formula10.database.update_queries import delete_race_guess, update_race_guess
from formula10.domain.cache_invalidator import cache_invalidate_race_guess_updated
from formula10.domain.domain_model import Model
from formula10.domain.points_model import PointsModel
from formula10.domain.template_model import TemplateModel
from formula10 import app
@ -22,8 +26,9 @@ def race_active_user(user_name: str) -> str:
user_name = unquote(user_name)
model = TemplateModel(active_user_name=user_name,
active_result_race_name=None)
points = PointsModel()
return render_template("race.jinja", model=model)
return render_template("race.jinja", model=model, points=points)
@app.route("/race-guess/<race_name>/<user_name>", methods=["POST"])
@ -34,7 +39,12 @@ def race_guess_post(race_name: str, user_name: str) -> Response:
pxx: str | None = request.form.get("pxxselect")
dnf: str | None = request.form.get("dnfselect")
return update_race_guess(race_name, user_name, pxx, dnf)
cache_invalidate_race_guess_updated()
race_id: int = Model().race_by(race_name=race_name).id
user_id: int = Model().user_by(user_name=user_name).id
return update_race_guess(race_id, user_id,
int(pxx) if pxx is not None else None,
int(dnf) if dnf is not None else None)
@app.route("/race-guess-delete/<race_name>/<user_name>", methods=["POST"])
@ -42,4 +52,7 @@ def race_guess_delete_post(race_name: str, user_name: str) -> Response:
race_name = unquote(race_name)
user_name = unquote(user_name)
return delete_race_guess(race_name, user_name)
cache_invalidate_race_guess_updated()
race_id: int = Model().race_by(race_name=race_name).id
user_id: int = Model().user_by(user_name=user_name).id
return delete_race_guess(race_id, user_id)

View File

@ -5,7 +5,10 @@ from werkzeug import Response
from formula10.database.model.db_team import DbTeam
from formula10.database.update_queries import update_season_guess
from formula10.domain.cache_invalidator import cache_invalidate_season_guess_updated
from formula10.domain.domain_model import Model
from formula10.domain.model.team import NONE_TEAM
from formula10.domain.points_model import PointsModel
from formula10.domain.template_model import TemplateModel
from formula10 import app, db
@ -20,8 +23,9 @@ def season_active_user(user_name: str) -> str:
user_name = unquote(user_name)
model = TemplateModel(active_user_name=user_name,
active_result_race_name=None)
points = PointsModel()
return render_template("season.jinja", model=model)
return render_template("season.jinja", model=model, points=points)
@app.route("/season-guess/<user_name>", methods=["POST"])
@ -37,8 +41,10 @@ def season_guess_post(user_name: str) -> Response:
]
# TODO: This is pretty ugly, to do queries in the controller
team_winner_guesses: List[str | None] = [
request.form.get(f"teamwinner-{team.name}") for team in db.session.query(DbTeam).all() if team.name != NONE_TEAM.name
request.form.get(f"teamwinner-{team.id}") for team in db.session.query(DbTeam).all() if team.id != NONE_TEAM.id
]
podium_driver_guesses: List[str] = request.form.getlist("podiumdrivers")
return update_season_guess(user_name, guesses, team_winner_guesses, podium_driver_guesses)
cache_invalidate_season_guess_updated()
user_id: int = Model().user_by(user_name=user_name).id
return update_season_guess(user_id, guesses, team_winner_guesses, podium_driver_guesses)

View File

@ -1,10 +1,11 @@
from flask import render_template
from formula10 import app
from formula10.domain.points_model import PointsModel
from formula10.domain.template_model import TemplateModel
@app.route("/graphs")
def graphs_root() -> str:
@app.route("/stats")
def stats_root() -> str:
model = TemplateModel(active_user_name=None, active_result_race_name=None)
points = PointsModel()

View File

@ -3,8 +3,8 @@ from formula10.database.model.db_race_result import DbRaceResult
from formula10.database.model.db_user import DbUser
from formula10 import db
def race_has_result(race_name: str) -> bool:
return db.session.query(DbRaceResult).filter_by(race_name=race_name).first() is not None
def race_has_result(race_id: int) -> bool:
return db.session.query(DbRaceResult).filter_by(race_id=race_id).first() is not None
def user_exists_and_enabled(user_name: str) -> bool:
@ -15,9 +15,9 @@ def user_exists_and_disabled(user_name: str) -> bool:
return db.session.query(DbUser).filter_by(name=user_name, enabled=False).first() is not None
def find_single_driver_strict(driver_name: str) -> DbDriver:
db_driver: DbDriver | None = db.session.query(DbDriver).filter_by(name=driver_name).first()
def find_single_driver_strict(driver_id: int) -> DbDriver:
db_driver: DbDriver | None = db.session.query(DbDriver).filter_by(id=driver_id).first()
if db_driver is None:
raise Exception(f"Could not find driver with name {driver_name} in database")
raise Exception(f"Could not find driver with id {driver_id} in database")
return db_driver

View File

@ -1,111 +0,0 @@
import csv
import os.path
from typing import List, Any
from formula10 import db
from formula10.database.model.db_driver import DbDriver
from formula10.database.model.db_race import DbRace
from formula10.database.model.db_race_guess import DbRaceGuess
from formula10.database.model.db_race_result import DbRaceResult
from formula10.database.model.db_season_guess import DbSeasonGuess
from formula10.database.model.db_season_guess_result import DbSeasonGuessResult
from formula10.database.model.db_team import DbTeam
from formula10.database.model.db_user import DbUser
def load_csv(filename: str) -> List[List[str]]:
if not os.path.exists(filename):
print(f"Could not load data from file {filename}, as it doesn't exist!")
return []
with open(filename, "r", newline="") as file:
reader = csv.reader(file, delimiter=",")
next(reader, None) # skip header
return list(reader)
def write_csv(filename: str, objects: List[Any]):
if len(objects) == 0:
print(f"Could not write objects to file {filename}, as no objects were given!")
return
with open(filename, "w", newline="") as file:
writer = csv.writer(file, delimiter=",")
writer.writerow(objects[0].__csv_header__)
for obj in objects:
writer.writerow(obj.to_csv())
# Reload static database data, this has to be called from the app context
def reload_static_data():
print("Initializing database with static values...")
# Create it/update tables (if it/they doesn't exist!)
db.create_all()
# Clear static data
db.session.query(DbTeam).delete()
db.session.query(DbDriver).delete()
db.session.query(DbRace).delete()
# Reload static data
for row in load_csv("data/static_import/teams.csv"):
db.session.add(DbTeam.from_csv(row))
for row in load_csv("data/static_import/drivers.csv"):
db.session.add(DbDriver.from_csv(row))
for row in load_csv("data/static_import/races.csv"):
db.session.add(DbRace.from_csv(row))
db.session.commit()
def reload_dynamic_data():
print("Initializing database with dynamic values...")
# Create it/update tables (if it/they doesn't exist!)
db.create_all()
# Clear dynamic data
db.session.query(DbUser).delete()
db.session.query(DbRaceResult).delete()
db.session.query(DbRaceGuess).delete()
db.session.query(DbSeasonGuess).delete()
# Reload dynamic data
for row in load_csv("data/dynamic_export/users.csv"):
db.session.add(DbUser.from_csv(row))
for row in load_csv("data/dynamic_export/raceresults.csv"):
db.session.add(DbRaceResult.from_csv(row))
for row in load_csv("data/dynamic_export/raceguesses.csv"):
db.session.add(DbRaceGuess.from_csv(row))
for row in load_csv("data/dynamic_export/seasonguesses.csv"):
db.session.add(DbSeasonGuess.from_csv(row))
db.session.commit()
def reload_season_guess_result_data():
print("Loading season guess results...")
# Create it/update tables (if it/they doesn't exist!)
db.create_all()
# Clear result data
db.session.query(DbSeasonGuessResult).delete()
# Reload result data
for row in load_csv("data/static_import/season_guess_results.csv"):
db.session.add(DbSeasonGuessResult.from_csv(row))
db.session.commit()
def export_dynamic_data():
print("Exporting Userdata...")
users: List[DbUser] = db.session.query(DbUser).all()
raceresults: List[DbRaceResult] = db.session.query(DbRaceResult).all()
raceguesses: List[DbRaceGuess] = db.session.query(DbRaceGuess).all()
seasonguesses: List[DbSeasonGuess] = db.session.query(DbSeasonGuess).all()
write_csv("data/dynamic_export/users.csv", users)
write_csv("data/dynamic_export/raceresults.csv", raceresults)
write_csv("data/dynamic_export/raceguesses.csv", raceguesses)
write_csv("data/dynamic_export/seasonguesses.csv", seasonguesses)

View File

@ -1,5 +1,4 @@
from typing import List
from sqlalchemy import String, ForeignKey
from sqlalchemy import Integer, String, ForeignKey, Boolean
from sqlalchemy.orm import mapped_column, Mapped, relationship
from formula10.database.model.db_team import DbTeam
@ -13,21 +12,15 @@ class DbDriver(db.Model):
"""
__tablename__ = "driver"
def __init__(self, *, name: str):
self.name = name # Primary key
def __init__(self, *, id: int):
self.id = id # Primary key
@classmethod
def from_csv(cls, row: List[str]):
db_driver: DbDriver = cls(name=str(row[0]))
db_driver.abbr = str(row[1])
db_driver.team_name = str(row[2])
db_driver.country_code = str(row[3])
return db_driver
name: Mapped[str] = mapped_column(String(32), primary_key=True)
abbr: Mapped[str] = mapped_column(String(4))
team_name: Mapped[str] = mapped_column(ForeignKey("team.name"))
country_code: Mapped[str] = mapped_column(String(2)) # alpha-2 code
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=False)
name: Mapped[str] = mapped_column(String(32), nullable=False, unique=True)
abbr: Mapped[str] = mapped_column(String(4), nullable=False, unique=True)
team_id: Mapped[str] = mapped_column(ForeignKey("team.id"), nullable=False)
country_code: Mapped[str] = mapped_column(String(2), nullable=False) # alpha-2 code
active: Mapped[bool] = mapped_column(Boolean, nullable=False)
# Relationships
team: Mapped[DbTeam] = relationship("DbTeam", foreign_keys=[team_name])
team: Mapped[DbTeam] = relationship("DbTeam", foreign_keys=[team_id])

View File

@ -1,6 +1,5 @@
from datetime import datetime
from typing import List
from sqlalchemy import DateTime, Integer, String
from sqlalchemy import Boolean, DateTime, Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from formula10 import db
@ -13,22 +12,13 @@ class DbRace(db.Model):
"""
__tablename__ = "race"
def __init__(self, *, name: str, number: int, date: datetime, pxx: int):
self.name = name # Primary key
def __init__(self, *, id: int):
self.id = id # Primary key
self.number = number
self.date = date
self.pxx = pxx
@classmethod
def from_csv(cls, row: List[str]):
db_race: DbRace = cls(name=str(row[0]),
number=int(row[1]),
date=datetime.strptime(str(row[2]), "%Y-%m-%d-%H-%M"),
pxx=int(row[3]))
return db_race
name: Mapped[str] = mapped_column(String(64), primary_key=True)
number: Mapped[int] = mapped_column(Integer)
date: Mapped[datetime] = mapped_column(DateTime)
pxx: Mapped[int] = mapped_column(Integer) # This is the place to guess
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=False)
name: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
number: Mapped[int] = mapped_column(Integer, nullable=False, unique=True)
date: Mapped[datetime] = mapped_column(DateTime, nullable=False, unique=True)
pxx: Mapped[int] = mapped_column(Integer, nullable=False) # This is the place to guess
quali_date: Mapped[datetime] = mapped_column(DateTime, nullable=False, unique=True)
has_sprint: Mapped[bool] = mapped_column(Boolean, nullable=False)

View File

@ -1,4 +1,3 @@
from typing import Any, List
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
@ -14,38 +13,18 @@ class DbRaceGuess(db.Model):
It stores the corresponding race and the guessed drivers for PXX and DNF.
"""
__tablename__ = "raceguess"
__csv_header__ = ["user_name", "race_name", "pxx_driver_name", "dnf_driver_name"]
def __init__(self, *, user_name: str, race_name: str, pxx_driver_name: str, dnf_driver_name: str):
self.user_name = user_name # Primary key
self.race_name = race_name # Primary key
def __init__(self, *, user_id: int, race_id: int):
self.user_id = user_id # Primary key
self.race_id = race_id # Primary key
self.dnf_driver_name = dnf_driver_name
self.pxx_driver_name = pxx_driver_name
@classmethod
def from_csv(cls, row: List[str]):
db_race_guess: DbRaceGuess = cls(user_name=str(row[0]),
race_name=str(row[1]),
pxx_driver_name=str(row[2]),
dnf_driver_name=str(row[3]))
return db_race_guess
def to_csv(self) -> List[Any]:
return [
self.user_name,
self.race_name,
self.pxx_driver_name,
self.dnf_driver_name
]
user_name: Mapped[str] = mapped_column(ForeignKey("user.name"), primary_key=True)
race_name: Mapped[str] = mapped_column(ForeignKey("race.name"), primary_key=True)
pxx_driver_name: Mapped[str] = mapped_column(ForeignKey("driver.name"))
dnf_driver_name: Mapped[str] = mapped_column(ForeignKey("driver.name"))
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), primary_key=True)
race_id: Mapped[int] = mapped_column(ForeignKey("race.id"), primary_key=True)
pxx_driver_id: Mapped[int] = mapped_column(ForeignKey("driver.id"), nullable=False)
dnf_driver_id: Mapped[int] = mapped_column(ForeignKey("driver.id"), nullable=False)
# Relationships
user: Mapped[DbUser] = relationship("DbUser", foreign_keys=[user_name])
race: Mapped[DbRace] = relationship("DbRace", foreign_keys=[race_name])
pxx: Mapped[DbDriver] = relationship("DbDriver", foreign_keys=[pxx_driver_name])
dnf: Mapped[DbDriver] = relationship("DbDriver", foreign_keys=[dnf_driver_name])
user: Mapped[DbUser] = relationship("DbUser", foreign_keys=[user_id])
race: Mapped[DbRace] = relationship("DbRace", foreign_keys=[race_id])
pxx: Mapped[DbDriver] = relationship("DbDriver", foreign_keys=[pxx_driver_id])
dnf: Mapped[DbDriver] = relationship("DbDriver", foreign_keys=[dnf_driver_id])

View File

@ -1,6 +1,6 @@
from typing import Any, List
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from formula10.database.model.db_driver import DbDriver
from formula10.database.model.db_race import DbRace
from formula10 import db
@ -11,39 +11,20 @@ class DbRaceResult(db.Model):
It stores the corresponding race and dictionaries of place-/dnf-order and a list of drivers that are excluded from the standings for this race.
"""
__tablename__ = "raceresult"
__csv_header__ = ["race_name", "pxx_driver_names_json", "first_dnf_driver_names_json", "dnf_driver_names_json", "excluded_driver_names_json"]
def __init__(self, *, race_name: str, pxx_driver_names_json: str, first_dnf_driver_names_json: str, dnf_driver_names_json: str, excluded_driver_names_json: str):
self.race_name = race_name # Primary key
def __init__(self, *, race_id: int):
self.race_id = race_id # Primary key
self.pxx_driver_names_json = pxx_driver_names_json
self.first_dnf_driver_names_json = first_dnf_driver_names_json
self.dnf_driver_names_json = dnf_driver_names_json
self.excluded_driver_names_json = excluded_driver_names_json
race_id: Mapped[int] = mapped_column(ForeignKey("race.id"), primary_key=True)
pxx_driver_ids_json: Mapped[str] = mapped_column(String(1024), nullable=False)
first_dnf_driver_ids_json: Mapped[str] = mapped_column(String(1024), nullable=False)
dnf_driver_ids_json: Mapped[str] = mapped_column(String(1024), nullable=False)
excluded_driver_ids_json: Mapped[str] = mapped_column(String(1024), nullable=False)
@classmethod
def from_csv(cls, row: List[str]):
db_race_result: DbRaceResult = cls(race_name=str(row[0]),
pxx_driver_names_json=str(row[1]),
first_dnf_driver_names_json=str(row[2]),
dnf_driver_names_json=str(row[3]),
excluded_driver_names_json=str(row[4]))
return db_race_result
def to_csv(self) -> List[Any]:
return [
self.race_name,
self.pxx_driver_names_json,
self.first_dnf_driver_names_json,
self.dnf_driver_names_json,
self.excluded_driver_names_json
]
race_name: Mapped[str] = mapped_column(ForeignKey("race.name"), primary_key=True)
pxx_driver_names_json: Mapped[str] = mapped_column(String(1024))
first_dnf_driver_names_json: Mapped[str] = mapped_column(String(1024))
dnf_driver_names_json: Mapped[str] = mapped_column(String(1024))
excluded_driver_names_json: Mapped[str] = mapped_column(String(1024))
fastest_lap_id: Mapped[int] = mapped_column(ForeignKey("driver.id"), nullable=False)
sprint_dnf_driver_ids_json: Mapped[str] = mapped_column(String(1024), nullable=False)
sprint_points_json: Mapped[str] = mapped_column(String(1024), nullable=False)
# Relationships
race: Mapped[DbRace] = relationship("DbRace", foreign_keys=[race_name])
race: Mapped[DbRace] = relationship("DbRace", foreign_keys=[race_id])
fastest_lap_driver: Mapped[DbDriver] = relationship("DbDriver", foreign_keys=[fastest_lap_id])

View File

@ -1,4 +1,3 @@
from typing import Any, List
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
@ -12,56 +11,24 @@ class DbSeasonGuess(db.Model):
A collection of bonus guesses for the entire season.
"""
__tablename__ = "seasonguess"
__csv_header__ = ["user_name", "hot_take", "p2_team_name",
"overtake_driver_name", "dnf_driver_name", "gained_driver_name", "lost_driver_name",
"team_winners_driver_names_json", "podium_drivers_driver_names_json"]
def __init__(self, *, user_name: str, team_winners_driver_names_json: str, podium_drivers_driver_names_json: str):
self.user_name = user_name # Primary key
def __init__(self, *, user_id: int):
self.user_id = user_id # Primary key
self.team_winners_driver_names_json = team_winners_driver_names_json
self.podium_drivers_driver_names_json = podium_drivers_driver_names_json
@classmethod
def from_csv(cls, row: List[str]):
db_season_guess: DbSeasonGuess = cls(user_name=str(row[0]),
team_winners_driver_names_json=str(row[7]),
podium_drivers_driver_names_json=str(row[8]))
db_season_guess.hot_take = str(row[1])
db_season_guess.p2_team_name = str(row[2])
db_season_guess.overtake_driver_name = str(row[3])
db_season_guess.dnf_driver_name = str(row[4])
db_season_guess.gained_driver_name = str(row[5])
db_season_guess.lost_driver_name = str(row[6])
return db_season_guess
def to_csv(self) -> List[Any]:
return [
self.user_name,
self.hot_take,
self.p2_team_name,
self.overtake_driver_name,
self.dnf_driver_name,
self.gained_driver_name,
self.lost_driver_name,
self.team_winners_driver_names_json,
self.podium_drivers_driver_names_json
]
user_name: Mapped[str] = mapped_column(ForeignKey("user.name"), primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), primary_key=True)
hot_take: Mapped[str | None] = mapped_column(String(512), nullable=True)
p2_team_name: Mapped[str | None] = mapped_column(ForeignKey("team.name"), nullable=True)
overtake_driver_name: Mapped[str | None] = mapped_column(ForeignKey("driver.name"), nullable=True)
dnf_driver_name: Mapped[str | None] = mapped_column(ForeignKey("driver.name"), nullable=True)
gained_driver_name: Mapped[str | None] = mapped_column(ForeignKey("driver.name"), nullable=True)
lost_driver_name: Mapped[str | None] = mapped_column(ForeignKey("driver.name"), nullable=True)
team_winners_driver_names_json: Mapped[str] = mapped_column(String(1024))
podium_drivers_driver_names_json: Mapped[str] = mapped_column(String(1024))
p2_team_id: Mapped[int | None] = mapped_column(ForeignKey("team.id"), nullable=True)
overtake_driver_id: Mapped[int | None] = mapped_column(ForeignKey("driver.id"), nullable=True)
dnf_driver_id: Mapped[int | None] = mapped_column(ForeignKey("driver.id"), nullable=True)
gained_driver_id: Mapped[int | None] = mapped_column(ForeignKey("driver.id"), nullable=True)
lost_driver_id: Mapped[int | None] = mapped_column(ForeignKey("driver.id"), nullable=True)
team_winners_driver_ids_json: Mapped[str] = mapped_column(String(1024))
podium_drivers_driver_ids_json: Mapped[str] = mapped_column(String(1024))
# Relationships
user: Mapped[DbUser] = relationship("DbUser", foreign_keys=[user_name])
p2_team: Mapped[DbTeam | None] = relationship("DbTeam", foreign_keys=[p2_team_name])
overtake_driver: Mapped[DbDriver | None] = relationship("DbDriver", foreign_keys=[overtake_driver_name])
dnf_driver: Mapped[DbDriver | None] = relationship("DbDriver", foreign_keys=[dnf_driver_name])
gained_driver: Mapped[DbDriver | None] = relationship("DbDriver", foreign_keys=[gained_driver_name])
lost_driver: Mapped[DbDriver | None] = relationship("DbDriver", foreign_keys=[lost_driver_name])
user: Mapped[DbUser] = relationship("DbUser", foreign_keys=[user_id])
p2_team: Mapped[DbTeam | None] = relationship("DbTeam", foreign_keys=[p2_team_id])
overtake_driver: Mapped[DbDriver | None] = relationship("DbDriver", foreign_keys=[overtake_driver_id])
dnf_driver: Mapped[DbDriver | None] = relationship("DbDriver", foreign_keys=[dnf_driver_id])
gained_driver: Mapped[DbDriver | None] = relationship("DbDriver", foreign_keys=[gained_driver_id])
lost_driver: Mapped[DbDriver | None] = relationship("DbDriver", foreign_keys=[lost_driver_id])

View File

@ -1,5 +1,3 @@
from typing import List
from sqlalchemy import Boolean, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
@ -12,33 +10,13 @@ class DbSeasonGuessResult(db.Model):
"""
__tablename__ = "seasonguessresult"
__csv_header__ = ["user_name", "hot_take_correct", "overtakes_correct"]
def __init__(self, *, user_name: str, hot_take_correct: bool, overtakes_correct: bool):
self.user_name = user_name # Primary key
def __init__(self, *, user_id: int):
self.user_id = user_id # Primary key
self.hot_take_correct = hot_take_correct
self.overtakes_correct = overtakes_correct
@classmethod
def from_csv(cls, row: List[str]):
db_season_guess_result: DbSeasonGuessResult = cls(user_name=str(row[0]),
hot_take_correct=True if str(row[1])=="True" else False,
overtakes_correct=True if str(row[2])=="True" else False)
return db_season_guess_result
# This object can't be edited from the page context
# def to_csv(self) -> List[Any]:
# return [
# self.user_name,
# self.hot_take_correct,
# self.overtakes_correct
# ]
user_name: Mapped[str] = mapped_column(ForeignKey("user.name"), primary_key=True)
hot_take_correct: Mapped[bool] = mapped_column(Boolean)
overtakes_correct: Mapped[bool] = mapped_column(Boolean)
user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), primary_key=True)
hot_take_correct: Mapped[bool] = mapped_column(Boolean, nullable=False)
overtakes_correct: Mapped[bool] = mapped_column(Boolean, nullable=False)
# Relationships
user: Mapped[DbUser] = relationship("DbUser", foreign_keys=[user_name])
user: Mapped[DbUser] = relationship("DbUser", foreign_keys=[user_id])

View File

@ -1,5 +1,4 @@
from typing import List
from sqlalchemy import String
from sqlalchemy import Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from formula10 import db
@ -11,12 +10,8 @@ class DbTeam(db.Model):
"""
__tablename__ = "team"
def __init__(self, *, name: str):
self.name = name # Primary key
def __init__(self, *, id: int):
self.id = id # Primary key
@classmethod
def from_csv(cls, row: List[str]):
db_team: DbTeam = cls(name=str(row[0]))
return db_team
name: Mapped[str] = mapped_column(String(32), primary_key=True)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=False)
name: Mapped[str] = mapped_column(String(32), nullable=False, unique=True)

View File

@ -1,5 +1,4 @@
from typing import Any, List
from sqlalchemy import Boolean, String
from sqlalchemy import Boolean, Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from formula10 import db
@ -10,24 +9,11 @@ class DbUser(db.Model):
A user that can guess races (name only).
"""
__tablename__ = "user"
__csv_header__ = ["name", "enabled"]
def __init__(self, *, name: str, enabled: bool):
self.name = name # Primary key
def __init__(self, *, id: int | None):
if id is not None:
self.id = id # Primary key
self.enabled = enabled
@classmethod
def from_csv(cls, row: List[str]):
db_user: DbUser = cls(name=str(row[0]),
enabled=True if str(row[1])=="True" else False)
return db_user
def to_csv(self) -> List[Any]:
return [
self.name,
self.enabled
]
name: Mapped[str] = mapped_column(String(32), primary_key=True)
enabled: Mapped[bool] = mapped_column(Boolean)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(32), nullable=False, unique=True)
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False)

View File

@ -6,6 +6,7 @@ from werkzeug import Response
from formula10.controller.error_controller import error_redirect
from formula10.database.common_queries import race_has_result, user_exists_and_disabled, user_exists_and_enabled
from formula10.database.model.db_race import DbRace
from formula10.database.model.db_race_guess import DbRaceGuess
from formula10.database.model.db_race_result import DbRaceResult
from formula10.database.model.db_season_guess import DbSeasonGuess
@ -14,159 +15,182 @@ from formula10.database.validation import any_is_none, positions_are_contiguous,
from formula10 import ENABLE_TIMING, db
def find_or_create_race_guess(user_name: str, race_name: str) -> DbRaceGuess:
def find_or_create_race_guess(user_id: int, race_id: int) -> DbRaceGuess:
# There can be a single RaceGuess at most, since (user_name, race_name) is the composite primary key
race_guess: DbRaceGuess | None = db.session.query(DbRaceGuess).filter_by(user_name=user_name, race_name=race_name).first()
race_guess: DbRaceGuess | None = db.session.query(DbRaceGuess).filter_by(user_id=user_id, race_id=race_id).first()
if race_guess is not None:
return race_guess
# Insert a new RaceGuess
race_guess = DbRaceGuess(user_name=user_name, race_name=race_name, pxx_driver_name="TEMP", dnf_driver_name="TEMP")
race_guess = DbRaceGuess(user_id=user_id, race_id=race_id)
race_guess.pxx_driver_id = 9999
race_guess.dnf_driver_id = 9999
db.session.add(race_guess)
db.session.commit()
# Double check if database insertion worked and obtain any values set by the database
race_guess = db.session.query(DbRaceGuess).filter_by(user_name=user_name, race_name=race_name).first()
race_guess = db.session.query(DbRaceGuess).filter_by(user_id=user_id, race_id=race_id).first()
if race_guess is None:
raise Exception("Failed adding RaceGuess to the database")
return race_guess
def update_race_guess(race_name: str, user_name: str, pxx_select: str | None, dnf_select: str | None) -> Response:
if any_is_none(pxx_select, dnf_select):
return error_redirect(f"Picks for race \"{race_name}\" were not saved, because you did not fill all the fields.")
def update_race_guess(race_id: int, user_id: int, pxx_select_id: int | None, dnf_select_id: int | None) -> Response:
if any_is_none(pxx_select_id, dnf_select_id):
return error_redirect(f"Picks for race \"{race_id}\" were not saved, because you did not fill all the fields.")
if ENABLE_TIMING and race_has_started(race_name=race_name):
return error_redirect(f"No picks for race \"{race_name}\" can be entered, as this race has already started.")
if ENABLE_TIMING and race_has_started(race_id=race_id):
return error_redirect(f"No picks for race \"{race_id}\" can be entered, as this race has already started.")
if race_has_result(race_name):
return error_redirect(f"No picks for race \"{race_name}\" can be entered, as this race has already finished.")
if race_has_result(race_id):
return error_redirect(f"No picks for race \"{race_id}\" can be entered, as this race has already finished.")
pxx_driver_name: str = cast(str, pxx_select)
dnf_driver_name: str = cast(str, dnf_select)
pxx_driver_id: int = cast(int, pxx_select_id)
dnf_driver_id: int = cast(int, dnf_select_id)
race_guess: DbRaceGuess = find_or_create_race_guess(user_name, race_name)
race_guess.pxx_driver_name = pxx_driver_name
race_guess.dnf_driver_name = dnf_driver_name
race_guess: DbRaceGuess = find_or_create_race_guess(user_id, race_id)
race_guess.pxx_driver_id = pxx_driver_id
race_guess.dnf_driver_id = dnf_driver_id
db.session.commit()
return redirect("/race/Everyone")
def delete_race_guess(race_name: str, user_name: str) -> Response:
def delete_race_guess(race_id: int, user_id: int) -> Response:
# Don't change guesses that are already over
if ENABLE_TIMING and race_has_started(race_name=race_name):
return error_redirect(f"No picks for race \"{race_name}\" can be deleted, as this race has already started.")
if ENABLE_TIMING and race_has_started(race_id=race_id):
return error_redirect(f"No picks for race with id \"{race_id}\" can be deleted, as this race has already started.")
if race_has_result(race_name):
return error_redirect(f"No picks for race \"{race_name}\" can be deleted, as this race has already finished.")
if race_has_result(race_id):
return error_redirect(f"No picks for race \"{race_id}\" can be deleted, as this race has already finished.")
# Does not throw if row doesn't exist
db.session.query(DbRaceGuess).filter_by(race_name=race_name, user_name=user_name).delete()
db.session.query(DbRaceGuess).filter_by(race_id=race_id, user_id=user_id).delete()
db.session.commit()
return redirect("/race/Everyone")
def find_or_create_season_guess(user_name: str) -> DbSeasonGuess:
def find_or_create_season_guess(user_id: int) -> DbSeasonGuess:
# There can be a single SeasonGuess at most, since user_name is the primary key
season_guess: DbSeasonGuess | None = db.session.query(DbSeasonGuess).filter_by(user_name=user_name).first()
season_guess: DbSeasonGuess | None = db.session.query(DbSeasonGuess).filter_by(user_id=user_id).first()
if season_guess is not None:
return season_guess
# Insert a new SeasonGuess
season_guess = DbSeasonGuess(user_name=user_name, team_winners_driver_names_json=json.dumps(["TEMP"]), podium_drivers_driver_names_json=json.dumps(["TEMP"]))
season_guess = DbSeasonGuess(user_id=user_id)
season_guess.team_winners_driver_ids_json=json.dumps(["9999"])
season_guess.podium_drivers_driver_ids_json=json.dumps(["9999"])
db.session.add(season_guess)
db.session.commit()
# Double check if database insertion worked and obtain any values set by the database
season_guess = db.session.query(DbSeasonGuess).filter_by(user_name=user_name).first()
season_guess = db.session.query(DbSeasonGuess).filter_by(user_id=user_id).first()
if season_guess is None:
raise Exception("Failed adding SeasonGuess to the database")
return season_guess
def update_season_guess(user_name: str, guesses: List[str | None], team_winner_guesses: List[str | None], podium_driver_guesses: List[str]) -> Response:
def update_season_guess(user_id: int, guesses: List[str | None], team_winner_guesses: List[str | None], podium_driver_guesses: List[str]) -> Response:
# Pylance marks type errors here, but those are intended. Columns are marked nullable.
if ENABLE_TIMING and race_has_started(race_name="Bahrain"):
if ENABLE_TIMING and race_has_started(race_id=1):
return error_redirect("No season picks can be entered, as the season has already begun!")
season_guess: DbSeasonGuess = find_or_create_season_guess(user_name)
season_guess: DbSeasonGuess = find_or_create_season_guess(user_id)
season_guess.hot_take = guesses[0] # type: ignore
season_guess.p2_team_name = guesses[1] # type: ignore
season_guess.overtake_driver_name = guesses[2] # type: ignore
season_guess.dnf_driver_name = guesses[3] # type: ignore
season_guess.gained_driver_name = guesses[4] # type: ignore
season_guess.lost_driver_name = guesses[5] # type: ignore
season_guess.team_winners_driver_names_json = json.dumps(team_winner_guesses)
season_guess.podium_drivers_driver_names_json = json.dumps(podium_driver_guesses)
season_guess.p2_team_id = guesses[1] # type: ignore
season_guess.overtake_driver_id = guesses[2] # type: ignore
season_guess.dnf_driver_id = guesses[3] # type: ignore
season_guess.gained_driver_id = guesses[4] # type: ignore
season_guess.lost_driver_id = guesses[5] # type: ignore
season_guess.team_winners_driver_ids_json = json.dumps(team_winner_guesses)
season_guess.podium_drivers_driver_ids_json = json.dumps(podium_driver_guesses)
db.session.commit()
return redirect(f"/season/Everyone")
def find_or_create_race_result(race_name: str) -> DbRaceResult:
def find_or_create_race_result(race_id: int) -> DbRaceResult:
# There can be a single RaceResult at most, since race_name is the primary key
race_result: DbRaceResult | None = db.session.query(DbRaceResult).filter_by(race_name=race_name).first()
race_result: DbRaceResult | None = db.session.query(DbRaceResult).filter_by(race_id=race_id).first()
if race_result is not None:
return race_result
race_result = DbRaceResult(race_name=race_name,
pxx_driver_names_json=json.dumps(["TEMP"]),
first_dnf_driver_names_json=json.dumps(["TEMP"]),
dnf_driver_names_json=json.dumps(["TEMP"]),
excluded_driver_names_json=json.dumps(["TEMP"]))
race_result = DbRaceResult(race_id=race_id)
race_result.pxx_driver_ids_json = json.dumps(["9999"])
race_result.first_dnf_driver_ids_json = json.dumps(["9999"])
race_result.dnf_driver_ids_json = json.dumps(["9999"])
race_result.excluded_driver_ids_json = json.dumps(["9999"])
race_result.fastest_lap_id = 9999
race_result.sprint_dnf_driver_ids_json = json.dumps([])
race_result.sprint_points_json = json.dumps({})
db.session.add(race_result)
db.session.commit()
# Double check if database insertion worked and obtain any values set by the database
race_result = db.session.query(DbRaceResult).filter_by(race_name=race_name).first()
race_result = db.session.query(DbRaceResult).filter_by(race_id=race_id).first()
if race_result is None:
raise Exception("Failed adding RaceResult to the database")
return race_result
def update_race_result(race_name: str, pxx_driver_names_list: List[str], first_dnf_driver_names_list: List[str], dnf_driver_names_list: List[str], excluded_driver_names_list: List[str]) -> Response:
if ENABLE_TIMING and not race_has_started(race_name=race_name):
def update_race_result(race_id: int, pxx_driver_ids_list: List[str], first_dnf_driver_ids_list: List[str], dnf_driver_ids_list: List[str], excluded_driver_ids_list: List[str],
fastest_lap_driver_id: int, sprint_pxx_driver_ids_list: List[str], sprint_dnf_driver_ids_list: List[str]) -> Response:
if ENABLE_TIMING and not race_has_started(race_id=race_id):
return error_redirect("No race result can be entered, as the race has not begun!")
# Use strings as keys, as these dicts will be serialized to json
pxx_driver_names: Dict[str, str] = {
str(position + 1): driver for position, driver in enumerate(pxx_driver_names_list)
pxx_driver_ids: Dict[str, str] = {
str(position + 1): driver_id for position, driver_id in enumerate(pxx_driver_ids_list)
}
# Not counted drivers have to be at the end
excluded_driver_names: Dict[str, str] = {
str(position + 1): driver for position, driver in enumerate(pxx_driver_names_list)
if driver in excluded_driver_names_list
excluded_driver_ids: Dict[str, str] = {
str(position + 1): driver_id for position, driver_id in enumerate(pxx_driver_ids_list)
if driver_id in excluded_driver_ids_list
}
if len(excluded_driver_names) > 0 and (not "20" in excluded_driver_names or not positions_are_contiguous(list(excluded_driver_names.keys()))):
if len(excluded_driver_ids) > 0 and (not "20" in excluded_driver_ids or not positions_are_contiguous(list(excluded_driver_ids.keys()))):
return error_redirect("Race result was not saved, as excluded drivers must be contiguous and at the end of the field!")
# First DNF drivers have to be contained in DNF drivers
for driver_name in first_dnf_driver_names_list:
if driver_name not in dnf_driver_names_list:
dnf_driver_names_list.append(driver_name)
for driver_id in first_dnf_driver_ids_list:
if driver_id not in dnf_driver_ids_list:
dnf_driver_ids_list.append(driver_id)
# There can't be dnfs but no initial dnfs
if len(dnf_driver_names_list) > 0 and len(first_dnf_driver_names_list) == 0:
if len(dnf_driver_ids_list) > 0 and len(first_dnf_driver_ids_list) == 0:
return error_redirect("Race result was not saved, as there cannot be DNFs without (an) initial DNF(s)!")
race_result: DbRaceResult = find_or_create_race_result(race_name)
race_result.pxx_driver_names_json = json.dumps(pxx_driver_names)
race_result.first_dnf_driver_names_json = json.dumps(first_dnf_driver_names_list)
race_result.dnf_driver_names_json = json.dumps(dnf_driver_names_list)
race_result.excluded_driver_names_json = json.dumps(excluded_driver_names_list)
race_result: DbRaceResult = find_or_create_race_result(race_id)
race_result.pxx_driver_ids_json = json.dumps(pxx_driver_ids)
race_result.first_dnf_driver_ids_json = json.dumps(first_dnf_driver_ids_list)
race_result.dnf_driver_ids_json = json.dumps(dnf_driver_ids_list)
race_result.excluded_driver_ids_json = json.dumps(excluded_driver_ids_list)
# Extra stats for points calculation
sprint_pxx_driver_ids: Dict[str, str] = {
str(position + 1): driver_id for position, driver_id in enumerate(sprint_pxx_driver_ids_list)
}
race_result.fastest_lap_id = fastest_lap_driver_id
race_result.sprint_dnf_driver_ids_json = json.dumps(sprint_dnf_driver_ids_list)
race_result.sprint_points_json = json.dumps(sprint_pxx_driver_ids)
db.session.commit()
return redirect(f"/result/{quote(race_name)}")
race: DbRace | None = db.session.query(DbRace).filter_by(id=race_id).first()
if race is None:
raise Exception(f"Could not find DbRace with id {race_id}")
return redirect(f"/result/{quote(race.name)}")
def update_user(user_name: str | None, add: bool = False, delete: bool = False) -> Response:
@ -194,7 +218,9 @@ def update_user(user_name: str | None, add: bool = False, delete: bool = False)
disabled_user.enabled = True
else:
user: DbUser = DbUser(name=user_name, enabled=True)
user: DbUser = DbUser(id=None)
user.name = user_name
user.enabled = True
db.session.add(user)
db.session.commit()

View File

@ -31,19 +31,19 @@ def race_has_started(*, race: Race) -> bool:
return race_has_started(race=race)
@overload
def race_has_started(*, race_name: str) -> bool:
return race_has_started(race_name=race_name)
def race_has_started(*, race_id: int) -> bool:
return race_has_started(race_id=race_id)
def race_has_started(*, race: Race | None = None, race_name: str | None = None) -> bool:
if race is None and race_name is not None:
_race: DbRace | None = db.session.query(DbRace).filter_by(name=race_name).first()
def race_has_started(*, race: Race | None = None, race_id: int | None = None) -> bool:
if race is None and race_id is not None:
_race: DbRace | None = db.session.query(DbRace).filter_by(id=race_id).first()
if _race is None:
raise Exception(f"Couldn't obtain race {race_name} to check date")
raise Exception(f"Couldn't obtain race with id {race_id} to check date")
return datetime.now() > _race.date
if race is not None and race_name is None:
if race is not None and race_id is None:
return datetime.now() > race.date
raise Exception("race_has_started received illegal arguments")
@ -76,6 +76,19 @@ def find_multiple_strict(predicate: Callable[[_T], bool], iterable: Iterable[_T]
return filtered
def find_atleast_strict(predicate: Callable[[_T], bool], iterable: Iterable[_T], count: int = 0) -> List[_T]:
"""
Finds at least <count> elements in a sequence matching a predicate.
Throws exception if fewer elements were found than specified.
"""
filtered = list(filter(predicate, iterable))
if len(filtered) < count:
raise Exception(f"find_atleast found {len(filtered)} matching elements but expected at least {count}")
return filtered
def find_single_strict(predicate: Callable[[_T], bool], iterable: Iterable[_T]) -> _T:
"""
Find a single element in a sequence matching a predicate.

View File

@ -0,0 +1,98 @@
from typing import Callable, List
from formula10 import cache
from formula10.domain.domain_model import Model
from formula10.domain.points_model import PointsModel
def cache_invalidate_user_updated() -> None:
caches: List[str] = [
"domain_all_users",
"domain_all_race_guesses",
"domain_all_season_guesses",
"points_points_per_step",
"points_user_standing",
]
memoized_caches: List[Callable] = [
PointsModel.points_by,
PointsModel.race_guesses_by,
PointsModel.season_guesses_by,
]
for c in caches:
cache.delete(c)
for c in memoized_caches:
cache.delete_memoized(c)
def cache_invalidate_race_result_updated() -> None:
caches: List[str] = [
"domain_all_race_results",
"points_points_per_step",
"points_team_points_per_step",
"points_dnfs",
"points_driver_points_per_step_cumulative",
"points_wdc_standing_by_position",
"points_wdc_standing_by_driver",
"points_most_dnf_names",
"points_most_gained_names",
"points_most_lost_names",
"points_team_points_per_step_cumulative",
"points_teams_sorted_by_points",
"points_wcc_standing_by_position",
"points_wcc_standing_by_team",
"points_user_standing",
"template_first_race_without_result",
]
memoized_caches: List[Callable] = [
PointsModel.driver_points_per_step,
PointsModel.driver_points_by,
PointsModel.total_driver_points_by,
PointsModel.drivers_sorted_by_points,
PointsModel.total_team_points_by,
PointsModel.points_by,
PointsModel.is_team_winner,
PointsModel.has_podium,
PointsModel.picks_with_points_count,
]
for c in caches:
cache.delete(c)
for c in memoized_caches:
cache.delete_memoized(c)
def cache_invalidate_race_guess_updated() -> None:
caches: List[str] = [
"domain_all_race_guesses",
]
memoized_caches: List[Callable] = [
Model.race_guesses_by,
]
for c in caches:
cache.delete(c)
for c in memoized_caches:
cache.delete_memoized(c)
def cache_invalidate_season_guess_updated() -> None:
caches: List[str] = [
"domain_all_season_guesses",
]
memoized_caches: List[Callable] = [
Model.season_guesses_by,
]
for c in caches:
cache.delete(c)
for c in memoized_caches:
cache.delete_memoized(c)

View File

@ -6,119 +6,109 @@ from formula10.database.model.db_race import DbRace
from formula10.database.model.db_race_guess import DbRaceGuess
from formula10.database.model.db_race_result import DbRaceResult
from formula10.database.model.db_season_guess import DbSeasonGuess
from formula10.database.model.db_season_guess_result import DbSeasonGuessResult
from formula10.database.model.db_team import DbTeam
from formula10.database.model.db_user import DbUser
from formula10.database.validation import find_multiple_strict, find_single_or_none_strict, find_single_strict
from formula10.database.validation import find_multiple_strict, find_single_or_none_strict, find_single_strict, find_atleast_strict
from formula10.domain.model.driver import NONE_DRIVER, Driver
from formula10.domain.model.race import Race
from formula10.domain.model.race_guess import RaceGuess
from formula10.domain.model.race_result import RaceResult
from formula10.domain.model.season_guess import SeasonGuess
from formula10.domain.model.season_guess_result import SeasonGuessResult
from formula10.domain.model.team import NONE_TEAM, Team
from formula10.domain.model.user import User
from formula10 import db
from formula10 import db, cache
class Model():
_all_users: List[User] | None = None
_all_race_results: List[RaceResult] | None = None
_all_race_guesses: List[RaceGuess] | None = None
_all_season_guesses: List[SeasonGuess] | None = None
_all_races: List[Race] | None = None
_all_drivers: List[Driver] | None = None
_all_teams: List[Team] | None = None
def all_users(self) -> List[User]:
class Model:
@staticmethod
@cache.cached(timeout=None, key_prefix="domain_all_users") # Clear when adding/deleting users
def all_users() -> List[User]:
"""
Returns a list of all enabled users.
"""
if self._all_users is None:
self._all_users = [
User.from_db_user(db_user)
for db_user in db.session.query(DbUser).filter_by(enabled=True).all()
]
db_users = db.session.query(DbUser).filter_by(enabled=True).all()
return [User.from_db_user(db_user) for db_user in db_users]
return self._all_users
def all_race_results(self) -> List[RaceResult]:
@staticmethod
@cache.cached(timeout=None, key_prefix="domain_all_race_results") # Clear when adding/updating results
def all_race_results() -> List[RaceResult]:
"""
Returns a list of all race results, in descending order (most recent first).
"""
if self._all_race_results is None:
self._all_race_results = [
RaceResult.from_db_race_result(db_race_result)
for db_race_result in db.session.query(DbRaceResult).join(DbRaceResult.race).order_by(desc(DbRace.number)).all()
]
db_race_results = db.session.query(DbRaceResult).join(DbRaceResult.race).order_by(desc("number")).all()
return [RaceResult.from_db_race_result(db_race_result) for db_race_result in db_race_results]
return self._all_race_results
def all_race_guesses(self) -> List[RaceGuess]:
@staticmethod
@cache.cached(timeout=None, key_prefix="domain_all_race_guesses") # Clear when adding/updating race guesses or users
def all_race_guesses() -> List[RaceGuess]:
"""
Returns a list of all race guesses (of enabled users).
"""
if self._all_race_guesses is None:
self._all_race_guesses = [
RaceGuess.from_db_race_guess(db_race_guess)
for db_race_guess in db.session.query(DbRaceGuess).join(DbRaceGuess.user).filter_by(enabled=True).all() # Ignore disabled users
]
db_race_guesses = db.session.query(DbRaceGuess).join(DbRaceGuess.user).filter_by(enabled=True).all()
return [RaceGuess.from_db_race_guess(db_race_guess) for db_race_guess in db_race_guesses]
return self._all_race_guesses
def all_season_guesses(self) -> List[SeasonGuess]:
@staticmethod
@cache.cached(timeout=None, key_prefix="domain_all_season_guesses") # Clear when adding/updating season guesses or users
def all_season_guesses() -> List[SeasonGuess]:
"""
Returns a list of all season guesses (of enabled users).
"""
if self._all_season_guesses is None:
self._all_season_guesses = [
SeasonGuess.from_db_season_guess(db_season_guess)
for db_season_guess in db.session.query(DbSeasonGuess).join(DbSeasonGuess.user).filter_by(enabled=True).all() # Ignore disabled users
]
db_season_guesses = db.session.query(DbSeasonGuess).join(DbSeasonGuess.user).filter_by(enabled=True).all()
return [SeasonGuess.from_db_season_guess(db_season_guess) for db_season_guess in db_season_guesses]
return self._all_season_guesses
@staticmethod
@cache.cached(timeout=None, key_prefix="domain_all_season_guess_results") # No cleanup, bc entered manually
def all_season_guess_results() -> List[SeasonGuessResult]:
"""
Returns a list of all season guess results (of enabled users).
"""
db_season_guess_results = db.session.query(DbSeasonGuessResult).join(DbSeasonGuessResult.user).filter_by(enabled=True).all()
return [SeasonGuessResult.from_db_season_guess_result(db_season_guess_result) for db_season_guess_result in db_season_guess_results]
def all_races(self) -> List[Race]:
@staticmethod
@cache.cached(timeout=None, key_prefix="domain_all_races") # No cleanup, bc entered manually
def all_races() -> List[Race]:
"""
Returns a list of all races, in descending order (last race first).
"""
if self._all_races is None:
self._all_races = [
Race.from_db_race(db_race)
for db_race in db.session.query(DbRace).order_by(desc(DbRace.number)).all()
]
db_races = db.session.query(DbRace).order_by(desc("number")).all()
return [Race.from_db_race(db_race) for db_race in db_races]
return self._all_races
def all_drivers(self, *, include_none: bool) -> List[Driver]:
@staticmethod
@cache.memoize(timeout=None) # No cleanup, bc entered manually
def all_drivers(*, include_none: bool, include_inactive: bool) -> List[Driver]:
"""
Returns a list of all drivers.
Returns a list of all active drivers.
"""
if self._all_drivers is None:
self._all_drivers = [
Driver.from_db_driver(db_driver)
for db_driver in db.session.query(DbDriver).all()
]
db_drivers = db.session.query(DbDriver).all()
drivers = [Driver.from_db_driver(db_driver) for db_driver in db_drivers]
if include_none:
return self._all_drivers
else:
if not include_inactive:
predicate: Callable[[Driver], bool] = lambda driver: driver.active
drivers = find_multiple_strict(predicate, drivers)
if not include_none:
predicate: Callable[[Driver], bool] = lambda driver: driver != NONE_DRIVER
return find_multiple_strict(predicate, self._all_drivers)
drivers = find_multiple_strict(predicate, drivers)
def all_teams(self, *, include_none: bool) -> List[Team]:
return drivers
@staticmethod
@cache.memoize(timeout=None) # No cleanup, bc entered manually
def all_teams(*, include_none: bool) -> List[Team]:
"""
Returns a list of all teams.
"""
if self._all_teams is None:
self._all_teams = [
Team.from_db_team(db_team)
for db_team in db.session.query(DbTeam).all()
]
db_teams = db.session.query(DbTeam).all()
teams = [Team.from_db_team(db_team) for db_team in db_teams]
if include_none:
return self._all_teams
else:
if not include_none:
predicate: Callable[[Team], bool] = lambda team: team != NONE_TEAM
return find_multiple_strict(predicate, self._all_teams)
return find_multiple_strict(predicate, teams)
return teams
#
# User queries
@ -191,6 +181,7 @@ class Model():
"""
return self.race_guesses_by()
@cache.memoize(timeout=None, args_to_ignore=["self"]) # Cleanup when adding/updating race guesses or users
def race_guesses_by(self, *, user_name: str | None = None, race_name: str | None = None) -> RaceGuess | List[RaceGuess] | Dict[str, Dict[str, RaceGuess]] | None:
# List of all guesses by a single user
if user_name is not None and race_name is None:
@ -227,7 +218,7 @@ class Model():
#
@overload
def season_guesses_by(self, *, user_name: str) -> SeasonGuess:
def season_guesses_by(self, *, user_name: str) -> SeasonGuess | None:
"""
Returns the season guess made by a specific user.
"""
@ -240,6 +231,7 @@ class Model():
"""
return self.season_guesses_by()
@cache.memoize(timeout=None, args_to_ignore=["self"]) # Cleanup when adding/updating season guesses or users
def season_guesses_by(self, *, user_name: str | None = None) -> SeasonGuess | Dict[str, SeasonGuess] | None:
if user_name is not None:
predicate: Callable[[SeasonGuess], bool] = lambda guess: guess.user.name == user_name
@ -256,38 +248,53 @@ class Model():
raise Exception("season_guesses_by encountered illegal combination of arguments")
#
# Season guess result queries
#
def season_guess_result_by(self, *, user_name: str) -> SeasonGuessResult | None:
predicate: Callable[[SeasonGuessResult], bool] = lambda guess: guess.user.name == user_name
return find_single_or_none_strict(predicate, self.all_season_guess_results())
#
# Team queries
#
def none_team(self) -> Team:
@staticmethod
def none_team() -> Team:
return NONE_TEAM
#
# Driver queries
#
def none_driver(self) -> Driver:
@staticmethod
def none_driver() -> Driver:
return NONE_DRIVER
@overload
def drivers_by(self, *, team_name: str) -> List[Driver]:
def drivers_by(self, *, team_name: str, include_inactive: bool) -> List[Driver]:
"""
Returns a list of all drivers driving for a certain team.
"""
return self.drivers_by(team_name=team_name)
return self.drivers_by(team_name=team_name, include_inactive=include_inactive)
@overload
def drivers_by(self) -> Dict[str, List[Driver]]:
def drivers_by(self, *, include_inactive: bool) -> Dict[str, List[Driver]]:
"""
Returns a dictionary of drivers mapped to team names.
"""
return self.drivers_by()
return self.drivers_by(include_inactive=include_inactive)
def drivers_by(self, *, team_name: str | None = None) -> List[Driver] | Dict[str, List[Driver]]:
@cache.memoize(timeout=None, args_to_ignore=["self"]) # No Cleanup, data added manually
def drivers_by(self, *, team_name: str | None = None, include_inactive: bool) -> List[Driver] | Dict[str, List[Driver]]:
if team_name is not None:
predicate: Callable[[Driver], bool] = lambda driver: driver.team.name == team_name
return find_multiple_strict(predicate, self.all_drivers(include_none=False), 2)
if include_inactive:
return find_atleast_strict(predicate, self.all_drivers(include_none=False, include_inactive=True), 2)
else:
return find_multiple_strict(predicate, self.all_drivers(include_none=False, include_inactive=False), 2)
if team_name is None:
drivers_by: Dict[str, List[Driver]] = dict()
@ -296,7 +303,7 @@ class Model():
for team in self.all_teams(include_none=False):
drivers_by[team.name] = []
for driver in self.all_drivers(include_none=False):
for driver in self.all_drivers(include_none=False, include_inactive=include_inactive):
drivers_by[driver.team.name] += [driver]
return drivers_by

View File

@ -4,33 +4,47 @@ from formula10.database.model.db_driver import DbDriver
from formula10.domain.model.team import NONE_TEAM, Team
class Driver():
class Driver:
@classmethod
def from_db_driver(cls, db_driver: DbDriver):
driver: Driver = cls()
driver.id = db_driver.id
driver.name = db_driver.name
driver.abbr = db_driver.abbr
driver.country = db_driver.country_code
driver.team = Team.from_db_team(db_driver.team)
driver.active = db_driver.active
return driver
def to_db_driver(self) -> DbDriver:
db_driver: DbDriver = DbDriver(name=self.name)
db_driver: DbDriver = DbDriver(id=self.id)
db_driver.name = self.name
db_driver.abbr = self.abbr
db_driver.country_code = self.country
db_driver.team_name = self.team.name
db_driver.team_id = self.team.name
db_driver.active = self.active
return db_driver
def __eq__(self, __value: object) -> bool:
if isinstance(__value, Driver):
return self.name == __value.name
return self.id == __value.id
return NotImplemented
def __hash__(self) -> int:
return hash(self.id)
# This is important to memoize functions getting a Driver as input.
# The repr() will be appended to the cache key.
def __repr__(self) -> str:
return f"Driver(id={self.id}, name={self.name})"
id: int
name: str
abbr: str
country: str
team: Team
active: bool
@property
def name_sanitized(self) -> str:
@ -38,7 +52,9 @@ class Driver():
NONE_DRIVER: Driver = Driver()
NONE_DRIVER.id = 0
NONE_DRIVER.name = "None"
NONE_DRIVER.abbr = "None"
NONE_DRIVER.country = "NO"
NONE_DRIVER.team = NONE_TEAM
NONE_DRIVER.active = True

View File

@ -4,33 +4,48 @@ from urllib.parse import quote
from formula10.database.model.db_race import DbRace
class Race():
class Race:
@classmethod
def from_db_race(cls, db_race: DbRace):
race: Race = cls()
race.id = db_race.id
race.name = db_race.name
race.number = db_race.number
race.date = db_race.date
race.place_to_guess = db_race.pxx
race.quali_date = db_race.quali_date
race.has_sprint = db_race.has_sprint
return race
def to_db_race(self) -> DbRace:
db_race: DbRace = DbRace(name=self.name,
number=self.number,
date=self.date,
pxx=self.place_to_guess)
db_race: DbRace = DbRace(id=self.id)
db_race.name = self.name
db_race.number = self.number
db_race.date = self.date
db_race.pxx = self.place_to_guess
db_race.quali_date = self.date
db_race.has_sprint = self.has_sprint
return db_race
def __eq__(self, __value: object) -> bool:
if isinstance(__value, Race):
return self.name == __value.name
return self.id == __value.id
return NotImplemented
def __hash__(self) -> int:
return hash(self.id)
def __repr__(self) -> str:
return f"race(\n\tid={self.id}, name={self.name}, number={self.number},\n\tdate={self.date}, quali_date={self.quali_date},\n\thas_sprint={self.has_sprint}, place_to_guess={self.place_to_guess}\n)"
id: int
name: str
number: int
date: datetime
place_to_guess: int
quali_date: datetime
has_sprint: bool
@property
def name_sanitized(self) -> str:

View File

@ -4,7 +4,7 @@ from formula10.domain.model.race import Race
from formula10.domain.model.user import User
class RaceGuess():
class RaceGuess:
@classmethod
def from_db_race_guess(cls, db_race_guess: DbRaceGuess):
race_guess: RaceGuess = cls()
@ -15,10 +15,9 @@ class RaceGuess():
return race_guess
def to_db_race_guess(self) -> DbRaceGuess:
db_race_guess: DbRaceGuess = DbRaceGuess(user_name=self.user.name,
race_name=self.race.name,
pxx_driver_name=self.pxx_guess.name,
dnf_driver_name=self.dnf_guess.name)
db_race_guess: DbRaceGuess = DbRaceGuess(user_id=self.user.id, race_id=self.race.id)
db_race_guess.pxx_driver_id = self.pxx_guess.id
db_race_guess.dnf_driver_id = self.dnf_guess.id
return db_race_guess
def __eq__(self, __value: object) -> bool:
@ -27,6 +26,9 @@ class RaceGuess():
return NotImplemented
def __hash__(self) -> int:
return hash((self.user, self.race))
user: User
race: Race
pxx_guess: Driver

View File

@ -12,30 +12,41 @@ class RaceResult:
def from_db_race_result(cls, db_race_result: DbRaceResult):
race_result: RaceResult = cls()
race_result.race = Race.from_db_race(db_race_result.race)
race_result.fastest_lap_driver = Driver.from_db_driver(db_race_result.fastest_lap_driver)
# Deserialize from json
standing: Dict[str, str] = json.loads(db_race_result.pxx_driver_names_json)
initial_dnf: List[str] = json.loads(db_race_result.first_dnf_driver_names_json)
all_dnfs: List[str] = json.loads(db_race_result.dnf_driver_names_json)
standing_exclusions: List[str] = json.loads(db_race_result.excluded_driver_names_json)
standing: Dict[str, str] = json.loads(db_race_result.pxx_driver_ids_json)
initial_dnf: List[str] = json.loads(db_race_result.first_dnf_driver_ids_json)
all_dnfs: List[str] = json.loads(db_race_result.dnf_driver_ids_json)
standing_exclusions: List[str] = json.loads(db_race_result.excluded_driver_ids_json)
sprint_dnfs: List[str] = json.loads(db_race_result.sprint_dnf_driver_ids_json)
sprint_standing: Dict[str, str] = json.loads(db_race_result.sprint_points_json)
# Populate relationships
race_result.standing = {
position: Driver.from_db_driver(find_single_driver_strict(driver_name))
for position, driver_name in standing.items()
position: Driver.from_db_driver(find_single_driver_strict(int(driver_id)))
for position, driver_id in standing.items()
}
race_result.initial_dnf = [
Driver.from_db_driver(find_single_driver_strict(driver_name))
for driver_name in initial_dnf
Driver.from_db_driver(find_single_driver_strict(int(driver_id)))
for driver_id in initial_dnf
]
race_result.all_dnfs = [
Driver.from_db_driver(find_single_driver_strict(driver_name))
for driver_name in all_dnfs
Driver.from_db_driver(find_single_driver_strict(int(driver_id)))
for driver_id in all_dnfs
]
race_result.standing_exclusions = [
Driver.from_db_driver(find_single_driver_strict(driver_name))
for driver_name in standing_exclusions
Driver.from_db_driver(find_single_driver_strict(int(driver_id)))
for driver_id in standing_exclusions
]
race_result.sprint_dnfs = [
Driver.from_db_driver(find_single_driver_strict(int(driver_id)))
for driver_id in sprint_dnfs
]
race_result.sprint_standing = {
position: Driver.from_db_driver(find_single_driver_strict(int(driver_id)))
for position, driver_id in sprint_standing.items()
}
return race_result
@ -45,21 +56,30 @@ class RaceResult:
position: driver.name for position, driver in self.standing.items()
}
initial_dnf: List[str] = [
driver.name for driver in self.initial_dnf if driver
str(driver.id) for driver in self.initial_dnf if driver
]
all_dnfs: List[str] = [
driver.name for driver in self.all_dnfs if driver
str(driver.id) for driver in self.all_dnfs if driver
]
standing_exclusions: List[str] = [
driver.name for driver in self.standing_exclusions if driver
str(driver.id) for driver in self.standing_exclusions if driver
]
sprint_dnfs: List[str] = [
str(driver.id) for driver in self.sprint_dnfs if driver
]
sprint_standing: Dict[str, str] = {
position: driver.name for position, driver in self.sprint_standing.items()
}
# Serialize to json
db_race_result: DbRaceResult = DbRaceResult(race_name=self.race.name,
pxx_driver_names_json=json.dumps(standing),
first_dnf_driver_names_json=json.dumps(initial_dnf),
dnf_driver_names_json=json.dumps(all_dnfs),
excluded_driver_names_json=json.dumps(standing_exclusions))
db_race_result: DbRaceResult = DbRaceResult(race_id=self.race.id)
db_race_result.pxx_driver_ids_json = json.dumps(standing)
db_race_result.first_dnf_driver_ids_json = json.dumps(initial_dnf)
db_race_result.dnf_driver_ids_json = json.dumps(all_dnfs)
db_race_result.excluded_driver_ids_json = json.dumps(standing_exclusions)
db_race_result.fastest_lap_id = self.fastest_lap_driver.id
db_race_result.sprint_dnf_driver_ids_json = json.dumps(sprint_dnfs)
db_race_result.sprint_points_json = json.dumps(sprint_standing)
return db_race_result
@ -69,12 +89,19 @@ class RaceResult:
return NotImplemented
def __hash__(self) -> int:
return hash(self.race)
race: Race
standing: Dict[str, Driver] # Always contains all 20 drivers, even if DNF'ed or excluded
initial_dnf: List[Driver] # initial_dnf is empty if no-one DNF'ed
all_dnfs: List[Driver]
standing_exclusions: List[Driver]
fastest_lap_driver: Driver
sprint_dnfs: List[Driver]
sprint_standing: Dict[str, Driver]
def offset_from_place_to_guess(self, offset: int, respect_nc:bool = True) -> Driver:
position: str = str(self.race.place_to_guess + offset)
@ -127,6 +154,8 @@ class RaceResult:
return points_strings[position_offset]
else:
return "0 Points"
elif driver == _driver and driver in self.standing_exclusions:
return "0 Points"
raise Exception(f"Could not get points string for driver {driver.name}")
@ -147,6 +176,11 @@ class RaceResult:
self.standing[str(position)] for position in range(1, 21)
]
def ordered_sprint_standing_list(self) -> List[Driver]:
return [
self.sprint_standing[str(position)] for position in range(1, 21)
]
def initial_dnf_string(self) -> str:
if len(self.initial_dnf) == 0:
return NONE_DRIVER.name

View File

@ -7,7 +7,7 @@ from formula10.domain.model.team import Team
from formula10.domain.model.user import User
class SeasonGuess():
class SeasonGuess:
@classmethod
def from_db_season_guess(cls, db_season_guess: DbSeasonGuess):
season_guess: SeasonGuess = cls()
@ -20,17 +20,17 @@ class SeasonGuess():
season_guess.most_wdc_lost = Driver.from_db_driver(db_season_guess.lost_driver) if db_season_guess.lost_driver is not None else None
# Deserialize from json
team_winners: List[str | None] = json.loads(db_season_guess.team_winners_driver_names_json)
podiums: List[str] = json.loads(db_season_guess.podium_drivers_driver_names_json)
team_winners: List[str | None] = json.loads(db_season_guess.team_winners_driver_ids_json)
podiums: List[str] = json.loads(db_season_guess.podium_drivers_driver_ids_json)
# Populate relationships
season_guess.team_winners = [
Driver.from_db_driver(find_single_driver_strict(driver_name)) if driver_name is not None else None
for driver_name in team_winners
Driver.from_db_driver(find_single_driver_strict(int(driver_id))) if driver_id is not None else None
for driver_id in team_winners
]
season_guess.podiums = [
Driver.from_db_driver(find_single_driver_strict(driver_name))
for driver_name in podiums
Driver.from_db_driver(find_single_driver_strict(int(driver_id)))
for driver_id in podiums
]
return season_guess
@ -46,19 +46,27 @@ class SeasonGuess():
]
# Serialize to json
db_season_guess: DbSeasonGuess = DbSeasonGuess(user_name=self.user.name,
team_winners_driver_names_json=json.dumps(team_winners),
podium_drivers_driver_names_json=json.dumps(podiums))
db_season_guess.user_name = self.user.name
db_season_guess: DbSeasonGuess = DbSeasonGuess(user_id=self.user.id)
db_season_guess.hot_take = self.hot_take
db_season_guess.p2_team_name = self.p2_wcc.name if self.p2_wcc is not None else None
db_season_guess.overtake_driver_name = self.most_overtakes.name if self.most_overtakes is not None else None
db_season_guess.dnf_driver_name = self.most_dnfs.name if self.most_dnfs is not None else None
db_season_guess.gained_driver_name = self.most_wdc_gained.name if self.most_wdc_gained is not None else None
db_season_guess.lost_driver_name = self.most_wdc_lost.name if self.most_wdc_lost is not None else None
db_season_guess.p2_team_id = self.p2_wcc.id if self.p2_wcc is not None else None
db_season_guess.overtake_driver_id = self.most_overtakes.id if self.most_overtakes is not None else None
db_season_guess.dnf_driver_id = self.most_dnfs.id if self.most_dnfs is not None else None
db_season_guess.gained_driver_id = self.most_wdc_gained.id if self.most_wdc_gained is not None else None
db_season_guess.lost_driver_id = self.most_wdc_lost.id if self.most_wdc_lost is not None else None
db_season_guess.team_winners_driver_ids_json=json.dumps(team_winners)
db_season_guess.podium_drivers_driver_ids_json=json.dumps(podiums)
return db_season_guess
def __eq__(self, __value: object) -> bool:
if isinstance(__value, SeasonGuess):
return self.user == __value.user
return NotImplemented
def __hash__(self) -> int:
return hash(self.user)
user: User
hot_take: str | None
p2_wcc: Team | None

View File

@ -0,0 +1,32 @@
from formula10.database.model.db_season_guess_result import DbSeasonGuessResult
from formula10.domain.model.user import User
class SeasonGuessResult:
@classmethod
def from_db_season_guess_result(cls, db_season_guess_result: DbSeasonGuessResult):
season_guess_result: SeasonGuessResult = cls()
season_guess_result.user = User.from_db_user(db_season_guess_result.user)
season_guess_result.hot_take_correct = db_season_guess_result.hot_take_correct
season_guess_result.overtakes_correct = db_season_guess_result.overtakes_correct
return season_guess_result
def to_db_season_guess_result(self) -> DbSeasonGuessResult:
db_season_guess_result: DbSeasonGuessResult = DbSeasonGuessResult(user_id=self.user.id)
db_season_guess_result.hot_take_correct = self.hot_take_correct
db_season_guess_result.overtakes_correct = self.overtakes_correct
return db_season_guess_result
def __eq__(self, __value: object) -> bool:
if isinstance(__value, SeasonGuessResult):
return self.user == __value.user
return NotImplemented
def __hash__(self) -> int:
return hash(self.user)
user: User
hot_take_correct: bool
overtakes_correct: bool

View File

@ -3,23 +3,29 @@ from urllib.parse import quote
from formula10.database.model.db_team import DbTeam
class Team():
class Team:
@classmethod
def from_db_team(cls, db_team: DbTeam):
team: Team = cls()
team.id = db_team.id
team.name = db_team.name
return team
def to_db_team(self) -> DbTeam:
db_team: DbTeam = DbTeam(name=self.name)
db_team: DbTeam = DbTeam(id=self.id)
db_team.name = self.name
return db_team
def __eq__(self, __value: object) -> bool:
if isinstance(__value, Team):
return self.name == __value.name
return self.id == __value.id
return NotImplemented
def __hash__(self) -> int:
return hash(self.id)
id: int
name: str
@property
@ -27,4 +33,5 @@ class Team():
return quote(self.name)
NONE_TEAM: Team = Team()
NONE_TEAM.id = 0
NONE_TEAM.name = "None"

View File

@ -3,24 +3,31 @@ from urllib.parse import quote
from formula10.database.model.db_user import DbUser
class User():
class User:
@classmethod
def from_db_user(cls, db_user: DbUser):
user: User = cls()
user.id = db_user.id
user.name = db_user.name
user.enabled = db_user.enabled
return user
def to_db_user(self) -> DbUser:
db_user: DbUser = DbUser(name=self.name, enabled=self.enabled)
db_user: DbUser = DbUser(id=self.id)
db_user.name = self.name
db_user.enabled = self.enabled
return db_user
def __eq__(self, __value: object) -> bool:
if isinstance(__value, User):
return self.name == __value.name
return self.id == __value.id
return NotImplemented
def __hash__(self) -> int:
return hash(self.id)
id: int
name: str
enabled: bool

View File

@ -1,19 +1,22 @@
from typing import Dict, List, overload
import json
from typing import Any, Callable, Dict, List, overload, Tuple
import numpy as np
from formula10 import cache
from formula10.domain.domain_model import Model
from formula10.domain.model.driver import NONE_DRIVER
from formula10.domain.model.driver import NONE_DRIVER, Driver
from formula10.domain.model.race_guess import RaceGuess
from formula10.domain.model.race_result import RaceResult
from formula10.domain.model.season_guess import SeasonGuess
from formula10.domain.model.season_guess_result import SeasonGuessResult
from formula10.domain.model.team import Team
from formula10.domain.model.user import User
from formula10.database.validation import find_single_or_none_strict
RACE_GUESS_OFFSET_POINTS: Dict[int, int] = {
3: 1,
2: 3,
1: 6,
0: 10
}
# Guess points
RACE_GUESS_OFFSET_POINTS: Dict[int, int] = {3: 1, 2: 3, 1: 6, 0: 10}
RACE_GUESS_DNF_POINTS: int = 10
SEASON_GUESS_HOT_TAKE_POINTS: int = 10
SEASON_GUESS_P2_POINTS: int = 10
SEASON_GUESS_OVERTAKES_POINTS: int = 10
@ -25,7 +28,26 @@ SEASON_GUESS_TEAMWINNER_FALSE_POINTS: int = -3
SEASON_GUESS_PODIUMS_CORRECT_POINTS: int = 3
SEASON_GUESS_PODIUMS_FALSE_POINTS: int = -2
STANDING_2023: Dict[str, int] = {
# Driver points
DRIVER_RACE_POINTS: Dict[int, int] = {
1: 25,
2: 18,
3: 15,
4: 12,
5: 10,
6: 8,
7: 6,
8: 4,
9: 2,
10: 1,
}
DRIVER_SPRINT_POINTS: Dict[int, int] = {1: 8, 2: 7, 3: 6, 4: 5, 5: 4, 6: 3, 7: 2, 8: 1}
DRIVER_FASTEST_LAP_POINTS: int = 1
# Last season results
WDC_STANDING_2023: Dict[str, int] = {
"Max Verstappen": 1,
"Sergio Perez": 2,
"Lewis Hamilton": 3,
@ -33,7 +55,7 @@ STANDING_2023: Dict[str, int] = {
"Charles Leclerc": 5,
"Lando Norris": 6,
"Carlos Sainz": 7,
"George Russel": 8,
"George Russell": 8,
"Oscar Piastri": 9,
"Lance Stroll": 10,
"Pierre Gasly": 11,
@ -45,11 +67,60 @@ STANDING_2023: Dict[str, int] = {
"Daniel Ricciardo": 17,
"Zhou Guanyu": 18,
"Kevin Magnussen": 19,
"Logan Sargeant": 21
"Logan Sargeant": 21,
}
WCC_STANDING_2023: Dict[str, int] = {
"Red Bull": 1,
"Mercedes": 2,
"Ferrari": 3,
"McLaren": 4,
"Aston Martin": 5,
"Alpine": 6,
"Williams": 7,
"VCARB": 8,
"Sauber": 9,
"Haas": 10,
}
# In case a substitute driver is driving, those points have to be subtracted from the actual driver
# (Driver_ID, Race_ID, Points)
WDC_SUBSTITUTE_POINTS: List[Tuple[int, int, int]] = [
(15, 2, 6), # Bearman raced for Sainz in Saudi Arabia
(8, 17, 1), # Bearman raced for Magnussen in Azerbaijan
]
WDC_STANDING_2024: Dict[str, int] = {
"Max Verstappen": 1,
"Lando Norris": 2,
"Charles Leclerc": 3,
"Oscar Piastri": 4,
"Carlos Sainz": 5,
"George Russell": 6,
"Lewis Hamilton": 7,
"Sergio Perez": 8,
"Fernando Alonso": 9,
"Pierre Gasly": 10,
"Nico Hulkenberg": 11,
"Yuki Tsunoda": 12,
"Lance Stroll": 13,
"Esteban Ocon": 14,
"Kevin Magnussen": 15,
"Alexander Albon": 16,
"Daniel Ricciardo": 17,
"Oliver Bearman": 18,
"Franco Colapinto": 19,
"Zhou Guanyu": 20,
"Liam Lawson": 21,
"Valtteri Bottas": 22,
"Logan Sargeant": 23,
"Jack Doohan": 24
}
def standing_points(race_guess: RaceGuess, race_result: RaceResult) -> int:
guessed_driver_position: int | None = race_result.driver_standing_position(driver=race_guess.pxx_guess)
guessed_driver_position: int | None = race_result.driver_standing_position(
driver=race_guess.pxx_guess
)
if guessed_driver_position is None:
return 0
@ -59,6 +130,7 @@ def standing_points(race_guess: RaceGuess, race_result: RaceResult) -> int:
return RACE_GUESS_OFFSET_POINTS[position_offset]
def dnf_points(race_guess: RaceGuess, race_result: RaceResult) -> int:
if race_guess.dnf_guess in race_result.initial_dnf:
return RACE_GUESS_DNF_POINTS
@ -68,36 +140,472 @@ def dnf_points(race_guess: RaceGuess, race_result: RaceResult) -> int:
return 0
def substitute_points(driver: Driver, race_number: int) -> int:
predicate: Callable[[Tuple[int, int, int]], bool] = lambda substitution: driver.id == substitution[0] and race_number == substitution[1]
substitution: Tuple[int, int, int] = find_single_or_none_strict(predicate, WDC_SUBSTITUTE_POINTS)
if substitution is not None:
return substitution[2]
else:
return 0
class PointsModel(Model):
"""
This class bundles all data + functionality required to do points calculations.
"""
_points_per_step: Dict[str, List[int]] | None = None
def __init__(self):
Model.__init__(self)
@cache.cached(
timeout=None, key_prefix="points_points_per_step"
) # Clear when adding/updating race results or users
def points_per_step(self) -> Dict[str, List[int]]:
"""
Returns a dictionary of lists, containing points per race for each user.
"""
if self._points_per_step is None:
self._points_per_step = dict()
points_per_step = dict()
for user in self.all_users():
self._points_per_step[user.name] = [0] * 24 # Start at index 1, like the race numbers
points_per_step[user.name] = [0] * (
len(self.all_races()) + 1
) # Start at index 1, like the race numbers
for race_guess in self.all_race_guesses():
user_name: str = race_guess.user.name
race_number: int = race_guess.race.number
race_result: RaceResult | None = self.race_result_by(race_name=race_guess.race.name)
race_result: RaceResult | None = self.race_result_by(
race_name=race_guess.race.name
)
if race_result is None:
continue
self._points_per_step[user_name][race_number] = standing_points(race_guess, race_result) + dnf_points(race_guess, race_result)
points_per_step[user_name][race_number] = standing_points(
race_guess, race_result
) + dnf_points(race_guess, race_result)
return self._points_per_step
return points_per_step
@cache.memoize(
timeout=None, args_to_ignore=["self"]
) # Clear when adding/updating race results
def driver_points_per_step(self, *, include_inactive: bool) -> Dict[str, List[int]]:
"""
Returns a dictionary of lists, containing points per race for each driver.
"""
driver_points_per_step = dict()
for driver in self.all_drivers(
include_none=False, include_inactive=include_inactive
):
driver_points_per_step[driver.name] = [0] * (
len(self.all_races()) + 1
) # Start at index 1, like the race numbers
for race_result in self.all_race_results():
race_number: int = race_result.race.number
for position, driver in race_result.standing.items():
driver_points_per_step[driver.name][race_number] = (
DRIVER_RACE_POINTS[int(position)]
if int(position) in DRIVER_RACE_POINTS
else 0
)
driver_points_per_step[driver.name][race_number] += (
DRIVER_FASTEST_LAP_POINTS
if race_result.fastest_lap_driver == driver
and int(position) <= 10
else 0
)
driver_points_per_step[driver.name][race_number] -= substitute_points(driver, race_number)
for position, driver in race_result.sprint_standing.items():
driver_name: str = driver.name
driver_points_per_step[driver_name][race_number] += (
DRIVER_SPRINT_POINTS[int(position)]
if int(position) in DRIVER_SPRINT_POINTS
else 0
)
return driver_points_per_step
@cache.cached(timeout=None, key_prefix="points_team_points_per_step")
def team_points_per_step(self) -> Dict[str, List[int]]:
"""
Returns a dictionary of lists, containing points per race for each team.
"""
team_points_per_step = dict()
for team in self.all_teams(include_none=False):
team_points_per_step[team.name] = [0] * (
len(self.all_races()) + 1
) # Start at index 1, like the race numbers
for race_result in self.all_race_results():
for driver in race_result.standing.values():
team_name: str = driver.team.name
race_number: int = race_result.race.number
team_points_per_step[team_name][
race_number
] += self.driver_points_per_step(include_inactive=True)[driver.name][
race_number
]
return team_points_per_step
@cache.cached(timeout=None, key_prefix="points_dnfs")
def dnfs(self) -> Dict[str, int]:
dnfs = dict()
for driver in self.all_drivers(include_none=False, include_inactive=True):
dnfs[driver.name] = 0
for race_result in self.all_race_results():
for driver in race_result.all_dnfs:
dnfs[driver.name] += 1
for driver in race_result.sprint_dnfs:
dnfs[driver.name] += 1
return dnfs
#
# Driver stats
#
@cache.cached(
timeout=None, key_prefix="points_driver_points_per_step_cumulative"
) # Cleanup when adding/updating race results
def driver_points_per_step_cumulative(self) -> Dict[str, List[int]]:
"""
Returns a dictionary of lists, containing cumulative points per race for each driver.
"""
points_per_step_cumulative: Dict[str, List[int]] = dict()
for driver_name, points in self.driver_points_per_step(
include_inactive=True
).items():
points_per_step_cumulative[driver_name] = np.cumsum(points).tolist()
return points_per_step_cumulative
@overload
def driver_points_by(
self, *, driver_name: str, include_inactive: bool
) -> List[int]:
"""
Returns a list of points per race for a specific driver.
"""
return self.driver_points_by(
driver_name=driver_name, include_inactive=include_inactive
)
@overload
def driver_points_by(
self, *, race_name: str, include_inactive: bool
) -> Dict[str, int]:
"""
Returns a dictionary of points per driver for a specific race.
"""
return self.driver_points_by(
race_name=race_name, include_inactive=include_inactive
)
@overload
def driver_points_by(
self, *, driver_name: str, race_name: str, include_inactive: bool
) -> int:
"""
Returns the points for a specific race for a specific driver.
"""
return self.driver_points_by(
driver_name=driver_name,
race_name=race_name,
include_inactive=include_inactive,
)
@cache.memoize(
timeout=None, args_to_ignore=["self"]
) # Cleanup when adding/updating race results
def driver_points_by(
self,
*,
driver_name: str | None = None,
race_name: str | None = None,
include_inactive: bool
) -> List[int] | Dict[str, int] | int:
if driver_name is not None and race_name is None:
return self.driver_points_per_step(include_inactive=include_inactive)[
driver_name
]
if driver_name is None and race_name is not None:
race_number: int = self.race_by(race_name=race_name).number
points_by_race: Dict[str, int] = dict()
for _driver_name, points in self.driver_points_per_step(
include_inactive=include_inactive
).items():
points_by_race[_driver_name] = points[race_number]
return points_by_race
if driver_name is not None and race_name is not None:
race_number: int = self.race_by(race_name=race_name).number
return self.driver_points_per_step(include_inactive=include_inactive)[
driver_name
][race_number]
raise Exception("driver_points_by received an illegal combination of arguments")
@cache.memoize(
timeout=None, args_to_ignore=["self"]
) # Cleanup when adding/updating race results
def total_driver_points_by(self, driver_name: str) -> int:
return sum(
self.driver_points_by(driver_name=driver_name, include_inactive=True)
)
@cache.memoize(
timeout=None, args_to_ignore=["self"]
) # Cleanup when adding/updating race results
def drivers_sorted_by_points(self, *, include_inactive: bool) -> List[Driver]:
comparator: Callable[[Driver], int] = (
lambda driver: self.total_driver_points_by(driver.name)
)
return sorted(
self.all_drivers(include_none=False, include_inactive=include_inactive),
key=comparator,
reverse=True,
)
@cache.cached(
timeout=None, key_prefix="points_wdc_standing_by_position"
) # Cleanup when adding/updating race results
def wdc_standing_by_position(self) -> Dict[int, List[str]]:
standing: Dict[int, List[str]] = dict()
if WDC_STANDING_2024 is None:
for position in range(
1, len(self.all_drivers(include_none=False, include_inactive=True)) + 1
):
standing[position] = list()
position: int = 1
last_points: int = 0
for driver in self.drivers_sorted_by_points(include_inactive=True):
points: int = self.total_driver_points_by(driver.name)
if points < last_points:
# If multiple drivers have equal points, a place is shared.
# In this case, the next driver does not occupy the immediate next position.
position += len(standing[position])
standing[position].append(driver.name)
last_points = points
if WDC_STANDING_2024 is not None:
for position in range(1, len(WDC_STANDING_2024) + 1):
standing[position] = list()
for driver, position in WDC_STANDING_2024.items():
standing[position] += [driver]
return standing
@cache.cached(
timeout=None, key_prefix="points_wdc_standing_by_driver"
) # Cleanup when adding/updating race results
def wdc_standing_by_driver(self) -> Dict[str, int]:
standing: Dict[str, int] = dict()
if WDC_STANDING_2024 is None:
position: int = 1
last_points: int = 0
for driver in self.drivers_sorted_by_points(include_inactive=True):
points: int = self.total_driver_points_by(driver.name)
if points < last_points:
drivers_with_this_position = 0
for _driver, _position in standing.items():
if _position == position:
drivers_with_this_position += 1
# If multiple drivers have equal points, a place is shared.
# In this case, the next driver does not occupy the immediate next position.
position += drivers_with_this_position
standing[driver.name] = position
last_points = points
return standing
if WDC_STANDING_2024 is not None:
return WDC_STANDING_2024
def wdc_diff_2023_by(self, driver_name: str) -> int:
if not driver_name in WDC_STANDING_2023:
return 0
return (
WDC_STANDING_2023[driver_name] - self.wdc_standing_by_driver()[driver_name]
)
@cache.cached(
timeout=None, key_prefix="points_most_dnf_names"
) # Cleanup when adding/updating race results
def most_dnf_names(self) -> List[str]:
dnf_names: List[str] = list()
most_dnfs: int = 0
for dnfs in self.dnfs().values():
if dnfs > most_dnfs:
most_dnfs = dnfs
for driver_name, dnfs in self.dnfs().items():
if dnfs == most_dnfs:
dnf_names.append(driver_name)
return dnf_names
@cache.cached(
timeout=None, key_prefix="points_most_gained_names"
) # Cleanup when adding/updating race results
def most_gained_names(self) -> List[str]:
most_gained_names: List[str] = list()
most_gained: int = 0
for driver in self.all_drivers(include_none=False, include_inactive=True):
gained: int = self.wdc_diff_2023_by(driver.name)
if gained > most_gained:
most_gained = gained
for driver in self.all_drivers(include_none=False, include_inactive=True):
gained: int = self.wdc_diff_2023_by(driver.name)
if gained == most_gained:
most_gained_names.append(driver.name)
return most_gained_names
@cache.cached(
timeout=None, key_prefix="points_most_lost_names"
) # Cleanup when adding/updating race results
def most_lost_names(self) -> List[str]:
most_lost_names: List[str] = list()
most_lost: int = 100
for driver in self.all_drivers(include_none=False, include_inactive=True):
lost: int = self.wdc_diff_2023_by(driver.name)
if lost < most_lost:
most_lost = lost
for driver in self.all_drivers(include_none=False, include_inactive=True):
lost: int = self.wdc_diff_2023_by(driver.name)
if lost == most_lost:
most_lost_names.append(driver.name)
return most_lost_names
#
# Team points
#
@cache.cached(
timeout=None, key_prefix="points_team_points_per_step_cumulative"
) # Cleanup when adding/updating race results
def team_points_per_step_cumulative(self) -> Dict[str, List[int]]:
"""
Returns a dictionary of lists, containing cumulative points per race for each team.
"""
points_per_step_cumulative: Dict[str, List[int]] = dict()
for team_name, points in self.team_points_per_step().items():
points_per_step_cumulative[team_name] = np.cumsum(points).tolist()
return points_per_step_cumulative
@cache.memoize(
timeout=None, args_to_ignore=["self"]
) # Cleanup when adding/updating race results
def total_team_points_by(self, team_name: str) -> int:
teammates: List[Driver] = self.drivers_by(
team_name=team_name, include_inactive=True
)
return sum(
sum(self.driver_points_by(driver_name=teammate.name, include_inactive=True))
for teammate in teammates
)
@cache.cached(
timeout=None, key_prefix="points_teams_sorted_by_points"
) # Cleanup when adding/updating race results
def teams_sorted_by_points(self) -> List[Team]:
comparator: Callable[[Team], int] = lambda team: self.total_team_points_by(
team.name
)
return sorted(self.all_teams(include_none=False), key=comparator, reverse=True)
@cache.cached(
timeout=None, key_prefix="points_wcc_standing_by_position"
) # Cleanup when adding/updating race results
def wcc_standing_by_position(self) -> Dict[int, List[str]]:
standing: Dict[int, List[str]] = dict()
for position in range(1, len(self.all_teams(include_none=False)) + 1):
standing[position] = list()
position: int = 1
last_points: int = 0
for team in self.teams_sorted_by_points():
points: int = self.total_team_points_by(team.name)
if points < last_points:
# If multiple teams have equal points, a place is shared.
# In this case, the next team does not occupy the immediate next position.
position += len(standing[position])
standing[position].append(team.name)
last_points = points
return standing
@cache.cached(
timeout=None, key_prefix="points_wcc_standing_by_team"
) # Cleanup when adding/updating race results
def wcc_standing_by_team(self) -> Dict[str, int]:
standing: Dict[str, int] = dict()
position: int = 1
last_points: int = 0
for team in self.teams_sorted_by_points():
points: int = self.total_team_points_by(team.name)
if points < last_points:
teams_with_this_position = 0
for _team, _position in standing.items():
if _position == position:
teams_with_this_position += 1
# If multiple teams have equal points, a place is shared.
# In this case, the next team does not occupy the immediate next position.
position += teams_with_this_position
standing[team.name] = position
last_points = points
return standing
def wcc_diff_2023_by(self, team_name: str) -> int:
return WCC_STANDING_2023[team_name] - self.wcc_standing_by_team()[team_name]
#
# User stats
#
def points_per_step_cumulative(self) -> Dict[str, List[int]]:
"""
@ -130,7 +638,12 @@ class PointsModel(Model):
"""
return self.points_by(user_name=user_name, race_name=race_name)
def points_by(self, *, user_name: str | None = None, race_name: str | None = None) -> List[int] | Dict[str, int] | int:
@cache.memoize(
timeout=None, args_to_ignore=["self"]
) # Cleanup when adding/updating race results or users
def points_by(
self, *, user_name: str | None = None, race_name: str | None = None
) -> List[int] | Dict[str, int] | int:
if user_name is not None and race_name is None:
return self.points_per_step()[user_name]
@ -150,28 +663,100 @@ class PointsModel(Model):
raise Exception("points_by received an illegal combination of arguments")
def total_points_by(self, user_name: str) -> int:
def season_points_by(self, *, user_name: str) -> int:
"""
Returns the number of points from seasonguesses for a specific user.
"""
big_picks = (int(self.hot_take_correct(user_name=user_name)) * 10
+ int(self.p2_constructor_correct(user_name=user_name)) * 10
+ int(self.overtakes_correct(user_name=user_name)) * 10
+ int(self.dnfs_correct(user_name=user_name)) * 10
+ int(self.most_gained_correct(user_name=user_name)) * 10
+ int(self.most_lost_correct(user_name=user_name)) * 10)
small_picks = 0
guess: SeasonGuess = self.season_guesses_by(user_name=user_name)
for driver in guess.team_winners:
if self.is_team_winner(driver):
small_picks += 3
else:
small_picks -= 3
# NOTE: Not picked drivers that had a podium are also wrong
for driver in self.all_drivers(include_none=False, include_inactive=True):
if driver in guess.podiums and self.has_podium(driver):
small_picks += 3
elif driver in guess.podiums and not self.has_podium(driver):
small_picks -=2
elif driver not in guess.podiums and self.has_podium(driver):
small_picks -=2
return big_picks + small_picks
def total_points_by(self, *, user_name: str, include_season: bool) -> int:
"""
Returns the total number of points for a specific user.
"""
if include_season:
return sum(self.points_by(user_name=user_name)) + self.season_points_by(user_name=user_name)
else:
return sum(self.points_by(user_name=user_name))
def users_sorted_by_points(self, *, include_season: bool) -> List[User]:
"""
Returns the list of users, sorted by their points from race guesses (in descending order).
"""
comparator: Callable[[User], int] = lambda user: self.total_points_by(user_name=user.name, include_season=include_season)
return sorted(self.all_users(), key=comparator, reverse=True)
@cache.cached(
timeout=None, key_prefix="points_user_standing"
) # Cleanup when adding/updating race results or users
def user_standing(self, *, include_season: bool) -> Dict[str, int]:
standing: Dict[str, int] = dict()
position: int = 1
last_points: int = 0
for user in self.users_sorted_by_points(include_season=include_season):
if self.total_points_by(user_name=user.name, include_season=include_season) < last_points:
users_with_this_position = 0
for _user, _position in standing.items():
if _position == position:
users_with_this_position += 1
# If multiple users have equal points, a place is shared.
# In this case, the next user does not occupy the immediate next position.
position += users_with_this_position
standing[user.name] = position
last_points = self.total_points_by(user_name=user.name, include_season=include_season)
return standing
def picks_count(self, user_name: str) -> int:
# Treat standing + dnf picks separately
return len(self.race_guesses_by(user_name=user_name)) * 2
@cache.memoize(
timeout=None, args_to_ignore=["self"]
) # Cleanup when adding/updating race results
def picks_with_points_count(self, user_name: str) -> int:
count: int = 0
for race_guess in self.race_guesses_by(user_name=user_name):
race_result: RaceResult | None = self.race_result_by(race_name=race_guess.race.name)
race_result: RaceResult | None = self.race_result_by(
race_name=race_guess.race.name
)
if race_result is None:
continue
if standing_points(race_guess, race_result) > 0:
count = count + 1
count += 1
if dnf_points(race_guess, race_result) > 0:
count = count + 1
count += 1
return count
@ -179,4 +764,144 @@ class PointsModel(Model):
if self.picks_count(user_name) == 0:
return 0.0
return self.total_points_by(user_name) / self.picks_count(user_name)
return self.total_points_by(user_name=user_name, include_season=False) / self.picks_count(user_name)
#
# Season guess evaluation
#
def hot_take_correct(self, user_name: str) -> bool:
season_guess_result: SeasonGuessResult | None = self.season_guess_result_by(
user_name=user_name
)
return (
season_guess_result.hot_take_correct
if season_guess_result is not None
else False
)
def p2_constructor_correct(self, user_name: str) -> bool:
season_guess: SeasonGuess | None = self.season_guesses_by(user_name=user_name)
if season_guess is None or season_guess.p2_wcc is None:
return False
return season_guess.p2_wcc.name in self.wcc_standing_by_position()[2]
def overtakes_correct(self, user_name: str) -> bool:
season_guess_result: SeasonGuessResult | None = self.season_guess_result_by(
user_name=user_name
)
return (
season_guess_result.overtakes_correct
if season_guess_result is not None
else False
)
def dnfs_correct(self, user_name: str) -> bool:
season_guess: SeasonGuess | None = self.season_guesses_by(user_name=user_name)
if season_guess is None or season_guess.most_dnfs is None:
return False
return season_guess.most_dnfs.name in self.most_dnf_names()
def most_gained_correct(self, user_name: str) -> bool:
season_guess: SeasonGuess | None = self.season_guesses_by(user_name=user_name)
if season_guess is None or season_guess.most_wdc_gained is None:
return False
return season_guess.most_wdc_gained.name in self.most_gained_names()
def most_lost_correct(self, user_name: str) -> bool:
season_guess: SeasonGuess | None = self.season_guesses_by(user_name=user_name)
if season_guess is None or season_guess.most_wdc_lost is None:
return False
return season_guess.most_wdc_lost.name in self.most_lost_names()
@cache.memoize(
timeout=None, args_to_ignore=["self"]
) # Cleanup when adding/updating race results
def is_team_winner(self, driver: Driver) -> bool:
teammates: List[Driver] = self.drivers_by(
team_name=driver.team.name, include_inactive=True
)
# Min - Highest position is the lowest place number
winner: Driver = min(teammates, key=lambda driver: self.wdc_standing_by_driver()[driver.name])
return driver == winner
@cache.memoize(
timeout=None, args_to_ignore=["self"]
) # Cleanup when adding/updating race results
def has_podium(self, driver: Driver) -> bool:
for race_result in self.all_race_results():
position: int | None = race_result.driver_standing_position(driver)
if position is not None and position <= 3:
return True
return False
#
# Diagram queries
#
def cumulative_points_data(self) -> str:
data: Dict[Any, Any] = dict()
data["labels"] = [0] + [
race.name for race in sorted(self.all_races(), key=lambda race: race.number)
] # + ["Season"]
data["datasets"] = [
{
"data": self.points_per_step_cumulative()[user.name], # + [self.total_points_by(user_name=user.name, include_season=True)],
"label": user.name,
"fill": False,
}
for user in self.all_users()
]
return json.dumps(data)
def cumulative_driver_points_data(self) -> str:
data: Dict[Any, Any] = dict()
data["labels"] = [0] + [
race.name for race in sorted(self.all_races(), key=lambda race: race.number)
]
data["datasets"] = [
{
"data": self.driver_points_per_step_cumulative()[driver.name],
"label": driver.abbr,
"fill": False,
}
for driver in self.all_drivers(include_none=False, include_inactive=True)
]
return json.dumps(data)
def cumulative_team_points_data(self) -> str:
data: Dict[Any, Any] = dict()
data["labels"] = [0] + [
race.name for race in sorted(self.all_races(), key=lambda race: race.number)
]
data["datasets"] = [
{
"data": self.team_points_per_step_cumulative()[team.name],
"label": team.name,
"fill": False,
}
for team in self.all_teams(include_none=False)
]
return json.dumps(data)

View File

@ -1,12 +1,12 @@
from typing import List, Callable
from formula10 import ENABLE_TIMING
from formula10 import ENABLE_TIMING, cache
from formula10.domain.domain_model import Model
from formula10.domain.model.driver import Driver
from formula10.domain.model.race import Race
from formula10.domain.model.race_result import RaceResult
from formula10.domain.model.user import User
from formula10.database.validation import find_first_else_none, find_multiple_strict, race_has_started
from formula10.database.validation import find_first_else_none, find_multiple_strict, find_single_strict, race_has_started
class TemplateModel(Model):
@ -29,14 +29,18 @@ class TemplateModel(Model):
if active_result_race_name is not None:
self.active_result = self.race_result_by(race_name=active_result_race_name)
def race_guess_open(self, race: Race) -> bool:
@staticmethod
def race_guess_open(race: Race) -> bool:
return not race_has_started(race=race) if ENABLE_TIMING else True
def season_guess_open(self) -> bool:
return not race_has_started(race_name="Bahrain") if ENABLE_TIMING else True
@staticmethod
def season_guess_open() -> bool:
return not race_has_started(race_id=1) if ENABLE_TIMING else True
def race_result_open(self, race_name: str) -> bool:
return race_has_started(race_name=race_name) if ENABLE_TIMING else True
predicate: Callable[[Race], bool] = lambda race: race.name == race_name
race: Race = find_single_strict(predicate, self.all_races())
return race_has_started(race_id=race.id) if ENABLE_TIMING else True
def active_user_name_or_everyone(self) -> str:
return self.active_user.name if self.active_user is not None else "Everyone"
@ -50,6 +54,7 @@ class TemplateModel(Model):
return self.all_users()
@cache.cached(timeout=None, key_prefix="template_first_race_without_result") # Cleanup when adding/updating race results
def first_race_without_result(self) -> Race | None:
"""
Returns the first race-object with no associated race result.
@ -67,6 +72,14 @@ class TemplateModel(Model):
def current_race(self) -> Race | None:
return self.first_race_without_result()
def active_result_race_or_current_race(self) -> Race:
if self.active_result is not None:
return self.active_result.race
elif self.current_race is not None:
return self.current_race
else:
return self.all_races()[0]
def active_result_race_name_or_current_race_name(self) -> str:
if self.active_result is not None:
return self.active_result.race.name
@ -83,9 +96,12 @@ class TemplateModel(Model):
else:
return self.all_races()[0].name_sanitized
def all_drivers_or_active_result_standing_drivers(self) -> List[Driver]:
return self.active_result.ordered_standing_list() if self.active_result is not None else self.all_drivers(include_none=False)
def all_active_drivers_or_active_result_standing_drivers(self) -> List[Driver]:
return self.active_result.ordered_standing_list() if self.active_result is not None else self.all_drivers(include_none=False, include_inactive=False)
def drivers_for_wdc_gained(self) -> List[Driver]:
def all_active_drivers_or_active_result_sprint_standing_drivers(self) -> List[Driver]:
return self.active_result.ordered_sprint_standing_list() if self.active_result is not None else self.all_drivers(include_none=False, include_inactive=False)
def active_drivers_for_wdc_gained(self) -> List[Driver]:
predicate: Callable[[Driver], bool] = lambda driver: driver.abbr not in self._wdc_gained_excluded_abbrs
return find_multiple_strict(predicate, self.all_drivers(include_none=False))
return find_multiple_strict(predicate, self.all_drivers(include_none=False, include_inactive=True))

View File

View File

@ -0,0 +1,364 @@
# https://docs.fastf1.dev/events.html#event-formats
from typing import Optional
from fastf1.core import Lap, DriverResult, Session
from fastf1.events import Event
from pandas import Timestamp, Timedelta
FASTF1_SESSIONTYPE_NORMAL: str = "conventional"
FASTF1_SESSIONTYPE_SPRINT: str = "sprint_qualifying"
FASTF1_SPRINT_QUALIFYING_SESSION: int = 2
FASTF1_SPRINT_SESSION: int = 3
FASTF1_QUALIFYING_SESSION: int = 4
FASTF1_RACE_SESSION: int = 5
class EventDataHelper:
"""
Helper class that provides easy access to EventDataFrame columns.
"""
def __init__(self, event: Event):
self.__event = event
__event: Event
@property
def round_number(self) -> int:
return self.__event["RoundNumber"]
@property
def country(self) -> str:
return self.__event["Country"]
@property
def location(self) -> str:
return self.__event["Location"]
@property
def official_event_name(self) -> str:
"""
Get the event name including sponsor names etc.
"""
return self.__event["OfficialEventName"]
@property
def event_name(self) -> str:
return self.__event["EventName"]
@property
def event_date(self) -> Timestamp:
return self.__event["EventDate"]
@property
def event_format(self) -> str:
"""
Get the event format.
@return: Either conventional, sprint, sprint_shootout, sprint_qualifying or testing (sprint_qualifying is the 2024 sprint format).
"""
return self.__event["EventFormat"]
def session(self, session_number: int) -> str:
"""
Get the format of a session belonging to this event.
@param session_number: Either 1, 2, 3, 4 or 5
@return: Either Pactice 1, Practice 2, Practice 3, Qualifying, Sprint, Sprint Shootout or Race
"""
return self.__event[f"Session{session_number}"]
def session_date(self, session_number: int) -> Timestamp:
"""
Get the date of a session belonging to this event.
@param session_number: Either 1, 2, 3, 4 or 5
"""
return self.__event[f"Session{session_number}Date"]
def session_date_utc(self, session_number: int) -> Timestamp:
"""
Get the date a session belonging to this event in coordinated universal time.
@param session_number: Either 1, 2, 3, 4 or 5
"""
return self.__event[f"Session{session_number}DateUtc"]
@property
def f1_api_support(self) -> bool:
return self.__event["F1ApiSupport"]
# The following doesn't correspond to DataFrame columns
@property
def sprint_qualifying(self):
return self.__event.get_sprint_qualifying()
@property
def sprint(self):
return self.__event.get_sprint()
@property
def qualifying(self) -> Session:
return self.__event.get_qualifying()
@property
def race(self) -> Session:
return self.__event.get_race()
class LapDataHelper:
"""
Helper class that provides easy access to Lap DataFrame columns.
"""
def __init__(self, lap: Lap):
self.__lap = lap
__lap: Lap
@property
def time(self) -> Timedelta:
return self.__lap["Time"]
@property
def driver(self) -> str:
return self.__lap["Driver"]
@property
def driver_number(self) -> str:
return self.__lap["DriverNumber"]
@property
def lap_time(self) -> Timedelta:
return self.__lap["LapTime"]
@property
def lap_number(self) -> int:
return self.__lap["LapNumber"]
@property
def stint(self) -> int:
return self.__lap["Stint"]
@property
def pit_out_time(self) -> Timedelta:
"""
Get the session time when the car left the pit.
"""
return self.__lap["PitOutTime"]
@property
def pit_in_time(self) -> Timedelta:
"""
Get the session time when the car entered the pit.
"""
return self.__lap["PitInTime"]
def sector_time(self, sector_number: int) -> Timedelta:
"""
Get the sector times set in this lap.
@param sector_number: Either 1, 2 or 3.
"""
return self.__lap[f"Sector{sector_number}Time"]
def sector_session_time(self, sector_number: int) -> Timedelta:
"""
Get the session time when the sector time was set.
@param sector_number: Either 1, 2 or 3.
"""
return self.__lap[f"Sector{sector_number}SessionTime"]
def speed(self, speedtrap: str) -> float:
"""
Get car speed at measure point in this lap.
@param speedtrap: Either I1, I2, FL or ST (sector 1, sector 2, finish line or longest straight)
"""
return self.__lap[f"Speed{speedtrap}"]
@property
def is_personal_best(self) -> bool:
return self.__lap["IsPersonalBest"]
@property
def compound(self) -> str:
"""
Get compound used in this lap.
@return: Either SOFT, MEDIUM, HARD, INTERMEDIATE or WET
"""
return self.__lap["Compound"]
@property
def tyre_life(self) -> int:
"""
Get laps driven on the current tyre (includes laps from other sessions).
"""
return self.__lap["TyreLife"]
@property
def fresh_tyre(self) -> bool:
return self.__lap["FreshTyre"]
@property
def team(self) -> str:
return self.__lap["Team"]
@property
def lap_start_time(self) -> Timedelta:
return self.__lap["LapStartTime"]
@property
def lap_start_date(self) -> Timestamp:
return self.__lap["LapStartDate"]
@property
def track_status(self) -> str:
"""
Get the track status in this lap.
@return: Either 1, 2, 3, 4, 5, 6 or 7 (clear, yellow flag, ?, SC, red flag, VSC and VSC ending).
"""
return self.__lap["TrackStatus"]
@property
def position(self) -> int:
return self.__lap["Position"]
@property
def deleted(self) -> Optional[bool]:
"""
Determine if the lap was deleted (only available if race control messages are loaded).
"""
return self.__lap["Deleted"]
@property
def deleted_reason(self) -> str:
return self.__lap["DeletedReason"]
@property
def fast_f1_generated(self) -> bool:
"""
Determine if the lap was generated by FastF1 (information is interpolated).
"""
return self.__lap["FastF1Generated"]
@property
def is_accurate(self) -> bool:
"""
Determine if lap start and end match with other laps before and after.
"""
return self.__lap["IsAccurate"]
class DriverResultDataHelper:
"""
Helper class that provides easy access to DriverResult DataFrame columns.
"""
def __init__(self, driver_result: DriverResult):
self.__driver_result = driver_result
__driver_result: DriverResult
@property
def driver_number(self) -> str:
return self.__driver_result["DriverNumber"]
@property
def broadcast_name(self) -> str:
"""
Get this driver's broadcast name.
@return: For example P GASLY
"""
return self.__driver_result["BroadcastName"]
@property
def full_name(self) -> str:
return self.__driver_result["FullName"]
@property
def abbreviation(self) -> str:
return self.__driver_result["Abbreviation"]
# @property
# def driver_id(self) -> str:
# """
# Get the driverId used by the Ergast API.
# """
# return self.__driver_result["DriverID"]
@property
def team_name(self) -> str:
return self.__driver_result["TeamName"]
@property
def team_color(self) -> str:
return self.__driver_result["TeamColor"]
# @property
# def team_id(self) -> str:
# """
# Get the constructorId used by the Ergast API.
# @return:
# """
# return self.__driver_result["TeamID"]
@property
def first_name(self) -> str:
return self.__driver_result["FirstName"]
@property
def last_name(self) -> str:
return self.__driver_result["LastName"]
@property
def headshot_url(self) -> str:
return self.__driver_result["HeadshotUrl"]
@property
def country_code(self) -> str:
"""
Get a driver's three-letter country code.
@return: For example FRA
"""
return self.__driver_result["CountryCode"]
@property
def position(self) -> int:
return self.__driver_result["Position"]
@property
def classified_position(self) -> str:
"""
Get the classification result for this driver.
@return: Either R, D, E, W, F, N (retired, disqualified, excluded, withdrawn, failed to qualify and not classified)
"""
return self.__driver_result["ClassifiedPosition"]
@property
def grid_position(self) -> int:
"""
Get the driver's starting position.
"""
return self.__driver_result["GridPosition"]
def qualifying_time(self, qualifying_number: int) -> Timedelta:
"""
Get the driver's best qualifying time.
@param qualifying_number: Either 1, 2 or 3 (for Q1, Q2 and Q3)
"""
return self.__driver_result[f"Q{qualifying_number}"]
@property
def time(self) -> Timedelta:
"""
Get the driver's total race time.
"""
return self.__driver_result["Time"]
@property
def status(self) -> str:
"""
Get the driver's finishing status.
@return: For example Finished, +1 Lap, Crash, Gearbox...
"""
return self.__driver_result["Status"]
@property
def points(self) -> int:
return self.__driver_result["Points"]

View File

View File

View File

@ -0,0 +1,55 @@
from typing import Any, Callable, Dict
class OpenF1Driver:
__type_conversion_map__: Dict[str, Callable[[Any], Any]] = {
"session_key": int,
"meeting_key": int,
"full_name": str,
"first_name": str,
"last_name": str,
"name_acronym": str,
"broadcast_name": str,
"country_code": str,
"headshot_url": str,
"driver_number": int,
"team_colour": str,
"team_name": str
}
def __init__(self, response: dict[str, str] | None):
if response is None:
return
for key in response:
if not hasattr(self, key):
raise Exception(f"Mismatch between response data and {type(self).__name__} (key={key})")
if not key in self.__type_conversion_map__:
raise Exception(f"Mismatch between response data and {type(self).__name__}.__type_map__ (key={key})")
setattr(self, key, self.__type_conversion_map__[key](response[key]))
print("OpenF1Driver:", self.__dict__)
def to_params(self) -> Dict[str, str]:
params: Dict[str, str] = dict()
for key in self.__dict__:
params[str(key)] = str(self.__dict__[key])
return params
# Set all members to None so hasattr works above
session_key: int = None # type: ignore
meeting_key: int = None # type: ignore
full_name: str = None # type: ignore
first_name: str = None # type: ignore
last_name: str = None # type: ignore
name_acronym: str = None # type: ignore
broadcast_name: str = None # type: ignore
country_code: str = None # type: ignore
headshot_url: str = None # type: ignore
driver_number: int = None # type: ignore
team_colour: str = None # type: ignore
team_name: str = None # type: ignore

View File

@ -0,0 +1,40 @@
from datetime import datetime
from typing import Any, Callable, Dict
class OpenF1Position:
__type_conversion_map__: Dict[str, Callable[[Any], Any]] = {
"session_key": int,
"meeting_key": int,
"driver_number": int,
"date": lambda date: datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%f"),
"position": int
}
def __init__(self, response: dict[str, str] | None):
if response is None:
return
for key in response:
if not hasattr(self, key):
raise Exception(f"Mismatch between response data and {type(self).__name__} (key={key})")
if not key in self.__type_conversion_map__:
raise Exception(f"Mismatch between response data and {type(self).__name__}.__type_map__ (key={key})")
setattr(self, key, self.__type_conversion_map__[key](response[key]))
print("OpenF1Position:", self.__dict__)
def to_params(self) -> Dict[str, str]:
params: Dict[str, str] = dict()
for key in self.__dict__:
params[str(key)] = str(self.__dict__[key])
return params
session_key: int = None # type: ignore
meeting_key: int = None # type: ignore
driver_number: int = None # type: ignore
date: datetime = None # type: ignore
position: int = None # type: ignore

View File

@ -0,0 +1,60 @@
from datetime import datetime, time
from typing import Any, Callable, Dict
class OpenF1Session:
__type_conversion_map__: Dict[str, Callable[[Any], Any]] = {
"location": str,
"country_key": int,
"country_code": str,
"country_name": str,
"circuit_key": int,
"circuit_short_name": str,
"session_type": str,
"session_name": str,
"date_start": lambda date: datetime.strptime(date, "%Y-%m-%dT%H:%M:%S"),
"date_end": lambda date: datetime.strptime(date, "%Y-%m-%dT%H:%M:%S"),
"gmt_offset": lambda time: datetime.strptime(time, "%H:%M:%S").time(),
"session_key": int,
"meeting_key": int,
"year": int
}
def __init__(self, response: dict[str, str] | None):
if response is None:
return
for key in response:
if not hasattr(self, key):
raise Exception(f"Mismatch between response data and {type(self).__name__} (key={key})")
if not key in self.__type_conversion_map__:
raise Exception(f"Mismatch between response data and {type(self).__name__}.__type_map__ (key={key})")
setattr(self, key, self.__type_conversion_map__[key](response[key]))
print("OpenF1Session:", self.__dict__)
def to_params(self) -> Dict[str, str]:
params: Dict[str, str] = dict()
for key in self.__dict__:
params[str(key)] = str(self.__dict__[key])
return params
location: str = None # type: ignore
country_key: int = None # type: ignore
country_code: str = None # type: ignore
country_name: str = None # type: ignore
circuit_key: int = None # type: ignore
circuit_short_name: str = None # type: ignore
session_type: str = None # type: ignore
session_name: str = None # type: ignore
date_start: datetime = None # type: ignore
date_end: datetime = None # type: ignore
gmt_offset: time = None # type: ignore
session_key: int = None # type: ignore
meeting_key: int = None # type: ignore
year: int = None # type: ignore

View File

@ -0,0 +1,9 @@
OPENF1_URL: str = "https://api.openf1.org/v1"
OPENF1_SESSION_ENDPOINT: str = f"{OPENF1_URL}/sessions"
OPENF1_POSITION_ENDPOINT: str = f"{OPENF1_URL}/position"
OPENF1_DRIVER_ENDPOINT: str = f"{OPENF1_URL}/drivers"
OPENF1_SESSION_TYPE_RACE: str = "Race"
OPENF1_SESSION_NAME_RACE: str = "Race"
OPENF1_SESSION_NAME_SPRINT: str = "Sprint"

View File

@ -0,0 +1,76 @@
from datetime import datetime
import json
from typing import Any, Callable, Dict, List, cast
from requests import Response, get
from formula10.openf1.model.openf1_driver import OpenF1Driver
from formula10.openf1.model.openf1_position import OpenF1Position
from formula10.openf1.model.openf1_session import OpenF1Session
from formula10.openf1.openf1_definitions import OPENF1_DRIVER_ENDPOINT, OPENF1_POSITION_ENDPOINT, OPENF1_SESSION_ENDPOINT, OPENF1_SESSION_NAME_RACE, OPENF1_SESSION_NAME_SPRINT, OPENF1_SESSION_TYPE_RACE
def openf1_request_helper(endpoint: str, params: Dict[str, str]) -> List[Dict[str, str]]:
response: Response = get(endpoint, params=params)
if not response.ok:
raise Exception(f"OpenF1 request to {response.request.url} failed")
obj: Any = json.loads(response.text)
if isinstance(obj, List):
return cast(List[Dict[str, str]], obj)
elif isinstance(obj, Dict):
return [cast(Dict[str, str], obj)]
else:
# @todo Fail gracefully
raise Exception(f"Unexpected OpenF1 response from {response.request.url}: {obj}")
def openf1_fetch_latest_session(session_name: str) -> OpenF1Session:
# ApiSession object only supports integer session_keys
response: List[Dict[str, str]] = openf1_request_helper(OPENF1_SESSION_ENDPOINT, {
"session_key": "latest",
"session_type": OPENF1_SESSION_TYPE_RACE,
"session_name": session_name
})
return OpenF1Session(response[0])
def openf1_fetch_latest_race_session_key() -> int:
return openf1_fetch_latest_session(OPENF1_SESSION_NAME_RACE).session_key
def openf1_fetch_latest_sprint_session_key() -> int:
return openf1_fetch_latest_session(OPENF1_SESSION_NAME_SPRINT).session_key
def openf1_fetch_session(session_name: str, country_code: str) -> OpenF1Session:
_session: OpenF1Session = OpenF1Session(None)
_session.session_type = OPENF1_SESSION_TYPE_RACE # includes races + sprints
_session.year = 2024
_session.country_code = country_code
_session.session_name = session_name
response: List[Dict[str, str]] = openf1_request_helper(OPENF1_SESSION_ENDPOINT, _session.to_params())
return OpenF1Session(response[0])
def openf1_fetch_driver(session_key: int, name_acronym: str) -> OpenF1Driver:
_driver: OpenF1Driver = OpenF1Driver(None)
_driver.name_acronym = name_acronym
_driver.session_key = session_key
response: List[Dict[str, str]] = openf1_request_helper(OPENF1_DRIVER_ENDPOINT, _driver.to_params())
return OpenF1Driver(response[0])
def openf1_fetch_position(session_key: int, position: int):
_position: OpenF1Position = OpenF1Position(None)
_position.session_key = session_key
_position.position = position
response: List[Dict[str, str]] = openf1_request_helper(OPENF1_POSITION_ENDPOINT, _position.to_params())
# Find the last driver that was on this position at last
predicate: Callable[[Dict[str, str]], datetime] = lambda position: datetime.strptime(position["date"], "%Y-%m-%dT%H:%M:%S.%f")
return OpenF1Position(max(response, key=predicate))

View File

@ -0,0 +1,18 @@
.chart-container {
width: 100%;
height: 40vh;
}
@media only screen and (max-width: 900px) {
.chart-container {
width: 100%;
height: 50vh;
}
}
@media only screen and (orientation: landscape) and (max-width: 900px) {
.chart-container {
width: 100%;
height: 100vh;
}
}

View File

@ -0,0 +1,27 @@
.card-grid {
grid-template-columns: repeat(auto-fit, minmax(325px, 1fr));
grid-row-gap: 0;
grid-column-gap: 8px;
}
@media only screen and (min-width: 470px) {
.card-grid {
grid-template-columns: repeat(auto-fit, minmax(450px, 1fr));
grid-row-gap: 0;
grid-column-gap: 8px;
}
}
.card-grid-2 {
grid-template-columns: repeat(1, minmax(325px, 1fr));
grid-row-gap: 0;
grid-column-gap: 8px;
}
@media only screen and (min-width: 950px) {
.card-grid-2 {
grid-template-columns: repeat(2, minmax(450px, 1fr));
grid-row-gap: 0;
grid-column-gap: 8px;
}
}

View File

@ -24,17 +24,17 @@
{% endmacro %}
{# Simple driver select for forms #}
{% macro driver_select(name, label, include_none, drivers=none, disabled=false) %}
{% macro driver_select(name, label, include_none, include_inactive=false, drivers=none, disabled=false, border="") %}
<div class="form-floating">
<select name="{{ name }}" id="{{ name }}" class="form-select" aria-label="{{ name }}" {% if disabled %}disabled="disabled"{% endif %}>
<select name="{{ name }}" id="{{ name }}" class="form-select {{ border }}" aria-label="{{ name }}" {% if disabled %}disabled="disabled"{% endif %}>
<option value="" selected disabled hidden></option>
{% if drivers == none %}
{% set drivers = model.all_drivers(include_none=include_none) %}
{% set drivers = model.all_drivers(include_none=include_none, include_inactive=include_inactive) %}
{% endif %}
{% for driver in drivers %}
<option value="{{ driver.name }}">{{ driver.abbr }}</option>
<option value="{{ driver.id }}">{{ driver.abbr }}</option>
{% endfor %}
</select>
<label for="{{ name }}" class="text-primary">{{ label }}</label>
@ -42,26 +42,26 @@
{% endmacro %}
{# Driver select for forms where a value might be preselected #}
{% macro driver_select_with_preselect(driver_match, name, label, include_none, drivers=none, disabled=false) %}
{% macro driver_select_with_preselect(driver_match, name, label, include_none, include_inactive=false, drivers=none, disabled=false, border="") %}
<div class="form-floating">
<select name="{{ name }}" id="{{ name }}" class="form-select" aria-label="{{ name }}" {% if disabled %}disabled="disabled"{% endif %}>
<select name="{{ name }}" id="{{ name }}" class="form-select {{ border }}" aria-label="{{ name }}" {% if disabled %}disabled="disabled"{% endif %}>
{# Use namespace wrapper to persist scope between loop iterations #}
{% set user_has_chosen = namespace(driverpre=false) %}
{% if drivers == none %}
{% set drivers = model.all_drivers(include_none=include_none) %}
{% set drivers = model.all_drivers(include_none=include_none, include_inactive=include_inactive) %}
{% endif %}
{% for driver in drivers %}
{% if driver_match == driver %}
{% set user_has_chosen.driverpre = true %}
<option selected="selected" value="{{ driver.name }}">{{ driver.abbr }}</option>
<option selected="selected" value="{{ driver.id }}">{{ driver.abbr }}</option>
{% else %}
<option value="{{ driver.name }}">{{ driver.abbr }}</option>
<option value="{{ driver.id }}">{{ driver.abbr }}</option>
{% endif %}
{% if (include_none == true) and (driver == model.none_driver()) %}
<option disabled>──────────</option>
<option disabled="disabled">──────────</option>
{% endif %}
{% endfor %}
@ -75,9 +75,9 @@
{% endmacro %}
{# Simple team select for forms #}
{% macro team_select(name, label, include_none, teams=none, disabled=false) %}
{% macro team_select(name, label, include_none, teams=none, disabled=false, border="") %}
<div class="form-floating">
<select name="{{ name }}" id="{{ name }}" class="form-select" aria-label="{{ name }}" {% if disabled %}disabled="disabled"{% endif %}>
<select name="{{ name }}" id="{{ name }}" class="form-select {{ border }}" aria-label="{{ name }}" {% if disabled %}disabled="disabled"{% endif %}>
<option value="" selected disabled hidden></option>
{% if teams == none %}
@ -85,7 +85,7 @@
{% endif %}
{% for team in teams %}
<option value="{{ team.name }}">{{ team.name }}</option>
<option value="{{ team.id }}">{{ team.name }}</option>
{% endfor %}
</select>
<label for="{{ name }}" class="text-primary">{{ label }}</label>
@ -93,9 +93,9 @@
{% endmacro %}
{# Team select for forms where a value might be preselected #}
{% macro team_select_with_preselect(team_match, name, label, include_none, teams=none, disabled=false) %}
{% macro team_select_with_preselect(team_match, name, label, include_none, teams=none, disabled=false, border="") %}
<div class="form-floating">
<select name="{{ name }}" id="{{ name }}" class="form-select" aria-label="{{ name }}" {% if disabled %}disabled="disabled"{% endif %}>
<select name="{{ name }}" id="{{ name }}" class="form-select {{ border }}" aria-label="{{ name }}" {% if disabled %}disabled="disabled"{% endif %}>
{# Use namespace wrapper to persist scope between loop iterations #}
{% set user_has_chosen = namespace(teampre=false) %}
@ -106,13 +106,13 @@
{% for team in teams %}
{% if team_match == team %}
{% set user_has_chosen.teampre = true %}
<option selected="selected" value="{{ team.name }}">{{ team.name }}</option>
<option selected="selected" value="{{ team.id }}">{{ team.name }}</option>
{% else %}
<option value="{{ team.name }}">{{ team.name }}</option>
<option value="{{ team.id }}">{{ team.name }}</option>
{% endif %}
{% if (include_none == true) and (team == model.none_team()) %}
<option disabled>──────────</option>
<option disabled="disabled">──────────</option>
{% endif %}
{% endfor %}
@ -176,6 +176,12 @@
<link href="../static/style/bootstrap.css" rel="stylesheet">
<script src="../static/script/bootstrap.bundle.js"></script>
<!-- ChartJS -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!-- Custom -->
<link href="../static/style/grid.css" rel="stylesheet">
<link href="../static/style/diagram.css" rel="stylesheet">
<script defer>
{# Initialize Bootstrap Tooltips #}
let tooltipTriggerList = document.querySelectorAll("[data-bs-toggle='tooltip']")
@ -204,6 +210,7 @@
{{ nav_selector(page="/race/" ~ model.active_user_name_sanitized_or_everyone(), text="Race Picks") }}
{{ nav_selector(page="/season/" ~ model.active_user_name_sanitized_or_everyone(), text="Season Picks") }}
{{ nav_selector(page="/graphs", text="Leaderboard") }}
{{ nav_selector(page="/stats", text="Statistics") }}
{{ nav_selector(page="/rules", text="Rules") }}
</div>

View File

@ -1,123 +0,0 @@
{% extends 'base.jinja' %}
{% block title %}Formula 10 - Race Result{% endblock title %}
{% set active_page = "/result" %}
{% block head_extra %}
<link href="../static/style/draggable.css" rel="stylesheet">
<script src="../static/script/draggable.js" defer></script>
{% endblock head_extra %}
{% block navbar_center %}
{% if model.all_race_results() | length > 0 %}
<div class="dropdown">
<button class="btn btn-outline-danger dropdown-toggle" type="button" data-bs-toggle="dropdown"
aria-expanded="false">
{{ model.active_result_race_name_or_current_race_name() }}
</button>
<ul class="dropdown-menu">
{% if model.current_race is not none %}
<li>
<a class="dropdown-item"
href="/result/{{ model.current_race.name_sanitized }}">{{ model.current_race.name }}</a>
</li>
<li>
<hr class="dropdown-divider">
</li>
{% endif %}
{% for result in model.all_race_results() %}
<li>
<a class="dropdown-item"
href="/result/{{ result.race.name_sanitized }}">{{ result.race.name }}</a>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endblock navbar_center %}
{% block body %}
<div class="grid" style="grid-template-columns: repeat(auto-fit, minmax(450px, 1fr));">
<div class="card shadow-sm" style="width: 450px;">
<div class="card-body">
<h5 class="card-title">
{{ model.active_result_race_name_or_current_race_name() }}
</h5>
{% set race_result_open=model.race_result_open(model.active_result_race_name_or_current_race_name()) %}
{% if race_result_open == true %}
{% set action_save_href = "/result-enter/" ~ model.active_result_race_name_or_current_race_name_sanitized() %}
{% else %}
{% set action_save_href = "" %}
{% endif %}
<form action="{{ action_save_href }}" method="post">
<ul class="list-group list-group-flush d-inline-block">
{% for driver in model.all_drivers_or_active_result_standing_drivers() %}
<li class="list-group-item p-1"><span id="place_number"
class="fw-bold">P{{ "%02d" % loop.index }}</span>:
</li>
{% endfor %}
</ul>
<ul id="columns" class="list-group list-group-flush d-inline-block float-end">
{% for driver in model.all_drivers_or_active_result_standing_drivers() %}
<li class="list-group-item {% if race_result_open == true %}column{% endif %} p-1" {% if race_result_open == true %}draggable="true"{% endif %}>
{{ driver.name }}
<div class="d-inline-block float-end" style="margin-left: 30px;">
{# Driver DNFed at first #}
<div class="form-check form-check-reverse d-inline-block">
<input type="checkbox" class="form-check-input" value="{{ driver.name }}"
id="first-dnf-{{ driver.name }}" name="first-dnf-drivers"
{% if (model.active_result is not none) and (driver in model.active_result.initial_dnf) %}checked{% endif %}
{% if race_result_open == false %}disabled="disabled"{% endif %}>
<label for="first-dnf-{{ driver.name }}"
class="form-check-label text-muted">1. DNF</label>
</div>
{# Driver DNFed #}
<div class="form-check form-check-reverse d-inline-block mx-2">
<input type="checkbox" class="form-check-input" value="{{ driver.name }}"
id="dnf-{{ driver.name }}" name="dnf-drivers"
{% if (model.active_result is not none) and (driver in model.active_result.all_dnfs) %}checked{% endif %}
{% if race_result_open == false %}disabled="disabled"{% endif %}>
<label for="dnf-{{ driver.name }}"
class="form-check-label text-muted">DNF</label>
</div>
{# Driver Excluded #}
<div class="form-check form-check-reverse d-inline-block">
<input type="checkbox" class="form-check-input" value="{{ driver.name }}"
id="exclude-{{ driver.name }}" name="excluded-drivers"
{% if (model.active_result is not none) and (driver in model.active_result.standing_exclusions) %}checked{% endif %}
{% if race_result_open == false %}disabled="disabled"{% endif %}>
<label for="exclude-{{ driver.name }}"
class="form-check-label text-muted" data-bs-toggle="tooltip"
title="Driver is not counted for standing">NC</label>
</div>
</div>
{# Standing order #}
<input type="hidden" name="pxx-drivers" value="{{ driver.name }}">
</li>
{% endfor %}
</ul>
<input type="submit" class="btn btn-danger mt-2 w-100" value="Save"
{% if race_result_open == false %}disabled="disabled"{% endif %}>
</form>
</div>
</div>
</div>
{% endblock body %}

View File

@ -5,10 +5,12 @@
{% set active_page = "/error" %}
{% block body %}
<div class="card">
<div class="card shadow-sm mb-2">
<div class="card-header">
<span class="text-danger fw-bold">Error</span>
</div>
<div class="card-body">
<h5 class="card-title text-danger fw-bold">Error</h5>
<h6 class="card-subtitle">{{ error_message }}</h6>
{{ error_message }}
</div>
</div>
{% endblock body %}

View File

@ -0,0 +1,110 @@
{% extends 'base.jinja' %}
{% block title %}Formula 10 - Leaderboard{% endblock title %}
{% set active_page = "/graphs" %}
{% block body %}
{# <div class="card shadow-sm mb-2">#}
{# <div class="card-header">#}
{# Note#}
{# </div>#}
{##}
{# <div class="card-body">#}
{# Points only include race picks.#}
{# </div>#}
{# </div>#}
<div class="card shadow-sm mb-2">
<div class="card-header">
Leaderboard
</div>
<div class="card-body">
<div class="d-inline-block overflow-x-scroll w-100">
<table class="table table-bordered table-sm table-responsive">
<thead>
<tr>
<th scope="col" class="text-center" style="min-width: 50px;">Place</th>
<th scope="col" class="text-center" style="min-width: 50px;">User</th>
<th scope="col" class="text-center" style="min-width: 100px;">Points</th>
<th scope="col" class="text-center" style="min-width: 100px;">Points (Race)</th>
<th scope="col" class="text-center" style="min-width: 100px;">Points (Season)</th>
<th scope="col" class="text-center" style="min-width: 100px;">Total picks</th>
<th scope="col" class="text-center" style="min-width: 100px;" data-bs-toggle="tooltip"
title="Any points count as correct">Correct picks
</th>
<th scope="col" class="text-center" style="min-width: 100px;">Points per pick</th>
</tr>
</thead>
<tbody>
{% for user in points.users_sorted_by_points(include_season=True) %}
{% set user_standing = points.user_standing(include_season=True)[user.name] %}
<tr class="{% if user_standing == 1 %}table-danger{% endif %}">
<td class="text-center text-nowrap">{{ user_standing }}</td>
<td class="text-center text-nowrap">{{ user.name }}</td>
<td class="text-center text-nowrap">{{ points.total_points_by(user_name=user.name, include_season=True) }}</td>
<td class="text-center text-nowrap">{{ points.total_points_by(user_name=user.name, include_season=False) }}</td>
<td class="text-center text-nowrap">{{ points.season_points_by(user_name=user.name) }}</td>
<td class="text-center text-nowrap">{{ points.picks_count(user.name) }}</td>
<td class="text-center text-nowrap">{{ points.picks_with_points_count(user.name) }}</td>
<td class="text-center text-nowrap">{{ "%0.2f" % points.points_per_pick(user.name) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<div class="card shadow-sm mb-2">
<div class="card-header">
History (Race)
</div>
<div class="card-body">
<div class="chart-container">
<canvas id="line-chart"></canvas>
</div>
<script>
function cumulative_points(data) {
return new Chart(document.getElementById("line-chart"), {
type: 'line',
data: data,
options: {
title: {
display: true,
text: 'History'
},
{#tension: 0,#}
responsive: true,
maintainAspectRatio: false,
pointRadius: 5,
pointHoverRadius: 10,
scales: {
y: {
min: 0,
{#max: 100#}
}
}
}
});
}
cumulative_points({{ points.cumulative_points_data() | safe }})
</script>
</div>
</div>
{# <div class="card mt-2">#}
{# <div class="card-body">#}
{# <h5 class="card-title">Statistics</h5>#}
{# Various statistics: Driver voted most for DNF #}
{# </div>#}
{# </div>#}
{% endblock body %}

View File

@ -14,16 +14,19 @@
{% block body %}
{# Put table in this div to make right padding work #}
<div class="d-inline-block overflow-x-scroll w-100 mb-2">
<table class="table table-bordered table-sm table-responsive shadow-sm">
<thead>
<tr>
<th scope="col" rowspan="2" class="text-center" style="width: 125px !important;">Race</th>
<th scope="col" rowspan="2" class="text-center" style="width: 125px;">Race</th>
<th scope="col" {% if model.active_user is none %}colspan="{{ model.all_users() | length }}"{% endif %}
class="text-center">Call
</th>
<th scope="col" rowspan="2" class="text-center" style="width: 125px !important;">Result</th>
<th scope="col" rowspan="2" class="text-center" style="width: 125px;">Result</th>
</tr>
</thead>
@ -35,11 +38,14 @@
{# Link should only be visible if all users are visible #}
{% if model.active_user is not none %}
<td class="text-center text-nowrap" style="width: 1%;">{{ model.active_user.name }}</td>
<td class="text-center text-nowrap" style="min-width: 100px;">{{ model.active_user.name }}
({{ points.total_points_by(user_name=model.active_user.name, include_season=False) }})
</td>
{% else %}
{% for user in model.all_users() %}
<td class="text-center text-nowrap" style="min-width: 100px;">
<a href="/race/{{ user.name_sanitized }}" class="link-dark">{{ user.name }}</a>
<a href="/race/{{ user.name_sanitized }}" class="link-dark">{{ user.name }}
({{ points.total_points_by(user_name=user.name, include_season=False) }})</a>
</td>
{% endfor %}
{% endif %}
@ -129,7 +135,7 @@
{# Delete guess #}
<form action="{{ action_delete_href }}" method="post">
<input type="submit" class="btn btn-dark mt-2 w-100" value="Delete"
{% if race_guess_open == false %}disabled{% endif %}>
{% if race_guess_open == false %}disabled="disabled"{% endif %}>
</form>
</td>
@ -193,5 +199,6 @@
</tbody>
</table>
</div>
{% endblock body %}

View File

@ -0,0 +1,217 @@
{% extends 'base.jinja' %}
{% block title %}Formula 10 - Race Result{% endblock title %}
{% set active_page = "/result" %}
{% block head_extra %}
<link href="../static/style/draggable.css" rel="stylesheet">
<script src="../static/script/draggable.js" defer></script>
{% endblock head_extra %}
{% block navbar_center %}
{% if model.all_race_results() | length > 0 %}
<div class="dropdown">
<button class="btn btn-outline-danger dropdown-toggle" type="button" data-bs-toggle="dropdown"
aria-expanded="false">
{{ model.active_result_race_name_or_current_race_name() }}
</button>
<ul class="dropdown-menu">
{% if model.current_race is not none %}
<li>
<a class="dropdown-item"
href="/result/{{ model.current_race.name_sanitized }}">{{ model.current_race.name }}</a>
</li>
<li>
<hr class="dropdown-divider">
</li>
{% endif %}
{% for result in model.all_race_results() %}
<li>
<a class="dropdown-item"
href="/result/{{ result.race.name_sanitized }}">{{ result.race.name }}</a>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endblock navbar_center %}
{% block body %}
{% set race_result_open=model.race_result_open(model.active_result_race_name_or_current_race_name()) %}
{% if race_result_open == true %}
{% set action_save_href = "/result-enter/" ~ model.active_result_race_name_or_current_race_name_sanitized() %}
{% set action_fetch_href = "/result-fetch/" ~ model.active_result_race_name_or_current_race_name_sanitized() %}
{% else %}
{% set action_save_href = "" %}
{% set action_fetch_href = "" %}
{% endif %}
<form action="{{ action_fetch_href }}" method="post">
<div class="card shadow-sm mb-2 w-100">
<div class="card-header">
Autofill
</div>
<div class="card-body">
<input type="submit" class="btn btn-danger mt-2 w-100" value="Fetch using FastF1"
{% if race_result_open == false %}disabled="disabled"{% endif %}>
</div>
</div>
</form>
<form class="grid card-grid" action="{{ action_save_href }}" method="post">
{# Race result #}
<div class="card shadow-sm mb-2 w-100">
<div class="card-header">
{{ model.active_result_race_name_or_current_race_name() }}
</div>
<div class="card-body">
<div class="d-inline-block overflow-x-scroll w-100">
<div style="width: 460px;">
{# Place numbers #}
<ul class="list-group list-group-flush d-inline-block">
{% for driver in model.all_active_drivers_or_active_result_standing_drivers() %}
<li class="list-group-item p-1"><span id="place_number"
class="fw-bold">P{{ "%02d" % loop.index }}</span>:
</li>
{% endfor %}
</ul>
{# Drag and drop, "#columns .column" is the selector for the JS #}
<ul id="columns" class="list-group list-group-flush d-inline-block float-end">
{% for driver in model.all_active_drivers_or_active_result_standing_drivers() %}
<li class="list-group-item {% if race_result_open == true %}column{% endif %} p-1"
{% if race_result_open == true %}draggable="true"{% endif %}>
{{ driver.name }}
<div class="d-inline-block float-end" style="margin-left: 30px;">
{# Fastest lap #}
<div class="form-check form-check-reverse d-inline-block">
<input type="radio" class="form-check-input"
value="{{ driver.id }}"
id="fastest-lap-{{ driver.id }}" name="fastest-lap"
{% if (model.active_result is not none) and (driver.id == model.active_result.fastest_lap_driver.id) %}checked{% endif %}
{% if race_result_open == false %}disabled="disabled"{% endif %}>
<label for="fastest-lap-{{ driver.id }}"
class="form-check-label text-muted" data-bs-toggle="tooltip"
title="Fastest lap">Lap</label>
</div>
{# Driver DNFed at first #}
<div class="form-check form-check-reverse d-inline-block"
style="margin-left: 2px;">
<input type="checkbox" class="form-check-input"
value="{{ driver.id }}"
id="first-dnf-{{ driver.id }}" name="first-dnf-drivers"
{% if (model.active_result is not none) and (driver in model.active_result.initial_dnf) %}checked{% endif %}
{% if race_result_open == false %}disabled="disabled"{% endif %}>
<label for="first-dnf-{{ driver.id }}"
class="form-check-label text-muted">1. DNF</label>
</div>
{# Driver DNFed #}
<div class="form-check form-check-reverse d-inline-block"
style="margin-left: 2px;">
<input type="checkbox" class="form-check-input"
value="{{ driver.id }}"
id="dnf-{{ driver.id }}" name="dnf-drivers"
{% if (model.active_result is not none) and (driver in model.active_result.all_dnfs) %}checked{% endif %}
{% if race_result_open == false %}disabled="disabled"{% endif %}>
<label for="dnf-{{ driver.id }}"
class="form-check-label text-muted">DNF</label>
</div>
{# Driver Excluded #}
<div class="form-check form-check-reverse d-inline-block"
style="margin-left: 2px;">
<input type="checkbox" class="form-check-input"
value="{{ driver.id }}"
id="exclude-{{ driver.id }}" name="excluded-drivers"
{% if (model.active_result is not none) and (driver in model.active_result.standing_exclusions) %}checked{% endif %}
{% if race_result_open == false %}disabled="disabled"{% endif %}>
<label for="exclude-{{ driver.id }}"
class="form-check-label text-muted" data-bs-toggle="tooltip"
title="Driver is not counted for standing">NC</label>
</div>
</div>
{# Standing order #}
<input type="hidden" name="pxx-drivers" value="{{ driver.id }}">
</li>
{% endfor %}
</ul>
<input type="submit" class="btn btn-danger mt-2 w-100" value="Save"
{% if race_result_open == false %}disabled="disabled"{% endif %}>
</div>
</div>
</div>
</div>
{# Sprint result #}
{% if model.active_result_race_or_current_race().has_sprint == true %}
<div class="card shadow-sm mb-2 w-100">
<div class="card-header">
Sprint
</div>
<div class="card-body">
<div class="d-inline-block overflow-x-scroll w-100">
<div style="width: 275px;">
{# Place numbers #}
<ul class="list-group list-group-flush d-inline-block">
{% for driver in model.all_active_drivers_or_active_result_sprint_standing_drivers() %}
<li class="list-group-item p-1"><span id="place_number"
class="fw-bold">P{{ "%02d" % loop.index }}</span>:
</li>
{% endfor %}
</ul>
{# Drag and drop, "#columns .column" is the selector for the JS #}
<ul id="columns" class="list-group list-group-flush d-inline-block float-end">
{% for driver in model.all_active_drivers_or_active_result_sprint_standing_drivers() %}
<li class="list-group-item {% if race_result_open == true %}column{% endif %} p-1"
{% if race_result_open == true %}draggable="true"{% endif %}>
{{ driver.name }}
<div class="d-inline-block float-end" style="margin-left: 30px;">
{# Driver DNFed #}
<div class="form-check form-check-reverse d-inline-block"
style="margin-left: 2px;">
<input type="checkbox" class="form-check-input"
value="{{ driver.id }}"
id="sprint-dnf-{{ driver.id }}" name="sprint-dnf-drivers"
{% if (model.active_result is not none) and (driver in model.active_result.sprint_dnfs) %}checked{% endif %}
{% if race_result_open == false %}disabled="disabled"{% endif %}>
<label for="sprint-dnf-{{ driver.id }}"
class="form-check-label text-muted">DNF</label>
</div>
</div>
{# Standing order #}
<input type="hidden" name="sprint-pxx-drivers" value="{{ driver.id }}">
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</div>
{% endif %}
</form>
{% endblock body %}

View File

@ -5,10 +5,12 @@
{% set active_page = "/rules" %}
{% block body %}
<div class="card">
<div class="card-body">
<h5 class="card-title">Format</h5>
<div class="card shadow-sm mb-2">
<div class="card-header">
Format
</div>
<div class="card-body">
<p>
Das Tippspiel wird dieses Jahr in zwei Teile aufgespalten: Renntips (für jedes Rennen) und Bonustipps
(für die ganze Saison).
@ -20,10 +22,12 @@
</div>
</div>
<div class="card mt-2">
<div class="card-body">
<h5 class="card-title">Renntipps</h5>
<div class="card shadow-sm mb-2">
<div class="card-header">
Renntipps
</div>
<div class="card-body">
<p>
Jedes Rennen besteht aus zwei Tipps.
Einem Platzierungstipp (PX-Tipp) und einem Tipp welcher Fahrer als Erstes das Rennen abbricht
@ -39,10 +43,12 @@
</div>
</div>
<div class="card mt-2">
<div class="card-body">
<h5 class="card-title">Punkte</h5>
<div class="card shadow-sm mb-2">
<div class="card-header">
Punkte
</div>
<div class="card-body">
<p>
Für einen korrekten Tipp werden 10 Punkte vergeben.
Beim PX-Tipp werden 6 Punkte für einen Platz Abweichung, 3 Punkte für zwei plätze Abweichung und 1 Punkt
@ -56,10 +62,12 @@
</div>
</div>
<div class="card mt-2">
<div class="card-body">
<h5 class="card-title">Bonustipps</h5>
<div class="card shadow-sm mb-2">
<div class="card-header">
Bonustipps
</div>
<div class="card-body">
<p>
Zusätzlich gibt es dieses Jahr auch Tipps, die sich auf die ganze Saison beziehen und verschieden
bepunktet werden.

View File

@ -10,21 +10,32 @@
{% block body %}
<div class="grid" style="grid-template-columns: repeat(auto-fit, minmax(450px, 1fr));">
{# <div class="card shadow-sm mb-2">#}
{# <div class="card-header">#}
{# Note#}
{# </div>#}
{##}
{# <div class="card-body">#}
{# Picks that match the current standings are marked in green, except for the hot-take and overtake picks, as#}
{# those are not evaluated automatically.<br>#}
{# </div>#}
{# </div>#}
<div class="grid card-grid">
{% for user in model.all_users_or_active_user() %}
<div class="card mb-2 shadow-sm" style="width: 450px;">
<div class="card-body">
<div class="card shadow-sm mb-2">
<div class="card-header">
{# Link should only be visible if all users are visible #}
{% if model.active_user is not none %}
<h5 class="card-title">{{ user.name }}</h5>
{{ user.name }}
{% else %}
<a href="/season/{{ user.name }}" class="link-dark">
<h5 class="card-title">{{ user.name }}</h5>
</a>
<a href="/season/{{ user.name }}" class="link-dark">{{ user.name }}</a>
{% endif %}
</div>
<div class="card-body">
{% set user_guess = model.season_guesses_by(user_name=user.name) %}
@ -38,7 +49,9 @@
{# Hot Take #}
<div class="form-floating">
<textarea class="form-control" id="hot-take-input-{{ user.name }}" name="hottakeselect"
<textarea
class="form-control {% if points.hot_take_correct(user.name) %}border-success{% else %}border-danger{% endif %}"
id="hot-take-input-{{ user.name }}" name="hottakeselect"
style="height: 150px"
{% if season_guess_open == false %}disabled="disabled"{% endif %}>
{%- if user_guess is not none -%}{{ user_guess.hot_take_string() }}{%- endif -%}
@ -49,54 +62,69 @@
{# P2 Constructor #}
<div class="mt-2">
{{ team_select_with_preselect(team_match=user_guess.p2_wcc, name="p2select", label="P2 in WCC:", include_none=false, disabled=not season_guess_open) }}
{{ team_select_with_preselect(team_match=user_guess.p2_wcc, name="p2select", label="P2 in WCC:",
include_none=false, disabled=not season_guess_open,
border=("border-success" if points.p2_constructor_correct(user.name) else "border-danger")) }}
</div>
{# Most Overtakes + DNFs #}
<div class="input-group mt-2">
{{ driver_select_with_preselect(driver_match=user_guess.most_overtakes, name="overtakeselect", label="Most overtakes:", include_none=false, disabled=not season_guess_open) }}
{{ driver_select_with_preselect(driver_match=user_guess.most_dnfs, name="dnfselect", label="Most DNFs:", include_none=false, disabled=not season_guess_open) }}
{{ driver_select_with_preselect(driver_match=user_guess.most_overtakes, name="overtakeselect",
label="Most overtakes:", include_none=false, include_inactive=true, disabled=not season_guess_open,
border=("border-success" if points.overtakes_correct(user.name) else "border-danger")) }}
{{ driver_select_with_preselect(driver_match=user_guess.most_dnfs, name="dnfselect", label="Most DNFs:",
include_none=false, include_inactive=true, disabled=not season_guess_open,
border=("border-success" if points.dnfs_correct(user.name) else "border-danger")) }}
</div>
{# Most Gained + Lost #}
<div class="input-group mt-2" data-bs-toggle="tooltip"
title="Which driver will gain/lose the most places in comparison to last season's results?">
{{ driver_select_with_preselect(driver_match=user_guess.most_wdc_gained, name="gainedselect", label="Most WDC places gained:", include_none=false, drivers=model.drivers_for_wdc_gained(), disabled=not season_guess_open) }}
{{ driver_select_with_preselect(driver_match=user_guess.most_wdc_lost, name="lostselect", label="Most WDC places lost:", include_none=false, disabled=not season_guess_open) }}
{{ driver_select_with_preselect(driver_match=user_guess.most_wdc_gained, name="gainedselect",
label="Most WDC pl. gained:", include_none=false, include_inactive=true,
drivers=model.active_drivers_for_wdc_gained(), disabled=not season_guess_open,
border=("border-success" if points.most_gained_correct(user.name) else "border-danger")) }}
{{ driver_select_with_preselect(driver_match=user_guess.most_wdc_lost, name="lostselect",
label="Most WDC pl. lost:", include_none=false, include_inactive=true, disabled=not season_guess_open,
border=("border-success" if points.most_lost_correct(user.name) else "border-danger")) }}
</div>
{# Team-internal Winners #}
<h6 class="card-subtitle mt-2" data-bs-toggle="tooltip"
title="Which driver will finish the season higher than his teammate?">Teammate battle
winners:</h6>
<div class="grid mt-2" style="width: 450px; row-gap: 0;">
<div class="grid mt-2 container" style="row-gap: 0;">
{% for team in model.all_teams(include_none=false) %}
{% set driver_a = model.drivers_by(team_name=team.name)[0] %}
{% set driver_b = model.drivers_by(team_name=team.name)[1] %}
{# HACK: Choosing 0 and 1 will chose the drivers with the lowest IDs (although there could be others). #}
{# This means the drivers chosen from at the start of the season will be visible. #}
{% set driver_a = model.drivers_by(team_name=team.name, include_inactive=True)[0] %}
{% set driver_b = model.drivers_by(team_name=team.name, include_inactive=True)[1] %}
<div class="g-col-6">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio"
name="teamwinner-{{ team.name }}"
id="teamwinner-{{ team.name }}-1-{{ user.name }}"
value="{{ driver_a.name }}"
name="teamwinner-{{ team.id }}"
id="teamwinner-{{ team.id }}-1-{{ user.id }}"
value="{{ driver_a.id }}"
{% if (user_guess is not none) and (driver_a in user_guess.team_winners) %}checked="checked"{% endif %}
{% if season_guess_open == false %}disabled="disabled"{% endif %}>
<label class="form-check-label"
for="teamwinner-{{ team.name }}-1-{{ user.name }}">{{ driver_a.name }}</label>
<label class="form-check-label {% if (user_guess is not none) and (driver_a in user_guess.team_winners) and points.is_team_winner(driver_a) %}text-success
{% elif (user_guess is not none) and (driver_a in user_guess.team_winners) and (not points.is_team_winner(driver_a)) %}text-danger{% endif %}"
for="teamwinner-{{ team.id }}-1-{{ user.id }}">{{ driver_a.name }}</label>
</div>
</div>
<div class="g-col-6">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio"
name="teamwinner-{{ team.name }}"
id="teamwinner-{{ team.name }}-2-{{ user.name }}"
value="{{ driver_b.name }}"
name="teamwinner-{{ team.id }}"
id="teamwinner-{{ team.id }}-2-{{ user.id }}"
value="{{ driver_b.id }}"
{% if (user_guess is not none) and (driver_b in user_guess.team_winners) %}checked="checked"{% endif %}
{% if season_guess_open == false %}disabled="disabled"{% endif %}>
<label class="form-check-label"
for="teamwinner-{{ team.name }}-2-{{ user.name }}">{{ driver_b.name }}</label>
<label class="form-check-label {% if (user_guess is not none) and (driver_b in user_guess.team_winners) and points.is_team_winner(driver_b) %}text-success
{% elif (user_guess is not none) and (driver_b in user_guess.team_winners) and (not points.is_team_winner(driver_b)) %}text-danger{% endif %}"
for="teamwinner-{{ team.id }}-2-{{ user.id }}">{{ driver_b.name }}</label>
</div>
</div>
{% endfor %}
@ -105,21 +133,25 @@
{# Drivers with Podiums #}
<h6 class="card-subtitle mt-2" data-bs-toggle="tooltip"
title="Which driver will reach at least a single podium?">Drivers with podium(s):</h6>
<div class="grid mt-2" style="width: 450px; row-gap: 0;">
<div class="grid mt-2 container" style="row-gap: 0;">
{% for team in model.all_teams(include_none=false) %}
{% set driver_a = model.drivers_by(team_name=team.name)[0] %}
{% set driver_b = model.drivers_by(team_name=team.name)[1] %}
{# HACK: Choosing 0 and 1 will chose the drivers with the lowest IDs (although there could be others). #}
{# This means the drivers chosen from at the start of the season will be visible. #}
{% set driver_a = model.drivers_by(team_name=team.name, include_inactive=True)[0] %}
{% set driver_b = model.drivers_by(team_name=team.name, include_inactive=True)[1] %}
<div class="g-col-6">
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox"
name="podiumdrivers"
id="podium-{{ driver_a.name }}-{{ user.name }}"
value="{{ driver_a.name }}"
id="podium-{{ driver_a.id }}-{{ user.id }}"
value="{{ driver_a.id }}"
{% if (user_guess is not none) and (driver_a in user_guess.podiums) %}checked="checked"{% endif %}
{% if season_guess_open == false %}disabled="disabled"{% endif %}>
<label class="form-check-label"
for="podium-{{ driver_a.name }}-{{ user.name }}">{{ driver_a.name }}</label>
<label class="form-check-label {% if (user_guess is not none) and (driver_a in user_guess.podiums) and points.has_podium(driver_a) %}text-success
{% elif (user_guess is not none) and (driver_a in user_guess.podiums) and (not points.has_podium(driver_a)) %}text-danger
{% elif (user_guess is not none) and (driver_a not in user_guess.podiums) and (points.has_podium(driver_a)) %}text-danger{% endif %}"
for="podium-{{ driver_a.id }}-{{ user.id }}">{{ driver_a.name }}</label>
</div>
</div>
@ -127,19 +159,21 @@
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox"
name="podiumdrivers"
id="podium-{{ driver_b.name }}-{{ user.name }}"
value="{{ driver_b.name }}"
id="podium-{{ driver_b.id }}-{{ user.id }}"
value="{{ driver_b.id }}"
{% if (user_guess is not none) and (driver_b in user_guess.podiums) %}checked="checked"{% endif %}
{% if season_guess_open == false %}disabled="disabled"{% endif %}>
<label class="form-check-label"
for="podium-{{ driver_b.name }}-{{ user.name }}">{{ driver_b.name }}</label>
<label class="form-check-label {% if (user_guess is not none) and (driver_b in user_guess.podiums) and points.has_podium(driver_b) %}text-success
{% elif (user_guess is not none) and (driver_b in user_guess.podiums) and (not points.has_podium(driver_b)) %}text-danger
{% elif (user_guess is not none) and (driver_b not in user_guess.podiums) and (points.has_podium(driver_b)) %}text-danger{% endif %}"
for="podium-{{ driver_b.id }}-{{ user.id }}">{{ driver_b.name }}</label>
</div>
</div>
{% endfor %}
</div>
<input type="submit" class="btn btn-danger mt-2 w-100" value="Save"
{% if season_guess_open == false %}disabled{% endif %}>
{% if season_guess_open == false %}disabled="disabled"{% endif %}>
</form>
</div>

View File

@ -1,56 +1,161 @@
{% extends 'base.jinja' %}
{% block title %}Formula 10 - Leaderboard{% endblock title %}
{% block title %}Formula 10 - Statistics{% endblock title %}
{% set active_page = "/graphs" %}
{% set active_page = "/stats" %}
{% block body %}
<div class="card">
<div class="card-body">
<h5 class="card-title">Leaderboard</h5>
<h6 class="card-subtitle">Points only include race picks</h6>
<div class="grid card-grid-2">
<table class="table table-bordered table-sm table-responsive mt-3">
<div class="card shadow-sm mb-2">
<div class="card-header">
Drivers
</div>
<div class="card-body">
<div class="d-inline-block overflow-x-scroll w-100">
<table class="table table-bordered table-sm table-responsive">
<thead>
<tr>
<th scope="col" class="text-center" style="width: 1%;">User</th>
<th scope="col" class="text-center" style="width: 1%;">Points</th>
<th scope="col" class="text-center" style="width: 1%;">Total picks</th>
<th scope="col" class="text-center" style="width: 1%;">Correct picks</th>
<th scope="col" class="text-center" style="width: 1%;">Points per pick</th>
<th scope="col" class="text-center" style="min-width: 50px;">Place</th>
<th scope="col" class="text-center" style="min-width: 50px;">Driver</th>
<th scope="col" class="text-center" style="min-width: 100px;">Points</th>
<th scope="col" class="text-center" style="min-width: 100px;">DNFs</th>
<th scope="col" class="text-center" style="min-width: 100px;">Place Delta</th>
</tr>
</thead>
<tbody>
{% for user in model.all_users() %}
<tr>
<td class="text-center text-nowrap">{{ user.name }}</td>
<td class="text-center text-nowrap">{{ points.total_points_by(user.name) }}</td>
<td class="text-center text-nowrap">{{ points.picks_count(user.name) }}</td>
<td class="text-center text-nowrap">{{ points.picks_with_points_count(user.name) }}</td>
<td class="text-center text-nowrap">{{ "%0.2f" % points.points_per_pick(user.name) }}</td>
{% for driver in points.drivers_sorted_by_points(include_inactive=True) %}
{% set driver_standing = points.wdc_standing_by_driver()[driver.name] %}
<tr class="{% if driver_standing == 1 %}table-danger{% endif %}">
<td class="text-center text-nowrap">{{ driver_standing }}</td>
<td class="text-center text-nowrap">{{ driver.name }}</td>
<td class="text-center text-nowrap">{{ points.total_driver_points_by(driver.name) }}</td>
<td class="text-center text-nowrap">{{ points.dnfs()[driver.name] }}</td>
<td class="text-center text-nowrap">{{ "%+d" % points.wdc_diff_2023_by(driver.name) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{# <div class="card mt-2">#}
{# <div class="card-body">#}
{# <h5 class="card-title">History</h5>#}
<div class="card shadow-sm mb-2">
<div class="card-header">
Constructors
</div>
{# Line chart of point history with a line per user #}
{# </div>#}
{# </div>#}
<div class="card-body">
<div class="d-inline-block overflow-x-scroll w-100">
<table class="table table-bordered table-sm table-responsive">
<thead>
<tr>
<th scope="col" class="text-center" style="min-width: 50px;">Place</th>
<th scope="col" class="text-center" style="min-width: 50px;">Team</th>
<th scope="col" class="text-center" style="min-width: 100px;">Points</th>
<th scope="col" class="text-center" style="min-width: 100px;">Place Delta</th>
</tr>
</thead>
{# <div class="card mt-2">#}
{# <div class="card-body">#}
{# <h5 class="card-title">Statistics</h5>#}
<tbody>
{% for team in points.teams_sorted_by_points() %}
{% set team_standing = points.wcc_standing_by_team()[team.name] %}
<tr class="{% if team_standing == 1 %}table-danger{% endif %}">
<td class="text-center text-nowrap">{{ team_standing }}</td>
<td class="text-center text-nowrap">{{ team.name }}</td>
<td class="text-center text-nowrap">{{ points.total_team_points_by(team.name) }}</td>
<td class="text-center text-nowrap">{{ points.wcc_diff_2023_by(team.name) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{# Various statistics: Driver voted most for DNF #}
{# </div>#}
{# </div>#}
<div class="card shadow-sm mb-2">
<div class="card-header">
Driver history
</div>
<div class="card-body">
<div class="chart-container">
<canvas id="driver-line-chart"></canvas>
</div>
<script>
function cumulative_driver_points(data) {
return new Chart(document.getElementById("driver-line-chart"), {
type: 'line',
data: data,
options: {
title: {
display: true,
text: 'History'
},
{#tension: 0,#}
responsive: true,
maintainAspectRatio: false,
pointRadius: 5,
pointHoverRadius: 10,
scales: {
y: {
min: 0,
{#max: 100#}
}
}
}
});
}
cumulative_driver_points({{ points.cumulative_driver_points_data() | safe }})
</script>
</div>
</div>
<div class="card shadow-sm mb-2">
<div class="card-header">
Team history
</div>
<div class="card-body">
<div class="chart-container">
<canvas id="team-line-chart"></canvas>
</div>
<script>
function cumulative_team_points(data) {
return new Chart(document.getElementById("team-line-chart"), {
type: 'line',
data: data,
options: {
title: {
display: true,
text: 'History'
},
{#tension: 0,#}
responsive: true,
maintainAspectRatio: false,
pointRadius: 5,
pointHoverRadius: 10,
scales: {
y: {
min: 0,
{#max: 100#}
}
}
}
});
}
cumulative_team_points({{ points.cumulative_team_points_data() | safe }})
</script>
</div>
</div>
</div>
{% endblock body %}

View File

@ -6,10 +6,12 @@
{% block body %}
<div class="card shadow-sm">
<div class="card-body">
<h5 class="card-title">Add User</h5>
<div class="card shadow-sm mb-2">
<div class="card-header">
Add user
</div>
<div class="card-body">
<form action="/user-add" method="post">
<div class="input-group">
<div class="form-floating">
@ -25,10 +27,12 @@
</div>
{% if model.all_users() | length > 0 %}
<div class="card mt-2 shadow-sm">
<div class="card-body">
<h5 class="card-title">Registered Users</h5>
<div class="card shadow-sm mb-2">
<div class="card-header">
Registered users
</div>
<div class="card-body">
<ul class="list-group list-group-flush">
{% for user in model.all_users() %}
<li class="list-group-item">{{ user.name }}</li>
@ -37,10 +41,12 @@
</div>
</div>
<div class="card mt-2 shadow-sm">
<div class="card-body">
<h5 class="card-title">Delete user</h5>
<div class="card shadow-sm mb-2">
<div class="card-header">
Delete user
</div>
<div class="card-body">
<form action="/user-delete" method="post">
<div class="input-group">
<select class="form-control form-select" aria-label="select-delete-user"
@ -66,16 +72,4 @@
</div>
{% endif %}
{# <div class="card mt-2 border-danger shadow-sm">#}
{# <div class="card-body">#}
{# <h5 class="card-title">Functions that should not be public</h5>#}
{# <h6 class="card-subtitle mb-2">(F you if you click this without knowing what it does)</h6>#}
{# <a class="btn btn-outline-danger" href="/save/all">Save all data</a>#}
{# <a class="btn btn-outline-danger" href="/load/all">Load all data</a>#}
{# <a class="btn btn-outline-danger" href="/load/static">Load static data</a>#}
{# <a class="btn btn-outline-danger" href="/load/dynamic">Load dynamic data</a>#}
{# </div>#}
{# </div>#}
{% endblock body %}

View File

@ -3,6 +3,12 @@ numpy
flask
flask-sqlalchemy
flask-caching
sqlalchemy
requests
werkzeug
fastf1
pandas
pytest