From c042ca1327b9587383fce7d2b968b5b081429417 Mon Sep 17 00:00:00 2001 From: Pejosonic Date: Wed, 4 Sep 2024 19:23:38 +0200 Subject: [PATCH] feat: inactive timeout --- README.md | 1 + src/index.ts | 1 + src/queue/index.ts | 30 ++++++++++++++++++++++-------- src/ui/button.ts | 7 ------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5ff60ce..5f2d017 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ miniGames.initLibrary(engine as any, syncEntity, playersApi, { environment: "dev", // time in ms gameTimeoutMs: _1_MIN, + inactiveTimeoutMs: 20 * _1_SEC_, // game area rectangle gameArea: { // top left point diff --git a/src/index.ts b/src/index.ts index 84c59d2..5f69c11 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ export type IOptions = { gameId: string environment: string gameTimeoutMs?: number + inactiveTimeoutMs?: number sceneRotation?: number gameArea?: { topLeft: TransformType['position'] diff --git a/src/queue/index.ts b/src/queue/index.ts index 8bf04a0..01166ff 100644 --- a/src/queue/index.ts +++ b/src/queue/index.ts @@ -1,4 +1,4 @@ -import { Entity } from '@dcl/sdk/ecs' +import { Entity, InputAction, PointerEventType } from '@dcl/sdk/ecs' import { initQueueDisplay } from './display' import { getSDK } from '../sdk' @@ -30,6 +30,7 @@ export function startPlayersQueue() { }) engine.addSystem(internalPlayerSystem()) + engine.addSystem(inputsCheckTimer()) } /** * Add current player to the queue @@ -121,6 +122,7 @@ export function setNextPlayer() { lastActivePlayer = nextPlayer.player.address Player.getMutable(nextPlayer.entity).active = true Player.getMutable(nextPlayer.entity).startPlayingAt = Date.now() + inactiveSince = Date.now() } if (listeners.onActivePlayerChange && activePlayer?.address !== nextPlayer?.player.address) { @@ -154,12 +156,10 @@ function internalPlayerSystem() { if (!activePlayer) { setNextPlayer() } - - // TIMER for active player const { config } = getSDK() + + // TIMER for active player and inactivity detection if ( - // true Conditions - config.gameTimeoutMs && activePlayer && // // I'm the player playnig @@ -167,9 +167,8 @@ function internalPlayerSystem() { // // If you are the only player connected, keep playing. getQueue().length > 1 && - // - // Check if the time has passed - Date.now() - activePlayer.startPlayingAt >= config.gameTimeoutMs + ((config.gameTimeoutMs && Date.now() - activePlayer.startPlayingAt >= config.gameTimeoutMs) || + (config.inactiveTimeoutMs && Date.now() > inactiveSince + config.inactiveTimeoutMs)) ) { setNextPlayer() } @@ -182,6 +181,21 @@ function internalPlayerSystem() { } } } + +let inactiveSince = 0 +function inputsCheckTimer() { + return function () { + const { inputSystem } = getSDK() + + if (!isActive()) return + + //update player inactivty timer + if (inputSystem.isTriggered(InputAction.IA_ANY, PointerEventType.PET_DOWN)) { + inactiveSince = Date.now() + } + } +} + function isPlayerConnected(userId: string) { const { engine, diff --git a/src/ui/button.ts b/src/ui/button.ts index ad73d8c..af3651a 100644 --- a/src/ui/button.ts +++ b/src/ui/button.ts @@ -104,13 +104,6 @@ export class MenuButton { }) engine.addSystem(() => { - // TODO: Why is not been triggered ? - // if (tweenSystem.tweenCompleted(this.button)) { - // console.log('asd', Tween.getOrNull(this.button)) - // } - - // TODO: this should be tweenCompleted but no idea why is not working :sadcat: - // if (TweenState.getOrNull(this.button)?.currentTime === 1 && TweenSequence.getOrNull(this.button)) { if (tweenSystem.tweenCompleted(this.button)) { if (!TweenSequence.get(this.button).sequence.length) { this.enable()