feat: cm admin poule editor

This commit is contained in:
2025-12-02 15:58:20 +01:00
parent 6f61db6817
commit bb901392fc
14 changed files with 887 additions and 57 deletions

View File

@@ -8,6 +8,9 @@
"name": "webapp",
"version": "0.0.0",
"dependencies": {
"@dnd-kit/core": "^6.3.1",
"@dnd-kit/sortable": "^10.0.0",
"@dnd-kit/utilities": "^3.2.2",
"@fortawesome/fontawesome-svg-core": "^6.5.1",
"@fortawesome/free-brands-svg-icons": "^6.5.1",
"@fortawesome/free-regular-svg-icons": "^6.5.1",
@@ -399,6 +402,55 @@
"node": ">=6.9.0"
}
},
"node_modules/@dnd-kit/accessibility": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/@dnd-kit/accessibility/-/accessibility-3.1.1.tgz",
"integrity": "sha512-2P+YgaXF+gRsIihwwY1gCsQSYnu9Zyj2py8kY5fFvUM1qm2WA2u639R6YNVfU4GWr+ZM5mqEsfHZZLoRONbemw==",
"dependencies": {
"tslib": "^2.0.0"
},
"peerDependencies": {
"react": ">=16.8.0"
}
},
"node_modules/@dnd-kit/core": {
"version": "6.3.1",
"resolved": "https://registry.npmjs.org/@dnd-kit/core/-/core-6.3.1.tgz",
"integrity": "sha512-xkGBRQQab4RLwgXxoqETICr6S5JlogafbhNsidmrkVv2YRs5MLwpjoF2qpiGjQt8S9AoxtIV603s0GIUpY5eYQ==",
"dependencies": {
"@dnd-kit/accessibility": "^3.1.1",
"@dnd-kit/utilities": "^3.2.2",
"tslib": "^2.0.0"
},
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": ">=16.8.0"
}
},
"node_modules/@dnd-kit/sortable": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/@dnd-kit/sortable/-/sortable-10.0.0.tgz",
"integrity": "sha512-+xqhmIIzvAYMGfBYYnbKuNicfSsk4RksY2XdmJhT+HAC01nix6fHCztU68jooFiMUB01Ky3F0FyOvhG/BZrWkg==",
"dependencies": {
"@dnd-kit/utilities": "^3.2.2",
"tslib": "^2.0.0"
},
"peerDependencies": {
"@dnd-kit/core": "^6.3.0",
"react": ">=16.8.0"
}
},
"node_modules/@dnd-kit/utilities": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/@dnd-kit/utilities/-/utilities-3.2.2.tgz",
"integrity": "sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg==",
"dependencies": {
"tslib": "^2.0.0"
},
"peerDependencies": {
"react": ">=16.8.0"
}
},
"node_modules/@emotion/is-prop-valid": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@emotion/is-prop-valid/-/is-prop-valid-1.2.1.tgz",

View File

