Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Modify real data #451

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common/types.basketball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type teamStats from "../worker/core/team/stats.basketball";

// Should all the extra ones be in teamStats["derived"]?
export type TeamStatAttr =
| typeof teamStats["raw"][number]
| (typeof teamStats)["raw"][number]
| "fgp"
| "oppFgp"
| "fgpAtRim"
Expand Down
2 changes: 1 addition & 1 deletion src/common/types.football.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type teamStats from "../worker/core/team/stats.football";

// Should all the extra ones be in teamStats["derived"]?
export type TeamStatAttr =
| typeof teamStats["raw"][number]
| (typeof teamStats)["raw"][number]
| "mov"
| "oppMov"
| "ptsPerGame"
Expand Down
24 changes: 21 additions & 3 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,14 @@ export type PlayerWithoutKey<PlayerRatings = any> = {
pFatigue?: number;
};

export type PlayerHistoricRatings = {
phrid?: number;
pid: number;
srId?: string;
season: number;
playerRatings: any;
};

export type Player<PlayerRatings = any> = {
pid: number;
} & PlayerWithoutKey<PlayerRatings>;
Expand Down Expand Up @@ -1525,11 +1533,21 @@ import type {
TeamStatAttr as TeamStatAttrBaseball,
TeamStatAttrByPos as TeamStatAttrByPosBaseball,
} from "./types.baseball";
import type { TeamStatAttr as TeamStatAttrBasketball } from "./types.basketball";
import type { TeamStatAttr as TeamStatAttrFootball } from "./types.football";
import type { TeamStatAttr as TeamStatAttrHockey } from "./types.hockey";
import type {
TeamStatAttr as TeamStatAttrBasketball,
PlayerRatings as PlayerRatingsBasketball,
} from "./types.basketball";
import type {
PlayerRatings as PlayerRatingsFootball,
TeamStatAttr as TeamStatAttrFootball,
} from "./types.football";
import type {
TeamStatAttr as TeamStatAttrHockey,
PlayerRatings as PlayerRatingsHockey,
} from "./types.hockey";
import type { TIEBREAKERS } from "./constants";
import type { DropdownOption } from "../ui/hooks/useDropdownOptions";

