Drop inactive instances after timeout

in order to no longer show them in the answers list after they left the
game

closes #5
This commit is contained in:
Knut Ahlers 2024-08-21 20:04:49 +02:00
parent a6b046fe5e
commit d1fb9d4ced
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
2 changed files with 21 additions and 5 deletions

View file

@ -133,7 +133,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { categories, gameSocketTemplate } from './config' import { categories, gameSocketTemplate, instanceTimeout } from './config'
import { defineComponent } from 'vue' import { defineComponent } from 'vue'
const baseBackoff = 100 // ms const baseBackoff = 100 // ms
@ -141,6 +141,11 @@ const maxBackoff = 10000 // ms
export default defineComponent({ export default defineComponent({
computed: { computed: {
activeInstances(): any {
return Object.fromEntries(Object.entries(this.knownInstances)
.filter((e: any[]) => this.now.getTime() - e[1].lastActive < instanceTimeout))
},
answersGiven(): Object { answersGiven(): Object {
const keys: string[] = [] const keys: string[] = []
for (const cat of this.gameState.categories) { for (const cat of this.gameState.categories) {
@ -150,9 +155,9 @@ export default defineComponent({
} }
return Object.fromEntries(keys.map(key => [ return Object.fromEntries(keys.map(key => [
key, Object.keys(this.knownInstances).map(instId => ({ key, Object.keys(this.activeInstances).map(instId => ({
answer: this.knownInstances[instId].answers[key], answer: this.activeInstances[instId].answers[key],
name: this.knownInstances[instId].name, name: this.activeInstances[instId].name,
})), })),
])) ]))
}, },
@ -169,6 +174,7 @@ export default defineComponent({
this.knownInstances[this.instance] = { this.knownInstances[this.instance] = {
answers: {}, answers: {},
lastActive: new Date().getTime(),
name: this.name, name: this.name,
} }
}, },
@ -189,6 +195,7 @@ export default defineComponent({
knownInstances: {} as any, knownInstances: {} as any,
localAnswers: {} as any, localAnswers: {} as any,
name: '', name: '',
now: new Date(),
} }
}, },
@ -312,7 +319,10 @@ export default defineComponent({
}, },
sendPing(): void { sendPing(): void {
this.sendMessage({ instanceState: this.knownInstances[this.instance], type: 'ping' }) this.sendMessage({ instanceState: {
...this.knownInstances[this.instance],
lastActive: new Date().getTime(),
}, type: 'ping' })
}, },
shuffle(list: Array<any>): Array<any> { shuffle(list: Array<any>): Array<any> {
@ -364,6 +374,9 @@ export default defineComponent({
this.connectToGame() this.connectToGame()
window.setInterval(() => this.sendPing(), 10000) window.setInterval(() => this.sendPing(), 10000)
window.setInterval(() => {
this.now = new Date()
}, 1000)
}, },
name: 'StadtLandFlussApp', name: 'StadtLandFlussApp',

View file

@ -34,7 +34,10 @@ const categories: string[] = [
const gameSocketTemplate: string = 'wss://tools.hub.luzifer.io/ws/slf-{gameId}' const gameSocketTemplate: string = 'wss://tools.hub.luzifer.io/ws/slf-{gameId}'
const instanceTimeout: number = 30000 // ms
export { export {
categories, categories,
gameSocketTemplate, gameSocketTemplate,
instanceTimeout,
} }