2024-03-06 14:07:12 +00:00
|
|
|
import {ReplayData} from "./replays";
|
2024-03-08 07:18:44 +00:00
|
|
|
import {differenceInDays, differenceInHours} from "date-fns/fp";
|
2024-02-14 16:43:11 +00:00
|
|
|
|
2024-02-25 12:09:27 +00:00
|
|
|
export function formatDuration(seconds: number): string | null {
|
|
|
|
|
if(!seconds) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 16:43:11 +00:00
|
|
|
const days = Math.floor(seconds / (3600 * 24));
|
|
|
|
|
const hours = Math.floor((seconds % (3600 * 24)) / 3600);
|
|
|
|
|
const minutes = Math.floor((seconds % 3600) / 60);
|
|
|
|
|
|
|
|
|
|
let result = "";
|
|
|
|
|
if (days > 0) result += `${days}d `;
|
|
|
|
|
if (hours > 0 || days > 0) result += `${hours}h `;
|
|
|
|
|
if (minutes > 0 || hours > 0 || days > 0) result += `${minutes}m`;
|
|
|
|
|
|
|
|
|
|
return result.trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function countryCodeToFlag(countryCode: string): string {
|
|
|
|
|
const code = countryCode.toUpperCase();
|
|
|
|
|
|
|
|
|
|
if (code.length !== 2) {
|
|
|
|
|
throw new Error('Invalid country code: Must be exactly 2 characters.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const codePoints = [...code].map(
|
|
|
|
|
char => 0x1F1E6 - 65 + char.charCodeAt(0)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return String.fromCodePoint(...codePoints);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-06 14:07:12 +00:00
|
|
|
export function calculateAccuracy(replayData: ReplayData): number {
|
2024-02-14 16:43:11 +00:00
|
|
|
if(!replayData) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hit300 = replayData.count_300;
|
|
|
|
|
const hit100 = replayData.count_100;
|
|
|
|
|
const hit50 = replayData.count_50;
|
|
|
|
|
const miss = replayData.count_miss;
|
|
|
|
|
const totalHits = hit300 + hit100 + hit50 + miss;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (totalHits === 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const accuracy = (300 * hit300 + 100 * hit100 + 50 * hit50) / (300 * totalHits);
|
|
|
|
|
return accuracy * 100;
|
|
|
|
|
}
|
2024-03-08 07:18:44 +00:00
|
|
|
|
|
|
|
|
export function calculateTimeAgo(dateStr: string): string {
|
|
|
|
|
const inputDate = new Date(dateStr);
|
|
|
|
|
const now = new Date();
|
|
|
|
|
|
|
|
|
|
if (isNaN(inputDate.getTime())) {
|
|
|
|
|
return "???";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const difference = Math.abs(differenceInHours(now, inputDate));
|
|
|
|
|
|
|
|
|
|
if (difference < 1) {
|
|
|
|
|
return "recently";
|
|
|
|
|
} else if (difference < 24) {
|
|
|
|
|
return `${difference}h ago`;
|
|
|
|
|
} else {
|
|
|
|
|
const days = Math.abs(differenceInDays(now, inputDate));
|
|
|
|
|
const hours = difference % 24;
|
|
|
|
|
return `${days}d ${hours}h ago`;
|
|
|
|
|
}
|
|
|
|
|
}
|