diff --git a/src/SlippiGame.ts b/src/SlippiGame.ts index 0291c2ca..19f54fcf 100644 --- a/src/SlippiGame.ts +++ b/src/SlippiGame.ts @@ -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); } } diff --git a/src/utils/getWinners.ts b/src/utils/getWinners.ts index 1cf34a8a..2de18e53 100644 --- a/src/utils/getWinners.ts +++ b/src/utils/getWinners.ts @@ -1,4 +1,4 @@ -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"; @@ -6,6 +6,7 @@ export function getWinners( gameEnd: GameEndType, settings: Pick, finalPostFrameUpdates: PostFrameUpdateType[], + latestFrame: FrameEntryType | null, ): PlacementType[] { const { placements, gameEndMethod, lrasInitiatorIndex } = gameEnd; const { players, isTeams } = settings; @@ -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 [];