Skip to content

Commit

Permalink
Merge pull request FreezingMoon#2447 from allmtz/master
Browse files Browse the repository at this point in the history
Improve types FreezingMoon#1969
  • Loading branch information
DreadKnight authored Jul 26, 2023
2 parents c83b84f + f1d06cd commit fb54459
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 18 deletions.
11 changes: 7 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"less": "^4.1.3",
"less-loader": "^11.1.0",
"lint-staged": "^13.2.3",
"prettier": "2.5.1",
"prettier": "^2.8.0",
"style-loader": "^3.3.1",
"terser-webpack-plugin": "^5.3.6",
"ts-loader": "^9.4.2",
Expand Down
29 changes: 18 additions & 11 deletions src/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { unitData } from './units';

export type UnitData = typeof unitData;

// Enables autocomplete for `key`
function getKeyValue<T extends object, K extends keyof T>(obj: T, key: K) {
return obj[key];
Expand All @@ -10,16 +12,21 @@ const realms = unitData.map((unit) => getKeyValue(unit, 'realm'));
const unitNames = unitData.map((unit) => getKeyValue(unit, 'name'));
const unitLevels = unitData.map((unit) => getKeyValue(unit, 'level'));

// Create an array containing the possible `creature.type` combinations.
// In `game.ts`: creature.type = realm.toUpperCase() + level,
const creatureTypes = realms.map((realm) => {
for (let i in unitLevels) {
return `${realm}${unitLevels[i]}` as const;
}
});
/*
* A creature's `type` is defined as `creature.realm` + `creature.level`
* Example: Dark Priest has `type` '--'
*
* This type helper makes it possible to exclude creature `type` combinations that don't actually exist on a creature
* For example: The combinations '-1' and 'A-' currently are not in use by any creature
*/
type ExtractValidCreatureTypes<T extends UnitData> = {
[key in keyof T]: `${T[key]['realm']}${T[key]['level']}`;
}[number];

// Create unions from the various arrays
export type UnitName = typeof unitNames[number];
export type Realm = typeof realms[number];
export type Level = typeof unitLevels[number];
export type CreatureType = typeof creatureTypes[number];
export type UnitName = (typeof unitNames)[number];
export type Realm = (typeof realms)[number];
export type Level = (typeof unitLevels)[number];

// Create a union of valid creature `type`s
export type CreatureType = ExtractValidCreatureTypes<UnitData>;
77 changes: 75 additions & 2 deletions src/data/units.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,76 @@
type Stats = {
health: number;
regrowth: number;
endurance: number;
energy: number;
meditation: number;
initiative: number;
offense: number;
defense: number;
movement: number;
pierce: number;
slash: number;
crush: number;
shock: number;
burn: number;
frost: number;
poison: number;
sonic: number;
mental: number;
};

type UnitDataStructure = readonly {
id: number;
name: string;
playable: boolean;
level: number | string;
realm: string;
size: number;
stats: Stats;
animation: { walk_speed: number };
type?: string;
drop?: { name: string } & Partial<Stats>;
movementType?: 'hover';
display?: { width: number; height: number; 'offset-x': number; 'offset-y': number };
set?: 'α' | 'β';

ability_info: readonly {
title: string;
desc: string;
info: string;
upgrade?: string;
affectedByMatSickness?: number;
details?: readonly string[];
effects?: readonly {
special?: string;
regrowth?: number;
offense?: number;
defense?: number;
frost?: number;
}[];
maxCharge?: number;
requirements?: { plasma: number; energy?: number };
damages?: {
special?: string;
shock?: number | string;
pure?: number | string;
pierce?: number;
slash?: number;
crush?: number;
burn?: number;
poison?: number;
frost?: number | string;
mental?: number;
sonic?: number;
};
bonus_damages?: { frost: number; pierce: number };
damages1?: { burn?: number; pierce?: number; poison?: number };
costs?: { plasma?: number | string; special?: string; energy?: number; movement?: string };
range?: { regular: number; upgraded: number; minimum?: number };
animation_data?: { duration: number; delay: number };
}[];
}[];

export const unitData = [
{
id: 0,
Expand Down Expand Up @@ -3927,7 +4000,7 @@ export const unitData = [
id: 50,
name: 'Shadow Leech',
playable: false,
level: '1',
level: 1,
realm: 'W',
size: 1,
set: 'α',
Expand Down Expand Up @@ -3996,4 +4069,4 @@ export const unitData = [
},
],
},
] as const;
] as const satisfies UnitDataStructure;

0 comments on commit fb54459

Please sign in to comment.