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

Update getWinner() to calculate winner for legacy slps #140

Open
wants to merge 4 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
4 changes: 3 additions & 1 deletion src/SlippiGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ export class SlippiGame {
finalPostFrameUpdates = extractFinalPostFrameUpdates(slpfile);
}

const latestFrame = this.getLatestFrame();

closeSlpFile(slpfile);
return getWinners(gameEnd, settings, finalPostFrameUpdates);
return getWinners(gameEnd, settings, finalPostFrameUpdates, latestFrame);
}
}
26 changes: 25 additions & 1 deletion src/utils/getWinners.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { GameEndType, GameStartType, PlacementType, PostFrameUpdateType } from "../types";
import type { FrameEntryType, GameEndType, GameStartType, PlacementType, PostFrameUpdateType } from "../types";
import { GameEndMethod } from "../types";
import { exists } from "./exists";

export function getWinners(
gameEnd: GameEndType,
settings: Pick<GameStartType, "players" | "isTeams">,
finalPostFrameUpdates: PostFrameUpdateType[],
latestFrame: FrameEntryType | null,
): PlacementType[] {
const { placements, gameEndMethod, lrasInitiatorIndex } = gameEnd;
const { players, isTeams } = settings;
Expand Down Expand Up @@ -53,6 +54,29 @@ export function getWinners(
return [];
}

// should only be true for legacy slps with no placements (< 3.13.0) or games without gameEnd payload
// should probably be expanded to include doubles/ffa but only works on signles for now
if (
latestFrame &&
placements.every((placement) => placement.position === null) &&
gameEndMethod === GameEndMethod.GAME
) {
if (players.length === 2) {
// not sure how safe the !s are here
const p1Idx = players[0]?.playerIndex!;
const p2Idx = players[1]?.playerIndex!;
const p1Stocks = latestFrame.players[0]?.post.stocksRemaining;
const p2Stocks = latestFrame.players[p2Idx]?.post.stocksRemaining;
if (p1Stocks !== undefined && p1Stocks !== null && p2Stocks !== undefined && p2Stocks !== null) {
if (p2Stocks === 0 && p1Stocks > 0) {
return [{ playerIndex: p1Idx, position: 0 }];
} else if (p1Stocks === 0 && p2Stocks > 0) {
return [{ playerIndex: p2Idx, position: 0 }];
}
}
}
}

const firstPosition = placements.find((placement) => placement.position === 0);
if (!firstPosition) {
return [];
Expand Down