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

Lose best player Challenge #311

Merged
merged 3 commits into from
Sep 7, 2020
Merged
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
1 change: 1 addition & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export type GameAttributesLeague = {
challengeNoFreeAgents: boolean;
challengeNoRatings: boolean;
challengeNoTrades: boolean;
challengeLoseBestPlayer: boolean;
confs: { cid: number; name: string }[];
daysLeft: number;
defaultStadiumCapacity: number;
Expand Down
7 changes: 7 additions & 0 deletions src/ui/views/GodMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Key =
| "challengeNoFreeAgents"
| "challengeNoRatings"
| "challengeNoTrades"
| "challengeLoseBestPlayer"
| "realPlayerDeterminism"
| "repeatSeason"
| "ties"
Expand Down Expand Up @@ -501,6 +502,12 @@ export const options: {
name: "No Trades",
type: "bool",
},
{
category: "Challenge Modes",
key: "challengeLoseBestPlayer",
name: "Lose Best Player",
type: "bool",
},
{
category: "Challenge Modes",
key: "challengeNoRatings",
Expand Down
32 changes: 32 additions & 0 deletions src/ui/views/NewLeague.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ type State = {
challengeNoFreeAgents: boolean;
challengeNoRatings: boolean;
challengeNoTrades: boolean;
challengeLoseBestPlayer: boolean;
equalizeRegions: boolean;
repeatSeason: boolean;
noStartingInjuries: boolean;
Expand Down Expand Up @@ -451,6 +452,9 @@ type Action =
| {
type: "toggleChallengeNoTrades";
}
| {
type: "toggleChallengeLoseBestPlayer";
}
| {
type: "toggleEqualizeRegions";
}
Expand Down Expand Up @@ -572,6 +576,7 @@ const reducer = (state: State, action: Action): State => {
challengeNoFreeAgents: boolean;
challengeNoRatings: boolean;
challengeNoTrades: boolean;
challengeLoseBestPlayer: boolean;
equalizeRegions: boolean;
expandOptions: boolean;
repeatSeason: boolean;
Expand All @@ -580,6 +585,7 @@ const reducer = (state: State, action: Action): State => {
challengeNoFreeAgents: state.challengeNoFreeAgents,
challengeNoRatings: state.challengeNoRatings,
challengeNoTrades: state.challengeNoTrades,
challengeLoseBestPlayer: state.challengeLoseBestPlayer,
equalizeRegions: state.equalizeRegions,
expandOptions: state.expandOptions,
repeatSeason: state.repeatSeason,
Expand Down Expand Up @@ -667,6 +673,12 @@ const reducer = (state: State, action: Action): State => {
challengeNoTrades: !state.challengeNoTrades,
};

case "toggleChallengeLoseBestPlayer":
return {
...state,
challengeLoseBestPlayer: !state.challengeLoseBestPlayer,
};

case "toggleEqualizeRegions":
return {
...state,
Expand Down Expand Up @@ -755,6 +767,7 @@ const NewLeague = (props: View<"newLeague">) => {
challengeNoFreeAgents: false,
challengeNoRatings: false,
challengeNoTrades: false,
challengeLoseBestPlayer: false,
repeatSeason: false,
noStartingInjuries: false,
equalizeRegions: false,
Expand Down Expand Up @@ -845,6 +858,7 @@ const NewLeague = (props: View<"newLeague">) => {
challengeNoFreeAgents: state.challengeNoFreeAgents,
challengeNoRatings: state.challengeNoRatings,
challengeNoTrades: state.challengeNoTrades,
challengeLoseBestPlayer: state.challengeLoseBestPlayer,
repeatSeason: state.repeatSeason,
noStartingInjuries: state.noStartingInjuries,
equalizeRegions: state.equalizeRegions,
Expand Down Expand Up @@ -892,6 +906,7 @@ const NewLeague = (props: View<"newLeague">) => {
state.challengeNoFreeAgents,
state.challengeNoRatings,
state.challengeNoTrades,
state.challengeLoseBestPlayer,
state.customize,
state.difficulty,
state.equalizeRegions,
Expand Down Expand Up @@ -1068,6 +1083,23 @@ const NewLeague = (props: View<"newLeague">) => {
No trades
</label>
</div>
<div className="form-check mb-2">
<input
className="form-check-input"
type="checkbox"
id="new-league-challengeLoseBestPlayer"
checked={state.challengeLoseBestPlayer}
onChange={() => {
dispatch({ type: "toggleChallengeLoseBestPlayer" });
}}
/>
<label
className="form-check-label"
htmlFor="new-league-challengeLoseBestPlayer"
>
Lose best player at end of each season
</label>
</div>
<div className="form-check mb-2">
<input
className="form-check-input"
Expand Down
7 changes: 7 additions & 0 deletions src/worker/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ const createLeague = async ({
challengeNoFreeAgents,
challengeNoRatings,
challengeNoTrades,
challengeLoseBestPlayer,
repeatSeason,
noStartingInjuries,
equalizeRegions,
Expand All @@ -283,6 +284,7 @@ const createLeague = async ({
challengeNoFreeAgents: boolean;
challengeNoRatings: boolean;
challengeNoTrades: boolean;
challengeLoseBestPlayer: boolean;
repeatSeason: boolean;
noStartingInjuries: boolean;
equalizeRegions: boolean;
Expand Down Expand Up @@ -374,6 +376,11 @@ const createLeague = async ({
"challengeNoTrades",
challengeNoTrades,
);
upsertGameAttribute(
leagueFile.gameAttributes,
"challengeLoseBestPlayer",
challengeLoseBestPlayer,
);
upsertGameAttribute(
leagueFile.gameAttributes,
"equalizeRegions",
Expand Down
34 changes: 33 additions & 1 deletion src/worker/core/phase/newPhaseBeforeDraft.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PLAYER } from "../../../common";
import { draft, player, season, team } from "..";
import { idb } from "../../db";
import release from "../player";
import {
achievement,
defaultGameAttributes,
Expand All @@ -17,13 +18,13 @@ import type {
MinimalPlayerRatings,
Player,
} from "../../../common/types";
import killOne from "../player/killOne";

const newPhaseBeforeDraft = async (
conditions: Conditions,
liveGameInProgress: boolean = false,
): Promise<PhaseReturn> => {
achievement.check("afterPlayoffs", conditions);

await season.doAwards(conditions);
const teams = await idb.getCopies.teamsPlus({
attrs: ["tid"],
Expand All @@ -50,6 +51,37 @@ const newPhaseBeforeDraft = async (
await idb.cache.players.put(p);
}
}
const challengeLoseBestPlayer = g.get("challengeLoseBestPlayer");
if (challengeLoseBestPlayer) {
const tids = g.get("userTids");
for (const tid of tids) {
const playersAll = await idb.cache.players.indexGetAll(
"playersByTid",
tid,
);
let pid = 0;
let bestOvr = 0;
let bestPlayer: Player | undefined;
//Find best player
playersAll.forEach((p: Player) => {
if (p.ratings.slice(-1)[0]["ovr"] > bestOvr) {
bestOvr = p.ratings.slice(-1)[0]["ovr"];
pid = p.pid;
bestPlayer = p;
}
});
if (bestPlayer) {
if (bestPlayer.real) {
//If he´s real make it a free agent
await player.retire(bestPlayer, conditions);
await idb.cache.players.put(bestPlayer);
} else {
//if he´s not real kill him
killOne({}, bestPlayer);
}
}
}
}

if (!g.get("repeatSeason")) {
// Do annual tasks for each player, like checking for retirement
Expand Down
30 changes: 18 additions & 12 deletions src/worker/core/player/killOne.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import retire from "./retire";
import { idb } from "../../db";
import { g, helpers, logEvent, random } from "../../util";
import type { Conditions } from "../../../common/types";
import type { Conditions, Player } from "../../../common/types";

const killOne = async (conditions: Conditions) => {
const killOne = async (conditions: Conditions, player?: Player) => {
const reason = random.choice([
"died from a drug overdose",
"was killed by a gunshot during an altercation at a night club",
Expand Down Expand Up @@ -75,17 +75,23 @@ const killOne = async (conditions: Conditions) => {
"drowned while crossing the Saleph River",
"uploaded himself to the cloud and can no longer participate in corporeal pursuits",
]);
let p: Player;
let tid;
if (!player) {
// Pick random team
const teams = (await idb.cache.teams.getAll()).filter(t => !t.disabled);
tid = random.choice(teams).tid;
const players = await idb.cache.players.indexGetAll("playersByTid", tid);

// Pick random team
const teams = (await idb.cache.teams.getAll()).filter(t => !t.disabled);
const tid = random.choice(teams).tid;
const players = await idb.cache.players.indexGetAll("playersByTid", tid);

// Pick a random player on that team
const p = random.choice(players.filter(p => !p.real));
if (!p) {
// Could happen, with real rosters
return;
// Pick a random player on that team
p = random.choice(players.filter(p => !p.real));
if (!p) {
// Could happen, with real rosters
return;
}
} else {
p = player;
tid = player.tid;
}

await retire(p, conditions, {
Expand Down
1 change: 1 addition & 0 deletions src/worker/util/defaultGameAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const defaultGameAttributes: GameAttributesLeagueWithHistory = {
challengeNoFreeAgents: false,
challengeNoRatings: false,
challengeNoTrades: false,
challengeLoseBestPlayer: false,
repeatSeason: undefined,
equalizeRegions: false,
realPlayerDeterminism: 0,
Expand Down
1 change: 1 addition & 0 deletions src/worker/views/godMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const updateGodMode = async (inputs: unknown, updateEvents: UpdateEvents) => {
challengeNoDraftPicks: g.get("challengeNoDraftPicks"),
challengeNoFreeAgents: g.get("challengeNoFreeAgents"),
challengeNoTrades: g.get("challengeNoTrades"),
challengeLoseBestPlayer: g.get("challengeLoseBestPlayer"),
challengeNoRatings: g.get("challengeNoRatings"),
realPlayerDeterminism: g.get("realPlayerDeterminism"),
repeatSeason: !!g.get("repeatSeason"),
Expand Down
3 changes: 3 additions & 0 deletions tools/lib/generateJSONSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ const generateJSONSchema = (sport /*: string*/) => {
gameAttribute("challengeNoTrades", {
type: "boolean",
}),
gameAttribute("challengeLoseBestPlayer", {
type: "boolean",
}),
gameAttribute("confs", {
type: "array",
minItems: 1,
Expand Down