From 74c40576cf98b172ee423661a6f32c6765eda014 Mon Sep 17 00:00:00 2001 From: texhno Date: Wed, 6 Dec 2023 01:41:04 +0100 Subject: [PATCH] 'Functional version' broke, cba to fix :) --- Tank.ts | 49 ---------------------------------------------- index.ts | 2 +- lib/Tank.ts | 4 ++-- lib/combinators.ts | 2 ++ lib/helpers.ts | 2 ++ lib/reducers.ts | 4 ++-- 6 files changed, 9 insertions(+), 54 deletions(-) delete mode 100644 Tank.ts diff --git a/Tank.ts b/Tank.ts deleted file mode 100644 index 06bf78f..0000000 --- a/Tank.ts +++ /dev/null @@ -1,49 +0,0 @@ -export interface ITank { - name: string; - health: number; - attackDelay: number; -} - -type BuildTank = (name: string) => ITank; -export const buildTank: BuildTank = (name) => ({ - 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) => { - // [TODO]: Refactor this imperative block - 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)); diff --git a/index.ts b/index.ts index 05fb31d..4de707f 100644 --- a/index.ts +++ b/index.ts @@ -9,7 +9,7 @@ * KAPOW!!! */ -import { buildTank, battleItOut } from "./Tank"; +import { buildTank, battleItOut } from "./lib/Tank"; const tankPrototypes = [ "Rust", diff --git a/lib/Tank.ts b/lib/Tank.ts index 2a1b3ea..712b3c3 100644 --- a/lib/Tank.ts +++ b/lib/Tank.ts @@ -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[]; diff --git a/lib/combinators.ts b/lib/combinators.ts index 4b857d1..e69b832 100644 --- a/lib/combinators.ts +++ b/lib/combinators.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + export const tap = (fn) => (input) => { fn(input); return input; diff --git a/lib/helpers.ts b/lib/helpers.ts index aadd25d..8379de3 100644 --- a/lib/helpers.ts +++ b/lib/helpers.ts @@ -9,6 +9,7 @@ type PickRandom = (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); diff --git a/lib/reducers.ts b/lib/reducers.ts index 5409002..ff942ab 100644 --- a/lib/reducers.ts +++ b/lib/reducers.ts @@ -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)], []);