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

Added analog metrics. #40

Open
wants to merge 3 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
8 changes: 8 additions & 0 deletions src/stats/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export interface InputCountsType {
total: number;
}

export interface AnalogMotionType {
distanceTraveled: number;
metersTraveled: number;
analogMotionFrameCount: number;
averageVelocity: RatioType;
}

export interface OverallType extends PlayerIndexedType {
inputCounts: InputCountsType;
conversionCount: number;
Expand All @@ -83,6 +90,7 @@ export interface OverallType extends PlayerIndexedType {
successfulConversions: RatioType;
inputsPerMinute: RatioType;
digitalInputsPerMinute: RatioType;
joystickMotion: AnalogMotionType;
openingsPerKill: RatioType;
damagePerOpening: RatioType;
neutralWinRatio: RatioType;
Expand Down
19 changes: 19 additions & 0 deletions src/stats/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface PlayerInput {
cstickInputCount: number;
buttonInputCount: number;
triggerInputCount: number;
joystickDistanceTraveled: number;
joystickMotionFrameCount: number;
}

export class InputComputer implements StatComputer<PlayerInput[]> {
Expand All @@ -41,6 +43,8 @@ export class InputComputer implements StatComputer<PlayerInput[]> {
cstickInputCount: 0,
buttonInputCount: 0,
triggerInputCount: 0,
joystickDistanceTraveled: 0,
joystickMotionFrameCount: 0,
};
this.state.set(indices, playerState);
});
Expand Down Expand Up @@ -115,6 +119,17 @@ function handleInputCompute(
state.inputCount += 1;
state.triggerInputCount += 1;
}

// Track the distance that the joystick has traveled. These are in unit coordinates and can
// be converted into meters, provided the controller being used has a standard gamecube stick.
const deltaX = playerFrame.joystickX - prevPlayerFrame.joystickX;
const deltaY = playerFrame.joystickY - prevPlayerFrame.joystickY;
const joystickDistance = getJoystickAnalogDistance(deltaX, deltaY);
state.joystickDistanceTraveled += joystickDistance;
if(joystickDistance > .001)
{
state.joystickMotionFrameCount += 1;
}
}

function countSetBits(x: number): number {
Expand All @@ -131,6 +146,10 @@ function countSetBits(x: number): number {
return count;
}

function getJoystickAnalogDistance(x: number, y: number): number {
return Math.sqrt(x * x + y * y)
}

function getJoystickRegion(x: number, y: number): JoystickRegion {
let region = JoystickRegion.DZ;

Expand Down
15 changes: 14 additions & 1 deletion src/stats/overall.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from "lodash";
import { ConversionType, PlayerIndexedType, StockType, OverallType, RatioType, InputCountsType } from "./common";
import { ConversionType, PlayerIndexedType, StockType, OverallType, RatioType, InputCountsType, AnalogMotionType } from "./common";
import { PlayerInput } from "./inputs";

interface ConversionsByPlayerByOpening {
Expand Down Expand Up @@ -45,6 +45,18 @@ export function generateOverallStats(
const totalDamage = _.sumBy(opponentStocks, "currentPercent") || 0;
const killCount = opponentEndedStocks.length;

const joystickAnalogDistance = _.get(playerInputs, "joystickDistanceTraveled");
const joystickMotionFrameCount = _.get(playerInputs, "joystickMotionFrameCount");
// Distance from stick top to gyro center is ~2cm. Distance from center to edge of gate is ~.47 rad.
// If there are more detailed measurements that have been taken, this is the place to use them.
const joystickMetersTraveled = joystickAnalogDistance * (.02 * .47);
const joystickMotion: AnalogMotionType = {
distanceTraveled: joystickAnalogDistance,
metersTraveled: joystickMetersTraveled,
analogMotionFrameCount: joystickMotionFrameCount,
averageVelocity: getRatio(joystickMetersTraveled, joystickMotionFrameCount / 60),
}

return {
playerIndex: playerIndex,
opponentIndex: opponentIndex,
Expand All @@ -56,6 +68,7 @@ export function generateOverallStats(
successfulConversions: getRatio(successfulConversionCount, conversionCount),
inputsPerMinute: getRatio(inputCounts.total, gameMinutes),
digitalInputsPerMinute: getRatio(inputCounts.buttons, gameMinutes),
joystickMotion: joystickMotion,
openingsPerKill: getRatio(conversionCount, killCount),
damagePerOpening: getRatio(totalDamage, conversionCount),
neutralWinRatio: getOpeningRatio(conversionsByPlayerByOpening, playerIndex, opponentIndex, "neutral-win"),
Expand Down