Refactored code architecture
This commit is contained in:
parent
bc77177723
commit
ad3a648765
37
Tank.ts
37
Tank.ts
@ -10,3 +10,40 @@ export const buildTank: BuildTank = (name) => ({
|
||||
health: 100,
|
||||
attackDelay: 0,
|
||||
} 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));
|
||||
|
38
index.ts
38
index.ts
@ -9,7 +9,7 @@
|
||||
* KAPOW!!!
|
||||
*/
|
||||
|
||||
import { buildTank, ITank } from "./Tank";
|
||||
import { buildTank, battleItOut } from "./Tank";
|
||||
|
||||
const tankPrototypes = [
|
||||
"Rust",
|
||||
@ -20,40 +20,4 @@ const tankPrototypes = [
|
||||
];
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user