09f0c17e657aa9b5dcf590d6edc42f0db908723e
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: clic sur "Valider" télécharge 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() {
133 doPairings: function() {
134 // Simple case first: 4 by 4
136 let currentTable
= [];
137 let ordering
= _
.shuffle(_
.range(this.players
.length
));
138 for (let i
=0; i
<ordering
.length
; i
++)
140 if ( ! this.players
[ordering
[i
]].available
)
142 if (currentTable
.length
>= 4)
144 tables
.push(currentTable
);
147 currentTable
.push(ordering
[i
]);
151 if (currentTable
.length
!= 0)
153 if (currentTable
.length
< 3)
155 let missingPlayers
= 3 - currentTable
.length
;
156 // Pick players from 'missingPlayers' random different tables, if possible
157 if (tables
.length
>= missingPlayers
)
159 let tblNums
= _
.sample(_
.range(tables
.length
), missingPlayers
);
160 tblNums
.forEach( num
=> {
161 currentTable
.push(tables
[num
].pop());
165 if (currentTable
.length
>= 3)
166 tables
.push(currentTable
);
168 this.unpaired
= currentTable
;
170 // Ensure that all tables have 4 players
171 tables
.forEach( t
=> {
173 t
.push(0); //index of "Toto", ghost player
175 this.tables
= tables
;
176 this.sessions
= tables
.map( t
=> { return []; }); //empty sessions
177 this.scored
= tables
.map( t
=> { return false; }); //nothing scored yet
178 this.currentIndex
= -1; //required if reset while scoring
180 showScoreForm: function(table
,index
) {
181 if (this.sessions
[index
].length
== 0)
182 this.sessions
[index
] = _
.times(table
.length
, _
.constant(0));
183 this.currentIndex
= index
;
185 closeScoreForm: function() {
186 if (!this.scored
[this.currentIndex
])
187 this.sessions
[this.currentIndex
] = [];
188 this.currentIndex
= -1;
190 getPdts: function() {
191 let sortedSessions
= this.sessions
[this.currentIndex
]
192 .map( (s
,i
) => { return {value:parseInt(s
), index:i
}; })
193 .sort( (a
,b
) => { return b
.value
- a
.value
; });
194 const ref_pdts
= [4, 2, 1, 0];
195 // NOTE: take care of ex-aequos (spread points subtotal)
196 let curSum
= 0, curCount
= 0, start
= 0;
198 for (let i
=0; i
<4; i
++)
200 curSum
+= ref_pdts
[i
];
202 if (i
==3 || sortedSessions
[i
].value
> sortedSessions
[i
+1].value
)
204 let pdt
= curSum
/ curCount
;
205 for (let j
=start
; j
<=i
; j
++)
206 sortedPdts
.push(pdt
);
212 // Re-order pdts to match table order
213 let pdts
= [0, 0, 0, 0];
214 for (let i
=0; i
<4; i
++)
215 pdts
[sortedSessions
[i
].index
] = sortedPdts
[i
];
218 setScore: function() {
219 let pdts
= this.getPdts();
220 for (let i
=0; i
<4; i
++)
222 this.players
[this.tables
[this.currentIndex
][i
]].pdt
+= pdts
[i
];
223 this.players
[this.tables
[this.currentIndex
][i
]].session
+= parseInt(this.sessions
[this.currentIndex
][i
]);
225 Vue
.set(this.scored
, this.currentIndex
, true);
226 this.currentIndex
= -1;
228 resetScore: function() {
229 let pdts
= this.getPdts();
230 for (let i
=0; i
<4; i
++)
232 this.players
[this.tables
[this.currentIndex
][i
]].pdt
-= pdts
[i
];
233 this.players
[this.tables
[this.currentIndex
][i
]].session
-= parseInt(this.sessions
[this.currentIndex
][i
]);
235 Vue
.set(this.scored
, this.currentIndex
, false);
242 time: 0, //remaining time, in seconds
244 initialTime: 90, //1h30, in minutes
246 setterTime: 0, //to input new initial time
250 <div id="timer" :style="{lineHeight: divHeight + 'px', fontSize: 0.66*divHeight + 'px', width: divWidth + 'px', height: divHeight + 'px'}">
251 <div v-show="!setter" @click.left="pauseResume()" @click.right.prevent="reset()" :class="{timeout:time==0}">
254 <input type="text" autofocus id="setter" @keyup.enter="setTime()" @keyup.esc="setter=false" v-show="setter" v-model="setterTime"></input>
255 <img class="close-cross" src="img/cross.svg" @click="$emit('clockover')"/>
259 formattedTime: function() {
260 let seconds
= this.time
% 60;
261 let minutes
= Math
.floor(this.time
/ 60);
262 return this.padToZero(minutes
) + ":" + this.padToZero(seconds
);
264 divHeight: function() {
265 return screen
.height
;
267 divWidth: function() {
272 setTime: function() {
273 this.initialTime
= this.setterTime
;
277 padToZero: function(a
) {
282 pauseResume: function() {
283 this.running
= !this.running
;
288 this.running
= false;
289 this.time
= this.initialTime
* 60;
296 new Audio("sounds/gong.mp3").play();
297 this.running
= false;
300 if (this.time
== this.initialTime
)
301 new Audio("sounds/gong.mp3").play(); //gong at the beginning
309 created: function() {
310 this.setterTime
= this.initialTime
;
313 mounted: function() {
314 let timer
= document
.getElementById("timer");
316 32: () => { this.setter
= true; }, //Space
317 27: () => { this.setter
= false; }, //Esc
319 document
.addEventListener("keyup", e
=> {
320 if (timer
.style
.display
!== "none")
322 let func
= keyDict
[e
.keyCode
];
333 props: ['players','sortByScore','commitScores'],
336 <table class="ranking">
343 <tr v-for="p in sortedPlayers">
344 <td>{{ p.rank }}</td>
345 <td>{{ p.prenom }} {{ p.nom }}</td>
347 <td>{{ p.session }}</td>
350 <div class="button-container-vertical" style="width:200px">
351 <a id="download" href="#"></a>
352 <button class="btn" @click="download()" title="Télécharge le classement courant au format CSV">Télécharger</button>
353 <button class="btn cancel" @click="resetPlayers()" title="Réinitialise les scores à zéro. ATTENTION: action irréversible">
360 sortedPlayers: function() {
361 let res
= this.rankPeople();
362 // Add rank information (taking care of ex-aequos)
364 for (let i
=0; i
<res
.length
; i
++)
366 if (i
==0 || this.sortByScore(res
[i
],res
[i
-1]) == 0)
368 else //strictly lower scoring
369 res
[i
].rank
= ++rank
;
375 rankPeople: function() {
377 .slice(1) //discard Toto
378 .sort(this.sortByScore
);
380 resetPlayers: function() {
381 if (confirm('Êtes-vous sûr ?'))
384 .slice(1) //discard Toto
391 document
.getElementById("doPairings").click();
394 download: function() {
395 // Prepare file content
396 let content
= "prénom,nom,pdt,session\n";
398 .slice(1) //discard Toto
399 .sort(this.sortByScore
)
401 content
+= p
.prenom
+ "," + p
.nom
+ "," + p
.pdt
+ "," + p
.session
+ "\n";
403 // Prepare and trigger download link
404 let downloadAnchor
= document
.getElementById("download");
405 downloadAnchor
.setAttribute("download", "classement.csv");
406 downloadAnchor
.href
= "data:text/plain;charset=utf-8," + encodeURIComponent(content
);
407 downloadAnchor
.click();
412 created: function() {
413 let players
= JSON
.parse(localStorage
.getItem("players"));
414 if (players
!== null)
416 this.addToto(players
);
417 this.players
= players
;
421 addToto: function(array
) {
422 array
.unshift({ //add ghost 4th player for 3-players tables
430 // Used both in ranking and pairings:
431 sortByScore: function(a
,b
) {
432 return b
.pdt
- a
.pdt
+ (Math
.atan(b
.session
- a
.session
) / (Math
.PI
/2)) / 2;
434 commitScores: function() {
435 localStorage
.setItem(
437 JSON
.stringify(this.players
.slice(1)) //discard Toto
440 // Used in players, reinit players array
441 initPlayers: function(csv
) {
443 .split(/\r\n|\n|\r/) //line breaks
444 .splice(1); //discard header
445 let players
= allLines
446 .filter( line
=> { return line
.length
> 0; }) //remove empty lines
448 let parts
= line
.split(",");
449 let p
= { prenom: parts
[0], nom: parts
[1] };
450 p
.pdt
= parts
.length
> 2 ? parseFloat(parts
[2]) : 0;
451 p
.session
= parts
.length
> 3 ? parseInt(parts
[3]) : 0;
452 p
.available
= parts
.length
> 4 ? parts
[4] : 1;
455 this.addToto(players
);
456 this.players
= players
;
457 this.commitScores(); //save players in memory