Skip to content

Commit

Permalink
improved squeeze
Browse files Browse the repository at this point in the history
  • Loading branch information
nalbam committed Nov 11, 2019
1 parent ac46f7f commit 6e073a3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 65 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@
| Passed | E |
| Reset | R |
| Clear | T |
| Remove | Y |
| Squeeze | G |
| Squeeze | Y |
55 changes: 24 additions & 31 deletions public/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -245,6 +240,8 @@ function send(name) {
socket.emit('timer', name);
}

// ** socket.io //

function exec(name) {
switch (name) {
case 'start':
Expand All @@ -265,9 +262,6 @@ function exec(name) {
case 'clear':
timer.clear();
break;
case 'remove':
timer.remove();
break;
case 'squeeze':
timer.squeeze();
break;
Expand All @@ -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) {
Expand Down
60 changes: 28 additions & 32 deletions public/timer.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
let url = window.WS_URL
/**
* timer.mjs
*/

class Timer {
constructor(limiter, display, bestlap, results) {
Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -264,6 +262,8 @@ function send(name) {
ws.send(JSON.stringify(msg));
}

// WebSocket ** //

function exec(name) {
switch (name) {
case 'start':
Expand All @@ -284,9 +284,6 @@ function exec(name) {
case 'clear':
timer.clear();
break;
case 'remove':
timer.remove();
break;
case 'squeeze':
timer.squeeze();
break;
Expand All @@ -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) {
Expand Down

0 comments on commit 6e073a3

Please sign in to comment.