@@ -10,6 +10,9 @@
"preview": "vite preview"
},
"dependencies": {
"@dnd-kit/core": "^6.3.1",
"@dnd-kit/sortable": "^10.0.0",
"@dnd-kit/utilities": "^3.2.2",
"@fortawesome/fontawesome-svg-core": "^6.5.1",
"@fortawesome/free-brands-svg-icons": "^6.5.1",
"@fortawesome/free-regular-svg-icons": "^6.5.1",

View File

@@ -1,11 +1,26 @@
import {useLoadingSwitcher} from "../../../hooks/useLoading.jsx";
import {useRequestWS, useWS} from "../../../hooks/useWS.jsx";
import {useEffect, useReducer, useRef, useState} from "react";
import React, {useEffect, useReducer, useRef, useState} from "react";
import {MarchReducer} from "../../../utils/MatchReducer.jsx";
import {CombName, useCombs, useCombsDispatch} from "../../../hooks/useComb.jsx";
import {from_sendTree, TreeNode} from "../../../utils/TreeUtils.js";
import {DrawGraph} from "../../result/DrawGraph.jsx";
import {SelectCombModalContent} from "./SelectCombModalContent.jsx";
import {createMatch, scoreToString, win} from "../../../utils/CompetitionTools.js";
import {DndContext, closestCenter, KeyboardSensor, PointerSensor, useSensor, useSensors} from '@dnd-kit/core';
import {SortableContext, sortableKeyboardCoordinates, verticalListSortingStrategy} from '@dnd-kit/sortable';
import {useSortable} from '@dnd-kit/sortable';
import {CSS} from '@dnd-kit/utilities';
import {toast} from "react-toastify";
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import {faTrash} from "@fortawesome/free-solid-svg-icons";
function CupImg() {
return <img decoding="async" loading="lazy" width={"16"} height={"16"} className="wp-image-1635"
style={{width: "16px"}} src="/img/171891.png"
alt=""/>
}
export function CategoryContent({cat, catId, setCat}) {
const setLoading = useLoadingSwitcher()
@@ -43,21 +58,36 @@ export function CategoryContent({cat, catId, setCat}) {
combDispatch({type: 'SET_ALL', payload: {source: "match", data: combsToAdd}});
}
const matchListener = ({data}) => {
reducer({type: 'UPDATE_OR_ADD', payload: {...data, c1: data.c1?.id, c2: data.c2?.id}});
combDispatch({type: 'SET_ALL', payload: {source: "match", data: [data.c1, data.c2].filter(d => d != null)}});
const matchListener = ({data: datas}) => {
for (const data of datas) {
reducer({type: 'UPDATE_OR_ADD', payload: {...data, c1: data.c1?.id, c2: data.c2?.id}});
combDispatch({type: 'SET_ALL', payload: {source: "match", data: [data.c1, data.c2].filter(d => d != null)}});
if (data.c1 !== null && !groupsRef.current.some(g => g.id === data.c1?.id))
setGroups(prev => [...prev, {id: data.c1?.id, poule: data.poule}]);
if (data.c2 !== null && !groupsRef.current.some(g => g.id === data.c2?.id))
setGroups(prev => [...prev, {id: data.c2?.id, poule: data.poule}]);
if (data.c1 !== null && !groupsRef.current.some(g => g.id === data.c1?.id))
setGroups(prev => [...prev, {id: data.c1?.id, poule: data.poule}]);
if (data.c2 !== null && !groupsRef.current.some(g => g.id === data.c2?.id))
setGroups(prev => [...prev, {id: data.c2?.id, poule: data.poule}]);
}
}
const matchOrder = ({data}) => {
reducer({type: 'REORDER', payload: data});
}
const deleteMatch = ({data: datas}) => {
for (const data of datas)
reducer({type: 'REMOVE', payload: data});
}
dispatch({type: 'addListener', payload: {callback: treeListener, code: 'sendTreeCategory'}})
dispatch({type: 'addListener', payload: {callback: matchListener, code: 'sendMatch'}})
dispatch({type: 'addListener', payload: {callback: matchOrder, code: 'sendMatchOrder'}})
dispatch({type: 'addListener', payload: {callback: deleteMatch, code: 'sendDeleteMatch'}})
return () => {
dispatch({type: 'removeListener', payload: treeListener})
dispatch({type: 'removeListener', payload: matchListener})
dispatch({type: 'removeListener', payload: matchOrder})
dispatch({type: 'removeListener', payload: deleteMatch})
}
}, [cat]);
@@ -84,9 +114,13 @@ export function CategoryContent({cat, catId, setCat}) {
combDispatch({type: 'SET_ALL', payload: {source: "match", data: combsToAdd}});
const activeMatches = matches2.filter(m => m.poule !== '-')
const combsIDs = activeMatches.flatMap(d => [d.c1, d.c2]).filter((v, i, a) => v != null && a.indexOf(v) === i)
.map(d => {
return {id: d, poule: activeMatches.find(m => m.c1 === d || m.c2 === d)?.poule}
let poule = activeMatches.find(m => (m.c1 === d || m.c2 === d) && m.categorie_ord !== -42)?.poule
if (!poule)
poule = '-'
return {id: d, poule: poule}
})
setGroups(combsIDs)
}).finally(() => setLoading(0))
@@ -110,7 +144,7 @@ export function CategoryContent({cat, catId, setCat}) {
<AddComb groups={groups} setGroups={setGroups} removeGroup={removeGroup}/>
</div>
<div className="col-md-9">
{cat && <ListMatch cat={cat} matches={matches} groups={groups}/>}
{cat && <ListMatch cat={cat} matches={matches} groups={groups} reducer={reducer}/>}
</div>
</>
}
@@ -266,8 +300,59 @@ function GroupModalContent({combId, groups, setGroups, removeGroup}) {
}
function ListMatch({cat, matches, groups}) {
function ListMatch({cat, matches, groups, reducer}) {
const {sendRequest} = useWS();
const [type, setType] = useState(1);
const bthRef = useRef(null);
useEffect(() => {
if ((cat.type & type) === 0)
setType(cat.type);
}, [cat]);
const handleCreatMatch = () => {
if (matches.some(m => m.poule !== '-')) {
bthRef.current.click();
return;
}
recalculateMatch(0);
}
const recalculateMatch = (mode) => {
let matchesToKeep = [];
let matchesToRemove = [];
if (mode === 2) {
matchesToKeep = matches.filter(m => m.categorie === cat.id && m.categorie_ord !== -42);
} else if (mode === 1) {
matchesToKeep = matches.filter(m => m.categorie === cat.id && m.end === true && m.categorie_ord !== -42)
matchesToRemove = matches.filter(m => m.categorie === cat.id && m.end === false && m.categorie_ord !== -42)
} else {
matchesToRemove = matches.filter(m => m.categorie === cat.id && m.categorie_ord !== -42)
}
try {
const {newMatch, matchOrderToUpdate, matchPouleToUpdate} = createMatch(cat, matchesToKeep, groups.filter(g => g.poule !== '-'))
toast.promise(sendRequest("recalculateMatch", {
categorie: cat.id,
newMatch,
matchOrderToUpdate: Object.fromEntries(matchOrderToUpdate),
matchPouleToUpdate: Object.fromEntries(matchPouleToUpdate),
matchesToRemove: matchesToRemove.map(m => m.id)
}),
{
pending: 'Création des matchs en cours...',
success: 'Matchs créés avec succès !',
error: 'Erreur lors de la création des matchs',
})
.finally(() => {
console.log("Finished creating matches");
})
} catch (e) {
toast.error("Erreur lors de la création des matchs: " + e.message);
}
}
return <>
{cat.type === 3 && <>
@@ -285,12 +370,281 @@ function ListMatch({cat, matches, groups}) {
</ul>
</>}
{type === 1 && <>
<MatchList matches={matches} groups={groups} cat={cat} reducer={reducer}/>
<button className="btn btn-primary float-end" onClick={handleCreatMatch}>Créer les matchs</button>
</>}
{type === 2 && <>
<BuildTree treeData={cat.trees} matches={matches} groups={groups}/>
</>}
<button ref={bthRef} data-bs-toggle="modal" data-bs-target="#makeMatchMode" style={{display: "none"}}>open</button>
<div className="modal fade" id="makeMatchMode" tabIndex="-1" role="dialog" aria-labelledby="makeMatchModeLabel" aria-hidden="true">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title" id="makeMatchModeLabel">Attention</h4>
</div>
<div className="modal-body">
Il y a déjà des matchs dans cette poule, que voulez-vous faire avec ?
</div>
<div className="modal-footer">
<div className="btn-group" role="group" aria-label="Basic mixed styles example">
<button className="btn btn-primary" data-dismiss="modal" data-bs-dismiss="modal" onClick={() => recalculateMatch(2)}>
Tout conserver
</button>
<button className="btn btn-success" data-bs-dismiss="modal" onClick={() => recalculateMatch(1)}>
Conserver uniquement les matchs terminés
</button>
<button className="btn btn-danger" data-bs-dismiss="modal" onClick={() => recalculateMatch(0)}>
Ne rien conserver
</button>
</div>
</div>
</div>
</div>
</div>
</>
}
function MatchList({matches, cat, groups, reducer}) {
const {sendRequest} = useWS();
const setLoading = useLoadingSwitcher()
const selectRef = useRef(null)
const tableRef = useRef(null)
const lastMatchClick = useRef(null)
const [combSelect, setCombSelect] = useState(0)
const [combC1nm, setCombC1nm] = useState(null)
const [combC2nm, setCombC2nm] = useState(null)
const liceName = (cat.liceName || "N/A").split(";");
const marches2 = matches.filter(m => m.categorie_ord !== -42)
.sort((a, b) => a.categorie_ord - b.categorie_ord)
.map(m => ({...m, win: win(m.scores)}))
const sensors = useSensors(
useSensor(PointerSensor),
useSensor(KeyboardSensor, {
coordinateGetter: sortableKeyboardCoordinates,
})
);
const handleDragEnd = async (event) => {
const {active, over} = event;
if (active.id !== over.id) {
const newIndex = marches2.findIndex(m => m.id === over.id);
reducer({type: 'REORDER', payload: {id: active.id, pos: newIndex}});
sendRequest('updateMatchOrder', {id: active.id, pos: newIndex}).then(__ => {
})
}
};
useEffect(() => {
if (!combC1nm || !combC2nm)
return;
if (combC1nm === combC2nm) {
toast.error("Un combattant ne peut pas s'affronter lui-même !");
return;
}
setLoading(1)
sendRequest('addMatch', {
categorie: cat.id,
categorie_ord: Math.max(...marches2.map(o => o.categorie_ord)) + 1,
poule: groups.find(g => g.id === combC1nm).poule,
c1: combC1nm,
c2: combC2nm
}).then(_ => {
setCombC1nm(null)
setCombC2nm(null)
}).finally(() => setLoading(0))
}, [combC1nm, combC2nm]);
useEffect(() => {
if (lastMatchClick.current == null)
return
const {matchId, combId} = lastMatchClick.current
if (matchId === -1) {
setCombC1nm(combSelect === 0 ? null : combSelect);
onClickVoid()
return
}
if (matchId === -2) {
setCombC2nm(combSelect === 0 ? null : combSelect);
onClickVoid()
return
}
const match = matches.find(m => m.id === matchId)
if (!match)
return
const combSelect_ = combSelect === 0 ? null : combSelect;
const data = {id: match.id, c1: match.c1, c2: match.c2}
if (match.c1 === combId) {
if (match.c1 === combSelect_)
return
data.c1 = combSelect_
} else if (match.c2 === combId) {
if (match.c2 === combSelect_)
return
data.c2 = combSelect_
}
if (data.c1 === data.c2) {
toast.error("Un combattant ne peut pas s'affronter lui-même !");
onClickVoid()
return;
}
setLoading(1)
sendRequest('updateMatchComb', data)
.finally(() => {
setLoading(0)
onClickVoid()
})
}, [combSelect])
const handleDelMatch = (matchId) => {
const match = matches.find(m => m.id === matchId)
if (!match)
return;
if (!confirm("Ce match a déjà des résultats, êtes-vous sûr de vouloir le supprimer ?"))
return;
setLoading(1)
sendRequest('deleteMatch', matchId)
.finally(() => setLoading(0))
}
const handleCombClick = (e, matchId, combId) => {
e.stopPropagation();
const tableRect = tableRef.current.getBoundingClientRect();
const rect = e.currentTarget.getBoundingClientRect();
const sel = selectRef.current;
sel.style.top = (rect.y - tableRect.y) + "px";
sel.style.left = (rect.x - tableRect.x) + "px";
sel.style.width = rect.width + "px";
sel.style.height = rect.height + "px";
sel.style.display = "block";
setCombSelect(combId || 0);
lastMatchClick.current = {matchId, combId};
}
const onClickVoid = () => {
const sel = selectRef.current;
sel.style.display = "none";
lastMatchClick.current = null;
}
const combsIDs = groups.map(m => m.id);
return <div style={{position: "relative"}}>
<DndContext sensors={sensors} collisionDetection={closestCenter} onDragEnd={handleDragEnd}>
<SortableContext items={marches2.map(m => m.id)} strategy={verticalListSortingStrategy}>
<div ref={tableRef} className="table-responsive-lg" style={{fontSize: "1rem"}} onClick={onClickVoid}>
<table className="table table-striped">
<thead>
<tr>
<th style={{textAlign: "center", paddingLeft: "0.2em", paddingRight: "0.2em"}} scope="col">N°</th>
<th style={{textAlign: "center", paddingLeft: "0.2em", paddingRight: "0.2em"}} scope="col">Poule</th>
<th style={{textAlign: "center", paddingLeft: "0.2em", paddingRight: "0.2em"}} scope="col">Lice</th>
<th style={{textAlign: "center"}} scope="col"></th>
<th style={{textAlign: "center"}} scope="col">Rouge</th>
<th style={{textAlign: "center"}} scope="col">Blue</th>
<th style={{textAlign: "center"}} scope="col"></th>
<th style={{textAlign: "center"}} scope="col">Résultat</th>
<th style={{textAlign: "center"}} scope="col"></th>
<th style={{textAlign: "center"}} scope="col"></th>
</tr>
</thead>
<tbody className="table-group-divider">
{marches2.map((m, index) => (
<SortableRow key={m.id} id={m.id}>
<th style={{textAlign: "center", cursor: "auto"}} scope="row">{index + 1}</th>
<td style={{textAlign: "center", cursor: "auto"}}>{m.poule}</td>
<td style={{textAlign: "center", cursor: "auto"}}>{liceName[index % liceName.length]}</td>
<td style={{textAlign: "right", cursor: "auto", paddingRight: "0"}}>{m.end && m.win > 0 && <CupImg/>}</td>
<td style={{textAlign: "center", minWidth: "11em", paddingLeft: "0.2em"}}
onClick={e => handleCombClick(e, m.id, m.c1)}>
<small><CombName combId={m.c1}/></small></td>
<td style={{textAlign: "center", minWidth: "11em", paddingRight: "0.2em"}}
onClick={e => handleCombClick(e, m.id, m.c2)}>
<small><CombName combId={m.c2}/></small></td>
<td style={{textAlign: "left", cursor: "auto", paddingLeft: "0"}}>{m.end && m.win < 0 && <CupImg/>}</td>
<td style={{textAlign: "center", cursor: "auto"}}>{scoreToString(m.scores)}</td>
<td style={{textAlign: "center", cursor: "pointer", color: "#ff1313"}} onClick={_ => handleDelMatch(m.id)}>
<FontAwesomeIcon icon={faTrash}/></td>
<td style={{textAlign: "center", cursor: "grab"}}></td>
</SortableRow>
))}
<tr>
<td>-</td>
<td></td>
<td></td>
<td></td>
<td style={{textAlign: "center", minWidth: "11em", paddingLeft: "0.2em"}}
onClick={e => handleCombClick(e, -1, combC1nm)}>
{combC1nm && <small><CombName combId={combC1nm}/></small>}</td>
<td style={{textAlign: "center", minWidth: "11em", paddingRight: "0.2em"}}
onClick={e => handleCombClick(e, -2, combC2nm)}>
{combC2nm && <small><CombName combId={combC2nm}/></small>}</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</SortableContext>
</DndContext>
<select ref={selectRef} className="form-select" style={{position: "absolute", top: 0, left: 0, display: "none"}}
value={combSelect} onChange={e => setCombSelect(Number(e.target.value))}>
<option value={0}>-- Sélectionner un combattant --</option>
{combsIDs.map((combId) => (
<option key={combId} value={combId}><CombName combId={combId}/></option>
))}
</select>
</div>
}
function SortableRow({id, children}) {
const {
attributes,
listeners,
setNodeRef,
transform,
transition,
isDragging,
} = useSortable({id});
const style = {
transform: CSS.Transform.toString(transform),
transition,
opacity: isDragging ? 0.5 : 1,
touchAction: 'none', // Désactive le scroll par défaut pendant le drag
};
return (
<tr ref={setNodeRef} style={style} {...attributes}>
{React.Children.map(children, (child, i) =>
i === children.length - 1 // Dernière cellule (icône de drag)
? React.cloneElement(child, {
...child.props, ...attributes, ...listeners, style: {...child.props.style, cursor: "grab", touchAction: 'none'},
})
: child
)}
</tr>
);
}
function BuildTree({treeData, matches, groups}) {
const scrollRef = useRef(null)
const selectRef = useRef(null)

View File

@@ -88,7 +88,7 @@ export function SelectCombModalContent({data, setGroups}) {
}
dispatch({type: 'addListener', payload: {callback: sendRegisterRemove, code: 'sendRegisterRemove'}})
return () => {
dispatch({type: 'removeListener', payload: {callback: sendRegisterRemove, code: 'sendRegisterRemove'}})
dispatch({type: 'removeListener', payload: sendRegisterRemove})
}
}, []);

View File

@@ -1,4 +1,5 @@
import {useEffect, useRef} from "react";
import {win} from "../../utils/CompetitionTools.js";
const max_x = 500;
const size = 24;
@@ -235,17 +236,6 @@ export function DrawGraph({
}
};
// Fonction pour déterminer le gagnant
const win = (scores) => {
let sum = 0;
for (const score of scores) {
if (score.s1 === -1000 || score.s2 === -1000) continue;
if (score.s1 > score.s2) sum++;
else if (score.s1 < score.s2) sum--;
}
return sum;
};
// Effet pour dessiner le graphique
useEffect(() => {
if (root.length === 0) return;

View File

@@ -6,6 +6,7 @@ import {ThreeDots} from "react-loader-spinner";
import {useEffect, useState} from "react";
import {DrawGraph} from "./DrawGraph.jsx";
import {TreeNode} from "../../utils/TreeUtils.js";
import {scoreToString} from "../../utils/CompetitionTools.js";
function CupImg() {
return <img decoding="async" loading="lazy" width="16" height="16" className="wp-image-1635"
@@ -72,25 +73,6 @@ function MenuBar({data, resultShow, setResultShow}) {
*/
}
function scoreToString(score) {
const scorePrint = (s1) => {
switch (s1) {
case -997:
return "disc.";
case -998:
return "abs.";
case -999:
return "for.";
case -1000:
return "";
default:
return String(s1);
}
}
return score.map(o => scorePrint(o.at(0)) + "-" + scorePrint(o.at(1))).join(" | ");
}
function BuildMatchArray({matchs}) {
return <>
<table className="table table-striped">

View File

@@ -0,0 +1,238 @@
export function win(scores) {
let sum = 0
for (const score of scores) {
if (score.s1 === -1000 || score.s2 === -1000) continue
if (score.s1 > score.s2) sum++
else if (score.s1 < score.s2) sum--
}
return sum
}
export function scoreToString(score) {
const scorePrint = (s1) => {
switch (s1) {
case -997:
return "disc."
case -998:
return "abs."
case -999:
return "for."
case -1000:
return ""
default:
return String(s1)
}
}
if (score === null || score === undefined || score.length === 0)
return ""
if (Array.isArray(score[0]))
return score.map(o => scorePrint(o.at(0)) + "-" + scorePrint(o.at(1))).join(" | ")
else
return score.sort((a, b) => a.n_round - b.n_round).map(o => scorePrint(o.s1) + "-" + scorePrint(o.s2)).join(" | ")
}
export function createMatch(category, matchs, groups) {
const combs_2 = {}
for (const group of groups) {
if (!combs_2.hasOwnProperty(group.poule))
combs_2[group.poule] = []
if (!combs_2[group.poule].includes(group.id))
combs_2[group.poule].push(group.id)
}
const ord = new Map()
const newMatch = []
const matchPouleToUpdate = new Map()
let groupeNumber = 0
const numberOfGroup = Object.keys(combs_2).length
for (const [k, combs_1] of Object.entries(combs_2)) {
let matchsList = calcPoule(combs_1.length, category.liceName.split(";").length)
let indexMatch = 0;
const combs = new Array(combs_1.length).fill(0);
// Trouver le premier match valide
while (indexMatch < matchs.length && (!combs_1.includes(matchs[indexMatch].c1) || !combs_1.includes(matchs[indexMatch].c2))) {
indexMatch++;
}
// Attribution aléatoire des combattants
for (const set of matchsList) {
if (indexMatch < matchs.length) {
if (!combs.includes(matchs[indexMatch].c1)) {
if (combs[set[0] - 1] === 0)
combs[set[0] - 1] = matchs[indexMatch].c1
else if (combs[set[1] - 1] === 0)
combs[set[1] - 1] = matchs[indexMatch].c1
if (combs.includes(matchs[indexMatch].c1)) {
const index = combs_1.indexOf(matchs[indexMatch].c1)
if (index !== -1)
combs_1.splice(index, 1)
}
}
if (!combs.includes(matchs[indexMatch].c2)) {
if (combs[set[0] - 1] === 0)
combs[set[0] - 1] = matchs[indexMatch].c2
else if (combs[set[1] - 1] === 0)
combs[set[1] - 1] = matchs[indexMatch].c2
if (combs.includes(matchs[indexMatch].c2)) {
const index = combs_1.indexOf(matchs[indexMatch].c2)
if (index !== -1)
combs_1.splice(index, 1)
}
}
// Passer aux matchs suivants si nécessaire
while (indexMatch < matchs.length &&
((combs.includes(matchs[indexMatch].c1) && combs.includes(matchs[indexMatch].c2)) ||
!combs_1.includes(matchs[indexMatch].c1) || !combs_1.includes(matchs[indexMatch].c2))) {
indexMatch++
}
}
// Remplir les places vides avec des combattants restants
if (combs[set[0] - 1] === 0) {
if (combs_1.length > 0) {
const index = Math.floor(Math.random() * combs_1.length)
combs[set[0] - 1] = combs_1[index]
combs_1.splice(index, 1)
}
}
if (combs[set[1] - 1] === 0) {
if (combs_1.length > 0) {
const index = Math.floor(Math.random() * combs_1.length)
combs[set[1] - 1] = combs_1[index]
combs_1.splice(index, 1)
}
}
if (combs_1.length === 0)
break
}
// Créer les nouveaux matchs
for (let i = 0; i < matchsList.length; i++) {
const set = matchsList[i]
const matchExistant = matchs.find(
(match) =>
(match.c1 === combs[set[0] - 1] && match.c2 === combs[set[1] - 1]) ||
(match.c1 === combs[set[1] - 1] && match.c2 === combs[set[0] - 1]))
if (matchExistant) {
ord.set(matchExistant.id, i * numberOfGroup + groupeNumber)
if (matchExistant.poule !== k)
matchPouleToUpdate.set(matchExistant.id, k)
matchs = matchs.filter((match) => match.id !== matchExistant.id)
continue
}
newMatch.push({
categorie: category.id,
categorie_ord: i * numberOfGroup + groupeNumber,
poule: k,
c1: combs[set[0] - 1],
c2: combs[set[1] - 1],
})
}
groupeNumber++
}
// Mise à jour des ordres et des groupes
const lenToAdd = matchs.length
newMatch.forEach((match) => {
match.categorie_ord += lenToAdd
})
ord.forEach((value, key) => {
ord.set(key, value + lenToAdd)
})
for (let i = 0; i < matchs.length; i++) {
ord.set(matchs[i].id, i)
matchPouleToUpdate.set(matchs[i].id, '-')
}
return {newMatch, matchOrderToUpdate: ord, matchPouleToUpdate}
}
const tt_ronde_opti = new Map()
tt_ronde_opti.set(2, [[1, 2]])
tt_ronde_opti.set(3, [[2, 3], [1, 3], [1, 2]])
tt_ronde_opti.set(4, [[1, 4], [2, 3], [1, 3], [2, 4], [3, 4], [1, 2]])
tt_ronde_opti.set(5, [[1, 2], [3, 4], [5, 1], [2, 3], [5, 4], [1, 3], [2, 5], [4, 1], [3, 5], [4, 2]])
tt_ronde_opti.set(6, [[1, 2], [4, 5], [2, 3], [5, 6], [3, 1], [6, 4], [2, 5], [1, 4], [5, 3], [1, 6], [4, 2], [3, 6], [5, 1], [3, 4], [6, 2]])
tt_ronde_opti.set(7, [[1, 4], [2, 5], [3, 6], [7, 1], [5, 4], [2, 3], [6, 7], [5, 1], [4, 3], [6, 2], [5, 7], [3, 1], [4, 6], [7, 2], [3, 5], [1, 6],
[2, 4], [7, 3], [6, 5], [1, 2], [4, 7]])
tt_ronde_opti.set(8, [[2, 3], [1, 5], [7, 4], [6, 8], [1, 2], [3, 4], [5, 6], [8, 7], [4, 1], [5, 2], [8, 3], [6, 7], [4, 2], [8, 1], [7, 5], [3, 6],
[2, 8], [5, 4], [6, 1], [3, 7], [4, 8], [2, 6], [3, 5], [1, 7], [4, 6], [8, 5], [7, 2], [1, 3]])
tt_ronde_opti.set(9, [[1, 9], [2, 8], [3, 7], [4, 6], [1, 5], [2, 9], [8, 3], [7, 4], [6, 5], [1, 2], [9, 3], [8, 4], [7, 5], [6, 1], [3, 2], [9, 4],
[5, 8], [7, 6], [3, 1], [2, 4], [5, 9], [8, 6], [7, 1], [4, 3], [5, 2], [6, 9], [8, 7], [4, 1], [5, 3], [6, 2], [9, 7], [1, 8], [4, 5], [3, 6],
[5, 7], [9, 8]])
tt_ronde_opti.set(10, [[1, 4], [6, 9], [2, 5], [7, 10], [3, 1], [8, 6], [4, 5], [9, 10], [2, 3], [7, 8], [5, 1], [10, 6], [4, 2], [9, 7], [5, 3],
[10, 8], [1, 2], [6, 7], [3, 4], [8, 9], [5, 10], [1, 6], [2, 7], [3, 8], [4, 9], [6, 5], [10, 2], [8, 1], [7, 4], [9, 3], [2, 6]
, [5, 8], [4, 10], [1, 9], [3, 7], [8, 2], [6, 4], [9, 5], [10, 3], [7, 1], [4, 8], [2, 9], [3, 6], [5, 7], [1, 10]])
tt_ronde_opti.set(11, [[1, 2], [7, 8], [4, 5], [10, 11], [2, 3], [8, 9], [5, 6], [3, 1], [9, 7], [6, 4], [2, 5], [8, 11], [1, 4], [7, 10], [5, 3],
[11, 9], [1, 6], [4, 2], [10, 8], [3, 6], [5, 1], [11, 7], [3, 4], [9, 10], [6, 2], [1, 7], [3, 9], [10, 4], [8, 2], [5, 11], [1, 8], [9, 2],
[3, 10], [4, 11], [6, 7], [9, 1], [2, 10], [11, 3], [7, 5], [6, 8], [10, 1], [11, 2], [4, 7], [8, 5], [6, 9], [11, 1], [7, 3], [4, 8], [9, 5],
[6, 10], [2, 7], [8, 3], [4, 9], [10, 5], [6, 11]])
function calcPoule(n_comb, n_lice) {
if (n_comb === 1)
throw new Error("Le nombre de combattants doit être supérieur à 1.")
if (n_comb / 2 < n_lice)
n_lice = Math.floor(n_comb / 2)
if (n_lice === 1 && n_comb <= 11) {
return [...tt_ronde_opti.get(n_comb)]
} else {
return rutsch(n_comb)
}
}
function rutsch(n_comb) {
const out = []
let virt_n_comb = n_comb
if (n_comb % 2 === 1) virt_n_comb++
// Initialisation du tableau temporaire
const tmp = Array.from({length: virt_n_comb / 2}, () => [0, 0])
for (let i = 0; i < virt_n_comb / 2; i++) {
tmp[i][0] = i + 1
tmp[i][1] = virt_n_comb - i
out.push([...tmp[i]])
}
// Boucle pour les rondes
for (let ronde = 1; ronde < virt_n_comb - 1; ronde++) {
// Déplacement des éléments
const old_val = tmp[virt_n_comb / 2 - 1][0]
for (let i = virt_n_comb / 2 - 1; i > 0; i--) {
tmp[i][0] = tmp[i - 1][0]
}
tmp[0][0] = tmp[0][1]
for (let i = 0; i < virt_n_comb / 2 - 1; i++) {
tmp[i][1] = tmp[i + 1][1]
}
tmp[virt_n_comb / 2 - 1][1] = old_val
// Échange des clés si nécessaire
if (tmp[1][0] === virt_n_comb) {
tmp[1][0] = tmp[0][0]
tmp[0][0] = tmp[0][1]
tmp[0][1] = virt_n_comb
}
// Ajout des paires à la sortie
for (let i = 0; i < virt_n_comb / 2; i++) {
out.push([...tmp[i]])
}
}
// Filtrer les paires dont les valeurs dépassent n_comb
return out.filter(pair => pair[0] <= n_comb && pair[1] <= n_comb)
}

View File

@@ -38,6 +38,34 @@ export function MarchReducer(datas, action) {
}
case 'SORT':
return datas.sort(action.payload)
case 'REORDER':
const oldIndex = datas.findIndex(data => data.id === action.payload.id)
if (oldIndex === -1 || datas[oldIndex].categorie_ord === action.payload.pos)
return datas // Do nothing
const oldPos = datas[oldIndex].categorie_ord
const newPos = action.payload.pos
const new_datas = []
for (const data of datas) {
if (data.id === action.payload.id) {
data.categorie_ord = newPos
} else {
if (oldPos < newPos) {
// Moving down
if (data.categorie_ord > oldPos && data.categorie_ord <= newPos) {
data.categorie_ord -= 1
}
} else if (oldPos > newPos) {
// Moving up
if (data.categorie_ord < oldPos && data.categorie_ord >= newPos) {
data.categorie_ord += 1
}
}
}
new_datas.push(data)
}
return new_datas
default:
throw new Error()
}