mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-07-23 12:36:57 +00:00
warn if ELO ordering of players does not match FFE rules of >100 gap
This commit is contained in:
@@ -11,12 +11,15 @@ import TeamSelection from "@/app/[locale]/(main)/match/components/TeamSelection"
|
|||||||
import { Button } from "@/components/Button";
|
import { Button } from "@/components/Button";
|
||||||
import InfoMessage, { InfoMessageType } from "@/components/InfoMessage";
|
import InfoMessage, { InfoMessageType } from "@/components/InfoMessage";
|
||||||
import { TextField } from "@/components/form/TextField";
|
import { TextField } from "@/components/form/TextField";
|
||||||
|
import { Player } from "@/interfaces";
|
||||||
import { matchSchema } from "@/schemas";
|
import { matchSchema } from "@/schemas";
|
||||||
import { generateFeuilleDeMatch } from "@/server/generateFeuilleDeMatch";
|
import { generateFeuilleDeMatch } from "@/server/generateFeuilleDeMatch";
|
||||||
import { Club } from "@/types";
|
import { Club } from "@/types";
|
||||||
|
|
||||||
export type MatchFormValues = z.infer<typeof matchSchema>;
|
export type MatchFormValues = z.infer<typeof matchSchema>;
|
||||||
|
|
||||||
|
type BoardPlayer = Partial<Pick<Player, "elo" | "name" | "nrFFE">>;
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
clubs: Club[];
|
clubs: Club[];
|
||||||
}
|
}
|
||||||
@@ -31,8 +34,43 @@ const MatchForm = ({ clubs }: IProps) => {
|
|||||||
resolver: zodResolver(matchSchema),
|
resolver: zodResolver(matchSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// check 100 point ELO rule
|
||||||
|
const checkPlayerOrder = (
|
||||||
|
team1Players: BoardPlayer[],
|
||||||
|
team2Players: BoardPlayer[],
|
||||||
|
) => {
|
||||||
|
function checkOrder(elos: number[]) {
|
||||||
|
for (let i = 0; i < elos.length - 1; i++) {
|
||||||
|
if (elos[i] < elos[i + 1]) {
|
||||||
|
if (elos[i + 1] - elos[i] > 100) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const team1Elos = team1Players
|
||||||
|
.filter((player) => player.elo !== "")
|
||||||
|
.map((player) => Number(player.elo?.split(" ")[0]));
|
||||||
|
|
||||||
|
const team2Elos = team2Players
|
||||||
|
.filter((player) => player.elo !== "")
|
||||||
|
.map((player) => Number(player.elo?.split(" ")[0]));
|
||||||
|
|
||||||
|
return !(!checkOrder(team1Elos) || !checkOrder(team2Elos));
|
||||||
|
};
|
||||||
|
|
||||||
const onSubmit = async (data: MatchFormValues) => {
|
const onSubmit = async (data: MatchFormValues) => {
|
||||||
try {
|
try {
|
||||||
|
if (!checkPlayerOrder(data.team1_players, data.team2_players)) {
|
||||||
|
const confirmResult = confirm(
|
||||||
|
"ELO ordering is not correct, please check your players.\nClick 'OK' to continue or 'Cancel' to abort.",
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!confirmResult) return false;
|
||||||
|
}
|
||||||
|
|
||||||
const result = await generateFeuilleDeMatch(data);
|
const result = await generateFeuilleDeMatch(data);
|
||||||
|
|
||||||
if (result?.data?.pdfBase64) {
|
if (result?.data?.pdfBase64) {
|
||||||
@@ -54,11 +92,18 @@ const MatchForm = ({ clubs }: IProps) => {
|
|||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
|
||||||
|
if (err instanceof Error) {
|
||||||
|
setResponseMessage({
|
||||||
|
type: "error",
|
||||||
|
message: err.message,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
setResponseMessage({
|
setResponseMessage({
|
||||||
type: "error",
|
type: "error",
|
||||||
message: "error creating feuille de match",
|
message: "error creating feuille de match",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const clubOptions = clubs.map((club) => ({
|
const clubOptions = clubs.map((club) => ({
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ export const styles = StyleSheet.create({
|
|||||||
page: {
|
page: {
|
||||||
flexDirection: "column",
|
flexDirection: "column",
|
||||||
paddingHorizontal: 30,
|
paddingHorizontal: 30,
|
||||||
paddingVertical: 10,
|
paddingVertical: 20,
|
||||||
fontSize: 10,
|
fontSize: 10,
|
||||||
},
|
},
|
||||||
header: { marginBottom: 10, textAlign: "center" },
|
header: { marginBottom: 10, textAlign: "center" },
|
||||||
@@ -64,7 +64,7 @@ export const styles = StyleSheet.create({
|
|||||||
footer: {
|
footer: {
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
zIndex: 1,
|
zIndex: 1,
|
||||||
bottom: 40,
|
bottom: 10,
|
||||||
left: 0,
|
left: 0,
|
||||||
right: 0,
|
right: 0,
|
||||||
fontSize: 8,
|
fontSize: 8,
|
||||||
|
|||||||
Reference in New Issue
Block a user