Skip to content

Commit

Permalink
feat: inactive timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Pejosonic committed Sep 5, 2024
1 parent 3b2021d commit c042ca1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type IOptions = {
gameId: string
environment: string
gameTimeoutMs?: number
inactiveTimeoutMs?: number
sceneRotation?: number
gameArea?: {
topLeft: TransformType['position']
Expand Down
30 changes: 22 additions & 8 deletions src/queue/index.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -30,6 +30,7 @@ export function startPlayersQueue() {
})

engine.addSystem(internalPlayerSystem())
engine.addSystem(inputsCheckTimer())
}
/**
* Add current player to the queue
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -154,22 +156,19 @@ 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
isActive() &&
//
// 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()
}
Expand All @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions src/ui/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c042ca1

Please sign in to comment.