mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-11-18 07:22:06 +00:00
Reorganise code - use src folder
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { SetStateAction, atom } from "jotai";
|
||||
|
||||
export default function atomWithDebounce<T>(
|
||||
initialValue: T,
|
||||
delayMilliseconds = 500,
|
||||
delayOnResetMilliseconds = 500,
|
||||
) {
|
||||
const prevTimeoutAtom = atom<ReturnType<typeof setTimeout> | undefined>(
|
||||
undefined,
|
||||
);
|
||||
|
||||
// DO NOT EXPORT currentValueAtom as using this atom to set state can cause
|
||||
// inconsistent state between currentValueAtom and debouncedValueAtom
|
||||
const _currentValueAtom = atom(initialValue);
|
||||
const isDebouncingAtom = atom(false);
|
||||
|
||||
const debouncedValueAtom = atom(
|
||||
initialValue,
|
||||
(get, set, update: SetStateAction<T>) => {
|
||||
clearTimeout(get(prevTimeoutAtom));
|
||||
|
||||
const prevValue = get(_currentValueAtom);
|
||||
const nextValue =
|
||||
typeof update === "function"
|
||||
? (update as (prev: T) => T)(prevValue)
|
||||
: update;
|
||||
|
||||
const onDebounceStart = () => {
|
||||
set(_currentValueAtom, nextValue);
|
||||
set(isDebouncingAtom, true);
|
||||
};
|
||||
|
||||
const onDebounceEnd = () => {
|
||||
set(debouncedValueAtom, nextValue);
|
||||
set(isDebouncingAtom, false);
|
||||
};
|
||||
|
||||
onDebounceStart();
|
||||
|
||||
const nextTimeoutId = setTimeout(
|
||||
() => {
|
||||
onDebounceEnd();
|
||||
},
|
||||
nextValue === initialValue
|
||||
? delayOnResetMilliseconds
|
||||
: delayMilliseconds,
|
||||
);
|
||||
|
||||
// set previous timeout atom in case it needs to get cleared
|
||||
set(prevTimeoutAtom, nextTimeoutId);
|
||||
},
|
||||
);
|
||||
|
||||
// exported atom setter to clear timeout if needed
|
||||
const clearTimeoutAtom = atom(null, (get, set) => {
|
||||
clearTimeout(get(prevTimeoutAtom));
|
||||
set(isDebouncingAtom, false);
|
||||
});
|
||||
|
||||
return {
|
||||
currentValueAtom: atom((get) => get(_currentValueAtom)),
|
||||
isDebouncingAtom,
|
||||
clearTimeoutAtom,
|
||||
debouncedValueAtom,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import { clamp, last } from "lodash";
|
||||
|
||||
// Fide uses teh following table to establish the chance of winning based on the difference in elo.
|
||||
// https://handbook.fide.com/chapter/B022017
|
||||
|
||||
const differenceToProbability: [
|
||||
start: number,
|
||||
end: number,
|
||||
winChance: number,
|
||||
][] = [
|
||||
[0, 3, 0.5],
|
||||
[4, 10, 0.51],
|
||||
[11, 17, 0.52],
|
||||
[18, 25, 0.53],
|
||||
[26, 32, 0.54],
|
||||
[33, 39, 0.55],
|
||||
[40, 46, 0.56],
|
||||
[47, 53, 0.57],
|
||||
[54, 61, 0.58],
|
||||
[62, 68, 0.59],
|
||||
[69, 76, 0.6],
|
||||
[77, 83, 0.61],
|
||||
[84, 91, 0.62],
|
||||
[92, 98, 0.63],
|
||||
[99, 106, 0.64],
|
||||
[107, 113, 0.65],
|
||||
[114, 121, 0.66],
|
||||
[122, 129, 0.67],
|
||||
[130, 137, 0.68],
|
||||
[138, 145, 0.69],
|
||||
[146, 153, 0.7],
|
||||
[154, 162, 0.71],
|
||||
[163, 170, 0.72],
|
||||
[171, 179, 0.73],
|
||||
[180, 188, 0.74],
|
||||
[189, 197, 0.75],
|
||||
[198, 206, 0.76],
|
||||
[207, 215, 0.77],
|
||||
[216, 225, 0.78],
|
||||
[226, 235, 0.79],
|
||||
[236, 245, 0.8],
|
||||
[246, 256, 0.81],
|
||||
[257, 267, 0.82],
|
||||
[268, 278, 0.83],
|
||||
[279, 290, 0.84],
|
||||
[291, 302, 0.85],
|
||||
[303, 315, 0.86],
|
||||
[316, 328, 0.87],
|
||||
[329, 344, 0.88],
|
||||
[345, 357, 0.89],
|
||||
[358, 374, 0.9],
|
||||
[375, 391, 0.91],
|
||||
[392, 411, 0.92],
|
||||
[412, 432, 0.93],
|
||||
[433, 456, 0.94],
|
||||
[457, 484, 0.95],
|
||||
[485, 517, 0.96],
|
||||
[518, 559, 0.97],
|
||||
[560, 619, 0.98],
|
||||
[620, 735, 0.99],
|
||||
[736, 1000, 1],
|
||||
];
|
||||
|
||||
export const getNewRating = (
|
||||
rating: number,
|
||||
opponentRating: number,
|
||||
result: 1 | 0 | 0.5,
|
||||
kFactor: number,
|
||||
) => {
|
||||
const differenceInElo = clamp(opponentRating - rating, -400, 400);
|
||||
const tableRow = (differenceToProbability.find(
|
||||
([start, end]) =>
|
||||
Math.abs(differenceInElo) >= start && Math.abs(differenceInElo) <= end,
|
||||
) ?? last(differenceToProbability)!)[2];
|
||||
|
||||
const probabilityOfWinning = differenceInElo < 0 ? tableRow : 1 - tableRow;
|
||||
const delta =
|
||||
Math.round(
|
||||
kFactor * (result - probabilityOfWinning + Number.EPSILON) * 100,
|
||||
) / 100;
|
||||
|
||||
return {
|
||||
delta,
|
||||
newRating: Math.round((rating + delta + Number.EPSILON) * 100) / 100,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
const formatDate = (inputDate: any) => {
|
||||
// TODO fix type
|
||||
const date = new Date(inputDate);
|
||||
const day = String(date.getDate()).padStart(2, "0");
|
||||
const month = String(date.getMonth() + 1).padStart(2, "0");
|
||||
const year = date.getFullYear();
|
||||
return `${day}/${month}/${year}`;
|
||||
};
|
||||
|
||||
export default formatDate;
|
||||
@@ -0,0 +1,7 @@
|
||||
export const infoLog = (...params: any[]) => {
|
||||
console.log(...params);
|
||||
};
|
||||
|
||||
export const errorLog = (...params: any[]) => {
|
||||
console.error(...params);
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
import {
|
||||
Pathnames,
|
||||
createLocalizedPathnamesNavigation,
|
||||
} from "next-intl/navigation";
|
||||
|
||||
export const locales = ["fr", "en"] as const;
|
||||
|
||||
// The `pathnames` object holds pairs of internal
|
||||
// and external paths, separated by locale.
|
||||
export const pathnames = {
|
||||
"/": "/",
|
||||
"/clubs": "/clubs",
|
||||
"/elo": "/elo",
|
||||
|
||||
"/tournaments": {
|
||||
fr: "/tournois",
|
||||
en: "/tournaments",
|
||||
},
|
||||
|
||||
"/add-tournament": {
|
||||
fr: "/ajouter-un-tournoi",
|
||||
en: "/add-tournament",
|
||||
},
|
||||
|
||||
"/contact-us": {
|
||||
fr: "/contactez-nous",
|
||||
en: "/contact-us",
|
||||
},
|
||||
} satisfies Pathnames<typeof locales>;
|
||||
|
||||
export const { Link, redirect, usePathname, useRouter, getPathname } =
|
||||
createLocalizedPathnamesNavigation({ locales, pathnames });
|
||||
@@ -0,0 +1,8 @@
|
||||
import { ScrollableElement } from "@/types";
|
||||
|
||||
const isBrowser = () => typeof window !== "undefined";
|
||||
|
||||
export const handleScrollToTop = (element: ScrollableElement | null) => {
|
||||
if (!isBrowser()) return;
|
||||
element?.scrollTo({ top: 0, behavior: "smooth" });
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
export const removeDiacritics = (str: string) => {
|
||||
// We normalize to the composed Unicode form, then we can remove the accents
|
||||
return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
||||
};
|
||||
|
||||
export const normalizedContains = (haystack: string, needle: string) => {
|
||||
const regExp = new RegExp(removeDiacritics(needle), "gi");
|
||||
return regExp.test(removeDiacritics(haystack));
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
import { createTRPCReact } from "@trpc/react-query";
|
||||
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
|
||||
|
||||
import type { AppRouter } from "@/server/appRouter";
|
||||
|
||||
export type APIRouterInput = inferRouterInputs<AppRouter>;
|
||||
export type APIRouterOutput = inferRouterOutputs<AppRouter>;
|
||||
|
||||
export type TournamentResultsData = APIRouterOutput["fetchTournamentResults"];
|
||||
|
||||
export const trpc = createTRPCReact<AppRouter>();
|
||||
Reference in New Issue
Block a user