| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- # -*- coding: utf-8 -*-
- import requests
- import sys
- import os
- import threading
- import json
- from json import JSONDecodeError
- import easygui as eg
- import tkinter as tk
- from game.const import game as G
- from game.const import rouge as R
- from game.file_loader.fileLoader import load_json, load_png, save_encrypt_single_rouge_file, load_rouge_data
- server_ip = "123.249.17.43:5000"
- ALLOWED_CHARACTERS = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:,.<>?/")
- def load_online_config():
- file_path = os.path.join(os.getcwd(), 'login.json') # 获取绝对路径
- if not os.path.exists(file_path):
- print("login.json 存在")
- login_json = {"online_mode": True, "UID": "", "username": "", "password": ""}
- with open(f'login.json', 'w', encoding='utf-8') as file:
- json.dump(login_json, file, indent=4, ensure_ascii=False)
- try:
- with open("login.json", encoding="utf-8") as f:
- login_json = json.load(f)
- except JSONDecodeError:
- with open("login.json", encoding="utf-8_sig") as f:
- login_json = json.load(f)
- G.UID = login_json["UID"]
- G.USERNAME = login_json["username"]
- G.PASSWORD = login_json["password"]
- def try_login():
- try:
- with open("login.json", encoding="utf-8") as f:
- login_json = json.load(f)
- except JSONDecodeError:
- with open("login.json", encoding="utf-8_sig") as f:
- login_json = json.load(f)
- if login_json["online_mode"]:
- if G.UID != "":
- _result = uid_login(G.UID, G.PASSWORD)
- if _result.get('message', '未知错误') == "登录成功":
- G.USERNAME = _result['user_info']['name']
- G.IS_ONLINE = True
- # eg.msgbox(f"{G.USERNAME}({G.UID})登录成功!", "成功")
- choice = eg.buttonbox(msg=f"{G.USERNAME}({G.UID})登录成功!", title="欢迎", choices=["进入主界面", "修改名字", "覆盖本地数据"])
- if choice == "修改名字":
- change_username()
- elif choice == "覆盖本地数据":
- update_local_data()
- return True
- if G.USERNAME != "":
- _result = username_login(G.USERNAME, G.PASSWORD)
- if _result.get('message', '未知错误') == "登录成功":
- G.UID = _result['user_info']['UID']
- G.IS_ONLINE = True
- eg.msgbox(f"{G.USERNAME}({G.UID})登录成功!", "成功")
- return True
- eg.msgbox(f"登录失败!请检查登录信息或网络连接!", "失败")
- G.IS_ONLINE = False
- return False
- def change_username():
- fields = ["新昵称", "当前密码*"]
- values = eg.multpasswordbox("修改昵称", "输入信息", fields)
- if not values or not all(values):
- if values is None:
- return
- eg.msgbox("新昵称不能为空!", "错误")
- change_username()
- else:
- new_username, password = values
- try:
- response = requests.post(
- f'http://{server_ip}/update_username',
- json={"uid": G.UID, "username": G.USERNAME, "new_username": new_username, "password": password},
- timeout=5
- )
- if response.status_code == 200:
- login_json = {"online_mode": True, "UID": G.UID, "username": new_username, "password": password}
- with open(f'login.json', 'w', encoding='utf-8') as file:
- json.dump(login_json, file, indent=4, ensure_ascii=False)
- G.USERNAME = new_username
- eg.msgbox(f"昵称修改成功!", "结果")
- else:
- error_msg = response.json().get("message", "未知错误")
- eg.msgbox(f"修改失败: {error_msg}", "错误")
- change_username()
- except requests.exceptions.RequestException as e:
- eg.msgbox(f"无法连接服务器: {str(e)}", "网络错误")
- change_username()
- def update_local_data():
- online_data = get_online_data()
- user_documents_folder = os.path.expanduser("~\\Documents") # 获取用户的"文档"文件夹路径
- qqtpve_folder = os.path.join(user_documents_folder, "QQTPVE") # 构建QQTPVE文件夹路径
- os.makedirs(qqtpve_folder, exist_ok=True) # 确保QQTPVE文件夹存在,如果不存在则创建
- rouge_file_path = os.path.join(qqtpve_folder, "rougeOB.rougedata") # 构建文件路径
- local_data = load_rouge_data(rouge_file_path)
- local_text = f"小倩等级{local_data['Fox']['lv']}; " \
- f"波波丽等级{local_data['Boboli']['lv']}; " \
- f"火影等级{local_data['Naruto_old']['lv']}; " \
- f"阿莎等级{local_data['Asha']['lv']}; " \
- f"玛利亚等级{local_data['Maria']['lv']}; " \
- f"???等级{local_data['Surprise']['lv']}"
- oneline_text = f"小倩等级{online_data['Fox']['lv']}; " \
- f"波波丽等级{online_data['Boboli']['lv']}; " \
- f"火影等级{online_data['Naruto_old']['lv']}; " \
- f"阿莎等级{online_data['Asha']['lv']}; " \
- f"玛利亚等级{online_data['Maria']['lv']}; " \
- f"???等级{online_data['Surprise']['lv']}"
- choice = eg.buttonbox(msg=f"是否将本地数据\n{local_text}\n更新为\n{oneline_text}\n?该操作不可重置!", title="注意", choices=["覆盖数据", "取消"])
- if choice == "覆盖数据":
- rouge_data = {}
- for hero in ["Fox","Boboli","Naruto_old","Asha","Maria","Surprise"]:
- rouge_data[hero] = online_data[hero]
- save_encrypt_single_rouge_file(rouge_data, "rougeOB")
- eg.msgbox(f"数据更新成功!", "结果")
- return
- def get_online_data():
- if not G.IS_ONLINE:
- return
- latest_data = get_profile(G.UID).get('profile', {})
- if not latest_data:
- return
- return latest_data
- def show_loading_window():
- window = tk.Tk()
- window.title("提示")
- window.geometry("200x100")
- label = tk.Label(window, text="数据同步中,请稍后...")
- label.pack(pady=20)
- # 存储窗口引用用于关闭
- window.after(100, lambda: window.lift())
- return window
- def save_online():
- print("正在同步至服务器...")
- latest_data = get_online_data()
- if latest_data is None:
- return
- new_data = {}
- real_exp = int(R.rouge_exp * R.diff_exp[R.difficulty])
- for hero in R.hero_name.keys():
- if hero == R.rouge_hero:
- exp = latest_data[hero]["exp"] + real_exp
- else:
- exp = latest_data[hero]["exp"]
- lv = R.current_lv(exp)
- new_data[hero] = {"exp": exp, "lv": lv}
- new_data['record'] = latest_data['record']
- _result = update_profile(G.UID, new_data, G.PASSWORD)
- print(_result.get('message', '保存失败'))
- print("同步完成")
- return
- def register():
- fields = ["昵称", "密码*"]
- values = eg.multpasswordbox("用户注册", "输入信息", fields)
- if not values or not all(values):
- if values is None:
- return
- eg.msgbox("昵称和密码不能为空!", "错误")
- register()
- else:
- username, password = values
- if any(char not in ALLOWED_CHARACTERS for char in password):
- eg.msgbox("密码含有非法字符!", "错误")
- try:
- response = requests.post(
- f'http://{server_ip}/register',
- json={"username": username, "password": password},
- timeout=5
- )
- if response.status_code == 201:
- uid = response.json().get("UID", "未知")
- login_json = {"online_mode": True, "UID": uid, "username": username, "password": password}
- with open(f'login.json', 'w', encoding='utf-8') as file:
- json.dump(login_json, file, indent=4, ensure_ascii=False)
- G.UID = uid
- G.USERNAME = username
- G.PASSWORD = password
- eg.msgbox(f"注册成功(设备更变后可在login.json中凭UID或用户名登录)!\nUID: {uid}", "结果")
- else:
- error_msg = response.json().get("message", "未知错误")
- eg.msgbox(f"注册失败: {error_msg}; 已有账号请自行在login.json中凭UID或用户名登录", "错误")
- register()
- except requests.exceptions.RequestException as e:
- eg.msgbox(f"无法连接服务器: {str(e)}", "网络错误")
- register()
- def uid_login(uid, password):
- url = f'http://{server_ip}/uid_login'
- try:
- response = requests.post(url, json={'uid': uid, 'password': password})
- return response.json()
- except requests.exceptions.ConnectionError:
- return {'message': '无法连接到服务器'}
- def username_login(username, password):
- url = f'http://{server_ip}/username_login'
- try:
- response = requests.post(url, json={'username': username, 'password': password})
- return response.json()
- except requests.exceptions.ConnectionError:
- return {'message': '无法连接到服务器'}
- def get_profile(uid):
- url = f'http://{server_ip}/get_profile'
- try:
- response = requests.post(url, json={'uid': uid})
- return response.json()
- except requests.exceptions.ConnectionError:
- return {'message': '无法连接到服务器'}
- def update_profile(uid, profile, password):
- url = f'http://{server_ip}/update_profile'
- try:
- response = requests.post(url, json={'uid': uid, 'profile': profile, 'password': password})
- return response.json()
- except requests.exceptions.ConnectionError:
- return {'message': '无法连接到服务器'}
- if __name__ == '__main__':
- # username = "admin"
- # password = "123456"
- # result = register(username, password)
- # print(result.get('message', '未知错误'))
- # print("账号UID: ", result.get('UID', '未知错误'))
- #
- # username = str(input("登陆用户名: "))
- # password = input("登陆密码: ")
- # result = username_login(username, password)
- # print(result.get('message', '未知错误'))
- #
- # uid = str(input("登陆UID: "))
- # password = input("登陆密码: ")
- # result = uid_login(uid, password)
- # print(result.get('message', '未知错误'))
- uid = input("查询UID: ")
- result = get_profile(uid)
- print(result.get('name', '未知错误'))
- print(result.get('profile', '未知错误'))
- new_exp = input("更新后的经验: ")
- uid = input("修改的UID: ")
- password = input("修改的登陆密码: ")
- new_info = {
- 'Asha': {'exp': new_exp, 'lv': 1},
- 'Boboli': {'exp': 0, 'lv': 1},
- 'Fox': {'exp': 0, 'lv': 1},
- 'Maria': {'exp': 0, 'lv': 1},
- 'Naruto_old': {'exp': 0, 'lv': 1},
- 'Surprise': {'exp': 0, 'lv': 1},
- 'record': 'null'
- }
- result = update_profile(uid, new_info, password)
- print(result.get('message', '未知错误'))
- uid = input("查询UID: ")
- result = get_profile(uid)
- print(result.get('name', '未知错误'))
- print(result.get('message', '未知错误'))
|