added game timeout

This commit is contained in:
Djalim Simaila 2023-03-27 23:41:58 +02:00
parent 7d27561697
commit 22ba243c60
4 changed files with 24 additions and 2 deletions

View File

@ -2,6 +2,8 @@
FLASK_SECRET="" FLASK_SECRET=""
DISCORD_BOT_TOKEN="" DISCORD_BOT_TOKEN=""
ORIGIN="https://example.com" ORIGIN="https://example.com"
GAME_TIMEOUT=1800
# Database # Database
DB_HOST="" DB_HOST=""

View File

@ -19,6 +19,11 @@ def register_extensions(app):
discord_bot.try_start() discord_bot.try_start()
routes_api.scheduler.init_app(app)
routes_api.scheduler.start()
def register_routes(app): def register_routes(app):
app.register_blueprint(routes_api.routes_api, url_prefix="/api/v1") app.register_blueprint(routes_api.routes_api, url_prefix="/api/v1")
app.register_blueprint(routes_ui.routes_ui, url_prefix="/") app.register_blueprint(routes_ui.routes_ui, url_prefix="/")

View File

@ -1,4 +1,5 @@
import string import string
import time
import random import random
from typing import Union from typing import Union
@ -61,6 +62,7 @@ class Game:
self.has_started = False self.has_started = False
self.gamedata = {} self.gamedata = {}
self.reaction_table = {} self.reaction_table = {}
self.creatation_timestamp = int(time.time())
def set_owner(self, username: str) -> Member: def set_owner(self, username: str) -> Member:
""" """

View File

@ -1,6 +1,7 @@
import json import json
import io import io
import time
import os
import flask import flask
from sqlalchemy import select from sqlalchemy import select
@ -10,8 +11,20 @@ from truthinquiry.ext.discord_bot import discord_bot
from truthinquiry.ext.socketio import socket_io from truthinquiry.ext.socketio import socket_io
from truthinquiry.logic import game_logic from truthinquiry.logic import game_logic
from flask_apscheduler import APScheduler
scheduler = APScheduler()
scheduler.api_enabled = True
routes_api = flask.Blueprint("api", __name__) routes_api = flask.Blueprint("api", __name__)
@scheduler.task('interval', id='cleanup_games', seconds=1)
def cleanup():
games_to_delete = []
for game_id, game in game_logic.games_list.items():
if game.creatation_timestamp + int(os.getenv("GAME_TIMEOUT")) < int(time.time()):
games_to_delete.append(game_id)
for game_id in games_to_delete:
del game_logic.games_list[game_id]
# API specification is documented in api_doc.yml # API specification is documented in api_doc.yml
@ -218,6 +231,6 @@ def check_anwser():
if game.has_finished(): if game.has_finished():
json_game_results = game.generate_game_results() json_game_results = game.generate_game_results()
socket_io.emit("gamefinished", json_game_results, room="game."+game.game_id) socket_io.emit("gamefinished", json_game_results, room="game."+game.game_id)
del game del game_logic.games_list[game.game_id]
response = {"error": 0} response = {"error": 0}
return response return response