[Refactor][Enhance] unlock items & Steps' difficulty restrict

- Refactor some codes about unlocking or locking some users' packs and singles
- Add support for restricting songs' difficulty in the map's steps of world mode
This commit is contained in:
Lost-MSth
2023-01-24 16:45:09 +08:00
parent 88d949fc18
commit 9fbdcd5edb
4 changed files with 96 additions and 49 deletions

View File

@@ -1,12 +1,12 @@
from .save import SaveData
from .sql import Connect, Sql
from .score import Score
from .download import DownloadList
from .save import SaveData
from .score import Score
from .sql import Connect, Sql
from .user import User
class BaseOperation:
name: str = None
_name: str = None
def __init__(self, *args, **kwargs):
pass
@@ -14,6 +14,9 @@ class BaseOperation:
def __call__(self, *args, **kwargs) -> None:
return self.run(*args, **kwargs)
def set_params(self, *args, **kwargs) -> None:
pass
def run(self, *args, **kwargs) -> None:
raise NotImplementedError
@@ -22,7 +25,7 @@ class RefreshAllScoreRating(BaseOperation):
'''
刷新所有成绩的评分
'''
name = 'refresh_all_score_rating'
_name = 'refresh_all_score_rating'
def run(self):
# 追求效率不用Song类尽量不用对象
@@ -61,8 +64,9 @@ class RefreshAllScoreRating(BaseOperation):
class RefreshSongFileCache(BaseOperation):
'''
刷新歌曲文件缓存包括文件hash缓存重建、文件目录重遍历、songlist重解析
注意在设置里预先计算关闭的情况下文件hash不会计算
'''
name = 'refresh_song_file_cache'
_name = 'refresh_song_file_cache'
def run(self):
DownloadList.clear_all_cache()
@@ -74,11 +78,16 @@ class SaveUpdateScore(BaseOperation):
云存档更新成绩,是覆盖式更新\
提供user参数时只更新该用户的成绩否则更新所有用户的成绩
'''
name = 'save_update_score'
_name = 'save_update_score'
def __init__(self, user=None):
self.user = user
def set_params(self, user_id: int = None, *args, **kwargs):
if user_id is not None:
self.user = User()
self.user.user_id = int(user_id)
def run(self, user=None):
'''
parameter:
@@ -166,3 +175,70 @@ class SaveUpdateScore(BaseOperation):
c.executemany(
'''insert or replace into best_score values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', new_scores)
class UnlockUserItem(BaseOperation):
'''
全解锁/锁定用户物品\
提供user参数时只更新该用户的否则更新所有用户的
'''
_name = 'unlock_user_item'
ALLOW_TYPES = ['single', 'pack', 'world_song',
'course_banner', 'world_unlock']
def __init__(self, user=None, method: str = 'unlock', item_types: list = ['single', 'pack']):
self.user = user
self.set_params(method=method, item_types=item_types)
def set_params(self, user_id: int = None, method: str = 'unlock', item_types: list = ['single', 'pack'], *args, **kwargs):
if user_id is not None:
self.user = User()
self.user.user_id = int(user_id)
if method in ['unlock', 'lock']:
self.method = method
if isinstance(item_types, list) and all([i in self.ALLOW_TYPES for i in item_types]):
self.item_types = item_types
def run(self):
if self.user is not None and self.user.user_id is not None:
if self.method == 'unlock':
self._one_user_insert()
else:
self._one_user_delete()
else:
if self.method == 'unlock':
self._all_insert()
else:
self._all_delete()
def _one_user_insert(self):
with Connect() as c:
c.execute(
f'''select item_id, type from item where type in ({','.join(['?'] * len(self.item_types))})''', self.item_types)
sql_list = [(self.user.user_id, i[0], i[1])
for i in c.fetchall()]
c.executemany(
'''insert or ignore into user_item values (?, ?, ?, 1)''', sql_list)
def _all_insert(self):
with Connect() as c:
c.execute('''select user_id from user''')
x = c.fetchall()
c.execute(
f'''select item_id, type from item where type in ({','.join(['?'] * len(self.item_types))})''', self.item_types)
y = c.fetchall()
sql_list = [(i[0], j[0], j[1])
for i in x for j in y]
c.executemany(
'''insert or ignore into user_item values (?, ?, ?, 1)''', sql_list)
def _one_user_delete(self):
with Connect() as c:
c.execute(
f'''delete from user_item where user_id = ? and type in ({','.join(['?'] * len(self.item_types))})''', (self.user.user_id, *self.item_types))
def _all_delete(self):
with Connect() as c:
c.execute(
f'''delete from user_item where type in ({','.join(['?'] * len(self.item_types))})''', self.item_types)

View File

@@ -50,6 +50,7 @@ class Step:
self.restrict_id: str = None
self.restrict_ids: list = []
self.restrict_type: str = None
self.restrict_difficulty: int = None
self.step_type: list = None
self.speed_limit_value: int = None
self.plus_stamina_value: int = None
@@ -61,12 +62,14 @@ class Step:
}
if self.items:
r['items'] = [i.to_dict() for i in self.items]
if self.restrict_id:
r['restrict_id'] = self.restrict_id
if self.restrict_ids:
r['restrict_ids'] = self.restrict_ids
if self.restrict_type:
r['restrict_type'] = self.restrict_type
if self.restrict_id:
r['restrict_id'] = self.restrict_id
if self.restrict_ids:
r['restrict_ids'] = self.restrict_ids
if self.restrict_difficulty is not None:
r['restrict_difficulty'] = self.restrict_difficulty
if self.step_type:
r['step_type'] = self.step_type
if self.speed_limit_value:
@@ -82,6 +85,7 @@ class Step:
self.restrict_id = d.get('restrict_id')
self.restrict_ids = d.get('restrict_ids')
self.restrict_type = d.get('restrict_type')
self.restrict_difficulty = d.get('restrict_difficulty')
self.step_type = d.get('step_type')
self.speed_limit_value = d.get('speed_limit_value')
self.plus_stamina_value = d.get('plus_stamina_value')

View File

@@ -2,7 +2,7 @@ import os
import time
from core.init import FileChecker
from core.operation import RefreshAllScoreRating, RefreshSongFileCache, SaveUpdateScore
from core.operation import RefreshAllScoreRating, RefreshSongFileCache, SaveUpdateScore, UnlockUserItem
from core.rank import RankList
from core.sql import Connect
from core.user import User
@@ -606,7 +606,7 @@ def edit_user_purchase():
if 'name' not in request.form and 'user_code' not in request.form:
flag = False
if method == '0':
web.system.unlock_all_user_item(c)
UnlockUserItem().run()
else:
c.execute(
'''delete from user_item where type in ('pack', 'single')''')
@@ -632,7 +632,9 @@ def edit_user_purchase():
user_id = user_id[0]
if method == '0':
web.system.unlock_user_item(c, user_id)
x = UnlockUserItem()
x.set_params(user_id=user_id)
x.run()
else:
c.execute('''delete from user_item where type in ('pack', 'single') and user_id = :user_id''', {
'user_id': user_id})

View File

@@ -40,41 +40,6 @@ def update_user_char(c):
(j[0], i[0], i[1], exp, i[2], 0))
def unlock_all_user_item(c):
# 解锁所有用户购买
c.execute('''select user_id from user''')
x = c.fetchall()
c.execute('''select item_id, type from purchase_item''')
y = c.fetchall()
if x and y:
for i in x:
for j in y:
c.execute('''select exists(select * from user_item where user_id=:a and item_id=:b and type=:c)''', {
'a': i[0], 'b': j[0], 'c': j[1]})
if c.fetchone() == (0,) and j[1] != 'character':
c.execute('''insert into user_item values(:a,:b,:c,1)''', {
'a': i[0], 'b': j[0], 'c': j[1]})
return
def unlock_user_item(c, user_id):
# 解锁用户购买
c.execute('''select item_id, type from purchase_item''')
y = c.fetchall()
for j in y:
c.execute('''select exists(select * from user_item where user_id=:a and item_id=:b and type=:c)''', {
'a': user_id, 'b': j[0], 'c': j[1]})
if c.fetchone() == (0,) and j[1] != 'character':
c.execute('''insert into user_item values(:a,:b,:c,1)''', {
'a': user_id, 'b': j[0], 'c': j[1]})
return
def get_all_item():
# 所有物品数据查询
with Connect() as c: