More lib files, more functional

Still missing that 'attack' fn, though :(
This commit is contained in:
2023-12-06 01:21:48 +01:00
parent 42b8afdecf
commit 545f8b4366
4 changed files with 43 additions and 15 deletions

View File

@@ -1,4 +1,6 @@
import { setStats, isLive, isOther, critMultiplier } from "./helpers";
import { doIf, tap } from "./combinators";
import { isLive, setProp, dealDamageToRandom, isReadyToAttack } from "./helpers";
import { mapR, pipe } from "./reducers";
export interface ITank {
name: string;
@@ -13,16 +15,15 @@ export const buildTank: BuildTank = (name) => ({
attackDelay: 0,
} as ITank);
type Attack = (_: ITank[], tank: ITank, tIndex: number, tanks: ITank[]) => ITank[];
type Attack = (_prevTanks: ITank[], tank: ITank, tIndex: number, tanks: ITank[]) => ITank[];
const attack: Attack = (_acc, tank, _iTank, tanks) => {
// [TODO]: Refactor this imperative block
if (tank.attackDelay === 0) {
const target = tanks.filter(isOther(tank.name))[Math.floor(Math.random() * tanks.length)];
const attackDamage = critMultiplier(tank.health) * tank.health / 100;
target.health -= attackDamage;
};
return tanks.map(setStats);
tap(
doIf(
isReadyToAttack,
dealDamageToRandom(tanks)
)
)(tank);
return mapR(setProp("attackDelay", tank))(tanks);
};
type Attacks = (tanks: ITank[]) => ITank[];