Skip to content

Commit

Permalink
complete all game timing features
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonSys committed Sep 3, 2019
1 parent eba022c commit 335d673
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Controller {
}
if (this.game.gameState === 'win') {
this.game.stopGame(this.grid);
this.interface.addGameSummary(this.game.stats.timerValue, this.game.stats.actionsCounter, this.game.initGame, this.grid, this.interface.timer, this.interface.actionsCounter, this.game, document.getElementById('container'))
this.interface.addGameSummary(this.game.stats._getTimerResoult(), this.game.stats.actionsCounter, this.game.initGame, this.grid, this.interface.timer, this.interface.actionsCounter, this.game, document.getElementById('container'))
}
})
});
Expand Down
27 changes: 23 additions & 4 deletions Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ class Stats {
this._actionsCounter = 0;
this._isPlay = false;
this._timerIntervalCount;
const _timerValues = {
minutes: 0,
seconds: 0,
stringMinutes: '',
stringResoult: ''
}
this._getTimerValues = () => _timerValues;
this._getTimerResoult = () => _timerValues.stringResoult;
}

get username() {
Expand Down Expand Up @@ -61,17 +69,28 @@ class Stats {

resetTimer(timer) {
if (this.isPlay) this.changeIsPlay();
const values = this._getTimerValues();
values.seconds = 0;
values.minutes = 0;
values.stringMinutes = '';
this._timerValue = 0;
timer.textContent = "0.00";
timer.textContent = "00.00";
}

updateTimer(timer) {
if (this.isPlay === false) return;
this._timerValue += 1;
timer.textContent = this.timerValue;
setTimeout(this.updateTimer.bind(this, timer), 1000);
const values = this._getTimerValues();
values.seconds = (this.timerValue / 100) % 60;
this.timerValue++;
if ((this.timerValue / 100) % 60 === 0) values.minutes++;
if (values.minutes > 0 && values.minutes < 10) values.stringMinutes = '0' + values.minutes + '.';
else if (values.minutes > 10) values.stringMinutes = values.minutes + '.';
values.stringResoult = `${values.stringMinutes}${values.seconds < 10 ? '0' + values.seconds.toFixed(2) : values.seconds.toFixed(2)}`;
timer.textContent = values.stringResoult;
setTimeout(this.updateTimer.bind(this, timer), 10);
}


updateActionsCounter() {
if (this.isPlay === true) {
this.actionsCounter += 1;
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</button>
<span id="username"></span>
</div>
<div class="timer">game time: <span id="timer">0.00</span></div>
<div class="timer">game time: <span id="timer">00.00</span></div>
<div class="actions">actions counter: <span id="actions">0</span></div>
</div>
<div id="grid" class="game-grid"></div>
Expand Down

0 comments on commit 335d673

Please sign in to comment.