Skip to content

Commit

Permalink
Refactor typings to reduce/remove type casts (#131)
Browse files Browse the repository at this point in the history
* avoid type casting

* prefer types over interfaces

* forgot one oops

* remove more type casting

* be a bit more explicit with return types
  • Loading branch information
vinceau authored Sep 19, 2023
1 parent 181a553 commit 433d515
Show file tree
Hide file tree
Showing 19 changed files with 155 additions and 146 deletions.
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 @@ import type { StatComputer } from "./stats";
// 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
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

0 comments on commit 433d515

Please sign in to comment.