-
+
{{ formattedTime }}
+
`,
@@ -214,6 +351,11 @@ new Vue({
},
},
methods: {
+ setTime: function() {
+ this.initialTime = this.setterTime;
+ this.setter = false;
+ this.reset();
+ },
padToZero: function(a) {
if (a < 10)
return "0" + a;
@@ -226,7 +368,7 @@ new Vue({
},
reset: function(e) {
this.running = false;
- this.time = 5400; //1:30
+ this.time = this.initialTime * 60;
},
start: function() {
if (!this.running)
@@ -237,6 +379,8 @@ new Vue({
this.running = false;
return;
}
+ if (this.time == this.initialTime * 60)
+ new Audio("sounds/gong.mp3").play(); //gong at the beginning
setTimeout(() => {
if (this.running)
this.time--;
@@ -245,11 +389,30 @@ new Vue({
},
},
created: function() {
+ this.setterTime = this.initialTime;
this.reset();
},
+ mounted: function() {
+ let timer = document.getElementById("timer");
+ let keyDict = {
+ 32: () => { this.setter = true; }, //Space
+ 27: () => { this.setter = false; }, //Esc
+ };
+ document.addEventListener("keyup", e => {
+ if (timer.style.display !== "none")
+ {
+ let func = keyDict[e.keyCode];
+ if (!!func)
+ {
+ e.preventDefault();
+ func();
+ }
+ }
+ });
+ },
},
'my-ranking': {
- props: ['players','sortByScore','writeScoreToDb'],
+ props: ['players','sortByScore','commitScores'],
template: `
@@ -267,8 +430,11 @@ new Vue({
`,
@@ -294,86 +460,83 @@ new Vue({
.sort(this.sortByScore);
},
resetPlayers: function() {
+ if (confirm('Ãtes-vous sûr ?'))
+ {
+ this.players
+ .slice(1) //discard Toto
+ .forEach( p => {
+ p.pdt = 0;
+ p.session = 0;
+ p.available = 1;
+ });
+ this.commitScores();
+ document.getElementById("doPairings").click();
+ }
+ },
+ download: function() {
+ // Prepare file content
+ let content = "prénom,nom,pdt,session\n";
this.players
.slice(1) //discard Toto
+ .sort(this.sortByScore)
.forEach( p => {
- p.pdt = 0;
- p.session = 0;
- p.available = 1;
+ content += p.prenom + "," + p.nom + "," + p.pdt + "," + p.session + "\n";
});
- this.writeScoreToDb();
- document.getElementById("runPairing").click();
- },
- 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,
- });
- // NOTE: Vue warning "do not mutate property" if direct self.players = players
- for (let i=1; i
{
- 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: "",
- pdt: 0,
- session: 0,
- available: 0,
- });
- self.players = players;
- }
- };
- xhr.open("GET", "scripts/rw_players.php", true);
- xhr.send(null);
+ let players = JSON.parse(localStorage.getItem("players"));
+ if (players !== null)
+ {
+ this.addToto(players);
+ this.players = players;
+ }
},
methods: {
+ addToto: function(array) {
+ array.unshift({ //add ghost 4th player for 3-players tables
+ prenom: "Toto",
+ nom: "",
+ pdt: 0,
+ session: 0,
+ available: 0,
+ });
+ },
// 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
- .sort(this.sortByScore);
- xhr.send("players="+encodeURIComponent(JSON.stringify(orderedPlayers)));
+ commitScores: function() {
+ localStorage.setItem(
+ "players",
+ JSON.stringify(this.players.slice(1)) //discard Toto
+ );
+ },
+ // Used in players, reinit players array
+ initPlayers: function(csv) {
+ const allLines = csv
+ .split(/\r\n|\n|\r/) //line breaks
+ .splice(1); //discard header
+ let players = allLines
+ .filter( line => { return line.length > 0; }) //remove empty lines
+ .map( line => {
+ let parts = line.split(",");
+ let p = { prenom: parts[0], nom: parts[1] };
+ p.pdt = parts.length > 2 ? parseFloat(parts[2]) : 0;
+ p.session = parts.length > 3 ? parseInt(parts[3]) : 0;
+ p.available = parts.length > 4 ? parts[4] : 1;
+ return p;
+ });
+ this.addToto(players);
+ this.players = players;
+ this.commitScores(); //save players in memory
},
},
});