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 3 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
19 changes: 19 additions & 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 All @@ -47,6 +48,7 @@ export function generateOverallStats(
conversionCount: conversionCount,
totalDamage: totalDamage,
killCount: killCount,
winnerIndex: determineWinner(playerStocks, opponentStocks),

successfulConversions: getRatio(successfulConversionCount, conversionCount),
inputsPerMinute: getRatio(inputCount, gameMinutes),
Expand All @@ -67,6 +69,23 @@ export function generateOverallStats(
return overall;
}

function determineWinner(playerStocks: StockType[], opponentStocks: StockType[]) : number {

if (playerStocks.length == 0 && opponentStocks.length == 0) return -1; //unknown winner;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we avoid returning undefined by taking out the if statement and moving the return -1 to the end of the function, will avoid weird edge cases or missing data errors later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!


if (playerStocks.length === opponentStocks.length) {
var playerLastStock = playerStocks[playerStocks.length - 1]
var opponentLastStock = opponentStocks[opponentStocks.length - 1];

if (playerLastStock.endPercent > opponentLastStock.endPercent) return opponentLastStock.playerIndex;
if (playerLastStock.endPercent < opponentLastStock.endPercent) return playerLastStock.playerIndex;
}

// More stock events, means more deaths.
if (playerStocks.length > opponentStocks.length) return playerStocks[0].opponentIndex;
if (playerStocks.length < opponentStocks.length) return opponentStocks[0].playerIndex;
}

function getRatio(count: number, total: number): RatioType {
return {
count: count,
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