diff --git a/latest version/database/arcaea_database.db b/latest version/database/arcaea_database.db index 5ec66aa..2c4a22c 100644 Binary files a/latest version/database/arcaea_database.db and b/latest version/database/arcaea_database.db differ diff --git a/latest version/database/database_initialize.py b/latest version/database/database_initialize.py index e5b04bd..6dc7b53 100644 --- a/latest version/database/database_initialize.py +++ b/latest version/database/database_initialize.py @@ -37,10 +37,11 @@ prog_boost int );''') c.execute('''create table if not exists login(access_token text, -user_id int primary key, -last_login_time int, -last_login_ip text, -last_login_device text +user_id int, +login_time int, +login_ip text, +login_device text, +primary key(access_token, user_id) );''') c.execute('''create table if not exists friend(user_id_me int, user_id_other int, diff --git a/latest version/main.py b/latest version/main.py index 2fa5a58..0ab400b 100644 --- a/latest version/main.py +++ b/latest version/main.py @@ -1,6 +1,8 @@ +# encoding: utf-8 + from flask import Flask, request, jsonify, make_response, send_from_directory from logging.config import dictConfig -import configparser +from setting import Config import base64 import server.auth import server.info @@ -87,32 +89,29 @@ def login(): id_pwd = headers['Authorization'] id_pwd = base64.b64decode(id_pwd[6:]).decode() name, password = id_pwd.split(':', 1) - try: - token, error_code = server.auth.arc_login(name, password) - if not error_code: - r = {"success": True, "token_type": "Bearer"} - r['access_token'] = token - return jsonify(r) - else: - return error_return(error_code) - except: - return error_return(108) + device_id = headers['DeviceId'] + token, error_code = server.auth.arc_login(name, password, device_id) + if not error_code: + r = {"success": True, "token_type": "Bearer"} + r['access_token'] = token + return jsonify(r) + else: + return error_return(error_code) @app.route('/latte/13/user/', methods=['POST']) # 注册接口 def register(): name = request.form['name'] password = request.form['password'] - try: - user_id, token, error_code = server.auth.arc_register(name, password) - if user_id is not None: - r = {"success": True, "value": { - 'user_id': user_id, 'access_token': token}} - return jsonify(r) - else: - return error_return(error_code) # 应该是101,用户名被占用,毕竟电子邮箱、设备号没记录 - except: - return error_return(108) + device_id = request.form['device_id'] + user_id, token, error_code = server.auth.arc_register( + name, password, device_id) + if user_id is not None: + r = {"success": True, "value": { + 'user_id': user_id, 'access_token': token}} + return jsonify(r) + else: + return error_return(error_code) # 应该是101,用户名被占用,毕竟电子邮箱没记录 # 集成式请求,没想到什么好办法处理,就先这样写着 @@ -149,7 +148,9 @@ def character_change(user_id): @app.route('/latte//toggle_uncap', methods=['POST']) # 角色觉醒切换 @server.auth.auth_required(request) def character_uncap(user_id, path): - character_id = int(path[22:]) + while '//' in path: + path = path.replace('//', '/') + character_id = int(path[21:]) r = server.setme.change_char_uncap(user_id, character_id) if r is not None: return jsonify({ @@ -280,7 +281,7 @@ def song_score_post(user_id): r, re = server.arcscore.arc_score_post(user_id, song_id, difficulty, score, shiny_perfect_count, perfect_count, near_count, miss_count, health, modifier, beyond_gauge, clear_type) - if r: + if r is not None: if re: return jsonify({ "success": True, @@ -512,12 +513,7 @@ def sys_set(user_id, path): def main(): - config = configparser.ConfigParser() - path = r'setting.ini' - config.read(path, encoding="utf-8") - HOST = config.get('CONFIG', 'HOST') - PORT = config.get('CONFIG', 'PORT') - app.config.from_mapping(SECRET_KEY='1145141919810') + app.config.from_mapping(SECRET_KEY=Config.SECRET_KEY) app.register_blueprint(web.login.bp) app.register_blueprint(web.index.bp) @@ -541,7 +537,7 @@ def main(): else: app.logger.info('Complete!') - app.run(HOST, PORT) + app.run(Config.HOST, Config.PORT) if __name__ == '__main__': diff --git a/latest version/run.bat b/latest version/run.bat index 7a84f97..01ab869 100644 --- a/latest version/run.bat +++ b/latest version/run.bat @@ -1 +1 @@ -python main.py \ No newline at end of file +python -B main.py \ No newline at end of file diff --git a/latest version/server/arcdownload.py b/latest version/server/arcdownload.py index e2eb846..9b06215 100644 --- a/latest version/server/arcdownload.py +++ b/latest version/server/arcdownload.py @@ -4,9 +4,10 @@ import sqlite3 from server.sql import Connect import time +from setting import Config -time_limit = 3000 # 每个玩家24小时下载次数限制 -time_gap_limit = 1000 # 下载链接有效秒数 +time_limit = Config.DOWNLOAD_TIMES_LIMIT # 每个玩家24小时下载次数限制 +time_gap_limit = Config.DOWNLOAD_TIME_GAP_LIMIT # 下载链接有效秒数 def get_file_md5(file_path): diff --git a/latest version/server/arcscore.py b/latest version/server/arcscore.py index 0294786..9dcbfaf 100644 --- a/latest version/server/arcscore.py +++ b/latest version/server/arcscore.py @@ -364,7 +364,7 @@ def insert_r30table(c, user_id, a, b): def arc_score_post(user_id, song_id, difficulty, score, shiny_perfect_count, perfect_count, near_count, miss_count, health, modifier, beyond_gauge, clear_type): # 分数上传,返回变化后的ptt,和世界模式变化 - ptt = 0 + ptt = None re = None with Connect() as c: rating = get_one_ptt(song_id, difficulty, score) diff --git a/latest version/server/auth.py b/latest version/server/auth.py index 0d97eba..80225d9 100644 --- a/latest version/server/auth.py +++ b/latest version/server/auth.py @@ -3,9 +3,10 @@ import server.arcworld from server.sql import Connect import functools +from setting import Config -def arc_login(name: str, password: str) -> str: # 登录判断 +def arc_login(name: str, password: str, device_id: str): # 登录判断 # 查询数据库中的user表,验证账号密码,返回并记录token,多返回个error code # token采用user_id和时间戳连接后hash生成(真的是瞎想的,没用bear) # 密码和token的加密方式为 SHA-256 @@ -27,14 +28,29 @@ def arc_login(name: str, password: str) -> str: # 登录判断 token = hashlib.sha256( (user_id + str(now)).encode("utf8")).hexdigest() c.execute( - '''select exists(select * from login where user_id = :user_id)''', {"user_id": user_id}) - - if c.fetchone() == (1,): # 删掉多余token - c.execute('''delete from login where user_id = :user_id''', - {'user_id': user_id}) - - c.execute('''insert into login(access_token, user_id) values(:access_token, :user_id)''', { - 'user_id': user_id, 'access_token': token}) + '''select login_device from login where user_id = :user_id''', {"user_id": user_id}) + y = c.fetchall() + if y: + device_list = [] + for i in y: + if i[0]: + device_list.append(i[0]) + else: + device_list.append('') + if device_id in device_list: + c.execute('''delete from login where login_device=:a''', { + 'a': device_id}) + should_delete_num = len( + device_list) - Config.LOGIN_DEVICE_NUMBER_LIMIT + else: + should_delete_num = len( + device_list) + 1 - Config.LOGIN_DEVICE_NUMBER_LIMIT + if should_delete_num >= 1: # 删掉多余token + c.execute('''delete from login where rowid in (select rowid from login where user_id=:user_id limit :a);''', + {'user_id': user_id, 'a': int(should_delete_num)}) + + c.execute('''insert into login(access_token, user_id, login_device) values(:access_token, :user_id, :device_id)''', { + 'user_id': user_id, 'access_token': token, 'device_id': device_id}) error_code = None else: # 密码错误 @@ -46,7 +62,7 @@ def arc_login(name: str, password: str) -> str: # 登录判断 return token, error_code -def arc_register(name: str, password: str): # 注册 +def arc_register(name: str, password: str, device_id: str): # 注册 # 账号注册,只记录hash密码和用户名,生成user_id和user_code,自动登录返回token # token和密码的处理同登录部分 @@ -100,8 +116,8 @@ def insert_user_char(c, user_id): token = hashlib.sha256( (str(user_id) + str(now)).encode("utf8")).hexdigest() - c.execute('''insert into login(access_token, user_id) values(:access_token, :user_id)''', { - 'user_id': user_id, 'access_token': token}) + c.execute('''insert into login(access_token, user_id, login_device) values(:access_token, :user_id, :device_id)''', { + 'user_id': user_id, 'access_token': token, 'device_id': device_id}) insert_user_char(c, user_id) error_code = 0 diff --git a/latest version/setting.ini b/latest version/setting.ini deleted file mode 100644 index 3179549..0000000 --- a/latest version/setting.ini +++ /dev/null @@ -1,7 +0,0 @@ -[CONFIG] -HOST = 192.168.1.113 -PORT = 80 - -[WEB] -USERNAME = admin -PASSWORD = admin \ No newline at end of file diff --git a/latest version/setting.py b/latest version/setting.py new file mode 100644 index 0000000..1188931 --- /dev/null +++ b/latest version/setting.py @@ -0,0 +1,62 @@ +class Config(): + ''' + This is the setting file. You can change some parameters here. + ''' + + ''' + -------------------- + 主机的地址和端口号 + Host and port of your server + ''' + HOST = '192.168.1.113' + PORT = '80' + ''' + -------------------- + ''' + + ''' + -------------------- + Web后台管理页面的用户名和密码 + Username and password of web background management page + ''' + USERNAME = 'admin' + PASSWORD = 'admin' + ''' + -------------------- + ''' + + ''' + -------------------- + Web后台管理页面的session秘钥,如果不知道是什么,请不要修改 + Session key of web background management page + If you don't know what it is, please don't modify it. + ''' + SECRET_KEY = '1145141919810' + ''' + -------------------- + ''' + + ''' + -------------------- + 玩家歌曲下载的24小时次数限制 + Player's song download limit times in 24 hours + ''' + DOWNLOAD_TIMES_LIMIT = 3000 + ''' + 歌曲下载链接的有效时长,单位:秒 + Effective duration of song download link, unit: seconds + ''' + DOWNLOAD_TIME_GAP_LIMIT = 1000 + ''' + -------------------- + ''' + + ''' + -------------------- + Arcaea登录的最大允许设备数量,最小值为1 + The maximum number of devices allowed to log in Arcaea, minimum: 1 + ''' + LOGIN_DEVICE_NUMBER_LIMIT = 1 + ''' + -------------------- + ''' diff --git a/latest version/web/index.py b/latest version/web/index.py index 8cdd749..9925cca 100644 --- a/latest version/web/index.py +++ b/latest version/web/index.py @@ -3,7 +3,7 @@ ) from web.login import login_required from werkzeug.utils import secure_filename -import sqlite3 +from server.sql import Connect import web.webscore import web.system import time @@ -43,26 +43,23 @@ def single_player_score(): user_code = request.form['user_code'] error = None if name or user_code: - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - if user_code: - c.execute('''select user_id from user where user_code=:a''', { - 'a': user_code}) - else: - c.execute( - '''select user_id from user where name=:a''', {'a': name}) - - user_id = c.fetchone() - posts = [] - if user_id: - user_id = user_id[0] - posts = web.webscore.get_user_score(c, user_id) - if not posts: - error = '无成绩 No score.' - else: - error = '玩家不存在 The player does not exist.' - conn.commit() - conn.close() + with Connect() as c: + if user_code: + c.execute('''select user_id from user where user_code=:a''', { + 'a': user_code}) + else: + c.execute( + '''select user_id from user where name=:a''', {'a': name}) + + user_id = c.fetchone() + posts = [] + if user_id: + user_id = user_id[0] + posts = web.webscore.get_user_score(c, user_id) + if not posts: + error = '无成绩 No score.' + else: + error = '玩家不存在 The player does not exist.' else: error = '输入为空 Null Input.' @@ -84,35 +81,33 @@ def single_player_ptt(): user_code = request.form['user_code'] error = None if name or user_code: - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - if user_code: - c.execute('''select user_id from user where user_code=:a''', { - 'a': user_code}) - else: - c.execute( - '''select user_id from user where name=:a''', {'a': name}) - - user_id = c.fetchone() - posts = [] - if user_id: - user_id = user_id[0] - user = web.webscore.get_user(c, user_id) - posts = web.webscore.get_user_score(c, user_id, 30) - recent, recentptt = web.webscore.get_user_recent30(c, user_id) - if not posts: - error = '无成绩 No score.' + with Connect() as c: + if user_code: + c.execute('''select user_id from user where user_code=:a''', { + 'a': user_code}) else: - bestptt = 0 - for i in posts: - if i['rating']: - bestptt += i['rating'] - bestptt = bestptt / 30 - else: - error = '玩家不存在 The player does not exist.' + c.execute( + '''select user_id from user where name=:a''', {'a': name}) + + user_id = c.fetchone() + posts = [] + if user_id: + user_id = user_id[0] + user = web.webscore.get_user(c, user_id) + posts = web.webscore.get_user_score(c, user_id, 30) + recent, recentptt = web.webscore.get_user_recent30( + c, user_id) + if not posts: + error = '无成绩 No score.' + else: + bestptt = 0 + for i in posts: + if i['rating']: + bestptt += i['rating'] + bestptt = bestptt / 30 + else: + error = '玩家不存在 The player does not exist.' - conn.commit() - conn.close() else: error = '输入为空 Null Input.' @@ -128,44 +123,41 @@ def single_player_ptt(): @login_required def all_player(): # 所有玩家数据,按照ptt排序 - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - c.execute('''select * from user order by rating_ptt DESC''') - x = c.fetchall() error = None - if x: - posts = [] - for i in x: - join_data = None - time_played = None - if i[3]: - join_date = time.strftime('%Y-%m-%d %H:%M:%S', - time.localtime(int(i[3])//1000)) - if i[20]: - time_played = time.strftime('%Y-%m-%d %H:%M:%S', - time.localtime(int(i[20])//1000)) - posts.append({'name': i[1], - 'user_id': i[0], - 'join_date': join_date, - 'user_code': i[4], - 'rating_ptt': i[5], - 'song_id': i[11], - 'difficulty': i[12], - 'score': i[13], - 'shiny_perfect_count': i[14], - 'perfect_count': i[15], - 'near_count': i[16], - 'miss_count': i[17], - 'time_played': time_played, - 'clear_type': i[21], - 'rating': i[22], - 'ticket': i[26] - }) - else: - error = '没有玩家数据 No player data.' + with Connect() as c: + c.execute('''select * from user order by rating_ptt DESC''') + x = c.fetchall() + if x: + posts = [] + for i in x: + join_data = None + time_played = None + if i[3]: + join_date = time.strftime('%Y-%m-%d %H:%M:%S', + time.localtime(int(i[3])//1000)) + if i[20]: + time_played = time.strftime('%Y-%m-%d %H:%M:%S', + time.localtime(int(i[20])//1000)) + posts.append({'name': i[1], + 'user_id': i[0], + 'join_date': join_date, + 'user_code': i[4], + 'rating_ptt': i[5], + 'song_id': i[11], + 'difficulty': i[12], + 'score': i[13], + 'shiny_perfect_count': i[14], + 'perfect_count': i[15], + 'near_count': i[16], + 'miss_count': i[17], + 'time_played': time_played, + 'clear_type': i[21], + 'rating': i[22], + 'ticket': i[26] + }) + else: + error = '没有玩家数据 No player data.' - conn.commit() - conn.close() if error: flash(error) return render_template('web/allplayer.html') @@ -184,26 +176,23 @@ def defnum(x): else: return None - conn = sqlite3.connect('./database/arcsong.db') - c = conn.cursor() - c.execute('''select * from songs''') - x = c.fetchall() error = None - if x: - posts = [] - for i in x: - posts.append({'song_id': i[0], - 'name_en': i[1], - 'rating_pst': defnum(i[12]), - 'rating_prs': defnum(i[13]), - 'rating_ftr': defnum(i[14]), - 'rating_byn': defnum(i[15]) - }) - else: - error = '没有铺面数据 No song data.' + with Connect('./database/arcsong.db') as c: + c.execute('''select * from songs''') + x = c.fetchall() + if x: + posts = [] + for i in x: + posts.append({'song_id': i[0], + 'name_en': i[1], + 'rating_pst': defnum(i[12]), + 'rating_prs': defnum(i[13]), + 'rating_ftr': defnum(i[14]), + 'rating_byn': defnum(i[15]) + }) + else: + error = '没有铺面数据 No song data.' - conn.commit() - conn.close() if error: flash(error) return render_template('web/allsong.html') @@ -221,15 +210,13 @@ def single_chart_top(): if difficulty.isdigit(): difficulty = int(difficulty) error = None - conn = sqlite3.connect('./database/arcsong.db') - c = conn.cursor() - song_name = '%'+song_name+'%' - c.execute('''select sid, name_en from songs where sid like :a limit 1''', - {'a': song_name}) - x = c.fetchone() - conn.commit() - conn.close() - print(x) + x = None + with Connect('./database/arcsong.db') as c: + song_name = '%'+song_name+'%' + c.execute('''select sid, name_en from songs where sid like :a limit 1''', + {'a': song_name}) + x = c.fetchone() + if x: song_id = x[0] posts = server.arcscore.arc_score_top(song_id, difficulty, -1) @@ -317,19 +304,16 @@ def get_rating(x): song_id = song_id[:200] if len(name_en) >= 256: name_en = name_en[:200] - conn = sqlite3.connect('./database/arcsong.db') - c = conn.cursor() - c.execute( - '''select exists(select * from songs where sid=:a)''', {'a': song_id}) - if c.fetchone() == (0,): - c.execute('''insert into songs(sid,name_en,rating_pst,rating_prs,rating_ftr,rating_byn) values(:a,:b,:c,:d,:e,:f)''', { - 'a': song_id, 'b': name_en, 'c': rating_pst, 'd': rating_prs, 'e': rating_ftr, 'f': rating_byd}) - flash('歌曲添加成功 Successfully add the song.') - else: - error = '歌曲已存在 The song exists.' - conn.commit() - conn.close() + with Connect('./database/arcsong.db') as c: + c.execute( + '''select exists(select * from songs where sid=:a)''', {'a': song_id}) + if c.fetchone() == (0,): + c.execute('''insert into songs(sid,name_en,rating_pst,rating_prs,rating_ftr,rating_byn) values(:a,:b,:c,:d,:e,:f)''', { + 'a': song_id, 'b': name_en, 'c': rating_pst, 'd': rating_prs, 'e': rating_ftr, 'f': rating_byd}) + flash('歌曲添加成功 Successfully add the song.') + else: + error = '歌曲已存在 The song exists.' if error: flash(error) @@ -344,18 +328,15 @@ def delete_song(): error = None song_id = request.form['sid'] - conn = sqlite3.connect('./database/arcsong.db') - c = conn.cursor() - c.execute( - '''select exists(select * from songs where sid=:a)''', {'a': song_id}) - if c.fetchone() == (1,): - c.execute('''delete from songs where sid=:a''', {'a': song_id}) - flash('歌曲删除成功 Successfully delete the song.') - else: - error = "歌曲不存在 The song doesn't exist." + with Connect('./database/arcsong.db') as c: + c.execute( + '''select exists(select * from songs where sid=:a)''', {'a': song_id}) + if c.fetchone() == (1,): + c.execute('''delete from songs where sid=:a''', {'a': song_id}) + flash('歌曲删除成功 Successfully delete the song.') + else: + error = "歌曲不存在 The song doesn't exist." - conn.commit() - conn.close() if error: flash(error) @@ -366,31 +347,27 @@ def delete_song(): @login_required def all_character(): # 所有角色数据 - - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - c.execute('''select * from character''') - x = c.fetchall() error = None - if x: - posts = [] - for i in x: - posts.append({'character_id': i[0], - 'name': i[1], - 'level': i[2], - 'frag': i[5], - 'prog': i[6], - 'overdrive': i[7], - 'skill_id': i[8], - 'skill_id_uncap': i[11], - 'char_type': i[12], - 'is_uncapped': i[14] == 1 - }) - else: - error = '没有角色数据 No character data.' + with Connect() as c: + c.execute('''select * from character''') + x = c.fetchall() + if x: + posts = [] + for i in x: + posts.append({'character_id': i[0], + 'name': i[1], + 'level': i[2], + 'frag': i[5], + 'prog': i[6], + 'overdrive': i[7], + 'skill_id': i[8], + 'skill_id_uncap': i[11], + 'char_type': i[12], + 'is_uncapped': i[14] == 1 + }) + else: + error = '没有角色数据 No character data.' - conn.commit() - conn.close() if error: flash(error) return render_template('web/allchar.html') @@ -443,49 +420,45 @@ def edit_char(): flash(error) return redirect(url_for('index.change_character')) - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - c.execute( - '''select exists(select * from character where character_id=:a)''', {'a': character_id}) - if c.fetchone() == (1,): - if level is None and frag is None and prog is None and overdrive is None and skill_id is None and skill_id_uncap is None: - error = '无修改 No change.' - else: - - sql = '''update character set level_exp=25000''' - sql_dict = {'character_id': character_id} - if level is not None: - sql += ', level = :level' - sql_dict['level'] = level - if frag is not None: - sql += ', frag = :frag' - sql_dict['frag'] = frag - if prog is not None: - sql += ', prog = :prog' - sql_dict['prog'] = prog - if overdrive is not None: - sql += ', overdrive = :overdrive' - sql_dict['overdrive'] = overdrive - if skill_id is not None: - sql += ', skill_id = :skill_id' - if skill_id == 'No_skill': - sql_dict['skill_id'] = '' - else: - sql_dict['skill_id'] = skill_id - if skill_id_uncap is not None: - sql += ', skill_id_uncap = :skill_id_uncap' - if skill_id_uncap == 'No_skill': - sql_dict['skill_id_uncap'] = '' - else: - sql_dict['skill_id_uncap'] = skill_id_uncap - sql += ' where character_id = :character_id' - c.execute(sql, sql_dict) - flash('角色修改成功 Successfully edit the character.') - else: - error = '角色不存在 The character does not exist.' + with Connect() as c: + c.execute( + '''select exists(select * from character where character_id=:a)''', {'a': character_id}) + if c.fetchone() == (1,): + if level is None and frag is None and prog is None and overdrive is None and skill_id is None and skill_id_uncap is None: + error = '无修改 No change.' + else: - conn.commit() - conn.close() + sql = '''update character set level_exp=25000''' + sql_dict = {'character_id': character_id} + if level is not None: + sql += ', level = :level' + sql_dict['level'] = level + if frag is not None: + sql += ', frag = :frag' + sql_dict['frag'] = frag + if prog is not None: + sql += ', prog = :prog' + sql_dict['prog'] = prog + if overdrive is not None: + sql += ', overdrive = :overdrive' + sql_dict['overdrive'] = overdrive + if skill_id is not None: + sql += ', skill_id = :skill_id' + if skill_id == 'No_skill': + sql_dict['skill_id'] = '' + else: + sql_dict['skill_id'] = skill_id + if skill_id_uncap is not None: + sql += ', skill_id_uncap = :skill_id_uncap' + if skill_id_uncap == 'No_skill': + sql_dict['skill_id_uncap'] = '' + else: + sql_dict['skill_id_uncap'] = skill_id_uncap + sql += ' where character_id = :character_id' + c.execute(sql, sql_dict) + flash('角色修改成功 Successfully edit the character.') + else: + error = '角色不存在 The character does not exist.' if error: flash(error) @@ -497,11 +470,8 @@ def edit_char(): @login_required def update_character(): # 更新角色数据 - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - web.system.update_user_char(c) - conn.commit() - conn.close() + with Connect() as c: + web.system.update_user_char(c) flash('数据更新成功 Success update data.') return redirect(url_for('index.change_character')) @@ -535,56 +505,53 @@ def edit_user(): flash(error) return redirect(url_for('index.change_user')) - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() + with Connect() as c: + + # 全修改 + if 'name' not in request.form and 'user_code' not in request.form: + flag = False + if not ticket: + error = '无修改 No change.' + else: + sql = '''update user set ticket = :ticket''' + sql_dict = {'ticket': ticket} + c.execute(sql, sql_dict) + flash("全部用户信息修改成功 Successfully edit all the users' information.") - # 全修改 - if 'name' not in request.form and 'user_code' not in request.form: - flag = False - if not ticket: - error = '无修改 No change.' else: - sql = '''update user set ticket = :ticket''' - sql_dict = {'ticket': ticket} - c.execute(sql, sql_dict) - flash("全部用户信息修改成功 Successfully edit all the users' information.") + name = request.form['name'] + user_code = request.form['user_code'] - else: - name = request.form['name'] - user_code = request.form['user_code'] + # 指定修改 - # 指定修改 + if name or user_code: - if name or user_code: + if user_code: + c.execute('''select user_id from user where user_code=:a''', { + 'a': user_code}) + else: + c.execute( + '''select user_id from user where name=:a''', {'a': name}) - if user_code: - c.execute('''select user_id from user where user_code=:a''', { - 'a': user_code}) - else: - c.execute( - '''select user_id from user where name=:a''', {'a': name}) + user_id = c.fetchone() + if user_id: + user_id = user_id[0] - user_id = c.fetchone() - if user_id: - user_id = user_id[0] + if not ticket: + error = '无修改 No change.' + else: + sql = '''update user set ticket = :ticket where user_id = :user_id''' + sql_dict = {'ticket': ticket, 'user_id': user_id} + c.execute(sql, sql_dict) + flash('用户信息修改成功 Successfully edit the user information.') - if not ticket: - error = '无修改 No change.' else: - sql = '''update user set ticket = :ticket where user_id = :user_id''' - sql_dict = {'ticket': ticket, 'user_id': user_id} - c.execute(sql, sql_dict) - flash('用户信息修改成功 Successfully edit the user information.') + error = '玩家不存在 The player does not exist.' else: - error = '玩家不存在 The player does not exist.' + if flag: + error = '输入为空 Null Input.' - else: - if flag: - error = '输入为空 Null Input.' - - conn.commit() - conn.close() if error: flash(error) @@ -614,53 +581,50 @@ def edit_user_purchase(): flash('输入为空 Null Input.') return redirect(url_for('index.change_user_purchase')) - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() + with Connect() as c: - # 全修改 - 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) - else: - c.execute('''delete from user_item''') + # 全修改 + 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) + else: + c.execute('''delete from user_item''') - flash("全部用户购买信息修改成功 Successfully edit all the users' purchase information.") + flash("全部用户购买信息修改成功 Successfully edit all the users' purchase information.") - else: - name = request.form['name'] - user_code = request.form['user_code'] + else: + name = request.form['name'] + user_code = request.form['user_code'] - # 指定修改 - if name or user_code: + # 指定修改 + if name or user_code: - if user_code: - c.execute('''select user_id from user where user_code=:a''', { - 'a': user_code}) - else: - c.execute( - '''select user_id from user where name=:a''', {'a': name}) + if user_code: + c.execute('''select user_id from user where user_code=:a''', { + 'a': user_code}) + else: + c.execute( + '''select user_id from user where name=:a''', {'a': name}) - user_id = c.fetchone() - if user_id: - user_id = user_id[0] + user_id = c.fetchone() + if user_id: + user_id = user_id[0] + + if method == '0': + web.system.unlock_user_item(c, user_id) + else: + c.execute('''delete from user_item where user_id=:a''', { + 'a': user_id}) + flash('用户购买信息修改成功 Successfully edit the user purchase information.') - if method == '0': - web.system.unlock_user_item(c, user_id) else: - c.execute('''delete from user_item where user_id=:a''', { - 'a': user_id}) - flash('用户购买信息修改成功 Successfully edit the user purchase information.') + error = '玩家不存在 The player does not exist.' else: - error = '玩家不存在 The player does not exist.' - - else: - if flag: - error = '输入为空 Null Input.' + if flag: + error = '输入为空 Null Input.' - conn.commit() - conn.close() if error: flash(error) @@ -730,40 +694,37 @@ def change_item(): flash(error) return redirect(url_for('index.change_item')) - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - c.execute( - '''select exists(select * from item where item_id=:a and type=:b)''', {'a': item_id, 'b': item_type}) - if c.fetchone() == (1,): - if is_available is None and price is None and orig_price is None and not discount_from and not discount_to: - error = '无修改 No change.' + with Connect() as c: + c.execute( + '''select exists(select * from item where item_id=:a and type=:b)''', {'a': item_id, 'b': item_type}) + if c.fetchone() == (1,): + if is_available is None and price is None and orig_price is None and not discount_from and not discount_to: + error = '无修改 No change.' + else: + sql = '''update item set type=:type''' + sql_dict = {'item_id': item_id, 'type': item_type} + if price is not None: + sql += ', price = :price' + sql_dict['price'] = price + if orig_price is not None: + sql += ', orig_price = :orig_price' + sql_dict['orig_price'] = orig_price + if discount_from is not None: + sql += ', discount_from = :discount_from' + sql_dict['discount_from'] = discount_from + if discount_to is not None: + sql += ', discount_to = :discount_to' + sql_dict['discount_to'] = discount_to + if is_available is not None: + sql += ', is_available = :is_available' + sql_dict['is_available'] = is_available + + sql += ' where item_id = :item_id and type = :type' + c.execute(sql, sql_dict) + flash('购买项目修改成功 Successfully edit the item.') else: - sql = '''update item set type=:type''' - sql_dict = {'item_id': item_id, 'type': item_type} - if price is not None: - sql += ', price = :price' - sql_dict['price'] = price - if orig_price is not None: - sql += ', orig_price = :orig_price' - sql_dict['orig_price'] = orig_price - if discount_from is not None: - sql += ', discount_from = :discount_from' - sql_dict['discount_from'] = discount_from - if discount_to is not None: - sql += ', discount_to = :discount_to' - sql_dict['discount_to'] = discount_to - if is_available is not None: - sql += ', is_available = :is_available' - sql_dict['is_available'] = is_available - - sql += ' where item_id = :item_id and type = :type' - c.execute(sql, sql_dict) - flash('购买项目修改成功 Successfully edit the item.') - else: - error = '购买项目不存在 The item does not exist.' + error = '购买项目不存在 The item does not exist.' - conn.commit() - conn.close() if error: flash(error) @@ -783,44 +744,41 @@ def update_user_save(): name = None user_code = None - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() + with Connect() as c: - # 全修改 - if 'name' not in request.form and 'user_code' not in request.form: - flag = False - web.system.update_all_save(c) - flash("全部用户存档同步成功 Successfully update all users' saves.") + # 全修改 + if 'name' not in request.form and 'user_code' not in request.form: + flag = False + web.system.update_all_save(c) + flash("全部用户存档同步成功 Successfully update all users' saves.") - else: - name = request.form['name'] - user_code = request.form['user_code'] + else: + name = request.form['name'] + user_code = request.form['user_code'] - # 指定修改 - if name or user_code: + # 指定修改 + if name or user_code: - if user_code: - c.execute('''select user_id from user where user_code=:a''', { - 'a': user_code}) - else: - c.execute( - '''select user_id from user where name=:a''', {'a': name}) + if user_code: + c.execute('''select user_id from user where user_code=:a''', { + 'a': user_code}) + else: + c.execute( + '''select user_id from user where name=:a''', {'a': name}) + + user_id = c.fetchone() + if user_id: + user_id = user_id[0] + web.system.update_one_save(c, user_id) + flash("用户存档同步成功 Successfully update the user's saves.") - user_id = c.fetchone() - if user_id: - user_id = user_id[0] - web.system.update_one_save(c, user_id) - flash("用户存档同步成功 Successfully update the user's saves.") + else: + error = '玩家不存在 The player does not exist.' else: - error = '玩家不存在 The player does not exist.' - - else: - if flag: - error = '输入为空 Null Input.' + if flag: + error = '输入为空 Null Input.' - conn.commit() - conn.close() if error: flash(error) @@ -832,30 +790,27 @@ def update_user_save(): def all_present(): # 所有奖励数据 - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - c.execute('''select * from present''') - x = c.fetchall() error = None - if x: - posts = [] - for i in x: - items = json.loads(i[2]) - items_string = '' - for j in items: - items_string = items_string + '\n' + \ - str(j['id']) + ': ' + str(j['amount']) - - posts.append({'present_id': i[0], - 'expire_ts': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(i[1])//1000)), - 'items': items_string, - 'description': i[3] - }) - else: - error = '没有奖励数据 No present data.' + with Connect() as c: + c.execute('''select * from present''') + x = c.fetchall() + if x: + posts = [] + for i in x: + items = json.loads(i[2]) + items_string = '' + for j in items: + items_string = items_string + '\n' + \ + str(j['id']) + ': ' + str(j['amount']) + + posts.append({'present_id': i[0], + 'expire_ts': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(i[1])//1000)), + 'items': items_string, + 'description': i[3] + }) + else: + error = '没有奖励数据 No present data.' - conn.commit() - conn.close() if error: flash(error) return render_template('web/allpresent.html') @@ -941,47 +896,42 @@ def deliver_present(): user_code = None present_id = request.form['present_id'] - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - if not web.system.is_present_available(c, present_id): - flash("奖励不存在 The present does not exist.") - conn.commit() - conn.close() - return render_template('web/deliverpresent.html') - - # 全修改 - if 'name' not in request.form and 'user_code' not in request.form: - flag = False - web.system.deliver_all_user_present(c, present_id) - flash("全部用户奖励分发成功 Successfully deliver the present to all users.") - else: - name = request.form['name'] - user_code = request.form['user_code'] + with Connect() as c: + if not web.system.is_present_available(c, present_id): + flash("奖励不存在 The present does not exist.") + return render_template('web/deliverpresent.html') - # 指定修改f - if name or user_code: - if user_code: - c.execute('''select user_id from user where user_code=:a''', { - 'a': user_code}) + # 全修改 + if 'name' not in request.form and 'user_code' not in request.form: + flag = False + web.system.deliver_all_user_present(c, present_id) + flash("全部用户奖励分发成功 Successfully deliver the present to all users.") else: - c.execute( - '''select user_id from user where name=:a''', {'a': name}) + name = request.form['name'] + user_code = request.form['user_code'] + + # 指定修改f + if name or user_code: + if user_code: + c.execute('''select user_id from user where user_code=:a''', { + 'a': user_code}) + else: + c.execute( + '''select user_id from user where name=:a''', {'a': name}) - user_id = c.fetchone() - if user_id: - user_id = user_id[0] - web.system.deliver_one_user_present(c, present_id, user_id) - flash("用户奖励分发成功 Successfully deliver the present to the user.") + user_id = c.fetchone() + if user_id: + user_id = user_id[0] + web.system.deliver_one_user_present(c, present_id, user_id) + flash("用户奖励分发成功 Successfully deliver the present to the user.") - else: - error = '玩家不存在 The player does not exist.' + else: + error = '玩家不存在 The player does not exist.' - else: - if flag: - error = '输入为空 Null Input.' + else: + if flag: + error = '输入为空 Null Input.' - conn.commit() - conn.close() if error: flash(error) @@ -993,29 +943,26 @@ def deliver_present(): def all_redeem(): # 所有兑换码数据 - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - c.execute('''select * from redeem''') - x = c.fetchall() error = None - if x: - posts = [] - for i in x: - items = json.loads(i[1]) - items_string = '' - for j in items: - items_string = items_string + '\n' + \ - str(j['id']) + ': ' + str(j['amount']) - - posts.append({'code': i[0], - 'items': items_string, - 'type': i[2] - }) - else: - error = '没有兑换码数据 No redeem code data.' + with Connect() as c: + c.execute('''select * from redeem''') + x = c.fetchall() + if x: + posts = [] + for i in x: + items = json.loads(i[1]) + items_string = '' + for j in items: + items_string = items_string + '\n' + \ + str(j['id']) + ': ' + str(j['amount']) + + posts.append({'code': i[0], + 'items': items_string, + 'type': i[2] + }) + else: + error = '没有兑换码数据 No redeem code data.' - conn.commit() - conn.close() if error: flash(error) return render_template('web/allredeem.html') @@ -1106,24 +1053,21 @@ def delete_redeem(): def one_redeem(code): # 某个兑换码的用户使用情况数据 - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() - c.execute( - '''select user_id, name, user_code from user where user_id in (select user_id from user_redeem where code=:a)''', {'a': code}) - x = c.fetchall() error = None - if x: - posts = [] - for i in x: - posts.append({'user_id': i[0], - 'name': i[1], - 'user_code': i[2] - }) - else: - error = '没有数据 No data.' + with Connect() as c: + c.execute( + '''select user_id, name, user_code from user where user_id in (select user_id from user_redeem where code=:a)''', {'a': code}) + x = c.fetchall() + if x: + posts = [] + for i in x: + posts.append({'user_id': i[0], + 'name': i[1], + 'user_code': i[2] + }) + else: + error = '没有数据 No data.' - conn.commit() - conn.close() if error: flash(error) return render_template('web/redeem.html', code=code) @@ -1152,32 +1096,28 @@ def edit_userpwd(): flash('密码太长或太短 Password is too long or too short!') return render_template('web/changeuserpwd.html') - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() + with Connect() as c: + if name or user_code: - if name or user_code: + if user_code: + c.execute('''select user_id from user where user_code=:a''', { + 'a': user_code}) + else: + c.execute( + '''select user_id from user where name=:a''', {'a': name}) - if user_code: - c.execute('''select user_id from user where user_code=:a''', { - 'a': user_code}) - else: - c.execute( - '''select user_id from user where name=:a''', {'a': name}) + user_id = c.fetchone() + if user_id: + user_id = user_id[0] + web.system.change_userpwd(c, user_id, pwd) + flash('用户密码修改成功 Successfully edit the user information.') - user_id = c.fetchone() - if user_id: - user_id = user_id[0] - web.system.change_userpwd(c, user_id, pwd) - flash('用户密码修改成功 Successfully edit the user information.') + else: + error = '玩家不存在 The player does not exist.' else: - error = '玩家不存在 The player does not exist.' - - else: - error = '输入为空 Null Input.' + error = '输入为空 Null Input.' - conn.commit() - conn.close() if error: flash(error) @@ -1196,31 +1136,27 @@ def ban_user(): name = request.form['name'] user_code = request.form['user_code'] - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() + with Connect() as c: + if name or user_code: + if user_code: + c.execute('''select user_id from user where user_code=:a''', { + 'a': user_code}) + else: + c.execute( + '''select user_id from user where name=:a''', {'a': name}) - if name or user_code: - if user_code: - c.execute('''select user_id from user where user_code=:a''', { - 'a': user_code}) - else: - c.execute( - '''select user_id from user where name=:a''', {'a': name}) + user_id = c.fetchone() + if user_id: + user_id = user_id[0] + web.system.ban_one_user(c, user_id) + flash('用户封禁成功 Successfully ban the user.') - user_id = c.fetchone() - if user_id: - user_id = user_id[0] - web.system.ban_one_user(c, user_id) - flash('用户封禁成功 Successfully ban the user.') + else: + error = '玩家不存在 The player does not exist.' else: - error = '玩家不存在 The player does not exist.' - - else: - error = '输入为空 Null Input.' + error = '输入为空 Null Input.' - conn.commit() - conn.close() if error: flash(error) @@ -1236,31 +1172,27 @@ def delete_user_score(): name = request.form['name'] user_code = request.form['user_code'] - conn = sqlite3.connect('./database/arcaea_database.db') - c = conn.cursor() + with Connect() as c: + if name or user_code: + if user_code: + c.execute('''select user_id from user where user_code=:a''', { + 'a': user_code}) + else: + c.execute( + '''select user_id from user where name=:a''', {'a': name}) - if name or user_code: - if user_code: - c.execute('''select user_id from user where user_code=:a''', { - 'a': user_code}) - else: - c.execute( - '''select user_id from user where name=:a''', {'a': name}) + user_id = c.fetchone() + if user_id: + user_id = user_id[0] + web.system.clear_user_score(c, user_id) + flash("用户成绩删除成功 Successfully delete the user's scores.") - user_id = c.fetchone() - if user_id: - user_id = user_id[0] - web.system.clear_user_score(c, user_id) - flash("用户成绩删除成功 Successfully delete the user's scores.") + else: + error = '玩家不存在 The player does not exist.' else: - error = '玩家不存在 The player does not exist.' - - else: - error = '输入为空 Null Input.' + error = '输入为空 Null Input.' - conn.commit() - conn.close() if error: flash(error) diff --git a/latest version/web/login.py b/latest version/web/login.py index 26d3540..2ba52d3 100644 --- a/latest version/web/login.py +++ b/latest version/web/login.py @@ -2,7 +2,7 @@ from flask import (Blueprint, flash, g, redirect, render_template, request, session, url_for) import functools -import configparser +from setting import Config bp = Blueprint('login', __name__, url_prefix='/web') @@ -15,18 +15,12 @@ def login(): password = request.form['password'] error = None - config = configparser.ConfigParser() - path = r'setting.ini' - config.read(path, encoding="utf-8") - USERNAME = config.get('WEB', 'USERNAME') - PASSWORD = config.get('WEB', 'PASSWORD') - - if username != USERNAME and password != PASSWORD: + if username != Config.USERNAME or password != Config.PASSWORD: error = '错误的用户名或密码 Incorrect username or password.' if error is None: session.clear() - session['user_id'] = USERNAME + PASSWORD + session['user_id'] = Config.USERNAME + Config.PASSWORD return redirect(url_for('index.index')) flash(error) @@ -48,16 +42,10 @@ def login_required(view): def wrapped_view(**kwargs): x = session.get('user_id') - config = configparser.ConfigParser() - path = r'setting.ini' - config.read(path, encoding="utf-8") - USERNAME = config.get('WEB', 'USERNAME') - PASSWORD = config.get('WEB', 'PASSWORD') - - if x != USERNAME + PASSWORD: + if x != Config.USERNAME + Config.PASSWORD: return redirect(url_for('login.login')) - g.user = {'user_id': x, 'username': USERNAME} + g.user = {'user_id': x, 'username': Config.USERNAME} return view(**kwargs) return wrapped_view