From e5f40cd90f5dcb34c2c77d144c923af1e289a970 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 20 Aug 2023 14:25:38 +0300 Subject: [PATCH] Make tournament summary give set win for rosters of 4 --- app/features/mmr/mmr-utils.server.ts | 5 +-- app/features/mmr/mmr-utils.ts | 4 +++ .../sendouq/queries/addSkills.server.ts | 2 +- .../core/summarizer.server.ts | 35 ++++++++++++++++--- .../core/summarizer.test.ts | 19 ++++++++++ .../queries/addSummary.server.ts | 2 +- 6 files changed, 57 insertions(+), 10 deletions(-) diff --git a/app/features/mmr/mmr-utils.server.ts b/app/features/mmr/mmr-utils.server.ts index aae3d0115..a1e1c42ae 100644 --- a/app/features/mmr/mmr-utils.server.ts +++ b/app/features/mmr/mmr-utils.server.ts @@ -1,6 +1,7 @@ import { rating } from "openskill"; import { findCurrentSkillByUserId } from "./queries/findCurrentSkillByUserId.server"; import { findCurrentTeamSkillByIdentifier } from "./queries/findCurrentTeamSkillByIdentifier.server"; +import { identifierToUserIds } from "./mmr-utils"; export function queryCurrentUserRating({ userId, @@ -57,7 +58,3 @@ export function queryTeamPlayerRatingAverage({ playerRatings.length, }; } - -export function identifierToUserIds(identifier: string) { - return identifier.split("-").map(Number); -} diff --git a/app/features/mmr/mmr-utils.ts b/app/features/mmr/mmr-utils.ts index 24fccdc0b..690815ef3 100644 --- a/app/features/mmr/mmr-utils.ts +++ b/app/features/mmr/mmr-utils.ts @@ -72,3 +72,7 @@ export function userIdsToIdentifier(userIds: number[]) { invariant(userIds.length === 4, "userIds for identifier must be length 4"); return [...userIds].sort((a, b) => a - b).join("-"); } + +export function identifierToUserIds(identifier: string) { + return identifier.split("-").map(Number); +} diff --git a/app/features/sendouq/queries/addSkills.server.ts b/app/features/sendouq/queries/addSkills.server.ts index 4cafa8e0d..9b58efbdc 100644 --- a/app/features/sendouq/queries/addSkills.server.ts +++ b/app/features/sendouq/queries/addSkills.server.ts @@ -1,7 +1,7 @@ import { ordinal } from "openskill"; import { sql } from "~/db/sql"; import type { Skill } from "~/db/types"; -import { identifierToUserIds } from "~/features/mmr/mmr-utils.server"; +import { identifierToUserIds } from "~/features/mmr/mmr-utils"; const getStm = (type: "user" | "team") => sql.prepare(/* sql */ ` diff --git a/app/features/tournament-bracket/core/summarizer.server.ts b/app/features/tournament-bracket/core/summarizer.server.ts index a712a9f5d..14d560819 100644 --- a/app/features/tournament-bracket/core/summarizer.server.ts +++ b/app/features/tournament-bracket/core/summarizer.server.ts @@ -10,7 +10,11 @@ import invariant from "tiny-invariant"; import { removeDuplicates } from "~/utils/arrays"; import type { FinalStanding } from "./finalStandings.server"; import type { Rating } from "openskill/dist/types"; -import { rate, userIdsToIdentifier } from "~/features/mmr/mmr-utils"; +import { + rate, + userIdsToIdentifier, + identifierToUserIds, +} from "~/features/mmr/mmr-utils"; import shuffle from "just-shuffle"; import type { Unpacked } from "~/utils/types"; @@ -393,10 +397,33 @@ function playerResultDeltas({ } } - const allUserIds = removeDuplicates(match.maps.flatMap((m) => m.userIds)); + const mostPopularUserIds = (() => { + const alphaIdentifiers: string[] = []; + const bravoIdentifiers: string[] = []; - for (const ownerUserId of allUserIds) { - for (const otherUserId of allUserIds) { + for (const map of match.maps) { + const alphaUserIds = map.userIds.filter( + (userId) => userIdsToTeamId[userId] === match.opponentOne.id + ); + const bravoUserIds = map.userIds.filter( + (userId) => userIdsToTeamId[userId] === match.opponentTwo.id + ); + + alphaIdentifiers.push(userIdsToIdentifier(alphaUserIds)); + bravoIdentifiers.push(userIdsToIdentifier(bravoUserIds)); + } + + const alphaIdentifier = selectMostPopular(alphaIdentifiers); + const bravoIdentifier = selectMostPopular(bravoIdentifiers); + + return [ + ...identifierToUserIds(alphaIdentifier), + ...identifierToUserIds(bravoIdentifier), + ]; + })(); + + for (const ownerUserId of mostPopularUserIds) { + for (const otherUserId of mostPopularUserIds) { if (ownerUserId === otherUserId) continue; const ownTournamentTeamId = userIdsToTeamId[ownerUserId]; diff --git a/app/features/tournament-bracket/core/summarizer.test.ts b/app/features/tournament-bracket/core/summarizer.test.ts index 23dcf0e9e..e0b7c0ae3 100644 --- a/app/features/tournament-bracket/core/summarizer.test.ts +++ b/app/features/tournament-bracket/core/summarizer.test.ts @@ -267,6 +267,25 @@ TournamentSummary( } ); +TournamentSummary( + "In the case of sub calculates player results based on the most common roster", + () => { + const summary = summarize({ + results: resultsWithSubbedRoster, + }); + + assert.not.ok( + summary.playerResultDeltas.find( + (p) => + p.ownerUserId === 5 && + p.otherUserId === 20 && + (p.setWins > 0 || p.setLosses > 0) + ), + "player 5 should not have a result against player 20 (sub for only one game)" + ); + } +); + TournamentSummary("calculates results of mates", () => { const summary = summarize(); diff --git a/app/features/tournament-bracket/queries/addSummary.server.ts b/app/features/tournament-bracket/queries/addSummary.server.ts index 6af26f938..6f61cb75a 100644 --- a/app/features/tournament-bracket/queries/addSummary.server.ts +++ b/app/features/tournament-bracket/queries/addSummary.server.ts @@ -2,7 +2,7 @@ import { sql } from "~/db/sql"; import type { TournamentSummary } from "../core/summarizer.server"; import { ordinal } from "openskill"; import type { Skill } from "~/db/types"; -import { identifierToUserIds } from "~/features/mmr/mmr-utils.server"; +import { identifierToUserIds } from "~/features/mmr/mmr-utils"; const addSkillStm = sql.prepare(/* sql */ ` insert into "Skill" (