scripts/src/lib/tw/utils.ts
Dawid Wysokiński cab4709463
All checks were successful
ci/woodpecker/push/test Pipeline was successful
chore(deps): update dependency date-fns to v3
2024-04-05 06:38:20 +02:00

23 lines
624 B
TypeScript

import { differenceInMinutes } from 'date-fns/differenceInMinutes';
import { LOYALTY_AFTER_CONQUER, MAX_LOYALTY } from './constants';
export const calcLoyalty = (
ennobledAt: Date | string | number,
speed: number
) => {
const loyalty =
LOYALTY_AFTER_CONQUER +
Math.abs(differenceInMinutes(new Date(ennobledAt), new Date())) *
(speed / 60);
return loyalty > MAX_LOYALTY ? MAX_LOYALTY : Math.floor(loyalty);
};
export type Coords = {
x: number;
y: number;
};
export const calcDistance = (coords1: Coords, coords2: Coords) => {
return Math.hypot(coords1.x - coords2.x, coords1.y - coords2.y);
};