Files
Arcaea-server/latest version/core/save.py
Lost-MSth 6fcca17918 Code refactoring
- Code refactoring
- Fix a bug that the other player will not become the host of the room at once, when the player disconnect in link play.

> Maybe add many unknown bugs. XD
> The song database `arcsong.db` will not used in the future. You can use a tool in `tool` folder to import old data.
2022-07-04 18:36:30 +08:00

113 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from .util import md5
from .error import InputError
from setting import Config
from time import time
import json
class SaveData:
def __init__(self, c=None) -> None:
self.c = c
self.scores_data = []
self.clearlamps_data = []
self.clearedsongs_data = []
self.unlocklist_data = []
self.installid_data = ''
self.devicemodelname_data = ''
self.story_data = []
self.createdAt = 0
@property
def to_dict(self):
return {
"user_id": self.user.user_id,
"story": {
"": self.story_data
},
"devicemodelname": {
"val": self.devicemodelname_data
},
"installid": {
"val": self.installid_data
},
"unlocklist": {
"": self.unlocklist_data
},
"clearedsongs": {
"": self.clearedsongs_data
},
"clearlamps": {
"": self.clearlamps_data
},
"scores": {
"": self.scores_data
},
"version": {
"val": 1
},
"createdAt": self.createdAt
}
def select_all(self, user) -> None:
'''
parameter: `user` - `User`类或子类的实例
'''
self.user = user
self.c.execute('''select * from user_save where user_id=:a''',
{'a': user.user_id})
x = self.c.fetchone()
if x:
self.scores_data = json.loads(x[1])[""]
self.clearlamps_data = json.loads(x[2])[""]
self.clearedsongs_data = json.loads(x[3])[""]
self.unlocklist_data = json.loads(x[4])[""]
self.installid_data = json.loads(x[5])["val"]
self.devicemodelname_data = json.loads(x[6])["val"]
self.story_data = json.loads(x[7])[""]
if x[8] is not None:
self.createdAt = int(x[8])
if Config.SAVE_FULL_UNLOCK:
self.installid_data = "0fcec8ed-7b62-48e2-9d61-55041a22b123" # 使得可以进入存档选择上传或下载界面
for i in self.story_data:
i['c'] = True
i['r'] = True
for i in self.unlocklist_data:
if i['unlock_key'][-3:] == '101':
i['complete'] = 100
elif i['unlock_key'][:16] == 'aegleseeker|2|3|':
i['complete'] = 10
elif i['unlock_key'] == 'saikyostronger|2|3|einherjar|2':
i['complete'] = 6
elif i['unlock_key'] == 'saikyostronger|2|3|laqryma|2':
i['complete'] = 3
else:
i['complete'] = 1
def update_all(self, user) -> None:
'''
parameter: `user` - `User`类或子类的实例
'''
self.createdAt = int(time() * 1000)
self.c.execute('''delete from user_save where user_id=:a''', {
'a': user.user_id})
self.c.execute('''insert into user_save values(:a,:b,:c,:d,:e,:f,:g,:h,:i)''', {
'a': user.user_id, 'b': json.dumps({'': self.scores_data}), 'c': json.dumps({'': self.clearlamps_data}), 'd': json.dumps({'': self.clearedsongs_data}), 'e': json.dumps({'': self.unlocklist_data}), 'f': json.dumps({'val': self.installid_data}), 'g': json.dumps({'val': self.devicemodelname_data}), 'h': json.dumps({'': self.story_data}), 'i': self.createdAt})
def set_value(self, key: str, value: str, checksum: str) -> None:
'''
从Arcaea客户端给的奇怪字符串中获取存档数据并进行数据校验
'''
if key not in self.__dict__:
raise KeyError(
'Property `%s` is not found in the instance of `SaveData` class.' % key)
if md5(value) == checksum:
if key == 'installid_data' or key == 'devicemodelname_data':
self.__dict__[key] = json.loads(value)['val']
else:
self.__dict__[key] = json.loads(value)['']
else:
raise InputError('Hash value of cloud save data mismatches.')