4 players: [], //array of objects, filled later
9 props: ['players','initPlayers'],
15 <tr v-for="p in sortedPlayers" v-if="p.available" @click="toggleAvailability(p.index)">
16 <td>{{ p.prenom }}</td>
21 <div id="inactive" class="right">
24 <tr v-for="p in sortedPlayers" v-if="!p.available && p.nom!=''" @click="toggleAvailability(p.index)">
25 <td>{{ p.prenom }}</td>
31 <input class="hide" id="upload" type="file" @change="upload"/>
32 <button class="btn block cancel" @click="uploadTrigger()" title="Charge la liste des joueurs, en principe en début de tournoi">
39 sortedPlayers: function() {
41 .map( (p
,i
) => { return Object
.assign({}, p
, {index: i
}); })
43 return a
.nom
.localeCompare(b
.nom
);
48 toggleAvailability: function(i
) {
49 this.players
[i
].available
= 1 - this.players
[i
].available
;
51 uploadTrigger: function() {
52 document
.getElementById("upload").click();
55 let file
= (e
.target
.files
|| e
.dataTransfer
.files
)[0];
56 var reader
= new FileReader();
57 reader
.onloadend
= ev
=> {
58 this.initPlayers(ev
.currentTarget
.result
);
60 reader
.readAsText(file
);
65 props: ['players','commitScores'],
69 tables: [], //array of arrays of players indices
70 sessions: [], //"mini-points" for each table
71 currentIndex: -1, //table index for scoring
72 scored: [], //boolean for each table index
77 <div v-show="currentIndex < 0">
78 <div class="button-container-horizontal">
79 <button class="btn cancel" :class="{hide: tables.length==0}" @click="cancelRound()" title="Annule la ronde courante : tous les scores en cours seront perdus, et un nouveau tirage effectué. ATTENTION : action irréversible">
82 <button id="doPairings" class="btn" :class="{cancel: tables.length>0}" :disabled="scored.some( s => { return !s; })" @click="doPairings()" title="Répartit les joueurs actifs aléatoirement sur les tables">
86 <div class="pairing" v-for="(table,index) in tables" :class="{scored: scored[index]}"
87 @click="showScoreForm(table,index)">
88 <p>Table {{ index+1 }}</p>
90 <tr v-for="(i,j) in table">
91 <td :class="{toto: players[i].prenom=='Toto'}">{{ players[i].prenom }} {{ players[i].nom }}</td>
92 <td class="score"><span v-show="sessions[index].length > 0">{{ sessions[index][j] }}</span></td>
96 <div v-if="unpaired.length>0" class="pairing unpaired">
98 <div v-for="i in unpaired">
99 {{ players[i].prenom }} {{ players[i].nom }}
103 <div id="scoreInput" v-if="currentIndex >= 0">
105 <tr v-for="(index,i) in tables[currentIndex]">
106 <td :class="{toto: players[tables[currentIndex][i]].prenom=='Toto'}">
107 {{ players[tables[currentIndex][i]].prenom }} {{ players[tables[currentIndex][i]].nom }}
109 <td><input type="text" v-model="sessions[currentIndex][i]" :disabled="scored[currentIndex]"/></td>
112 <div class="button-container-horizontal">
113 <button :class="{hide:scored[currentIndex]}" class="btn validate" @click="setScore()" title="Enregistre le score dans la base">
116 <button :class="{hide:!scored[currentIndex]}" class="btn cancel" @click="resetScore()" title="Annule le score précédemment enregistré">
119 <button class="btn" @click="closeScoreForm()">Fermer</button>
125 // TODO: télécharger la ronde courante
126 // TODO: mémoriser les appariements passés pour éviter que les mêmes joueurs se rencontrent plusieurs fois
127 // --> dans la base: tableau rounds, rounds[0] : {tables[0,1,...], chacune contenant 4 indices de joueurs; + sessions[0,1,...]}
128 // --> devrait séparer les components en plusieurs fichiers...
129 // cas à 5 joueurs : le joueur exempt doit tourner (c'est fait automatiquement en fait)
130 cancelRound: function() {
131 this.scored
.forEach( (s
,i
) => {
135 this.currentIndex
= i
; //TODO: clumsy. funcions should take "index" as argument
139 this.currentIndex
= -1;
142 doPairings: function() {
143 let rounds
= JSON
.parse(localStorage
.getItem("rounds"));
145 if (this.scored
.some( s
=> { return s
; }))
147 this.commitScores(); //TODO: temporary: shouldn't be here... (incremental commit)
150 rounds
.push(this.tables
);
153 // 1) Compute the "meeting" matrix: who played who and how many times
154 let meetMat
= _
.range(this.players
.length
).map( i
=> {
155 _
.range(this.players
.length
).map( j
=> {
159 rounds
.forEach( r
=> { //for each round
160 r
.forEach( t
=> { //for each table within round
161 for (let i
=0; i
<4; i
++) //TODO: these loops are ugly
163 for (let j
=0; j
<4; j
++)
172 // 2) Pre-compute tables repartition (in numbers): depends on active players count % 4
173 let activePlayers
= this.players
174 .map( (p
,i
) => { return Object
.Assign({}, p
, {index:i
}); })
175 .filter( p
=> { return p
.available
; });
176 let repartition
= _
.times(Math
.floor(activePlayers
.length
/4), _
.constant(4));
177 switch (activePlayers
.length
% 4)
181 if (repartition
.length
-1 >= 2)
185 repartition
[repartition
.length
-1] += 2;
190 if (repartition
.length
-1 >= 1)
193 repartition
[repartition
.length
-1]++;
198 // 3) Sort people by total games played (increasing) - naturally solve the potential unpaired case
199 let totalGames
= _
.range(this.players
.length
).map( i
=> { return 0; });
200 rounds
.forEach( r
=> {
207 let sortedPlayers
= activePlayers
208 .map( (p
,i
) => { return Object
.Assign({}, p
, {games:totalGames
[p
.index
]}); })
209 .sort( (a
,b
) => { return a
.games
- b
.games
; });
211 // 4) Affect people on tables, following total games sorted order (with random sampling on ex-aequos)
212 // --> et surtout en minimisant la somme des rencontres précédentes (ci-dessus : cas particulier rare à peu de joueurs)
214 // Simple case first: 4 by 4
216 let currentTable
= [];
217 let ordering
= _
.shuffle(_
.range(this.players
.length
));
218 for (let i
=0; i
<ordering
.length
; i
++)
220 if ( ! this.players
[ordering
[i
]].available
)
222 if (currentTable
.length
>= 4)
224 tables
.push(currentTable
);
227 currentTable
.push(ordering
[i
]);
231 if (currentTable
.length
!= 0)
233 if (currentTable
.length
< 3)
235 let missingPlayers
= 3 - currentTable
.length
;
236 // Pick players from 'missingPlayers' random different tables, if possible
237 if (tables
.length
>= missingPlayers
)
239 let tblNums
= _
.sample(_
.range(tables
.length
), missingPlayers
);
240 tblNums
.forEach( num
=> {
241 currentTable
.push(tables
[num
].pop());
245 if (currentTable
.length
>= 3)
246 tables
.push(currentTable
);
248 this.unpaired
= currentTable
;
250 // Ensure that all tables have 4 players
251 tables
.forEach( t
=> {
253 t
.push(0); //index of "Toto", ghost player
255 this.tables
= tables
;
256 this.sessions
= tables
.map( t
=> { return []; }); //empty sessions
257 this.scored
= tables
.map( t
=> { return false; }); //nothing scored yet
258 this.currentIndex
= -1; //required if reset while scoring
260 showScoreForm: function(table
,index
) {
261 if (this.sessions
[index
].length
== 0)
262 this.sessions
[index
] = _
.times(table
.length
, _
.constant(0));
263 this.currentIndex
= index
;
265 closeScoreForm: function() {
266 if (!this.scored
[this.currentIndex
])
267 this.sessions
[this.currentIndex
] = [];
268 this.currentIndex
= -1;
270 getPdts: function() {
271 let sortedSessions
= this.sessions
[this.currentIndex
]
272 .map( (s
,i
) => { return {value:parseInt(s
), index:i
}; })
273 .sort( (a
,b
) => { return b
.value
- a
.value
; });
274 const ref_pdts
= [4, 2, 1, 0];
275 // NOTE: take care of ex-aequos (spread points subtotal)
276 let curSum
= 0, curCount
= 0, start
= 0;
278 for (let i
=0; i
<4; i
++)
280 curSum
+= ref_pdts
[i
];
282 if (i
==3 || sortedSessions
[i
].value
> sortedSessions
[i
+1].value
)
284 let pdt
= curSum
/ curCount
;
285 for (let j
=start
; j
<=i
; j
++)
286 sortedPdts
.push(pdt
);
292 // Re-order pdts to match table order
293 let pdts
= [0, 0, 0, 0];
294 for (let i
=0; i
<4; i
++)
295 pdts
[sortedSessions
[i
].index
] = sortedPdts
[i
];
298 setScore: function() {
299 let pdts
= this.getPdts();
300 for (let i
=0; i
<4; i
++)
302 this.players
[this.tables
[this.currentIndex
][i
]].pdt
+= pdts
[i
];
303 this.players
[this.tables
[this.currentIndex
][i
]].session
+= parseInt(this.sessions
[this.currentIndex
][i
]);
305 Vue
.set(this.scored
, this.currentIndex
, true);
306 this.currentIndex
= -1;
308 resetScore: function() {
309 let pdts
= this.getPdts();
310 for (let i
=0; i
<4; i
++)
312 this.players
[this.tables
[this.currentIndex
][i
]].pdt
-= pdts
[i
];
313 this.players
[this.tables
[this.currentIndex
][i
]].session
-= parseInt(this.sessions
[this.currentIndex
][i
]);
315 Vue
.set(this.scored
, this.currentIndex
, false);
322 time: 0, //remaining time, in seconds
324 initialTime: 90, //1h30, in minutes
326 setterTime: 0, //to input new initial time
330 <div id="timer" :style="{lineHeight: divHeight + 'px', fontSize: 0.66*divHeight + 'px', width: divWidth + 'px', height: divHeight + 'px'}">
331 <div v-show="!setter" @click.left="pauseResume()" @click.right.prevent="reset()" :class="{timeout:time==0}">
334 <input type="text" autofocus id="setter" @keyup.enter="setTime()" @keyup.esc="setter=false" v-show="setter" v-model="setterTime"></input>
335 <img class="close-cross" src="img/cross.svg" @click="$emit('clockover')"/>
339 formattedTime: function() {
340 let seconds
= this.time
% 60;
341 let minutes
= Math
.floor(this.time
/ 60);
342 return this.padToZero(minutes
) + ":" + this.padToZero(seconds
);
344 divHeight: function() {
345 return screen
.height
;
347 divWidth: function() {
352 setTime: function() {
353 this.initialTime
= this.setterTime
;
357 padToZero: function(a
) {
362 pauseResume: function() {
363 this.running
= !this.running
;
368 this.running
= false;
369 this.time
= this.initialTime
* 60;
376 new Audio("sounds/gong.mp3").play();
377 this.running
= false;
380 if (this.time
== this.initialTime
)
381 new Audio("sounds/gong.mp3").play(); //gong at the beginning
389 created: function() {
390 this.setterTime
= this.initialTime
;
393 mounted: function() {
394 let timer
= document
.getElementById("timer");
396 32: () => { this.setter
= true; }, //Space
397 27: () => { this.setter
= false; }, //Esc
399 document
.addEventListener("keyup", e
=> {
400 if (timer
.style
.display
!== "none")
402 let func
= keyDict
[e
.keyCode
];
413 props: ['players','sortByScore','commitScores'],
416 <table class="ranking">
423 <tr v-for="p in sortedPlayers">
424 <td>{{ p.rank }}</td>
425 <td>{{ p.prenom }} {{ p.nom }}</td>
427 <td>{{ p.session }}</td>
430 <div class="button-container-vertical" style="width:200px">
431 <a id="download" href="#"></a>
432 <button class="btn" @click="download()" title="Télécharge le classement courant au format CSV">Télécharger</button>
433 <button class="btn cancel" @click="resetPlayers()" title="Réinitialise les scores à zéro. ATTENTION: action irréversible">
440 sortedPlayers: function() {
441 let res
= this.rankPeople();
442 // Add rank information (taking care of ex-aequos)
444 for (let i
=0; i
<res
.length
; i
++)
446 if (i
==0 || this.sortByScore(res
[i
],res
[i
-1]) == 0)
448 else //strictly lower scoring
449 res
[i
].rank
= ++rank
;
455 rankPeople: function() {
457 .slice(1) //discard Toto
458 .sort(this.sortByScore
);
460 resetPlayers: function() {
461 if (confirm('Êtes-vous sûr ?'))
464 .slice(1) //discard Toto
471 document
.getElementById("doPairings").click();
474 download: function() {
475 // Prepare file content
476 let content
= "prénom,nom,pdt,session\n";
478 .slice(1) //discard Toto
479 .sort(this.sortByScore
)
481 content
+= p
.prenom
+ "," + p
.nom
+ "," + p
.pdt
+ "," + p
.session
+ "\n";
483 // Prepare and trigger download link
484 let downloadAnchor
= document
.getElementById("download");
485 downloadAnchor
.setAttribute("download", "classement.csv");
486 downloadAnchor
.href
= "data:text/plain;charset=utf-8," + encodeURIComponent(content
);
487 downloadAnchor
.click();
492 created: function() {
493 let players
= JSON
.parse(localStorage
.getItem("players"));
494 if (players
!== null)
496 this.addToto(players
);
497 this.players
= players
;
501 addToto: function(array
) {
502 array
.unshift({ //add ghost 4th player for 3-players tables
510 // Used both in ranking and pairings:
511 sortByScore: function(a
,b
) {
512 return b
.pdt
- a
.pdt
+ (Math
.atan(b
.session
- a
.session
) / (Math
.PI
/2)) / 2;
514 commitScores: function() {
515 localStorage
.setItem(
517 JSON
.stringify(this.players
.slice(1)) //discard Toto
520 // Used in players, reinit players array
521 initPlayers: function(csv
) {
523 .split(/\r\n|\n|\r/) //line breaks
524 .splice(1); //discard header
525 let players
= allLines
526 .filter( line
=> { return line
.length
> 0; }) //remove empty lines
528 let parts
= line
.split(",");
529 let p
= { prenom: parts
[0], nom: parts
[1] };
530 p
.pdt
= parts
.length
> 2 ? parseFloat(parts
[2]) : 0;
531 p
.session
= parts
.length
> 3 ? parseInt(parts
[3]) : 0;
532 p
.available
= parts
.length
> 4 ? parts
[4] : 1;
535 this.addToto(players
);
536 this.players
= players
;
537 this.commitScores(); //save players in memory