forked from xjunko/Yuzumi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
94 lines (70 loc) · 2.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import logging
import asyncio
import coloredlogs
from quart import Quart
# sus
from objects import glob
from objects.player import Player
from objects.db import sqliteDB
# handlers
from handlers import (cho, api)
from handlers.response import Failed
#
import utils
# testing
from utils import pp
from objects.beatmap import Beatmap
def make_app():
app = Quart(__name__)
glob.db = sqliteDB()
# routes shit idk
routes = [cho, api]
for route in routes:
app.register_blueprint(route, url_prefix=route.prefix)
return app
app = make_app()
@app.before_serving
async def init_shit():
# check folder
utils.check_folder()
# connect to db
await glob.db.connect()
# Testing stuff
# recalc pp
# await pp.recalc_scores()
# test bmap object
# await Beatmap.from_md5('3ef4d1085a8bee29660a2908cbb1dec9')
# init players
player_ids = await glob.db.fetchall("SELECT id FROM users where id != -1")
for id in player_ids:
p = await Player.from_sql(id['id']) # maybe do this on login instead?, will prolly take alot of time to start if theres lot of players
glob.players.add(p)
async def background_tasks():
async def update_players_stats():
for p in glob.players:
await p.update_stats()
tasks = [update_players_stats]
for task in tasks:
try:
await task()
except Exception as err:
logging.error(f'Failed to complete task: {repr(err)}')
await asyncio.sleep(glob.config.cron_delay*60)
# run the background task
asyncio.ensure_future(background_tasks())
@app.after_serving
async def close_shit():
await glob.db.close()
@app.errorhandler(500)
async def server_fucked(err):
return Failed(f'Server Error: {repr(err)}')
@app.route('/')
async def index():
return {
'players': len(glob.players),
'online': len([_ for _ in glob.players if _.online]),
'title': 'when the impostor is sus'
}
if __name__ == '__main__':
coloredlogs.install(level=logging.INFO)
app.run(port=glob.config.port, use_reloader=False, host=glob.config.host, debug=False)