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

Add WinnerIndex to OverallType #25

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
29 changes: 28 additions & 1 deletion src/SlippiGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { SlpParser } from './utils/slpParser';
import {
StockComputer, ComboComputer, ActionsComputer, ConversionComputer, InputComputer, Stats,
FrameEntryType, FramesType, StatsType, getSinglesPlayerPermutationsFromSettings,
generateOverallStats
generateOverallStats,
StockType
} from './stats';

/**
Expand Down Expand Up @@ -147,6 +148,7 @@ export class SlippiGame {
actionCounts: this.actionsComputer.fetch(),
overall: overall,
gameComplete: this.parser.getGameEnd() !== null,
winningPlayerIndex: this.determineWinningPlayerIndex(stocks)
};

if (this.parser.getGameEnd() !== null) {
Expand All @@ -160,6 +162,31 @@ export class SlippiGame {
return stats;
}

public determineWinningPlayerIndex(stocks: StockType[]): number {
if (stocks.length == 0) return -1;

var playerIndexOccurences: Array<{playerIndex: number, count: number, lastPercent: number}> = [];

stocks.forEach(stockType => {
if(!playerIndexOccurences.some(occurences => occurences.playerIndex === stockType.playerIndex)){
playerIndexOccurences.push({playerIndex: stockType.playerIndex, count: 0, lastPercent: 0});
}

var occurence = playerIndexOccurences.find(occurence => occurence.playerIndex == stockType.playerIndex);
occurence.count++;
occurence.lastPercent = stockType.endPercent;
});

var sortedByOccurence = _.sortBy(playerIndexOccurences, occurence => occurence.count);
var mostOccurences = sortedByOccurence[sortedByOccurence.length - 1];

var matchingOccurences = sortedByOccurence.filter(occurence => occurence.count == mostOccurences.count);
if (matchingOccurences.length == 1) return mostOccurences.playerIndex;

var sortedByPercent = _.sortBy(playerIndexOccurences, occurence => occurence.lastPercent);
return sortedByPercent[sortedByPercent.length - 1].playerIndex;
}

public getMetadata(): MetadataType {
if (this.metadata) {
return this.metadata;
Expand Down
1 change: 1 addition & 0 deletions src/stats/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type StatsType = {
combos: ComboType[];
actionCounts: ActionCountsType[];
overall: OverallType[];
winningPlayerIndex: number;
};

export type RatioType = {
Expand Down
1 change: 1 addition & 0 deletions src/stats/overall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function generateOverallStats(
const conversions = _.get(conversionsByPlayer, playerIndex) || [];
const successfulConversions = conversions.filter(conversion => conversion.moves.length > 1);
const opponentStocks = _.get(stocksByPlayer, opponentIndex) || [];
const playerStocks = _.get(stocksByPlayer, playerIndex) || [];
const opponentEndedStocks = _.filter(opponentStocks, 'endFrame');

const conversionCount = conversions.length;
Expand Down
7 changes: 4 additions & 3 deletions src/stats/stocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import _ from 'lodash';

import { FrameEntryType, FramesType, isDead, didLoseStock, PlayerIndexedType, StockType } from "./common";
import { StatComputer } from './stats';
import { PostFrameUpdateType } from '../utils/slpReader';

interface StockState {
stock: StockType | null | undefined;
Expand Down Expand Up @@ -38,10 +39,10 @@ export class StockComputer implements StatComputer<StockType[]> {

function handleStockCompute(frames: FramesType, state: StockState, indices: PlayerIndexedType, frame: FrameEntryType, stocks: StockType[]): void {
const playerFrame = frame.players[indices.playerIndex].post;
// FIXME: use PostFrameUpdateType instead of any
const prevPlayerFrame: any = _.get(

const prevPlayerFrame: PostFrameUpdateType = _.get(
frames, [playerFrame.frame - 1, 'players', indices.playerIndex, 'post'], {}
);
) as PostFrameUpdateType;

// If there is currently no active stock, wait until the player is no longer spawning.
// Once the player is no longer spawning, start the stock
Expand Down