2025-03-15 10:34:01 +01:00
|
|
|
import { PerkId, PerksMap, Upgrade } from "./types";
|
2025-03-14 11:59:49 +01:00
|
|
|
|
|
|
|
export function getMajorityValue(arr: string[]): string {
|
2025-03-15 10:34:01 +01:00
|
|
|
const count: { [k: string]: number } = {};
|
|
|
|
arr.forEach((v) => (count[v] = (count[v] || 0) + 1));
|
|
|
|
// Object.values inline polyfill
|
|
|
|
const max = Math.max(...Object.keys(count).map((k) => count[k]));
|
|
|
|
return sample(Object.keys(count).filter((k) => count[k] == max));
|
2025-03-14 11:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sample<T>(arr: T[]): T {
|
2025-03-15 10:34:01 +01:00
|
|
|
return arr[Math.floor(arr.length * Math.random())];
|
2025-03-14 11:59:49 +01:00
|
|
|
}
|
|
|
|
|
2025-03-15 10:34:01 +01:00
|
|
|
export function sumOfKeys(obj: { [key: string]: number } | undefined | null) {
|
|
|
|
if (!obj) return 0;
|
|
|
|
return Object.values(obj)?.reduce((a, b) => a + b, 0) || 0;
|
2025-03-14 15:49:04 +01:00
|
|
|
}
|
|
|
|
|
2025-03-15 10:34:01 +01:00
|
|
|
export const makeEmptyPerksMap = (upgrades: { id: PerkId }[]) => {
|
|
|
|
const p = {} as any;
|
|
|
|
upgrades.forEach((u) => (p[u.id] = 0));
|
|
|
|
return p as PerksMap;
|
|
|
|
};
|