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... ?!)
49 props: ['players','sortByScore','rankPeople'],
52 <table class="ranking">
59 <tr v-for="p in sortedPlayers">
61 <td>{{ p.prenom }} {{ p.nom }}</td>
63 <td>{{ p.session }}</td>
69 sortedPlayers: function() {
70 let res
= this.rankPeople();
71 // Add rank information (taking care of ex-aequos)
73 for (let i
=0; i
<res
.length
; i
++)
75 if (i
==0 || this.sortByScore(res
[i
],res
[i
-1]) == 0)
77 else //strictly lower scoring
85 props: ['players','sortByScore'],
89 tables: [], //array of arrays of players indices
90 pdts: [], //"points de table" for each table
91 sessions: [], //"mini-points" for each table
92 currentIndex: -1, //table index for scoring
97 <div v-show="currentIndex < 0">
98 <button class="block btn" @click="shuffle()">Appariement</button>
99 <div class="pairing" v-for="(table,index) in tables" :class="{scored: pdts[index].length > 0}"
100 @click="showScoreForm(table,index)">
101 <p>Table {{ index+1 }}</p>
103 <tr v-for="(i,j) in table">
104 <td :class="{toto: players[i].prenom=='Toto'}">{{ players[i].prenom }} {{ players[i].nom }}</td>
105 <td class="score"><span v-show="sessions[index].length > 0">{{ sessions[index][j] }}</span></td>
109 <div v-if="unpaired.length>0" class="pairing unpaired">
111 <div v-for="i in unpaired">
112 {{ players[i].prenom }} {{ players[i].nom }}
116 <div id="scoreInput" v-if="currentIndex >= 0">
118 <tr v-for="(index,i) in tables[currentIndex]">
119 <td :class="{toto: players[tables[currentIndex][i]].prenom=='Toto'}">
120 {{ players[tables[currentIndex][i]].prenom }} {{ players[tables[currentIndex][i]].nom }}
122 <td><input type="text" v-model="sessions[currentIndex][i]" value="0"/></td>
125 <div class="button-container">
126 <button class="btn" @click="setScore()">Enregistrer</button>
127 <button class="btn cancel" @click="resetScore()">Annuler</button>
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.pdts
= tables
.map( t
=> { return []; }); //empty pdts
177 this.sessions
= tables
.map( t
=> { return []; }); //empty sessions
179 shuffle: function() {
182 showScoreForm: function(table
,index
) {
183 if (this.pdts
[index
].length
> 0)
184 return; //already scored
185 this.pdts
[index
] = _
.times(table
.length
, _
.constant(0));
186 this.sessions
[index
] = _
.times(table
.length
, _
.constant(0));
187 this.currentIndex
= index
;
189 setScore: function() {
190 let sortedSessions
= this.sessions
[this.currentIndex
]
191 .map( (s
,i
) => { return {value:s
, index:i
}; })
192 .sort( (a
,b
) => { return parseInt(b
.value
) - parseInt(a
.value
); });
193 let pdts
= [4, 2, 1, 0]; //TODO: ex-aequos ?!
194 for (let i
=0; i
<this.tables
[this.currentIndex
].length
; i
++)
196 this.players
[this.tables
[this.currentIndex
][sortedSessions
[i
].index
]].pdt
+= pdts
[i
];
197 this.players
[this.tables
[this.currentIndex
][i
]].session
+= parseInt(this.sessions
[this.currentIndex
][i
]);
199 this.currentIndex
= -1;
200 this.writeScoreToDb();
202 resetScore: function() {
203 this.pdts
[this.currentIndex
] = [];
204 this.sessions
[this.currentIndex
] = [];
205 this.currentIndex
= -1;
207 writeScoreToDb: function()
209 let xhr
= new XMLHttpRequest();
210 xhr
.open("POST", "scripts/rw_players.php");
211 xhr
.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
212 let orderedPlayers
= this.players
213 .slice(1) //discard Toto
214 .map( p
=> { return Object
.assign({}, p
); }) //deep (enough) copy
215 .sort(this.sortByScore
);
216 xhr
.send("players="+encodeURIComponent(JSON
.stringify(orderedPlayers
)));
221 created: function() {
222 let xhr
= new XMLHttpRequest();
224 xhr
.onreadystatechange = function() {
225 if (this.readyState
== 4 && this.status
== 200)
227 let players
= JSON
.parse(xhr
.responseText
);
228 players
.forEach( p
=> {
229 p
.pdt
= !!p
.pdt
? parseInt(p
.pdt
) : 0;
230 p
.session
= !!p
.session
? parseInt(p
.session
) : 0;
231 p
.available
= !!p
.available
? p
.available : 1; //use integer for fputcsv PHP func
233 players
.unshift({ //add ghost 4th player for 3-players tables
240 self
.players
= players
;
243 xhr
.open("GET", "scripts/rw_players.php", true);
247 rankPeople: function() {
249 .slice(1) //discard Toto
250 .map( p
=> { return Object
.assign({}, p
); }) //to not alter original array
251 .sort(this.sortByScore
);
253 sortByScore: function(a
,b
) {
254 return b
.pdt
- a
.pdt
+ (Math
.atan(b
.session
- a
.session
) / (Math
.PI
/2)) / 2;