X-Git-Url: https://git.auder.net/assets/img/logo_Westcastle.png?a=blobdiff_plain;f=js%2Findex.js;h=659a0dd7f5ca9bb7f67d265cf791148676e548e9;hb=2caa3889307c969ae089b6c261ab8096ae49107a;hp=870be76d8b0429c2f1e8f6835d904c82a0b7fd10;hpb=fd4a69e4ea369afb39c01f1207d79b0ab2d4cc8e;p=westcastle.git
diff --git a/js/index.js b/js/index.js
index 870be76..659a0dd 100644
--- a/js/index.js
+++ b/js/index.js
@@ -9,7 +9,7 @@ new Vue({
props: ['players'],
template: `
-
+
Présents
@@ -18,7 +18,7 @@ new Vue({
-
+
Absents
@@ -46,43 +46,37 @@ new Vue({
},
},
'my-ranking': {
- props: ['players'],
- data: function() {
- return {
- sortMethod: "score",
- };
- },
+ props: ['players','sortByScore','writeScoreToDb'],
template: `
Rang |
Joueur |
- Score |
- PdT |
+ Points |
+ Mini-pts |
-
+
{{ p.rank }} |
{{ p.prenom }} {{ p.nom }} |
- {{ p.score }} |
{{ p.pdt }} |
+ {{ p.session }} |
+
+
+
+
`,
- 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)
+ computed: {
sortedPlayers: function() {
- let sortFunc = this.sortMethod == "score"
- ? this.sortByScore
- : this.sortByPdt;
- let res = this.players
- .map( p => { return Object.assign({}, p); }) //to not alter original array
- .sort(sortFunc);
+ let res = this.rankPeople();
// Add rank information (taking care of ex-aequos)
let rank = 1;
for (let i=0; i { return Object.assign({}, p); }) //to not alter original array
+ .sort(this.sortByScore);
+ },
+ resetPlayers: function() {
+ this.players
+ .slice(1) //discard Toto
+ .forEach( p => {
+ p.pdt = 0;
+ p.session = 0;
+ p.available = 1;
+ });
+ this.writeScoreToDb();
+ document.getElementById("runPairing").click(); //TODO: hack...
},
- sortByPdt: function(a,b) {
- return b.pdt - a.pdt;
+ restoreLast: function() {
+ let xhr = new XMLHttpRequest();
+ let self = this;
+ xhr.onreadystatechange = function() {
+ if (this.readyState == 4 && this.status == 200)
+ {
+ let players = JSON.parse(xhr.responseText);
+ if (players.length > 0)
+ {
+ players.unshift({ //add ghost 4th player for 3-players tables
+ prenom: "Toto",
+ nom: "",
+ pdt: 0,
+ session: 0,
+ available: 0,
+ });
+ self.players = players;
+ }
+ }
+ };
+ xhr.open("GET", "scripts/rw_players.php?restore=1", true);
+ xhr.send(null);
},
},
},
'my-pairings': {
- props: ['players'],
+ props: ['players','writeScoreToDb'],
data: function() {
return {
unpaired: [],
tables: [], //array of arrays of players indices
- scores: [], //scores for each table (3 or 4 players)
- pdts: [], //"points de table" for each table (3 or 4 players)
+ sessions: [], //"mini-points" for each table
currentIndex: -1, //table index for scoring
+ scored: [], //boolean for each table index
};
},
template: `
-
@@ -152,7 +186,7 @@ new Vue({
// Simple case first: 4 by 4
let tables = [];
let currentTable = [];
- let ordering = _.shuffle(_.range(this.players.length)); //TODO: take scores into account?
+ let ordering = _.shuffle(_.range(this.players.length));
for (let i=0; i
{ return []; }); //empty scores
- this.pdts = tables.map( t => { return []; }); //empty pdts
- },
- shuffle: function() {
- this.doPairings();
+ this.sessions = tables.map( t => { return []; }); //empty sessions
+ this.scored = tables.map( t => { return false; }); //nothing scored yet
+ this.currentIndex = -1; //required if reset while scoring
},
showScoreForm: function(table,index) {
- if (this.scores[index].length > 0)
- return; //already scored
- this.scores[index] = _.times(table.length, _.constant(0));
- this.pdts[index] = _.times(table.length, _.constant(0));
+ if (this.sessions[index].length == 0)
+ this.sessions[index] = _.times(table.length, _.constant(0));
this.currentIndex = index;
},
setScore: function() {
- let sortedPdts = this.pdts[this.currentIndex]
+ let sortedSessions = this.sessions[this.currentIndex]
.map( (s,i) => { return {value:s, index:i}; })
.sort( (a,b) => { return parseInt(b.value) - parseInt(a.value); });
- let scores = [4, 2, 1, 0]; //TODO: ex-aequos ?!
- for (let i=0; i sortedSessions[i+1].value)
+ {
+ let pdt = curSum / curCount;
+ for (let j=start; j<=i; j++)
+ this.players[this.tables[this.currentIndex][sortedSessions[j].index]].pdt += pdt;
+ curSum = 0;
+ curCount = 0;
+ start = i+1;
+ }
+ // Update sessions:
+ this.players[this.tables[this.currentIndex][i]].session += parseInt(this.sessions[this.currentIndex][i]);
}
+ this.scored[this.currentIndex] = true;
this.currentIndex = -1;
this.writeScoreToDb();
},
- resetScore: function() {
- this.scores[this.currentIndex] = [];
- this.pdts[this.currentIndex] = [];
- this.currentIndex = -1;
+ },
+ },
+ 'my-timer': {
+ data: function() {
+ return {
+ time: 0, //remaining time, in seconds
+ running: false,
+ };
+ },
+ template: `
+
+
+ {{ formattedTime }}
+
+
+
+ `,
+ computed: {
+ formattedTime: function() {
+ let seconds = this.time % 60;
+ let minutes = Math.floor(this.time / 60);
+ return this.padToZero(minutes) + ":" + this.padToZero(seconds);
},
- writeScoreToDb: function()
- {
- let xhr = new XMLHttpRequest();
- xhr.open("POST", "scripts/rw_players.php");
- xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- let orderedPlayers = this.players
- .slice(1) //discard "Toto"
- .map( p => { return Object.assign({}, p); }) //deep (enough) copy
- .sort( (a,b) => { return b.score - a.score; });
- xhr.send("players="+encodeURIComponent(JSON.stringify(orderedPlayers)));
+ },
+ methods: {
+ padToZero: function(a) {
+ if (a < 10)
+ return "0" + a;
+ return a;
+ },
+ pauseResume: function() {
+ this.running = !this.running;
+ if (this.running)
+ this.start();
+ },
+ reset: function(e) {
+ this.running = false;
+ this.time = 10; //1:30
+ },
+ start: function() {
+ if (!this.running)
+ return;
+ if (this.time == 0)
+ {
+ new Audio("sounds/gong.mp3").play();
+ this.running = false;
+ return;
+ }
+ setTimeout(() => {
+ if (this.running)
+ this.time--;
+ this.start();
+ }, 1000);
},
},
+ created: function() {
+ this.reset();
+ },
},
},
created: function() {
@@ -244,15 +330,15 @@ new Vue({
{
let players = JSON.parse(xhr.responseText);
players.forEach( p => {
- p.score = !!p.score ? parseInt(p.score) : 0;
- p.pdt = !!p.pdt ? parseInt(p.pdt) : 0;
+ p.pdt = !!p.pdt ? parseFloat(p.pdt) : 0;
+ p.session = !!p.session ? parseInt(p.session) : 0;
p.available = !!p.available ? p.available : 1; //use integer for fputcsv PHP func
});
players.unshift({ //add ghost 4th player for 3-players tables
prenom: "Toto",
nom: "",
- score: 0,
pdt: 0,
+ session: 0,
available: 0,
});
self.players = players;
@@ -261,4 +347,20 @@ new Vue({
xhr.open("GET", "scripts/rw_players.php", true);
xhr.send(null);
},
+ methods: {
+ // Used both in ranking and pairings:
+ sortByScore: function(a,b) {
+ return b.pdt - a.pdt + (Math.atan(b.session - a.session) / (Math.PI/2)) / 2;
+ },
+ writeScoreToDb: function() {
+ let xhr = new XMLHttpRequest();
+ xhr.open("POST", "scripts/rw_players.php");
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+ let orderedPlayers = this.players
+ .slice(1) //discard Toto
+ .map( p => { return Object.assign({}, p); }) //deep (enough) copy
+ .sort(this.sortByScore);
+ xhr.send("players="+encodeURIComponent(JSON.stringify(orderedPlayers)));
+ },
+ },
});