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

Fix edge-case stats calculation #57

Merged
merged 18 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ module.exports = {
verbose: true,
globals: {
"ts-jest": {
diagnostics: {
warnOnly: true,
},
diagnostics: false,
},
},
};
Binary file added slp/consistencyTest/BowsVDK-SB-63.slp
Binary file not shown.
Binary file added slp/consistencyTest/FalcVBows-5UB-67.slp
Binary file not shown.
Binary file added slp/consistencyTest/GanonVDK-5UB-73.slp
Binary file not shown.
Binary file added slp/consistencyTest/KirbyVDK-Neutral-17.slp
Binary file not shown.
Binary file added slp/consistencyTest/MewTwoVDK-SB-42.slp
Binary file not shown.
Binary file added slp/consistencyTest/NessVFox-Absorb.slp
Binary file not shown.
Binary file added slp/consistencyTest/PichuVSelf-All-22.slp
Binary file not shown.
Binary file added slp/consistencyTest/Puff-MagnifyingGlass-10.slp
Binary file not shown.
Binary file added slp/consistencyTest/PuffVFalcon-Sing.slp
Binary file not shown.
Binary file added slp/consistencyTest/YoshiVDK-Egg-13.slp
Binary file not shown.
17 changes: 14 additions & 3 deletions src/stats/combos.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import _ from "lodash";
import { FrameEntryType, FramesType, PostFrameUpdateType } from "../types";
import { MoveLandedType, ComboType, PlayerIndexedType } from "./common";
import { isDamaged, isGrabbed, calcDamageTaken, isTeching, didLoseStock, Timers, isDown, isDead } from "./common";
import {
isDamaged,
isGrabbed,
isCommandGrabbed,
calcDamageTaken,
isTeching,
didLoseStock,
Timers,
isDown,
isDead,
} from "./common";
import { StatComputer } from "./stats";

