Skip to content

Commit

Permalink
Drop inactive instances after timeout
Browse files Browse the repository at this point in the history
in order to no longer show them in the answers list after they left the
game

closes #5
  • Loading branch information
Luzifer committed Aug 21, 2024
1 parent a6b046f commit d1fb9d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,19 @@
</template>

<script lang="ts">
import { categories, gameSocketTemplate } from './config'
import { categories, gameSocketTemplate, instanceTimeout } from './config'
import { defineComponent } from 'vue'
const baseBackoff = 100 // ms
const maxBackoff = 10000 // ms
export default defineComponent({
computed: {
activeInstances(): any {
return Object.fromEntries(Object.entries(this.knownInstances)
.filter((e: any[]) => this.now.getTime() - e[1].lastActive < instanceTimeout))
},
answersGiven(): Object {
const keys: string[] = []
for (const cat of this.gameState.categories) {
Expand All @@ -150,9 +155,9 @@ export default defineComponent({
}
return Object.fromEntries(keys.map(key => [
key, Object.keys(this.knownInstances).map(instId => ({
answer: this.knownInstances[instId].answers[key],
name: this.knownInstances[instId].name,
key, Object.keys(this.activeInstances).map(instId => ({
answer: this.activeInstances[instId].answers[key],
name: this.activeInstances[instId].name,
})),
]))
},
Expand All @@ -169,6 +174,7 @@ export default defineComponent({
this.knownInstances[this.instance] = {
answers: {},
lastActive: new Date().getTime(),
name: this.name,
}
},
Expand All @@ -189,6 +195,7 @@ export default defineComponent({
knownInstances: {} as any,
localAnswers: {} as any,
name: '',
now: new Date(),
}
},
Expand Down Expand Up @@ -312,7 +319,10 @@ export default defineComponent({
},
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> {
Expand Down Expand Up @@ -364,6 +374,9 @@ export default defineComponent({
this.connectToGame()
window.setInterval(() => this.sendPing(), 10000)
window.setInterval(() => {
this.now = new Date()
}, 1000)
},
name: 'StadtLandFlussApp',
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ const categories: string[] = [

const gameSocketTemplate: string = 'wss://tools.hub.luzifer.io/ws/slf-{gameId}'

const instanceTimeout: number = 30000 // ms

export {
categories,
gameSocketTemplate,
instanceTimeout,
}

0 comments on commit d1fb9d4

Please sign in to comment.