23 lines
620 B
TypeScript
23 lines
620 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);
|
|
};
|