4 players: [], //array of objects, filled later
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>
33 sortedPlayers: function() {
35 .map( (p
,i
) => { return Object
.assign({}, p
, {index: i
}); })
37 return a
.nom
.localeCompare(b
.nom
);
42 toggleAvailability: function(i
) {
43 this.players
[i
].available
= 1 - this.players
[i
].available
;
44 this.$forceUpdate(); //TODO (Vue.set... ?!)
49 props: ['players','sortByScore','writeScoreToDb'],
52 <table class="ranking">
59 <tr v-for="p in sortedPlayers">
61 <td>{{ p.prenom }} {{ p.nom }}</td>
63 <td>{{ p.session }}</td>
66 <div class="button-container-vertical" style="width:200px">
67 <button class="btn cancel" @click="resetPlayers()">Réinitialiser</button>
68 <button id="restoreBtn" class="btn" @click="restoreLast()">Restaurer</button>
73 sortedPlayers: function() {
74 let res
= this.rankPeople();
75 // Add rank information (taking care of ex-aequos)
77 for (let i
=0; i
<res
.length
; i
++)
79 if (i
==0 || this.sortByScore(res
[i
],res
[i
-1]) == 0)
81 else //strictly lower scoring
88 rankPeople: function() {
90 .slice(1) //discard Toto
91 .map( p
=> { return Object
.assign({}, p
); }) //to not alter original array
92 .sort(this.sortByScore
);
94 resetPlayers: function() {
96 .slice(1) //discard Toto
102 this.writeScoreToDb();
103 document
.getElementById("runPairing").click(); //TODO: hack...
105 restoreLast: function() {
106 let xhr
= new XMLHttpRequest();
108 xhr
.onreadystatechange = function() {
109 if (this.readyState
== 4 && this.status
== 200)
111 let players
= JSON
.parse(xhr
.responseText
);
112 if (players
.length
> 0)
114 players
.unshift({ //add ghost 4th player for 3-players tables
121 self
.players
= players
;
125 xhr
.open("GET", "scripts/rw_players.php?restore=1", true);
131 props: ['players','writeScoreToDb'],
135 tables: [], //array of arrays of players indices
136 sessions: [], //"mini-points" for each table
137 currentIndex: -1, //table index for scoring
138 scored: [], //boolean for each table index
143 <div v-show="currentIndex < 0">
144 <button id="runPairing" class="block btn" @click="doPairings()">Nouvelle ronde</button>
145 <div class="pairing" v-for="(table,index) in tables" :class="{scored: scored[index]}"
146 @click="showScoreForm(table,index)">
147 <p>Table {{ index+1 }}</p>
149 <tr v-for="(i,j) in table">
150 <td :class="{toto: players[i].prenom=='Toto'}">{{ players[i].prenom }} {{ players[i].nom }}</td>
151 <td class="score"><span v-show="sessions[index].length > 0">{{ sessions[index][j] }}</span></td>
155 <div v-if="unpaired.length>0" class="pairing unpaired">
157 <div v-for="i in unpaired">
158 {{ players[i].prenom }} {{ players[i].nom }}
162 <div id="scoreInput" v-if="currentIndex >= 0">
164 <tr v-for="(index,i) in tables[currentIndex]">
165 <td :class="{toto: players[tables[currentIndex][i]].prenom=='Toto'}">
166 {{ players[tables[currentIndex][i]].prenom }} {{ players[tables[currentIndex][i]].nom }}
168 <td><input type="text" v-model="sessions[currentIndex][i]" value="0"/></td>
171 <div class="button-container-horizontal">
172 <button class="btn validate" @click="setScore()">Enregistrer</button>
173 <button class="btn" @click="currentIndex = -1">Fermer</button>
175 <div v-if="scored[currentIndex]" class="warning">
176 Attention: un score a déjà été enregistré.
177 Les points indiqués ici s'ajouteront : il faut d'abord
178 <span class="link" @click="document.getElementById('restoreBtn').click()">restaurer l'état précédent.</span>
179 Si c'est déjà fait, ignorer ce message :)
185 doPairings: function() {
186 // Simple case first: 4 by 4
188 let currentTable
= [];
189 let ordering
= _
.shuffle(_
.range(this.players
.length
));
190 for (let i
=0; i
<ordering
.length
; i
++)
192 if ( ! this.players
[ordering
[i
]].available
)
194 if (currentTable
.length
>= 4)
196 tables
.push(currentTable
);
199 currentTable
.push(ordering
[i
]);
203 if (currentTable
.length
!= 0)
205 if (currentTable
.length
< 3)
207 let missingPlayers
= 3 - currentTable
.length
;
208 // Pick players from 'missingPlayers' random different tables, if possible
209 if (tables
.length
>= missingPlayers
)
211 let tblNums
= _
.sample(_
.range(tables
.length
), missingPlayers
);
212 tblNums
.forEach( num
=> {
213 currentTable
.push(tables
[num
].pop());
217 if (currentTable
.length
>= 3)
218 tables
.push(currentTable
);
220 this.unpaired
= currentTable
;
222 // Ensure that all tables have 4 players
223 tables
.forEach( t
=> {
225 t
.push(0); //index of "Toto", ghost player
227 this.tables
= tables
;
228 this.sessions
= tables
.map( t
=> { return []; }); //empty sessions
229 this.scored
= tables
.map( t
=> { return false; }); //nothing scored yet
230 this.currentIndex
= -1; //required if reset while scoring
232 showScoreForm: function(table
,index
) {
233 if (this.sessions
[index
].length
== 0)
234 this.sessions
[index
] = _
.times(table
.length
, _
.constant(0));
235 this.currentIndex
= index
;
237 setScore: function() {
238 let sortedSessions
= this.sessions
[this.currentIndex
]
239 .map( (s
,i
) => { return {value:s
, index:i
}; })
240 .sort( (a
,b
) => { return parseInt(b
.value
) - parseInt(a
.value
); });
241 let pdts
= [4, 2, 1, 0];
242 // NOTE: take care of ex-aequos (spread points subtotal)
243 let curSum
= 0, curCount
= 0, start
= 0;
244 for (let i
=0; i
<4; i
++)
249 if (i
==3 || sortedSessions
[i
].value
> sortedSessions
[i
+1].value
)
251 let pdt
= curSum
/ curCount
;
252 for (let j
=start
; j
<=i
; j
++)
253 this.players
[this.tables
[this.currentIndex
][sortedSessions
[j
].index
]].pdt
+= pdt
;
259 this.players
[this.tables
[this.currentIndex
][i
]].session
+= parseInt(this.sessions
[this.currentIndex
][i
]);
261 this.scored
[this.currentIndex
] = true;
262 this.currentIndex
= -1;
263 this.writeScoreToDb();
270 time: 0, //remaining time, in seconds
276 <div @click.left="pauseResume()" @click.right.prevent="reset()" :class="{timeout:time==0}">
279 <img class="close-cross" src="img/cross.svg" @click="$emit('clockover')"/>
283 formattedTime: function() {
284 let seconds
= this.time
% 60;
285 let minutes
= Math
.floor(this.time
/ 60);
286 return this.padToZero(minutes
) + ":" + this.padToZero(seconds
);
290 padToZero: function(a
) {
295 pauseResume: function() {
296 this.running
= !this.running
;
301 this.running
= false;
302 this.time
= 10; //1:30
309 new Audio("sounds/gong.mp3").play();
310 this.running
= false;
320 created: function() {
325 created: function() {
326 let xhr
= new XMLHttpRequest();
328 xhr
.onreadystatechange = function() {
329 if (this.readyState
== 4 && this.status
== 200)
331 let players
= JSON
.parse(xhr
.responseText
);
332 players
.forEach( p
=> {
333 p
.pdt
= !!p
.pdt
? parseFloat(p
.pdt
) : 0;
334 p
.session
= !!p
.session
? parseInt(p
.session
) : 0;
335 p
.available
= !!p
.available
? p
.available : 1; //use integer for fputcsv PHP func
337 players
.unshift({ //add ghost 4th player for 3-players tables
344 self
.players
= players
;
347 xhr
.open("GET", "scripts/rw_players.php", true);
351 // Used both in ranking and pairings:
352 sortByScore: function(a
,b
) {
353 return b
.pdt
- a
.pdt
+ (Math
.atan(b
.session
- a
.session
) / (Math
.PI
/2)) / 2;
355 writeScoreToDb: function() {
356 let xhr
= new XMLHttpRequest();
357 xhr
.open("POST", "scripts/rw_players.php");
358 xhr
.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
359 let orderedPlayers
= this.players
360 .slice(1) //discard Toto
361 .map( p
=> { return Object
.assign({}, p
); }) //deep (enough) copy
362 .sort(this.sortByScore
);
363 xhr
.send("players="+encodeURIComponent(JSON
.stringify(orderedPlayers
)));