'Functional version' broke, cba to fix :)

This commit is contained in:
2023-12-06 01:41:04 +01:00
parent 545f8b4366
commit 74c40576cf
6 changed files with 9 additions and 54 deletions

View File

@@ -1,6 +1,6 @@
import { mapR } from "./reducers";
import { doIf, tap } from "./combinators";
import { isLive, setProp, dealDamageToRandom, isReadyToAttack } from "./helpers";
import { mapR, pipe } from "./reducers";
export interface ITank {
name: string;
@@ -23,7 +23,7 @@ const attack: Attack = (_acc, tank, _iTank, tanks) => {
dealDamageToRandom(tanks)
)
)(tank);
return mapR(setProp("attackDelay", tank))(tanks);
return mapR(setProp("attackDelay", isReadyToAttack(tank) ? Math.floor(tank.health / 10) : tank.attackDelay - 1))(tanks);
};
type Attacks = (tanks: ITank[]) => ITank[];

View File

@@ -1,3 +1,5 @@
// @ts-nocheck
export const tap = (fn) => (input) => {
fn(input);
return input;

View File

@@ -9,6 +9,7 @@ type PickRandom = <T>(arr: T[]) => T;
export const pickRandom: PickRandom = (arr) =>
arr[Math.floor(Math.random() * arr.length)];
// @ts-ignore
export const setProp = (prop, val) => (obj) => obj[prop] = val;
type IsLive = (tank: ITank) => boolean;
@@ -21,6 +22,7 @@ export const isReadyToAttack = (tank: ITank) => tank.attackDelay === 0;
type DealDamageToRandom = (tanks: ITank[]) => (tank: ITank) => void;
export const dealDamageToRandom: DealDamageToRandom = (tanks) => (tank) => {
console.log("hp");
const target = pipe(filterR(isOther(tank.name)), pickRandom)(tanks);
const attackDamage = critMultiplier(tank.health) * tank.health / 100;
setProp("health", target.health - attackDamage)(target);

View File

@@ -1,8 +1,8 @@
// @ts-nocheck
export const pipe = (...fns) => (input) =>
fns.reduce((acc, fn) => fn(acc), input);
export const redFn = (acc, x) => [...acc, x];
export const mapR = (fn) => (input) =>
input.reduce((acc, x) => [...acc, fn(x)], []);