Code style refactors [minor]

This commit is contained in:
texhno 2023-12-05 20:22:01 +01:00
parent ad3a648765
commit 6e617b970e
2 changed files with 5 additions and 3 deletions

View File

@ -1,9 +1,9 @@
export interface ITank { export interface ITank {
name: string; name: string;
health: number; health: number;
attackDelay: number; attackDelay: number;
} }
type BuildTank = (name: string) => ITank; type BuildTank = (name: string) => ITank;
export const buildTank: BuildTank = (name) => ({ export const buildTank: BuildTank = (name) => ({
name, name,
@ -11,7 +11,6 @@ export const buildTank: BuildTank = (name) => ({
attackDelay: 0, attackDelay: 0,
} as ITank); } as ITank);
type CritMultiplier = (health: number) => number; type CritMultiplier = (health: number) => number;
const critMultiplier: CritMultiplier = (health) => const critMultiplier: CritMultiplier = (health) =>
Math.floor(Math.random() * 10) >= 10 - health / 10 ? 1 : 2; Math.floor(Math.random() * 10) >= 10 - health / 10 ? 1 : 2;
@ -24,6 +23,7 @@ const setStats: SetStats = (tank) => ({
type Attack = (_: ITank[], tank: ITank, tIndex: number, tanks: ITank[]) => ITank[]; type Attack = (_: ITank[], tank: ITank, tIndex: number, tanks: ITank[]) => ITank[];
const attack: Attack = (_acc, tank, _iTank, tanks) => { const attack: Attack = (_acc, tank, _iTank, tanks) => {
// Must refactor this imperative block
if (tank.attackDelay === 0) { if (tank.attackDelay === 0) {
const target = tanks[Math.floor(Math.random() * tanks.length)]; const target = tanks[Math.floor(Math.random() * tanks.length)];
const attackDamage = critMultiplier(tank.health) * tank.health / 100; const attackDamage = critMultiplier(tank.health) * tank.health / 100;

View File

@ -20,4 +20,6 @@ const tankPrototypes = [
]; ];
const initialTanks = tankPrototypes.map(buildTank); const initialTanks = tankPrototypes.map(buildTank);
console.log(battleItOut(initialTanks).name); const winner = battleItOut(initialTanks);
console.log(winner);