From 6e073a3ee956718a05c871be9fc03186ece150c4 Mon Sep 17 00:00:00 2001 From: nalbam Date: Mon, 11 Nov 2019 18:21:07 +0900 Subject: [PATCH] improved squeeze --- README.md | 3 +-- public/timer.js | 55 +++++++++++++++++++------------------------- public/timer.mjs | 60 ++++++++++++++++++++++-------------------------- 3 files changed, 53 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 419c9fa..38479e7 100644 --- a/README.md +++ b/README.md @@ -26,5 +26,4 @@ | Passed | E | | Reset | R | | Clear | T | -| Remove | Y | -| Squeeze | G | +| Squeeze | Y | diff --git a/public/timer.js b/public/timer.js index 670080e..d430013 100644 --- a/public/timer.js +++ b/public/timer.js @@ -46,8 +46,8 @@ class Timer { if (this.time) { return; } - this.latest = null; this.records = []; + this.sorted = []; this.limit = [4, 0, 0]; this.reset(); this.bestlap.innerText = ''; @@ -144,40 +144,27 @@ class Timer { console.log(`record ${this.format(this.times)}`); - this.latest = this.times; this.records.push(this.times); - this.records.sort(compare); - this.bestlap.innerText = this.format(this.records[0]); - } - - remove() { - if (this.records.length == 0) { - return; - } + this.sorted = this.records.slice(); + this.sorted.sort(compare); - console.log(`remove ${this.format(this.records[0])}`); - - this.latest = null; - this.records.splice(0, 1); - if (this.records.length == 0) { - this.bestlap.innerText = ""; - } else { - this.bestlap.innerText = this.format(this.records[0]); - } + this.bestlap.innerText = this.format(this.sorted[0]); } squeeze() { - if (!this.latest) { + if (this.records.length == 0) { return; } - console.log(`squeeze ${this.format(this.latest)}`); + let latest = this.records[this.records.length - 1]; + + console.log(`squeeze ${this.format(latest)}`); this.pause(); - this.times[2] += this.latest[2]; - this.times[1] += this.latest[1]; - this.times[0] += this.latest[0]; + this.times[2] += latest[2]; + this.times[1] += latest[1]; + this.times[0] += latest[0]; if (this.times[2] >= 1000) { this.times[2] -= 1000; this.times[1] += 1; @@ -193,10 +180,15 @@ class Timer { this.times[2] = 0; } - this.start(); + this.records.splice(this.records.length - 1, 1); + this.sorted = this.records.slice(); + this.sorted.sort(compare); + + this.bestlap.innerText = this.format(this.sorted[0]); - this.latest = null; this.results.removeChild(this.results.lastChild); + + this.start(); } format(times) { @@ -235,7 +227,10 @@ let timer = new Timer( document.querySelector('.results') ); +// ** socket.io // + let socket = io(); + socket.on('timer', function (name) { console.log(`socket timer ${name}`); exec(name); @@ -245,6 +240,8 @@ function send(name) { socket.emit('timer', name); } +// ** socket.io // + function exec(name) { switch (name) { case 'start': @@ -265,9 +262,6 @@ function exec(name) { case 'clear': timer.clear(); break; - case 'remove': - timer.remove(); - break; case 'squeeze': timer.squeeze(); break; @@ -280,8 +274,7 @@ let key_map = { '69': 'passed', // e '82': 'reset', // r '84': 'clear', // t - '89': 'remove', // y - '71': 'squeeze', // g + '89': 'squeeze', // y }; document.addEventListener('keydown', function (event) { diff --git a/public/timer.mjs b/public/timer.mjs index 3fd0a27..a311d89 100644 --- a/public/timer.mjs +++ b/public/timer.mjs @@ -1,4 +1,6 @@ -let url = window.WS_URL +/** + * timer.mjs + */ class Timer { constructor(limiter, display, bestlap, results) { @@ -44,8 +46,8 @@ class Timer { if (this.time) { return; } - this.latest = null; this.records = []; + this.sorted = []; this.limit = [4, 0, 0]; this.reset(); this.bestlap.innerText = ''; @@ -142,40 +144,27 @@ class Timer { console.log(`record ${this.format(this.times)}`); - this.latest = this.times; this.records.push(this.times); - this.records.sort(compare); - this.bestlap.innerText = this.format(this.records[0]); - } - - remove() { - if (this.records.length == 0) { - return; - } + this.sorted = this.records.slice(); + this.sorted.sort(compare); - console.log(`remove ${this.format(this.records[0])}`); - - this.latest = null; - this.records.splice(0, 1); - if (this.records.length == 0) { - this.bestlap.innerText = ""; - } else { - this.bestlap.innerText = this.format(this.records[0]); - } + this.bestlap.innerText = this.format(this.sorted[0]); } squeeze() { - if (!this.latest) { + if (this.records.length == 0) { return; } - console.log(`squeeze ${this.format(this.latest)}`); + let latest = this.records[this.records.length - 1]; + + console.log(`squeeze ${this.format(latest)}`); this.pause(); - this.times[2] += this.latest[2]; - this.times[1] += this.latest[1]; - this.times[0] += this.latest[0]; + this.times[2] += latest[2]; + this.times[1] += latest[1]; + this.times[0] += latest[0]; if (this.times[2] >= 1000) { this.times[2] -= 1000; this.times[1] += 1; @@ -191,10 +180,15 @@ class Timer { this.times[2] = 0; } - this.start(); + this.records.splice(this.records.length - 1, 1); + this.sorted = this.records.slice(); + this.sorted.sort(compare); + + this.bestlap.innerText = this.format(this.sorted[0]); - this.latest = null; this.results.removeChild(this.results.lastChild); + + this.start(); } format(times) { @@ -233,6 +227,10 @@ let timer = new Timer( document.querySelector('.results') ); +// ** WebSocket // + +let url = window.WS_URL + // setup the web socket let ws = new WebSocket(url); ws.onopen = open; @@ -264,6 +262,8 @@ function send(name) { ws.send(JSON.stringify(msg)); } +// WebSocket ** // + function exec(name) { switch (name) { case 'start': @@ -284,9 +284,6 @@ function exec(name) { case 'clear': timer.clear(); break; - case 'remove': - timer.remove(); - break; case 'squeeze': timer.squeeze(); break; @@ -299,8 +296,7 @@ let key_map = { '69': 'passed', // e '82': 'reset', // r '84': 'clear', // t - '89': 'remove', // y - '71': 'squeeze', // g + '89': 'squeeze', // y }; document.addEventListener('keydown', function (event) {