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

Refactor typings to reduce/remove type casts #131

Merged
merged 5 commits into from
Sep 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/console/communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export enum CommunicationType {
KEEP_ALIVE = 3,
}

export interface CommunicationMessage {
export type CommunicationMessage = {
type: CommunicationType;
payload: {
cursor: Uint8Array;
Expand All @@ -18,7 +18,7 @@ export interface CommunicationMessage {
forcePos: boolean;
nintendontVersion: string | null;
};
}
};

// This class is responsible for handling the communication protocol between the Wii and the
// desktop app
Expand Down
8 changes: 4 additions & 4 deletions src/console/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ export enum Ports {
RELAY_START = 53741,
}

export interface ConnectionDetails {
export type ConnectionDetails = {
consoleNick: string;
gameDataCursor: number | Uint8Array;
version: string;
clientToken?: number;
}
};

export interface ConnectionSettings {
export type ConnectionSettings = {
ipAddress: string;
port: number;
}
};

export interface Connection extends EventEmitter {
getStatus(): ConnectionStatus;
Expand Down
4 changes: 2 additions & 2 deletions src/melee/characterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import characters from "./characters.json";
export type CharacterColor = string;
const DEFAULT_COLOR: CharacterColor = "Default";

export interface CharacterInfo {
export type CharacterInfo = {
id: number;
name: string;
shortName: string;
colors: CharacterColor[];
}
};

export const UnknownCharacter: CharacterInfo = {
id: -1,
Expand Down
4 changes: 2 additions & 2 deletions src/melee/moveUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import moveNames from "./moves.json";

export interface Move {
export type Move = {
id: number;
name: string;
shortName: string;
}
};

export const UnknownMove: Move = {
id: -1,
Expand Down
4 changes: 2 additions & 2 deletions src/melee/stageUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import stageNames from "./stages.json";

export interface StageInfo {
export type StageInfo = {
id: number;
name: string;
}
};

export const UnknownStage: StageInfo = {
id: -1,
Expand Down
4 changes: 2 additions & 2 deletions src/stats/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// Frame pattern that indicates a dash dance turn was executed
const dashDanceAnimations = [State.DASH, State.TURN, State.DASH];

interface PlayerActionState {
type PlayerActionState = {
playerCounts: ActionCountsType;
animations: number[];
actionFrameCounters: number[];
}
};

export class ActionsComputer implements StatComputer<ActionCountsType[]> {
private playerPermutations = new Array<PlayerIndexedType>();
Expand Down Expand Up @@ -133,8 +133,8 @@
}

function handleActionCompute(state: PlayerActionState, indices: PlayerIndexedType, frame: FrameEntryType): void {
const playerFrame = frame.players[indices.playerIndex]!.post;

Check warning on line 136 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion

Check warning on line 136 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion
const opponentFrame = frame.players[indices.opponentIndex]!.post;

Check warning on line 137 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion

Check warning on line 137 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion
const incrementCount = (field: string, condition: boolean): void => {
if (!condition) {
return;
Expand All @@ -145,9 +145,9 @@
};

// Manage animation state
const currentAnimation = playerFrame.actionStateId!;

Check warning on line 148 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion

Check warning on line 148 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion
state.animations.push(currentAnimation);
const currentFrameCounter = playerFrame.actionStateCounter!;

Check warning on line 150 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion

Check warning on line 150 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion
state.actionFrameCounters.push(currentFrameCounter);

// Grab last 3 frames
Expand Down Expand Up @@ -221,7 +221,7 @@
incrementCount("throwCount.back", currentAnimation === State.THROW_BACK);

// Techs
const opponentDir = playerFrame.positionX! > opponentFrame.positionX! ? -1 : 1;

Check warning on line 224 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion

Check warning on line 224 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion

Check warning on line 224 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion

Check warning on line 224 in src/stats/actions.ts

View workflow job for this annotation

GitHub Actions / Build on node 14.x and ubuntu-latest

Forbidden non-null assertion
const facingOpponent = playerFrame.facingDirection === opponentDir;

incrementCount("groundTechCount.fail", isMissGroundTech(currentAnimation));
Expand Down
4 changes: 2 additions & 2 deletions src/stats/combos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export enum ComboEvent {
COMBO_END = "COMBO_END",
}

interface ComboState {
type ComboState = {
combo: ComboType | null;
move: MoveLandedType | null;
resetCounter: number;
lastHitAnimation: number | null;
event: ComboEvent | null;
}
};

export class ComboComputer extends EventEmitter implements StatComputer<ComboType[]> {
private playerPermutations = new Array<PlayerIndexedType>();
Expand Down
76 changes: 39 additions & 37 deletions src/stats/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { GameStartType, PostFrameUpdateType } from "../types";

export interface StatsType {
export type StatsType = {
gameComplete: boolean;
lastFrame: number;
playableFrameCount: number;
Expand All @@ -9,76 +9,78 @@ export interface StatsType {
combos: ComboType[];
actionCounts: ActionCountsType[];
overall: OverallType[];
}
};

export type StadiumStatsType = HomeRunContestResultType | TargetTestResultType;

export interface TargetTestResultType {
export type TargetTestResultType = {
type: "target-test";
targetBreaks: TargetBreakType[];
}
};

export interface HomeRunContestResultType {
export type HomeRunContestResultType = {
type: "home-run-contest";
distance: number;
units: "feet" | "meters";
}
};

export interface RatioType {
export type RatioType = {
count: number;
total: number;
ratio: number | null;
}
};

export interface PlayerIndexedType {
export type PlayerIndexedType = {
playerIndex: number;
opponentIndex: number;
}
};

export interface DurationType {
export type DurationType = {
startFrame: number;
endFrame?: number | null;
}
};

export interface DamageType {
export type DamageType = {
startPercent: number;
currentPercent: number;
endPercent?: number | null;
}
};

export interface StockType extends DurationType, DamageType {
playerIndex: number;
count: number;
deathAnimation?: number | null;
}
export type StockType = DurationType &
DamageType & {
playerIndex: number;
count: number;
deathAnimation?: number | null;
};

export interface MoveLandedType {
export type MoveLandedType = {
playerIndex: number;
frame: number;
moveId: number;
hitCount: number;
damage: number;
}
};

export interface ComboType extends DurationType, DamageType {
playerIndex: number;
moves: MoveLandedType[];
didKill: boolean;
lastHitBy: number | null;
}
export type ComboType = DurationType &
DamageType & {
playerIndex: number;
moves: MoveLandedType[];
didKill: boolean;
lastHitBy: number | null;
};

export interface TargetBreakType {
export type TargetBreakType = {
spawnId: number;
frameDestroyed: number | null;
positionX: number;
positionY: number;
}
};

export interface ConversionType extends ComboType {
export type ConversionType = ComboType & {
openingType: string;
}
};

export interface ActionCountsType {
export type ActionCountsType = {
playerIndex: number;
wavedashCount: number;
wavelandCount: number;
Expand Down Expand Up @@ -130,17 +132,17 @@ export interface ActionCountsType {
success: number;
fail: number;
};
}
};

export interface InputCountsType {
export type InputCountsType = {
buttons: number;
triggers: number;
joystick: number;
cstick: number;
total: number;
}
};

export interface OverallType {
export type OverallType = {
playerIndex: number;
inputCounts: InputCountsType;
conversionCount: number;
Expand All @@ -154,7 +156,7 @@ export interface OverallType {
neutralWinRatio: RatioType;
counterHitRatio: RatioType;
beneficialTradeRatio: RatioType;
}
};

export enum State {
// Animation ID ranges
Expand Down
8 changes: 4 additions & 4 deletions src/stats/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import {
} from "./common";
import type { StatComputer } from "./stats";

interface PlayerConversionState {
type PlayerConversionState = {
conversion: ConversionType | null;
move: MoveLandedType | null;
resetCounter: number;
lastHitAnimation: number | null;
}
};

interface MetadataType {
type MetadataType = {
lastEndFrameByOppIdx: {
[oppIdx: number]: number;
};
}
};

export class ConversionComputer extends EventEmitter implements StatComputer<ConversionType[]> {
private playerPermutations = new Array<PlayerIndexedType>();
Expand Down
4 changes: 2 additions & 2 deletions src/stats/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ enum JoystickRegion {
W = 8,
}

export interface PlayerInput {
export type PlayerInput = {
playerIndex: number;
opponentIndex: number;
inputCount: number;
joystickInputCount: number;
cstickInputCount: number;
buttonInputCount: number;
triggerInputCount: number;
}
};

export class InputComputer implements StatComputer<PlayerInput[]> {
private state = new Map<PlayerIndexedType, PlayerInput>();
Expand Down
4 changes: 2 additions & 2 deletions src/stats/overall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import type { GameStartType } from "../types";
import type { ConversionType, InputCountsType, OverallType, RatioType } from "./common";
import type { PlayerInput } from "./inputs";

interface ConversionsByPlayerByOpening {
type ConversionsByPlayerByOpening = {
[playerIndex: string]: {
[openingType: string]: ConversionType[];
};
}
};

export function generateOverallStats({
settings,
Expand Down
4 changes: 2 additions & 2 deletions src/stats/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export interface StatComputer<T> {
fetch(): T;
}

export interface StatOptions {
export type StatOptions = {
processOnTheFly: boolean;
}
};

const defaultOptions: StatOptions = {
processOnTheFly: false,
Expand Down
4 changes: 2 additions & 2 deletions src/stats/stocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { PlayerIndexedType, StockType } from "./common";
import { didLoseStock, getSinglesPlayerPermutationsFromSettings, isDead } from "./common";
import type { StatComputer } from "./stats";

interface StockState {
type StockState = {
stock?: StockType | null;
}
};

export class StockComputer implements StatComputer<StockType[]> {
private state = new Map<PlayerIndexedType, StockState>();
Expand Down
Loading
Loading