mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-13 06:21:21 -05:00
25 lines
802 B
TypeScript
25 lines
802 B
TypeScript
import { PlusRegion } from "@prisma/client";
|
|
|
|
export const getPercentageFromCounts = (
|
|
countsNA: number[],
|
|
countsEU: number[],
|
|
votedUserRegion: PlusRegion
|
|
) => {
|
|
const sameRegionArr = votedUserRegion === "NA" ? countsNA : countsEU;
|
|
const otherRegionArr = votedUserRegion === "NA" ? countsEU : countsNA;
|
|
|
|
const sameSum =
|
|
sameRegionArr[0] * 0 +
|
|
sameRegionArr[1] * 1 +
|
|
sameRegionArr[2] * 2 +
|
|
sameRegionArr[3] * 3;
|
|
const sameVoterCount = sameRegionArr.reduce((acc, cur) => acc + cur, 0);
|
|
const sameMax = sameVoterCount * 3;
|
|
|
|
const otherSum = otherRegionArr[1] * 0 + otherRegionArr[2] * 1;
|
|
const otherVoterCount = otherRegionArr.reduce((acc, cur) => acc + cur, 0);
|
|
const otherMax = otherVoterCount * 1;
|
|
|
|
return ((sameSum + otherSum) / (sameMax + otherMax)) * 100;
|
|
};
|