Skip to content

Commit

Permalink
Fix: timeout (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiscolin authored Sep 25, 2023
1 parent a7af24a commit 8741280
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
9 changes: 6 additions & 3 deletions web/assets/js/components/gameboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const Gameboard = class extends Component {
console.log('gameover for timeout');
clearTimeout(this.checkOngoingTimer);
const game = await actions.getGame(this.gameId);
console.log(game);
if (game.winner == 'none') {
console.log('no winner -> abandon');
this.call('finishGame', ['abandon', status], 'gameplayers', 'rival');
Expand All @@ -127,13 +128,15 @@ const Gameboard = class extends Component {
GameState.TIMEOUT
);
console.log('timeout with rival');
const winnerColor = gameState.winner === Winner.BLACK ? 'b' : 'w';
const amIwinner = winnerColor === this.color ? 'me' : 'rival';

if (valid) {
this.call(
'finishGame',
['winner', 'timeout'],
'gameplayers',
this.color === this.chess.turn() ? 'me' : 'rival'
amIwinner
);
setFinalState();
}
Expand Down Expand Up @@ -262,7 +265,7 @@ const Gameboard = class extends Component {
await exitforFirstMove();
}, timer); //30sec first move
} else {
this.call('startTimer', [init], 'gameplayers', 'me');
this.call('startTimer', [this.gameId], 'gameplayers', 'me');
}
} else {
// Rival turn to play
Expand All @@ -271,7 +274,7 @@ const Gameboard = class extends Component {
clearTimeout(this.initMoveTimer);
this.call('stopTimer', [init], 'gameplayers', 'me');
if (!this.rivalFirstMove) {
this.call('startTimer', [init], 'gameplayers', 'rival');
this.call('startTimer', [this.gameId], 'gameplayers', 'rival');
} else {
this.rivalFirstMove = false;
}
Expand Down
30 changes: 25 additions & 5 deletions web/assets/js/components/gameplayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component } from 'sevejs';
import { gsap } from 'gsap';
import { truncateString } from '../utils/truncate';
import { type Colors, GameState, GameTime, GameType } from '../types/types';
import Actions from '../actions.ts';

const Gameplayers = class extends Component {
constructor(opts: any) {
Expand Down Expand Up @@ -40,6 +41,7 @@ const Gameplayers = class extends Component {
address: string = '',
category: GameType
) {
console.log(time);
//-- config game --
//config token + type
this.DOM.token.innerHTML = truncateString(address, 4, 4);
Expand Down Expand Up @@ -71,19 +73,38 @@ const Gameplayers = class extends Component {
this.DOM.timer.innerHTML = `${pad(minutes)}:${pad(seconds)}`;
}

startTimer() {
async startTimer(gameId: string) {
clearInterval(this.clock);
const actions: Actions = await Actions.getInstance();

this.timerActive = true;

const clockAction = () => {
const clockAction = async () => {
this.timer--;
this._createTime(this.timer);

if (this.timer <= 0) {
clearInterval(this.clock);
this.DOM.timer.innerHTML = `00:00`;
this.call('engine', ['timeout'], 'gameboard'); // let engine know timer is finished
try {
// Claim timeout. If no error, timeout succeeded
const claimTimeout = await actions.claimTimeout(gameId);
console.log(claimTimeout);
this.call('engine', [false, GameState.TIMEOUT], 'gameboard'); // let engine know timer is finished
console.log('claimTimeout did work for ' + gameId);
} catch (e) {
console.log(e);
console.log('claimTimeout did not work for ' + gameId);
this.call(
'appear',
['Invalid claim timeout request', 'error'],
'toast'
);
this.call('engine', [false, GameState.TIMEOUT], 'gameboard'); // let engine know timer is finished
// Timeout request is invalid
// for the user (I assume fire event to end game)
}
} else {
this._createTime(this.timer);
}
};

Expand Down Expand Up @@ -141,7 +162,6 @@ const Gameplayers = class extends Component {
}

destroy() {
console.log('player is destroyed');
clearInterval(this.clock);
}
};
Expand Down

0 comments on commit 8741280

Please sign in to comment.