Skip to content

Commit

Permalink
Season initial powers script Closes #1450
Browse files Browse the repository at this point in the history
  • Loading branch information
Sendouc committed Sep 9, 2023
1 parent 6692241 commit 6b9f118
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"delete-user": "node --experimental-specifier-resolution=node --loader ts-node/esm -r tsconfig-paths/register scripts/delete-user.ts",
"ban-user": "node --experimental-specifier-resolution=node --loader ts-node/esm -r tsconfig-paths/register scripts/ban-user.ts",
"delete-skill": "node --experimental-specifier-resolution=node --loader ts-node/esm -r tsconfig-paths/register scripts/delete-skill.ts",
"season-initial-powers": "node --experimental-specifier-resolution=node --loader ts-node/esm -r tsconfig-paths/register scripts/season-initial-powers.ts",
"lint:ts": "eslint . --ext .ts,.tsx",
"lint:css": "stylelint \"app/styles/**/*.css\"",
"prettier:check": "prettier --check . --log-level warn",
Expand Down
115 changes: 115 additions & 0 deletions scripts/season-initial-powers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* eslint-disable no-console */
import "dotenv/config";
import { ordinal } from "openskill";
import invariant from "tiny-invariant";
import { sql } from "~/db/sql";
import type { Skill } from "~/db/types";
import type { TierName } from "~/features/mmr/mmr-constants";
import { freshUserSkills } from "~/features/mmr/tiered.server";
import { addInitialSkill } from "~/features/sendouq/queries/addInitialSkill.server";

const rawNth = process.argv[2]?.trim();

invariant(rawNth, "nth of new season needed (argument 1)");

const nth = Number(rawNth);
invariant(!Number.isNaN(nth), "nth of new season must be a number");

const skillsExistStm = sql.prepare(/* sql */ `
select
1
from "Skill"
where
"season" = @season
limit 1
`);

invariant(
skillsExistStm.get({ season: nth - 1 }),
`No skills for season ${nth - 1}`,
);
invariant(
!skillsExistStm.get({ season: nth }),
`Skills for season ${nth} already exist`,
);

// from prod database:
// sqlite> select avg(sigma) from skill where matchesCount > 10 and matchesCount < 20;
// 6.63571559436444
// sqlite> select avg(sigma) from skill where matchesCount > 15 and matchesCount < 25;
// 6.4242759350389
const DEFAULT_NEW_SIGMA = 6.5;

const TIER_TO_NEW_TIER: Record<TierName, TierName> = {
IRON: "BRONZE",
BRONZE: "BRONZE",
SILVER: "SILVER",
GOLD: "GOLD",
PLATINUM: "PLATINUM",
DIAMOND: "DIAMOND",
LEVIATHAN: "DIAMOND",
};

const allSkills = Object.entries(freshUserSkills(nth - 1).userSkills)
.map(([userId, skill]) => ({ userId: Number(userId), ...skill }))
.filter((s) => !s.approximate)
.sort((a, b) => b.ordinal - a.ordinal);
const skillsToConsider = allSkills.filter((s) =>
Object.values(TIER_TO_NEW_TIER).includes(s.tier.name),
);

const groupedSkills = skillsToConsider.reduce(
(acc, skill) => {
const { tier } = skill;
if (!acc[tier.name]) {
acc[tier.name] = [];
}
acc[tier.name].push(skill);
return acc;
},
{} as Record<TierName, typeof skillsToConsider>,
);

const skillStm = sql.prepare(/* sql */ `
select
*
from "Skill"
where
"userId" = @userId
and "ordinal" = @ordinal
`);
const midPoints = Object.entries(groupedSkills).reduce(
(acc, [tier, skills]) => {
const midPoint = skills[Math.floor(skills.length / 2)];
const midPointSkill = skillStm.get(midPoint) as Skill;
invariant(midPointSkill, "midPointSkill not found");

acc[tier as TierName] = midPointSkill;
return acc;
},
{} as Record<TierName, Skill>,
);

const newSkills = allSkills.map((s) => {
const newTier = TIER_TO_NEW_TIER[s.tier.name];
const mu = midPoints[newTier].mu;
const sigma = DEFAULT_NEW_SIGMA;

return {
userId: s.userId,
sigma,
mu,
ordinal: ordinal({ sigma, mu }),
season: nth,
};
});

sql.transaction(() => {
for (const skill of newSkills) {
addInitialSkill(skill);
}
})();

console.log(
`Done adding new skills for season ${nth} (${newSkills.length} added)`,
);

0 comments on commit 6b9f118

Please sign in to comment.