Compare commits

...

3 Commits

Author SHA1 Message Date
6cd253d5f0
prepare for live updates 2024-11-02 09:49:49 +01:00
97e16e6be2
go through possible teams with brute force 2024-11-02 09:48:17 +01:00
a444c7efd3
add table with suggested teams 2024-11-02 09:47:25 +01:00
3 changed files with 154 additions and 3 deletions

96
brute_force.py Normal file
View File

@ -0,0 +1,96 @@
import numpy as np
import json
import itertools
from pathlib import Path
rgen = np.random.default_rng(seed=42)
def load_stats():
db = Path("local_team.db")
players_list = "prefs_page/src/players.json"
with open(players_list, "r") as f:
players = json.load(f)
preferences = {}
for line in open(db, "r"):
date, person, prefs = line.split("\t")
if not person.strip() or not prefs.strip():
continue
preferences[person] = [p.strip() for p in prefs.split(",")]
for player in players:
if player not in preferences:
preferences[player] = []
return players, preferences
# synthetical data
# rgen = np.random.default_rng(seed=42)
# n_prefs = 8
# preferences = {
# player: rgen.choice(players, size=n_prefs, replace=False) for player in players
# }
def team_table_json():
players, preferences = load_stats()
mean, team0, team1 = apply_brute_force(players, preferences)
data = {}
for i, team in enumerate([team0, team1]):
tablename = f"Team {i+1}"
data[tablename] = []
for p in sorted(list(team)):
prefs = preferences[p]
matches = sum([pref in team for pref in preferences[p]])
data[tablename].append([p, matches, len(prefs)])
with open("prefs_page/src/table.json", "w") as f:
json.dump(data, f)
def apply_brute_force(players, preferences):
def evaluate_teams(team0, team1):
scores = []
percentages = []
for team in [team0, team1]:
for p in team:
scores.append(sum([pref in team for pref in preferences[p]]))
if len(preferences[p]) > 0:
percentages.append(scores[-1] / len(preferences[p]))
return np.mean(scores), np.mean(percentages) * 100
best_score = [(0, [], [])]
best_percentage = [(0, [], [])]
for team0 in itertools.combinations(players, 9):
team1 = {player for player in players if player not in team0}
score, percentage = evaluate_teams(team0, team1)
if score > best_score[0][0]:
best_score = [(score, team0, team1)]
if score == best_score[0][0] and set(team0) != set(best_score[0][1]):
best_score.append((score, team0, team1))
if percentage > best_percentage[0][0]:
best_percentage = [(percentage, team0, team1)]
if percentage == best_percentage[0][0] and set(team0) != set(best_score[0][1]):
best_percentage.append((percentage, team0, team1))
if __name__ == "__main__":
for result in best_score:
print(result[0])
print(result[1])
print(result[2])
for result in best_percentage:
print(result[0])
print(result[1])
print(result[2])
# team_table(score, team0, team1)
return best_score[0]
if __name__ == "__main__":
team_table_json()

View File

@ -17,6 +17,7 @@ import {
FormHelperText,
Stack,
} from "@mui/material";
import TeamTables from "./Table";
function getStyles(name: string, players: readonly string[], theme: Theme) {
return {
@ -29,7 +30,8 @@ function getStyles(name: string, players: readonly string[], theme: Theme) {
async function submit(
person: string,
players: string[],
setResponseStatus: (value: number) => void
setResponseStatus: (value: number) => void,
setDialog: (value: boolean) => void
) {
// console.log(JSON.stringify({ person: person, players: players }));
const response = await fetch("https://0124816.xyz/team/submit/", {
@ -40,6 +42,7 @@ async function submit(
body: JSON.stringify({ person: person, players: players }),
});
setResponseStatus(response.status);
setDialog(true);
}
type SubmitButtonProps = {
@ -49,17 +52,20 @@ type SubmitButtonProps = {
function SubmitButton(props: SubmitButtonProps) {
const [responseStatus, setResponseStatus] = React.useState(0);
const [dialog, setDialog] = React.useState(false);
return (
<div>
<Button
variant="contained"
color={responseStatus === 200 ? "success" : "primary"}
onClick={() => submit(props.person, props.players, setResponseStatus)}
onClick={() =>
submit(props.person, props.players, setResponseStatus, setDialog)
}
>
submit
</Button>
<Dialog open={responseStatus === 200}>
<Dialog onClose={() => setDialog(false)} open={dialog}>
<DialogTitle>thank you. please leave now.</DialogTitle>
</Dialog>
</div>
@ -194,6 +200,7 @@ function App() {
{SubmitButton({ person, players })}
<p>now: click submit.</p>
</div>
{TeamTables()}
<p className="read-the-docs">
something not working? message <a href="https://t.me/x0124816">me</a>.
</p>

48
prefs_page/src/Table.tsx Normal file
View File

@ -0,0 +1,48 @@
import * as React from "react";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import Paper from "@mui/material/Paper";
import data from "./table.json";
export default function TeamTables() {
return (
<>
{Object.keys(data).map((teamname) => (
<>
<h1>{teamname as String}</h1>
<TableContainer component={Paper}>
<Table
sx={{ m: "8px auto" }}
size="small"
aria-label="simple table"
>
<TableHead>
<TableRow>
<TableCell>player</TableCell>
<TableCell>wishes fulfilled</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data[teamname].map((row) => (
<TableRow
key={row[0]}
sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
>
<TableCell key={row[0] + "-cell"}>{row[0]}</TableCell>
<TableCell key={row[0] + "-cell-2"}>
{"♥️".repeat(row[1]) + "♡".repeat(row[2] - row[1])}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</>
))}
</>
);
}