Skip to content

Commit

Permalink
Fix for cid/did not indexed at 0
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbmatter committed Aug 2, 2023
1 parent 679196a commit 2e462a5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
5 changes: 3 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
random debuts should work in legends mode - corban
- probably should be default like cross era

random events
- probability of expansion draft vote
- pick 2 random teams
Expand Down Expand Up @@ -90,8 +93,6 @@ accessibility

add undrafted players to draft history page

add random debuts option to legends leagues https://twitter.com/messages/1928999030-766833475089408000

team finances
- later
- something like "Are you sure you want to discard your unsaved settings changes?" on navigation away with unsaved changes
Expand Down
29 changes: 17 additions & 12 deletions src/worker/core/phase/newPhaseBeforeDraft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,34 +217,38 @@ const doRelocate = async () => {
divs,
numTeamsPerDiv,
);
console.log("clusters", clusters);

for (const div of divs) {
const tids = clusters[div.did].pointIndexes;
for (let i = 0; i < divs.length; i++) {
const tids = clusters[i].pointIndexes;
if (tids) {
realigned[div.did] = tids;
realigned[i] = tids;
}
}

if (!geoSorted) {
// If, for whatever reason, we can't sort clusters geographically (like knowing the location of Atlantic vs Pacific), then try to keep as many teams in the same division as they were previously. Ideally we would test all permutations, but for many divisions that would be slow, so do it a shittier way.
const original = divs.map(() => [] as number[]);
for (const t of currentTeams) {
original[t.did].push(t.tid);
const divIndex = divs.findIndex(div => t.did === div.did);
original[divIndex].push(t.tid);
}

const dids = divs.map(div => div.did);
const divIndexes = divs.map((div, i) => i);

const getBestDid = (tids: number[], didsUsed: Set<number>) => {
let bestScore2 = -Infinity;
let bestDid;
for (let did = 0; did < original.length; did++) {
let bestDid: number | undefined;
for (let divIndex = 0; divIndex < original.length; divIndex++) {
const did = divs[divIndex].did;

if (didsUsed.has(did)) {
continue;
}

let score = 0;
for (const tid of tids) {
if (original[did].includes(tid)) {
if (original[divIndex].includes(tid)) {
score += 1;
}
}
Expand All @@ -261,6 +265,7 @@ const doRelocate = async () => {

return {
did: bestDid,
divIndex: divs.findIndex(div => div.did === bestDid),
score: bestScore2,
};
};
Expand All @@ -270,17 +275,17 @@ const doRelocate = async () => {

// Try a few times with random ordered dids, that's probably good enough
for (let iteration = 0; iteration < 20; iteration++) {
random.shuffle(dids);
random.shuffle(divIndexes);

let score = 0;
const attempt = divs.map(() => [] as number[]);
const didsUsed = new Set<number>();

for (const did of dids) {
const tids = realigned[did];
for (const divIndex of divIndexes) {
const tids = realigned[divIndex];
const result = getBestDid(tids, didsUsed);
didsUsed.add(result.did);
attempt[result.did] = tids;
attempt[result.divIndex] = tids;
score += result.score;
}

Expand Down
5 changes: 3 additions & 2 deletions src/worker/core/team/relocateVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ const relocateVote = async ({
const realigned = autoRelocate.realigned;
if (realign && realigned) {
const divs = g.get("divs");
for (const div of divs) {
const tids = realigned[div.did];
for (let i = 0; i < divs.length; i++) {
const div = divs[i];
const tids = realigned[i];
for (const tid of tids) {
const t = await idb.cache.teams.get(tid);
if (t) {
Expand Down
35 changes: 21 additions & 14 deletions src/worker/views/relocate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,32 @@ const getRealignInfo = (
name: string;
}[][][] = [];

const confs = g.get("confs");
const divs = g.get("divs");

for (const t of teams) {
if (!current[t.cid]) {
current[t.cid] = [];
const confIndex = confs.findIndex(conf => conf.cid === t.cid);
const divIndex = divs.findIndex(div => div.did === t.did);

if (!current[confIndex]) {
current[confIndex] = [];
}
if (!current[t.cid][t.did]) {
current[t.cid][t.did] = [];
if (!current[confIndex][divIndex]) {
current[confIndex][divIndex] = [];
}

const t2 = t.tid === override.tid ? override : t;

current[t.cid][t.did].push({
current[confIndex][divIndex].push({
tid: t2.tid,
region: t2.region,
name: t2.name,
});
}

// Indexed on did, so there are gaps unless we filter out undefined. Then it's no longer indexed by did but that's fine.
for (let cid = 0; cid < current.length; cid++) {
current[cid] = current[cid]
// Indexed on divIndex, so there are gaps unless we filter out undefined. Then it's no longer indexed by divIndex but that's fine.
for (let confIndex = 0; confIndex < current.length; confIndex++) {
current[confIndex] = current[confIndex]
.filter(row => row !== undefined)
.map(row => orderBy(row, ["region", "name"]));
}
Expand Down Expand Up @@ -103,14 +109,15 @@ const updateRelocate = async (inputs: void, updateEvents: UpdateEvents) => {
const confs = g.get("confs");
const divs = g.get("divs");

for (const div of divs) {
const tids = autoRelocate.realigned[div.did];
for (let i = 0; i < divs.length; i++) {
const div = divs[i];
const tids = autoRelocate.realigned[i];
if (tids) {
const conf = confs[div.cid];
if (!realigned[conf.cid]) {
realigned[conf.cid] = [];
const confIndex = confs.findIndex(conf => conf.cid === div.cid);
if (!realigned[confIndex]) {
realigned[confIndex] = [];
}
realigned[conf.cid].push(
realigned[confIndex].push(
orderBy(
tids.map(tid => {
const t =
Expand Down

0 comments on commit 2e462a5

Please sign in to comment.