Refactored code architecture

This commit is contained in:
texhno 2023-12-05 20:16:38 +01:00
parent bc77177723
commit ad3a648765
2 changed files with 38 additions and 37 deletions

37
Tank.ts
View File

@ -10,3 +10,40 @@ export const buildTank: BuildTank = (name) => ({
health: 100, health: 100,
attackDelay: 0, attackDelay: 0,
} as ITank); } as ITank);
type CritMultiplier = (health: number) => number;
const critMultiplier: CritMultiplier = (health) =>
Math.floor(Math.random() * 10) >= 10 - health / 10 ? 1 : 2;
type SetStats = (tank: ITank) => ITank;
const setStats: SetStats = (tank) => ({
...tank,
attackDelay: tank.attackDelay === 0 ? Math.floor(tank.health / 10) : tank.attackDelay - 1,
} as ITank);
type Attack = (_: ITank[], tank: ITank, tIndex: number, tanks: ITank[]) => ITank[];
const attack: Attack = (_acc, tank, _iTank, tanks) => {
if (tank.attackDelay === 0) {
const target = tanks[Math.floor(Math.random() * tanks.length)];
const attackDamage = critMultiplier(tank.health) * tank.health / 100;
target.health -= attackDamage;
};
return tanks.map(setStats);
};
type IsLive = (tank: ITank) => boolean;
const isLive: IsLive = (tank) => tank.health >= 0;
type Attacks = (tanks: ITank[]) => ITank[];
const attacksRound: Attacks = (tanks) =>
tanks
.reduce(attack, tanks)
.filter(isLive);
type Battle = (remainingTanks: ITank[]) => ITank;
export const battleItOut: Battle = (remainingTanks: ITank[]) =>
remainingTanks.length === 1
? remainingTanks[0]
: battleItOut(attacksRound(remainingTanks));

View File

@ -9,7 +9,7 @@
* KAPOW!!! * KAPOW!!!
*/ */
import { buildTank, ITank } from "./Tank"; import { buildTank, battleItOut } from "./Tank";
const tankPrototypes = [ const tankPrototypes = [
"Rust", "Rust",
@ -20,40 +20,4 @@ const tankPrototypes = [
]; ];
const initialTanks = tankPrototypes.map(buildTank); const initialTanks = tankPrototypes.map(buildTank);
type CritMultiplier = (health: number) => number;
const critMultiplier: CritMultiplier = (health) =>
Math.floor(Math.random() * 10) >= 10 - health / 10 ? 1 : 2;
type SetStats = (tank: ITank) => ITank;
const setStats: SetStats = (tank) => ({
...tank,
attackDelay: tank.attackDelay === 0 ? Math.floor(tank.health / 10) : tank.attackDelay - 1,
} as ITank);
type Attack = (_: ITank[], tank: ITank, tIndex: number, tanks: ITank[]) => ITank[];
const attack: Attack = (_acc, tank, _iTank, tanks) => {
if (tank.attackDelay === 0) {
const target = tanks[Math.floor(Math.random() * tanks.length)];
const attackDamage = critMultiplier(tank.health) * tank.health / 100;
target.health -= attackDamage;
};
return tanks.map(setStats);
};
type IsLive = (tank: ITank) => boolean;
const isLive: IsLive = (tank) => tank.health >= 0;
type Attacks = (tanks: ITank[]) => ITank[];
const attacksRound: Attacks = (tanks) =>
tanks
.reduce(attack, tanks)
.filter(isLive);
type Battle = (remainingTanks: ITank[]) => ITank;
const battleItOut: Battle = (remainingTanks: ITank[]) =>
remainingTanks.length === 1
? remainingTanks[0]
: battleItOut(attacksRound(remainingTanks));
console.log(battleItOut(initialTanks).name); console.log(battleItOut(initialTanks).name);