type TeamStatsPlus = Record<TeamStatAttrBaseball, number> &
Record<TeamStatAttrByPosBaseball, number[]> &
Record<TeamStatAttrBasketball, number> &
Expand Down
9 changes: 9 additions & 0 deletions src/ui/util/menuItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,15 @@ const menuItems: (MenuItemLink | MenuItemHeader)[] = [
path: ["edit_awards"],
text: "Edit Awards",
},
{
type: "link",
active: pageID => pageID === "playerRatingsOverride",
godMode: true,
league: true,
commandPalette: true,
path: ["player_ratings_override"],
text: "Player Ratings Override",
},
{
type: "link",
active: pageID => pageID === "exportLeague",
Expand Down
2 changes: 2 additions & 0 deletions src/ui/util/routeInfos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const routeInfos = {
"/l/:lid/trade_proposals": "tradeProposals",
"/l/:lid/edit_awards": "editAwards",
"/l/:lid/edit_awards/:season": "editAwards",
"/l/:lid/player_ratings_override": "playerRatingsOverride",
"/l/:lid/player_ratings_override/:pid": "playerRatingsOverride",
"/l/:lid/draft": "draft",
"/l/:lid/draft_history": "draftHistory",
"/l/:lid/draft_history/:season": "draftHistory",
Expand Down
235 changes: 235 additions & 0 deletions src/ui/views/PlayerRatingsOverride.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
import type { Player, PlayerHistoricRatings, View } from "src/common/types";
import SelectMultiple from "../components/SelectMultiple";
import { DataTable } from "../components";
import { getCols, helpers, logEvent, realtimeUpdate, toWorker } from "../util";
import { useState } from "react";
import type { DataTableRow } from "../components/DataTable";
import useTitleBar from "../hooks/useTitleBar";

type InputRow = {
season?: number;
pid: number;
ratings: {
rating: number;
name: string;
}[];
};

const getInputRow = (ratings: any[], pid: number, player: Player): InputRow => {
const playerRatings = player.ratings[player.ratings.length - 1];
let row = {
pid: pid,
ratings: ratings.map(rating => {
return {
rating: playerRatings[rating],
name: rating,
};
}),
};

return row;
};

const deletePlayerRatings = async (r: PlayerHistoricRatings) => {
toWorker("main", "deletePlayerHistoricRating", r.phrid);
realtimeUpdate();
};

const getHistoricRatingsColumns = (
playerHistoricRatings: PlayerHistoricRatings[],
cols: any[],
) => {
return playerHistoricRatings.map((r, i) => {
return {
key: i,
data: [
{
value: r.season,
},
...cols.map(rating => r.playerRatings[rating]),
{
value: (
<>
<button
className="btn btn-light-bordered btn-xs"
onClick={() => deletePlayerRatings(r)}
>
Delete
</button>
</>
),
},
],
};
});
};

const PlayerRatingsOverride = ({
players,
player,
playerHistoricRatings,
cols,
godMode,
}: View<"playerRatingsOverride">) => {
useTitleBar({
title: "Player Ratings Override",
jumpTo: true,
});

const savePlayerRatingsOverride = async () => {
try {
if (
playerHistoricRatings.filter(phr => phr.season == newRatings.season)
.length > 0
) {
console.log("yoo");
logEvent({
type: "error",
text: "You cannot have two overrides in the same season. Please delete the previous one",
saveToDb: false,
});
return;
}
console.log("save");
await toWorker("main", "updatePlayerRatingsOverride", newRatings);
realtimeUpdate();
} catch (error) {
console.log(error);
logEvent({
type: "error",
text: error.message,
saveToDb: false,
persistent: true,
});
}
};

const getInputRowColumns = (inputRow: InputRow): DataTableRow[] => {
return [
{
key: 0,
data: [
{
value: (
<>
<input
type="text"
onChange={event => handleAttributeChange(-1, event)}
value={inputRow.season}
className="form-control"
disabled={!godMode}
/>
</>
),
},
...inputRow.ratings.map((r, i) => {
return {
value: (
<>
<input
onChange={event => {
handleAttributeChange(i, event);
}}
value={r.rating}
type="text"
className="form-control"
disabled={!godMode}
/>
</>
),
};
}),
{
value: (
<>
<button
className="btn btn-light-bordered btn-xs"
onClick={savePlayerRatingsOverride}
>
Save
</button>
</>
),
},
],
},
];
};

const handleAttributeChange = (index: number, event: any) => {
const oldRatings = { ...newRatings };
if (index == -1) {
setNewRatings({
...oldRatings,
season: event.target.value,
});
return;
}
let newRatingsModified = oldRatings.ratings;
console.log(event);
newRatingsModified[index] = {
rating: event.target.value,
name: newRatingsModified[index].name,
};
setNewRatings({
...oldRatings,
ratings: newRatingsModified,
});
};

const handleChange = (p: Player | null) => {
console.log("wowo");
if (p != undefined) {
setPlayer(p);
setNewRatings(getInputRow(cols, p.pid, p));
const url = helpers.leagueUrl(["player_ratings_override", p?.pid ?? ""]);
console.log("wow");
updateUrl(url);
}
};

const updateUrl = async (url: string) => {
await realtimeUpdate([], url, undefined, true);
};

console.log("newReload");

const [playerSelected, setPlayer] = useState(player);
console.log(player.pid, playerSelected.pid);
const inputRow = getInputRow(cols, playerSelected.pid, player);
const [newRatings, setNewRatings] = useState(inputRow);
console.log(newRatings);
return (
<div>
<div className="col-lg-4 col-md-6 mb-3">
<SelectMultiple
options={players}
value={playerSelected}
getOptionLabel={p => p.lastName + " " + p.firstName}
getOptionValue={(p: Player) => String(p.pid)}
onChange={(event: Player | null) => handleChange(event)}
/>
</div>

<div>
<DataTable
cols={[
...getCols(["Year", ...cols.map(rating => `rating:${rating}`)]),
{ title: "Save" },
]}
defaultSort={[0, "asc"]}
defaultStickyCols={0}
clickable={false}
hideAllControls
name="Player:Ratings"
rows={[
...getHistoricRatingsColumns(playerHistoricRatings, cols),
...getInputRowColumns(newRatings),
]}
></DataTable>
</div>
</div>
);
};

export default PlayerRatingsOverride;
2 changes: 2 additions & 0 deletions src/ui/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import PlayerRatingDists from "./PlayerRatingDists";
import PlayerRatings from "./PlayerRatings";
import PlayerStatDists from "./PlayerStatDists";
import PlayerGraphs from "./PlayerGraphs";
import PlayerRatingsOverride from "./PlayerRatingsOverride";
import PlayerStats from "./PlayerStats";
import Playoffs from "./Playoffs";
import PowerRankings from "./PowerRankings";
Expand Down Expand Up @@ -185,6 +186,7 @@ export default {
PlayerRatings,
PlayerStatDists,
PlayerGraphs: PlayerGraphs,
PlayerRatingsOverride: PlayerRatingsOverride,
PlayerStats,
Playoffs,
PowerRankings,
Expand Down
32 changes: 32 additions & 0 deletions src/worker/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import type {
DunkAttempt,
AllStarPlayer,
League,
PlayerHistoricRatings,
} from "../../common/types";
import orderBy from "lodash-es/orderBy";
import {
Expand Down Expand Up @@ -3868,6 +3869,35 @@ const updateAwards = async (
await saveAwardsByPlayer(awardsByPlayer, conditions, awards.season, false);
};

const updatePlayerRatingsOverride = async (
input: any,
conditions: Conditions,
): Promise<any> => {
console.log("yo");
const ratings = RATINGS;
let newRatings: any = {};
console.log(input);
ratings.forEach((rating: string) => {
const r = input.ratings.find((rat: any) => rat.name === rating);
newRatings[r.name] = r.rating;
});
console.log(newRatings);
const playerHistoricRatings: PlayerHistoricRatings = {
pid: input.pid,
season: input.season,
playerRatings: newRatings,
};
console.log(playerHistoricRatings);
await idb.cache.playerHistoricRatings.put(playerHistoricRatings);
};

const deletePlayerHistoricRating = async (phrid: number | undefined) => {
console.log(phrid);
if (phrid !== undefined) {
await idb.cache.playerHistoricRatings.delete(phrid);
}
};

const upsertCustomizedPlayer = async (
{
p,
Expand Down Expand Up @@ -4284,6 +4314,7 @@ export default {
createTrade,
deleteOldData,
deleteScheduledEvents,
deletePlayerHistoricRating,
discardUnsavedProgress,
draftLottery,
draftUser,
Expand Down Expand Up @@ -4363,6 +4394,7 @@ export default {
tradeCounterOffer,
uiUpdateLocal,
updateAwards,
updatePlayerRatingsOverride,
updateBudget,
updateConfsDivs,
updateDefaultSettingsOverrides,
Expand Down
Loading