interface ComboState {
Expand Down Expand Up @@ -66,6 +76,7 @@ function handleComboCompute(
const oppActionStateId = opponentFrame.actionStateId!;
const opntIsDamaged = isDamaged(oppActionStateId);
const opntIsGrabbed = isGrabbed(oppActionStateId);
const opntIsCommandGrabbed = isCommandGrabbed(oppActionStateId);
const opntDamageTaken = prevOpponentFrame ? calcDamageTaken(opponentFrame, prevOpponentFrame) : 0;

// Keep track of whether actionState changes after a hit. Used to compute move count
Expand All @@ -84,7 +95,7 @@ function handleComboCompute(

// If opponent took damage and was put in some kind of stun this frame, either
// start a combo or count the moves for the existing combo
if (opntIsDamaged || opntIsGrabbed) {
if (opntIsDamaged || opntIsGrabbed || opntIsCommandGrabbed) {
if (!state.combo) {
state.combo = {
playerIndex: indices.playerIndex,
Expand Down Expand Up @@ -142,7 +153,7 @@ function handleComboCompute(
state.combo.currentPercent = opponentFrame.percent ?? 0;
}

if (opntIsDamaged || opntIsGrabbed || opntIsTeching || opntIsDowned || opntIsDying) {
if (opntIsDamaged || opntIsGrabbed || opntIsCommandGrabbed || opntIsTeching || opntIsDowned || opntIsDying) {
// If opponent got grabbed or damaged, reset the reset counter
state.resetCounter = 0;
} else {
Expand Down
24 changes: 23 additions & 1 deletion src/stats/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ export enum State {
FALL_BACKWARD = 0x1f,
GRAB = 0xd4,
CLIFF_CATCH = 0xfc,
DAMAGE_FALL = 0x26,

// Command Grabs
BARREL_WAIT = 0x125,
COMMAND_GRAB_RANGE1_START = 0x10a,
COMMAND_GRAB_RANGE1_END = 0x130,

COMMAND_GRAB_RANGE2_START = 0x147,
COMMAND_GRAB_RANGE2_END = 0x152,

COMMAND_GRAB_RANGE3_START = 0x177,
COMMAND_GRAB_RANGE3_END = 0x17e,
}

export const Timers = {
Expand Down Expand Up @@ -189,13 +201,23 @@ export function isDown(state: number): boolean {
}

export function isDamaged(state: number): boolean {
return state >= State.DAMAGE_START && state <= State.DAMAGE_END;
return (state >= State.DAMAGE_START && state <= State.DAMAGE_END) || state === State.DAMAGE_FALL;
}

export function isGrabbed(state: number): boolean {
return state >= State.CAPTURE_START && state <= State.CAPTURE_END;
}

// TODO: Find better implementation of 3 seperate ranges
export function isCommandGrabbed(state: number): boolean {
return (
((state >= State.COMMAND_GRAB_RANGE1_START && state <= State.COMMAND_GRAB_RANGE1_END) ||
(state >= State.COMMAND_GRAB_RANGE2_START && state <= State.COMMAND_GRAB_RANGE2_END) ||
(state >= State.COMMAND_GRAB_RANGE3_START && state <= State.COMMAND_GRAB_RANGE3_END)) &&
state !== State.BARREL_WAIT
);
}

export function isDead(state: number): boolean {
return state >= State.DYING_START && state <= State.DYING_END;
}
Expand Down
7 changes: 4 additions & 3 deletions src/stats/conversions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import { FrameEntryType, FramesType, PostFrameUpdateType } from "../types";
import { MoveLandedType, ConversionType, PlayerIndexedType } from "./common";
import { isDamaged, isGrabbed, calcDamageTaken, isInControl, didLoseStock, Timers } from "./common";
import { isDamaged, isGrabbed, isCommandGrabbed, calcDamageTaken, isInControl, didLoseStock, Timers } from "./common";
import { StatComputer } from "./stats";

interface PlayerConversionState {
Expand Down Expand Up @@ -113,6 +113,7 @@ function handleConversionCompute(
const oppActionStateId = opponentFrame.actionStateId!;
const opntIsDamaged = isDamaged(oppActionStateId);
const opntIsGrabbed = isGrabbed(oppActionStateId);
const opntIsCommandGrabbed = isCommandGrabbed(oppActionStateId);
const opntDamageTaken = prevOpponentFrame ? calcDamageTaken(opponentFrame, prevOpponentFrame) : 0;

// Keep track of whether actionState changes after a hit. Used to compute move count
Expand All @@ -131,7 +132,7 @@ function handleConversionCompute(

// If opponent took damage and was put in some kind of stun this frame, either
// start a conversion or
if (opntIsDamaged || opntIsGrabbed) {
if (opntIsDamaged || opntIsGrabbed || opntIsCommandGrabbed) {
if (!state.conversion) {
state.conversion = {
playerIndex: indices.playerIndex,
Expand Down Expand Up @@ -188,7 +189,7 @@ function handleConversionCompute(
state.conversion.currentPercent = opponentFrame.percent ?? 0;
}

if (opntIsDamaged || opntIsGrabbed) {
if (opntIsDamaged || opntIsGrabbed || opntIsCommandGrabbed) {
// If opponent got grabbed or damaged, reset the reset counter
state.resetCounter = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/stats/overall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export function generateOverallStats(

const conversionCount = conversions.length;
const successfulConversionCount = successfulConversions.length;
const totalDamage = _.sumBy(opponentStocks, "currentPercent") || 0;
const totalDamage =
_.sumBy(conversions, (conversion) => conversion.moves.reduce((total, move) => total + move.damage, 0)) || 0;
const killCount = opponentEndedStocks.length;

return {
Expand Down
108 changes: 108 additions & 0 deletions test/conversion.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { SlippiGame } from "../src";

describe("when calculating conversions", () => {
it("should include Puff's Sing", () => {
const game = new SlippiGame("slp/consistencyTest/PuffVFalcon-Sing.slp");
const stats = game.getStats();
const puff = stats.overall[0];
let totalDamagePuffDealt = 0;
stats.conversions.forEach((conversion) => {
if (conversion.playerIndex === puff.playerIndex) {
totalDamagePuffDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
});
expect(totalDamagePuffDealt).toBe(puff.totalDamage);
expect(puff.killCount).toBe(0);
expect(puff.conversionCount).toBe(2);
});

it("should include Bowser's command grab", () => {
const game = new SlippiGame("slp/consistencyTest/BowsVDK-SB-63.slp");
const stats = game.getStats();
const bowser = stats.overall[0];
let totalDamageBowserDealt = 0;
stats.conversions.forEach((conversion) => {
if (conversion.playerIndex === bowser.playerIndex) {
totalDamageBowserDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
});
expect(totalDamageBowserDealt).toBe(bowser.totalDamage);
expect(bowser.killCount).toBe(0);
expect(bowser.conversionCount).toBe(3);
});

it("should include Falcon's command grab", () => {
const game = new SlippiGame("slp/consistencyTest/FalcVBows-5UB-67.slp");
const stats = game.getStats();
const falcon = stats.overall[0];
let totalDamageFalconDealt = 0;
stats.conversions.forEach((conversion) => {
if (conversion.playerIndex === falcon.playerIndex) {
totalDamageFalconDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
});
expect(totalDamageFalconDealt).toBe(falcon.totalDamage);
expect(falcon.killCount).toBe(0);
expect(falcon.conversionCount).toBe(3);
});

it("should include Ganon's command grab", () => {
const game = new SlippiGame("slp/consistencyTest/GanonVDK-5UB-73.slp");
const stats = game.getStats();
const ganon = stats.overall[0];
let totalDamageGanonDealt = 0;
stats.conversions.forEach((conversion) => {
if (conversion.playerIndex === ganon.playerIndex) {
totalDamageGanonDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
});
expect(totalDamageGanonDealt).toBe(ganon.totalDamage);
expect(ganon.killCount).toBe(0);
expect(ganon.conversionCount).toBe(5);
});

it("should include Kirby's command grab", () => {
const game = new SlippiGame("slp/consistencyTest/KirbyVDK-Neutral-17.slp");
const stats = game.getStats();
const kirby = stats.overall[0];
let totalDamageKirbyDealt = 0;
stats.conversions.forEach((conversion) => {
if (conversion.playerIndex === kirby.playerIndex) {
totalDamageKirbyDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
});
expect(totalDamageKirbyDealt).toBe(kirby.totalDamage);
expect(kirby.killCount).toBe(0);
expect(kirby.conversionCount).toBe(3);
});

it("should include Yoshi's command grab", () => {
const game = new SlippiGame("slp/consistencyTest/YoshiVDK-Egg-13.slp");
const stats = game.getStats();
const yoshi = stats.overall[0];
let totalDamageYoshiDealt = 0;
stats.conversions.forEach((conversion) => {
if (conversion.playerIndex === yoshi.playerIndex) {
totalDamageYoshiDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
});
expect(totalDamageYoshiDealt).toBe(yoshi.totalDamage);
expect(yoshi.killCount).toBe(0);
expect(yoshi.conversionCount).toBe(2);
});

it("should include Mewtwo's command grab", () => {
const game = new SlippiGame("slp/consistencyTest/MewTwoVDK-SB-42.slp");
const stats = game.getStats();
const mewTwo = stats.overall[0];
let totalDamageMewTwoDealt = 0;
stats.conversions.forEach((conversion) => {
if (conversion.playerIndex === mewTwo.playerIndex) {
totalDamageMewTwoDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
});
expect(totalDamageMewTwoDealt).toBe(mewTwo.totalDamage);
expect(mewTwo.killCount).toBe(0);
expect(mewTwo.conversionCount).toBe(1);
});
});
70 changes: 70 additions & 0 deletions test/stats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,74 @@ describe("when calculating stats", () => {
expect(p2Success).toBe(5);
expect(p2Fail).toBe(4);
});

describe("when calculating total damage done", () => {
it("should ignore Blast Zone Magnifying Glass damage", () => {
const game = new SlippiGame("slp/consistencyTest/Puff-MagnifyingGlass-10.slp");
const stats = game.getStats();
const puff = stats.overall[0];
let totalDamagePuffDealt = 0;
stats.conversions.forEach((conversion) => {
if (conversion.playerIndex === puff.playerIndex) {
totalDamagePuffDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
});
expect(totalDamagePuffDealt).toBe(puff.totalDamage);
expect(puff.killCount).toBe(0);
expect(puff.conversionCount).toBe(0);
});

it("should ignore Pichu's self-damage", () => {
const game = new SlippiGame("slp/consistencyTest/PichuVSelf-All-22.slp");
const stats = game.getStats();
const pichu = stats.overall[0];
const ics = stats.overall[1];
const pichuStock = stats.stocks.filter((s) => s.playerIndex === pichu.playerIndex)[0];
const icsStock = stats.stocks.filter((s) => s.playerIndex === ics.playerIndex)[0];
let totalDamagePichuDealt = 0;
let icsDamageDealt = 0;
stats.conversions.forEach((conversion) => {
switch (conversion.playerIndex) {
case pichu.playerIndex: {
totalDamagePichuDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
break;
}
case ics.playerIndex: {
icsDamageDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
break;
}
}
});
expect(totalDamagePichuDealt).toBe(pichu.totalDamage);
// Pichu's self-damage should not count towards its own total damage dealt
expect(pichu.totalDamage).not.toBe(pichuStock.currentPercent + icsStock.currentPercent);
expect(pichu.killCount).toBe(0);
expect(icsDamageDealt).toBe(0);
expect(pichu.conversionCount).toBe(3);
});

it("should ignore Ness' damage recovery", () => {
const game = new SlippiGame("slp/consistencyTest/NessVFox-Absorb.slp");
const stats = game.getStats();
const ness = stats.overall[0];
const fox = stats.overall[1];
let totalDamageNessDealt = 0;
let totalDamageFoxDealt = 0;
stats.conversions.forEach((conversion) => {
if (conversion.playerIndex === ness.playerIndex) {
totalDamageNessDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
if (conversion.playerIndex === fox.playerIndex) {
totalDamageFoxDealt += conversion.moves.reduce((total, move) => total + move.damage, 0);
}
});
expect(totalDamageNessDealt).toBe(ness.totalDamage);
expect(ness.killCount).toBe(0);
expect(ness.conversionCount).toBe(0);

expect(totalDamageFoxDealt).toBe(fox.totalDamage);
expect(fox.killCount).toBe(0);
expect(fox.conversionCount).toBe(2);
});
});
});