mirror of
https://github.com/Lost-MSth/Arcaea-server.git
synced 2026-02-11 02:17:28 +08:00
[Enhance] Add background task
- Add background task module for log database - Update some data for Arcaea v4.4.4
This commit is contained in:
40
latest version/core/bgtask.py
Normal file
40
latest version/core/bgtask.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from atexit import register
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from .constant import Constant
|
||||
from .sql import Connect
|
||||
|
||||
|
||||
class BGTask:
|
||||
executor = ThreadPoolExecutor(max_workers=1)
|
||||
|
||||
def __init__(self, func, *args, **kwargs):
|
||||
self.future = self.executor.submit(func, *args, **kwargs)
|
||||
|
||||
def result(self):
|
||||
return self.future.result()
|
||||
|
||||
def cancel(self) -> bool:
|
||||
return self.future.cancel()
|
||||
|
||||
def done(self) -> bool:
|
||||
return self.future.done()
|
||||
|
||||
@staticmethod
|
||||
def shutdown(wait: bool = True):
|
||||
BGTask.executor.shutdown(wait)
|
||||
|
||||
|
||||
@register
|
||||
def atexit():
|
||||
BGTask.shutdown()
|
||||
|
||||
|
||||
def logdb_execute_func(sql, *args, **kwargs):
|
||||
with Connect(Constant.SQLITE_LOG_DATABASE_PATH) as c:
|
||||
c.execute(sql, *args, **kwargs)
|
||||
|
||||
|
||||
def logdb_execute(sql: str, *args, **kwargs):
|
||||
'''异步执行SQL,日志库写入,注意不会直接返回结果'''
|
||||
return BGTask(logdb_execute_func, sql, *args, **kwargs)
|
||||
@@ -1,6 +1,6 @@
|
||||
from .config_manager import Config
|
||||
|
||||
ARCAEA_SERVER_VERSION = 'v2.11.1.1'
|
||||
ARCAEA_SERVER_VERSION = 'v2.11.1.3'
|
||||
|
||||
|
||||
class Constant:
|
||||
|
||||
@@ -2,12 +2,13 @@ from base64 import b64encode
|
||||
from os import urandom
|
||||
from time import time
|
||||
|
||||
from .bgtask import logdb_execute
|
||||
from .constant import Constant
|
||||
from .course import CoursePlay
|
||||
from .error import NoData, StaminaNotEnough
|
||||
from .item import ItemCore
|
||||
from .song import Chart
|
||||
from .sql import Connect, Query, Sql
|
||||
from .sql import Query, Sql
|
||||
from .util import md5
|
||||
from .world import WorldPlay
|
||||
|
||||
@@ -427,9 +428,8 @@ class UserPlay(UserScore):
|
||||
|
||||
def record_score(self) -> None:
|
||||
'''向log数据库记录分数,请注意列名不同'''
|
||||
with Connect(Constant.SQLITE_LOG_DATABASE_PATH) as c2:
|
||||
c2.execute('''insert into user_score values(?,?,?,?,?,?,?,?,?,?,?,?,?)''', (self.user.user_id, self.song.song_id, self.song.difficulty, self.time_played,
|
||||
self.score, self.shiny_perfect_count, self.perfect_count, self.near_count, self.miss_count, self.health, self.modifier, self.clear_type, self.rating))
|
||||
logdb_execute('''insert into user_score values(?,?,?,?,?,?,?,?,?,?,?,?,?)''', (self.user.user_id, self.song.song_id, self.song.difficulty, self.time_played,
|
||||
self.score, self.shiny_perfect_count, self.perfect_count, self.near_count, self.miss_count, self.health, self.modifier, self.clear_type, self.rating))
|
||||
|
||||
def upload_score(self) -> None:
|
||||
'''上传分数,包括user的recent更新,best更新,recent30更新,世界模式计算'''
|
||||
@@ -477,6 +477,7 @@ class UserPlay(UserScore):
|
||||
self.user.rating_ptt = int(self.ptt.value * 100)
|
||||
self.c.execute('''update user set rating_ptt = :a where user_id = :b''', {
|
||||
'a': self.user.rating_ptt, 'b': self.user.user_id})
|
||||
# TODO: PTT log
|
||||
|
||||
# 世界模式判断
|
||||
if self.is_world_mode:
|
||||
|
||||
Reference in New Issue
Block a user