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','writeScoreToDb'],
53 tables: [], //array of arrays of players indices
54 sessions: [], //"mini-points" for each table
55 currentIndex: -1, //table index for scoring
56 scored: [], //boolean for each table index
61 <div v-show="currentIndex < 0">
62 <button id="runPairing" class="block btn" @click="doPairings()">Nouvelle ronde</button>
63 <div class="pairing" v-for="(table,index) in tables" :class="{scored: scored[index]}"
64 @click="showScoreForm(table,index)">
65 <p>Table {{ index+1 }}</p>
67 <tr v-for="(i,j) in table">
68 <td :class="{toto: players[i].prenom=='Toto'}">{{ players[i].prenom }} {{ players[i].nom }}</td>
69 <td class="score"><span v-show="sessions[index].length > 0">{{ sessions[index][j] }}</span></td>
73 <div v-if="unpaired.length>0" class="pairing unpaired">
75 <div v-for="i in unpaired">
76 {{ players[i].prenom }} {{ players[i].nom }}
80 <div id="scoreInput" v-if="currentIndex >= 0">
82 <tr v-for="(index,i) in tables[currentIndex]">
83 <td :class="{toto: players[tables[currentIndex][i]].prenom=='Toto'}">
84 {{ players[tables[currentIndex][i]].prenom }} {{ players[tables[currentIndex][i]].nom }}
86 <td><input type="text" v-model="sessions[currentIndex][i]"/></td>
89 <div class="button-container-horizontal">
90 <button class="btn validate" @click="setScore()">Enregistrer</button>
91 <button class="btn" @click="currentIndex = -1">Fermer</button>
93 <div v-if="scored[currentIndex]" class="warning">
94 Attention: un score a déjà été enregistré.
95 Les points indiqués ici s'ajouteront : il faut d'abord
96 <span class="link" @click="clickRestore()">restaurer l'état précédent.</span>
97 Si c'est déjà fait, ignorer ce message :)
103 doPairings: function() {
104 // Simple case first: 4 by 4
106 let currentTable
= [];
107 let ordering
= _
.shuffle(_
.range(this.players
.length
));
108 for (let i
=0; i
<ordering
.length
; i
++)
110 if ( ! this.players
[ordering
[i
]].available
)
112 if (currentTable
.length
>= 4)
114 tables
.push(currentTable
);
117 currentTable
.push(ordering
[i
]);
121 if (currentTable
.length
!= 0)
123 if (currentTable
.length
< 3)
125 let missingPlayers
= 3 - currentTable
.length
;
126 // Pick players from 'missingPlayers' random different tables, if possible
127 if (tables
.length
>= missingPlayers
)
129 let tblNums
= _
.sample(_
.range(tables
.length
), missingPlayers
);
130 tblNums
.forEach( num
=> {
131 currentTable
.push(tables
[num
].pop());
135 if (currentTable
.length
>= 3)
136 tables
.push(currentTable
);
138 this.unpaired
= currentTable
;
140 // Ensure that all tables have 4 players
141 tables
.forEach( t
=> {
143 t
.push(0); //index of "Toto", ghost player
145 this.tables
= tables
;
146 this.sessions
= tables
.map( t
=> { return []; }); //empty sessions
147 this.scored
= tables
.map( t
=> { return false; }); //nothing scored yet
148 this.currentIndex
= -1; //required if reset while scoring
150 showScoreForm: function(table
,index
) {
151 if (this.sessions
[index
].length
== 0)
152 this.sessions
[index
] = _
.times(table
.length
, _
.constant(0));
153 this.currentIndex
= index
;
155 setScore: function() {
156 let sortedSessions
= this.sessions
[this.currentIndex
]
157 .map( (s
,i
) => { return {value:s
, index:i
}; })
158 .sort( (a
,b
) => { return parseInt(b
.value
) - parseInt(a
.value
); });
159 let pdts
= [4, 2, 1, 0];
160 // NOTE: take care of ex-aequos (spread points subtotal)
161 let curSum
= 0, curCount
= 0, start
= 0;
162 for (let i
=0; i
<4; i
++)
167 if (i
==3 || sortedSessions
[i
].value
> sortedSessions
[i
+1].value
)
169 let pdt
= curSum
/ curCount
;
170 for (let j
=start
; j
<=i
; j
++)
171 this.players
[this.tables
[this.currentIndex
][sortedSessions
[j
].index
]].pdt
+= pdt
;
177 this.players
[this.tables
[this.currentIndex
][i
]].session
+= parseInt(this.sessions
[this.currentIndex
][i
]);
179 this.scored
[this.currentIndex
] = true;
180 this.currentIndex
= -1;
181 this.writeScoreToDb();
183 clickRestore: function() {
184 document
.getElementById('restoreBtn').click();
191 time: 0, //remaining time, in seconds
196 <div id="timer" :style="{lineHeight: textHeight + 'px', fontSize: 0.66*textHeight + 'px'}">
197 <div @click.left="pauseResume()" @click.right.prevent="reset()" :class="{timeout:time==0}">
200 <img class="close-cross" src="img/cross.svg" @click="$emit('clockover')"/>
204 formattedTime: function() {
205 let seconds
= this.time
% 60;
206 let minutes
= Math
.floor(this.time
/ 60);
207 return this.padToZero(minutes
) + ":" + this.padToZero(seconds
);
209 textHeight: function() {
210 return screen
.height
;
214 padToZero: function(a
) {
219 pauseResume: function() {
220 this.running
= !this.running
;
225 this.running
= false;
226 this.time
= 5400; //1:30
233 new Audio("sounds/gong.mp3").play();
234 this.running
= false;
244 created: function() {
249 props: ['players','sortByScore','writeScoreToDb'],
252 <table class="ranking">
259 <tr v-for="p in sortedPlayers">
260 <td>{{ p.rank }}</td>
261 <td>{{ p.prenom }} {{ p.nom }}</td>
263 <td>{{ p.session }}</td>
266 <div class="button-container-vertical" style="width:200px">
267 <button class="btn cancel" @click="resetPlayers()">Réinitialiser</button>
268 <button id="restoreBtn" class="btn" @click="restoreLast()">Restaurer</button>
273 sortedPlayers: function() {
274 let res
= this.rankPeople();
275 // Add rank information (taking care of ex-aequos)
277 for (let i
=0; i
<res
.length
; i
++)
279 if (i
==0 || this.sortByScore(res
[i
],res
[i
-1]) == 0)
281 else //strictly lower scoring
282 res
[i
].rank
= ++rank
;
288 rankPeople: function() {
290 .slice(1) //discard Toto
291 .sort(this.sortByScore
);
293 resetPlayers: function() {
295 .slice(1) //discard Toto
301 this.writeScoreToDb();
302 document
.getElementById("runPairing").click();
304 restoreLast: function() {
305 let xhr
= new XMLHttpRequest();
307 xhr
.onreadystatechange = function() {
308 if (this.readyState
== 4 && this.status
== 200)
310 let players
= JSON
.parse(xhr
.responseText
);
311 if (players
.length
> 0)
313 players
.unshift({ //add ghost 4th player for 3-players tables
320 // NOTE: Vue warning "do not mutate property" if direct self.players = players
321 for (let i
=1; i
<players
.length
; i
++)
323 players
[i
].pdt
= parseFloat(players
[i
].pdt
);
324 players
[i
].session
= parseInt(players
[i
].session
);
325 Vue
.set(self
.players
, i
, players
[i
]);
330 xhr
.open("GET", "scripts/rw_players.php?restore=1", true);
336 created: function() {
337 let xhr
= new XMLHttpRequest();
339 xhr
.onreadystatechange = function() {
340 if (this.readyState
== 4 && this.status
== 200)
342 let players
= JSON
.parse(xhr
.responseText
);
343 players
.forEach( p
=> {
344 p
.pdt
= !!p
.pdt
? parseFloat(p
.pdt
) : 0;
345 p
.session
= !!p
.session
? parseInt(p
.session
) : 0;
346 p
.available
= !!p
.available
? p
.available : 1; //use integer for fputcsv PHP func
348 players
.unshift({ //add ghost 4th player for 3-players tables
355 self
.players
= players
;
358 xhr
.open("GET", "scripts/rw_players.php", true);
362 // Used both in ranking and pairings:
363 sortByScore: function(a
,b
) {
364 return b
.pdt
- a
.pdt
+ (Math
.atan(b
.session
- a
.session
) / (Math
.PI
/2)) / 2;
366 writeScoreToDb: function() {
367 let xhr
= new XMLHttpRequest();
368 xhr
.open("POST", "scripts/rw_players.php");
369 xhr
.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
370 let orderedPlayers
= this.players
371 .slice(1) //discard Toto
372 .sort(this.sortByScore
);
373 xhr
.send("players="+encodeURIComponent(JSON
.stringify(orderedPlayers
)));