diff --git a/web/assets/js/components/gameboard.ts b/web/assets/js/components/gameboard.ts index c43d1d16..1a8b68a1 100644 --- a/web/assets/js/components/gameboard.ts +++ b/web/assets/js/components/gameboard.ts @@ -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'); @@ -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(); } @@ -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 @@ -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; } diff --git a/web/assets/js/components/gameplayers.ts b/web/assets/js/components/gameplayers.ts index cc6bd386..c7273c0f 100644 --- a/web/assets/js/components/gameplayers.ts +++ b/web/assets/js/components/gameplayers.ts @@ -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) { @@ -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); @@ -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); } }; @@ -141,7 +162,6 @@ const Gameplayers = class extends Component { } destroy() { - console.log('player is destroyed'); clearInterval(this.clock); } };