More lib files, more functional
Still missing that 'attack' fn, though :(
This commit is contained in:
21
lib/Tank.ts
21
lib/Tank.ts
@@ -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[];
|
||||
|
Reference in New Issue
Block a user