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>
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... ?!)
57 <table class="ranking">
61 <th @click="sortMethod='pdt'" class="scoring" :class="{active: sortMethod=='pdt'}">Points</th>
62 <th @click="sortMethod='session'" class="scoring" :class="{active: sortMethod=='session'}">Mini-pts</th>
64 <tr v-for="p in sortedPlayers" v-if="p.nom!=''">
66 <td>{{ p.prenom }} {{ p.nom }}</td>
68 <td>{{ p.session }}</td>
73 computed: { //TODO: first sort on score, then on Pdt (and reciprocally) --> function add fraction relative Pdt / score (compute min max first, take care of 0 case)
74 sortedPlayers: function() {
75 let sortFunc
= this.sortMethod
== "pdt"
78 let res
= this.players
79 .map( p
=> { return Object
.assign({}, p
); }) //to not alter original array
81 // Add rank information (taking care of ex-aequos)
83 for (let i
=0; i
<res
.length
; i
++)
85 if (i
==0 || sortFunc(res
[i
],res
[i
-1]) == 0)
87 else //strictly lower scoring
94 sortByPdt: function(a
,b
) {
97 sortBySession: function(a
,b
) {
98 return b
.session
- a
.session
;
107 tables: [], //array of arrays of players indices
108 pdts: [], //"points de table" for each table
109 sessions: [], //"mini-points" for each table
110 currentIndex: -1, //table index for scoring
115 <div v-show="currentIndex < 0">
116 <button class="block btn" @click="shuffle()">Appariement</button>
117 <div class="pairing" v-for="(table,index) in tables" :class="{scored: pdts[index].length > 0}"
118 @click="showScoreForm(table,index)">
119 <p>Table {{ index+1 }}</p>
121 <tr v-for="(i,j) in table">
122 <td :class="{toto: players[i].prenom=='Toto'}">{{ players[i].prenom }} {{ players[i].nom }}</td>
123 <td class="score"><span v-show="sessions[index].length > 0">{{ sessions[index][j] }}</span></td>
127 <div v-if="unpaired.length>0" class="pairing unpaired">
129 <div v-for="i in unpaired">
130 {{ players[i].prenom }} {{ players[i].nom }}
134 <div id="scoreInput" v-if="currentIndex >= 0">
136 <tr v-for="(index,i) in tables[currentIndex]">
137 <td :class="{toto: players[tables[currentIndex][i]].prenom=='Toto'}">
138 {{ players[tables[currentIndex][i]].prenom }} {{ players[tables[currentIndex][i]].nom }}
140 <td><input type="text" v-model="sessions[currentIndex][i]" value="0"/></td>
143 <div class="button-container">
144 <button class="btn" @click="setScore()">Enregistrer</button>
145 <button class="btn cancel" @click="resetScore()">Annuler</button>
151 doPairings: function() {
152 // Simple case first: 4 by 4
154 let currentTable
= [];
155 let ordering
= _
.shuffle(_
.range(this.players
.length
)); //TODO: take scores into account?
156 for (let i
=0; i
<ordering
.length
; i
++)
158 if ( ! this.players
[ordering
[i
]].available
)
160 if (currentTable
.length
>= 4)
162 tables
.push(currentTable
);
165 currentTable
.push(ordering
[i
]);
169 if (currentTable
.length
!= 0)
171 if (currentTable
.length
< 3)
173 let missingPlayers
= 3 - currentTable
.length
;
174 // Pick players from 'missingPlayers' random different tables, if possible
175 if (tables
.length
>= missingPlayers
)
177 let tblNums
= _
.sample(_
.range(tables
.length
), missingPlayers
);
178 tblNums
.forEach( num
=> {
179 currentTable
.push(tables
[num
].pop());
183 if (currentTable
.length
>= 3)
184 tables
.push(currentTable
);
186 this.unpaired
= currentTable
;
188 // Ensure that all tables have 4 players
189 tables
.forEach( t
=> {
191 t
.push(0); //index of "Toto", ghost player
193 this.tables
= tables
;
194 this.pdts
= tables
.map( t
=> { return []; }); //empty pdts
195 this.sessions
= tables
.map( t
=> { return []; }); //empty sessions
197 shuffle: function() {
200 showScoreForm: function(table
,index
) {
201 if (this.pdts
[index
].length
> 0)
202 return; //already scored
203 this.pdts
[index
] = _
.times(table
.length
, _
.constant(0));
204 this.sessions
[index
] = _
.times(table
.length
, _
.constant(0));
205 this.currentIndex
= index
;
207 setScore: function() {
208 let sortedSessions
= this.sessions
[this.currentIndex
]
209 .map( (s
,i
) => { return {value:s
, index:i
}; })
210 .sort( (a
,b
) => { return parseInt(b
.value
) - parseInt(a
.value
); });
211 let pdts
= [4, 2, 1, 0]; //TODO: ex-aequos ?!
212 for (let i
=0; i
<this.tables
[this.currentIndex
].length
; i
++)
214 this.players
[this.tables
[this.currentIndex
][sortedSessions
[i
].index
]].pdt
+= pdts
[i
];
215 this.players
[this.tables
[this.currentIndex
][i
]].session
+= parseInt(this.sessions
[this.currentIndex
][i
]);
217 this.currentIndex
= -1;
218 this.writeScoreToDb();
220 resetScore: function() {
221 this.pdts
[this.currentIndex
] = [];
222 this.sessions
[this.currentIndex
] = [];
223 this.currentIndex
= -1;
225 writeScoreToDb: function()
227 let xhr
= new XMLHttpRequest();
228 xhr
.open("POST", "scripts/rw_players.php");
229 xhr
.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
230 let orderedPlayers
= this.players
231 .slice(1) //discard "Toto"
232 .map( p
=> { return Object
.assign({}, p
); }) //deep (enough) copy
233 .sort( (a
,b
) => { return b
.pdt
- a
.pdt
; }); //TODO: re-use sorting function in ranking component
234 xhr
.send("players="+encodeURIComponent(JSON
.stringify(orderedPlayers
)));
239 created: function() {
240 let xhr
= new XMLHttpRequest();
242 xhr
.onreadystatechange = function() {
243 if (this.readyState
== 4 && this.status
== 200)
245 let players
= JSON
.parse(xhr
.responseText
);
246 players
.forEach( p
=> {
247 p
.pdt
= !!p
.pdt
? parseInt(p
.pdt
) : 0;
248 p
.session
= !!p
.session
? parseInt(p
.session
) : 0;
249 p
.available
= !!p
.available
? p
.available : 1; //use integer for fputcsv PHP func
251 players
.unshift({ //add ghost 4th player for 3-players tables
258 self
.players
= players
;
261 xhr
.open("GET", "scripts/rw_players.php", true);