2023-12-05 23:27:52 +00:00
|
|
|
import { ITank } from "./Tank";
|
2023-12-06 00:21:48 +00:00
|
|
|
import { filterR, pipe } from "./reducers";
|
2023-12-05 23:27:52 +00:00
|
|
|
|
|
|
|
type CritMultiplier = (health: number) => number;
|
|
|
|
export const critMultiplier: CritMultiplier = (health) =>
|
|
|
|
Math.floor(Math.random() * 10) >= 10 - health / 10 ? 1 : 2;
|
|
|
|
|
2023-12-06 00:21:48 +00:00
|
|
|
type PickRandom = <T>(arr: T[]) => T;
|
|
|
|
export const pickRandom: PickRandom = (arr) =>
|
|
|
|
arr[Math.floor(Math.random() * arr.length)];
|
|
|
|
|
2023-12-06 00:41:04 +00:00
|
|
|
// @ts-ignore
|
2023-12-06 00:21:48 +00:00
|
|
|
export const setProp = (prop, val) => (obj) => obj[prop] = val;
|
2023-12-05 23:27:52 +00:00
|
|
|
|
|
|
|
type IsLive = (tank: ITank) => boolean;
|
|
|
|
export const isLive: IsLive = (tank) => tank.health >= 0;
|
|
|
|
|
|
|
|
type IsOther = (name: string) => (tank: ITank) => boolean;
|
|
|
|
export const isOther: IsOther = (name) => (tank) => tank.name !== name;
|
2023-12-06 00:21:48 +00:00
|
|
|
|
|
|
|
export const isReadyToAttack = (tank: ITank) => tank.attackDelay === 0;
|
|
|
|
|
|
|
|
type DealDamageToRandom = (tanks: ITank[]) => (tank: ITank) => void;
|
|
|
|
export const dealDamageToRandom: DealDamageToRandom = (tanks) => (tank) => {
|
2023-12-06 00:41:04 +00:00
|
|
|
console.log("hp");
|
2023-12-06 00:21:48 +00:00
|
|
|
const target = pipe(filterR(isOther(tank.name)), pickRandom)(tanks);
|
|
|
|
const attackDamage = critMultiplier(tank.health) * tank.health / 100;
|
|
|
|
setProp("health", target.health - attackDamage)(target);
|
|
|
|
};
|