123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- # -*- coding: utf-8 -*-
- from datetime import datetime
- from flask import Flask, render_template
- from flask_socketio import SocketIO, emit
- from scriptBase.comon import *
- import pyautogui
- import base64
- import threading
- from dongri_task import *
- from collections import deque
- import json
- from concurrent.futures import ThreadPoolExecutor
- # 全局线程池,限制最大线程数为1
- executor = ThreadPoolExecutor(max_workers=1)
- app = Flask(__name__)
- socketio = SocketIO(app, cors_allowed_origins="*")
- event = threading.Event()
- g_status_list = []
- last_time = 0.0
- task_queue = deque()
- last_process = ''
- isGameBegin = True
- autoTask = None
- isReset = False
- g_times = 0
- g_cureNum = 500
- @app.after_request
- def add_no_cache_header(response):
- # 添加禁用缓存的响应头
- response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
- response.headers["Pragma"] = "no-cache"
- response.headers["Expires"] = "0"
- return response
- def thread_runTask():
- global last_process
- global task_queue,isReset
- while True:
- if event.is_set():
- task_queue.clear()
- if len(task_queue) != 0:
- task = task_queue[-1]
- task_queue.pop()
- last_process = task.name
- task.run()
- myTimeSleep_small()
- else:
- myTimeSleep_big()
- if isReset:
- isReset = False
- restart_game()
- @app.route('/')
- def index():
- return render_template('index_dongri.html')
- @socketio.on('connect')
- def handle_connect():
- print('Client connected')
- @socketio.on('disconnect')
- def handle_disconnect():
- print('Client disconnected')
- def send_hint(msg):#数组信息
- emit('processing_hint', msg)
- def send_todo():
- emit('processing_todo', get_todo_msg())
- def send_status(msg):#软件执行状态
- global g_status_list, g_times
- try:
- if not msg == "":
- # 添加新的状态消息和时间到列表
- timestamp = datetime.now().strftime('%H:%M:%S') # 获取当前时间
- status_entry = {'msg': msg, 'time': timestamp} # 存储消息和时间
- g_status_list.append(status_entry)
- # 如果列表超过 5 条,移除最早的一条
- if len(g_status_list) > 5:
- g_status_list.pop(0)
- else:
- sendStr = ''
- for item in g_status_list:
- sendStr = sendStr + f"{g_times}次-{item['time']}-{item['msg']}<br>"
- #print(sendStr)
- emit('processing_status', sendStr)
-
- # 如果消息是 "结束",发送所有状态并清空列表
- if msg == "结束":
- g_status_list = [] # 清空列表
- event.clear()
- except Exception as e:
- print(f"Error in send_status: {e}")
- return
- @socketio.on('monitor_begin')
- def monitor_begin():
- global last_time, last_process
- current_time = time.time()
- elapsed_time = current_time - last_time
- if elapsed_time < 0.5:
- return
- last_time = current_time
- regionRet, regionPos = game_region()
- screenshot = pyautogui.screenshot(region=regionPos)
- #binary_img = binarize_image(screenshot)
- compressed_data = compress_image(screenshot)
-
- image_data_base64 = base64.b64encode(compressed_data).decode('utf-8')
- socketio.emit('image_data', image_data_base64)
- task_arr = []
- if not event.is_set():
- task_arr.append(last_process)
- for item in reversed(task_queue):
- task_arr.append(item.name)
- send_hint(json.dumps(task_arr, ensure_ascii=False))
- send_todo()
- send_status('')
- #print("send img")
- @socketio.on('end_script')
- def handle_end_script():
- event.set()
- @socketio.on('end_game')
- def handle_end_game():
- event.set()
- task_close_game()
- send_status("结束2")
- event.clear()
- @socketio.on('get_title')
- def handle_get_title():
- str = task_getComputerName()
- dst = str + ' machine'
- emit('processing_title', dst)
- @socketio.on('reset_script')
- def handle_reset_script():
- python = sys.executable
- while '--reset' in sys.argv:
- # 从 sys.argv 列表中删除 --reset 参数
- sys.argv.remove('--reset')
- os.execl(python, python, *sys.argv)
- @socketio.on('restart_game')
- def handle_restart_game():
- python = sys.executable
- os.execl(python, python, *sys.argv, '--reset')
- @socketio.on('close_game')
- def handle_close_game():
- task_close_game()
- send_status("结束2")
- event.clear()
- @socketio.on('read_cfg')
- def handle_read_cfg():
- cfg = read_cfg()
- emit('processing_cfg', cfg)
- def restart_game():
- global isGameBegin
- isGameBegin = False
- while True:
- task_close_game()
- if True == task_start_game():
- break
- else:
- send_status("启动失败")
- isGameBegin = True
- send_status("结束")
- config = read_cfg()
- print("config", config)
- auto_task(config)
- def auto_participate():
- task_queue.appendleft(task_returnAllLine())
- timeout = 40 * 60
- start_time = time.time() # 记录开始时间
- while not event.is_set():
- if len(task_queue) < 4:
- task_queue.appendleft(task_paticipateInTeam())
- task_queue.appendleft(task_paticipateInTeam())
- task_queue.appendleft(task_paticipateInTeam())
- task_queue.appendleft(task_checkHelp(True))
- myTimeSleep_big()
- # 每次循环检查已用时间
- current_time = time.time()
- elapsed_time = current_time - start_time
-
- if elapsed_time >= timeout:
- handle_restart_game()
- break
- def add_auto_task(isMaxCollect, isJina, isSimple = False, isAddStrengh = False, activity = 'None', isAutoParticipate = True, isDailyConfig = False, train_type = 'None', always = False):
- global g_times, g_cureNum
- collectArr = [int(x) for x in isMaxCollect.split(",")]
- print("collectArr", collectArr)
- while not event.is_set():
- isLoginTask = True
- fight_big_monster_times = 0
- config = read_Dailycfg()
- print("config", config)
-
- if check_daily_config(config):
- today = datetime.now().strftime('%Y-%m-%d')
- isLoginTask = bool(config['daily'][today]["login_task"])
- fight_big_monster_times = int(config['daily'][today]["fight_bigMonster_times"])
- else:
- set_login_task(config, False)
- set_fight_big_monster_times(config, 0)
- clean_old_daily_configs(config)
- isLoginTask = False
- fight_big_monster_times = 0
- write_Dailycfg(config)
- send_status(f"isLoginTask:{isLoginTask}, fight_big_monster_times:{fight_big_monster_times}")
- if not isLoginTask:
- task_queue.appendleft(task_checkMaster())
- set_login_task(config, True)
- write_Dailycfg(config)
-
- if isDailyConfig and fight_big_monster_times < 10:
- isSuccess = task_fightMonster(isAddStrengh, True, isSimple)
- if isSuccess:
- set_fight_big_monster_times(config, fight_big_monster_times + 1)
- write_Dailycfg(config)
-
- task_queue.appendleft(task_information())
- if activity == 'lianmeng':
- task_queue.appendleft(task_activity_lianmeng())
- if activity == 'cure':
- task_queue.appendleft(task_cure(True, g_cureNum))
- task_queue.appendleft(check_buildOrResearch())
-
- task_queue.appendleft(task_cure(False, g_cureNum))
- task_queue.appendleft(task_checkStoreRoom())
- if not isSimple:
- if isJina == 'jina':
- task_queue.appendleft(task_fight_jina(isAddStrengh))
- elif isJina == 'yongbing':
- task_queue.appendleft(task_fight_yongbing(isAddStrengh))
- elif isJina == 'monster':
- task_queue.appendleft(task_fightMonster(isAddStrengh, False, isSimple))
- elif isJina == 'big_monster':
- task_queue.appendleft(task_fightMonster(isAddStrengh, True, isSimple))
- elif isJina == 'jina_call':
- task_queue.appendleft(task_call_jina())
- task_queue.appendleft(task_collect(collectArr, isSimple, isAddStrengh))
- task_queue.appendleft(task_train(train_type))
- task_queue.appendleft(task_checkActivities())
- if not isAddStrengh: # 如果不是添加体力,则添加一次采集
- task_queue.appendleft(task_collect(collectArr, isSimple, isAddStrengh))
-
- task_queue.appendleft(task_cure(False, g_cureNum))
- if isSimple:
- task_queue.appendleft(check_buildOrResearch())
- else:
- if isJina == 'monster' and isAddStrengh:
- task_queue.appendleft(task_fightMonster(isAddStrengh, False, isSimple))
- else:
- task_queue.appendleft(task_collect(collectArr, isSimple, isAddStrengh))
-
- g_times += 1
- if g_times % 3 == 0:
- task_queue.appendleft(task_checkDonata())
- task_queue.appendleft(task_checkAdventure())
- task_queue.appendleft(task_train(train_type))
- task_queue.appendleft(task_useAnnimalSkill())
- task_queue.appendleft(task_checkHelp(False))
- if auto_participate:
- task_queue.appendleft(task_checkConfilits())
- if always == False and g_times == 7:
- handle_end_game()
- if isAddStrengh:
- myTimeSleep(random.randint(350, 400), send_status)
- else:
- myTimeSleep(random.randint(1000, 2000), send_status)
- handle_restart_game()
- else:
- if isAddStrengh:
- myTimeSleep(random.randint(300, 350), send_status)
- else:
- myTimeSleep(random.randint(400, 500), send_status)
-
-
- task_queue.clear()
- send_status(f'自动模式结束')
- event.clear()
- daily_config = {
- "login_task": False,
- "fight_bigMonster_times": 0
- }
- def check_daily_config(config):
- today = datetime.now().strftime('%Y-%m-%d') # 获取当前日期
- print(f"Today: {today}") # 打印当前日期
- print(f"Config: {config}") # 打印传入的配置
-
- if "daily" not in config:
- print("Daily key not found, creating it.") # 调试信息
- config["daily"] = {}
- return False
-
- if today not in config["daily"]:
- print(f"Today's config not found: {today}") # 调试信息
- return False
- else:
- print(f"Today's config found: {today}") # 调试信息
- return True
-
- # 修改或添加 "login_task" 的值
- def set_login_task(config, value):
- today = datetime.now().strftime('%Y-%m-%d') # 获取当前日期
- if today not in config["daily"]: # 如果当天的配置不存在
- config["daily"][today] = {} # 创建当天的配置
- config["daily"][today]["login_task"] = value # 设置或更新 "login_task"
- return config
- # 修改或添加 "fight_bigMonster_times" 的值
- def set_fight_big_monster_times(config, value):
- today = datetime.now().strftime('%Y-%m-%d') # 获取当前日期
- if today not in config["daily"]: # 如果当天的配置不存在
- config["daily"][today] = {} # 创建当天的配置
- config["daily"][today]["fight_bigMonster_times"] = value # 设置或更新 "fight_bigMonster_times"
- return config
- def add_today_daily_config(config, daily_config, overwrite=False):
- today = datetime.now().strftime('%Y-%m-%d') # 获取当前日期
- if today not in config["daily"] or overwrite: # 如果不存在或允许覆盖
- config["daily"][today] = daily_config # 添加或更新
- return config
- # 清理非当天的每日配置
- def clean_old_daily_configs(config):
- today = datetime.now().strftime('%Y-%m-%d') # 获取当前日期
- keys_to_remove = [key for key in config["daily"] if key != today] # 找到非当天的每日配置
- for key in keys_to_remove:
- del config["daily"][key] # 删除非当天的每日配置
- return config
- def write_cfg(config):
- with open('config.json', 'w') as config_file:
- json.dump(config, config_file, indent=4)
- def read_cfg():
- try:
- with open('config.json', 'r') as config_file:
- config = json.load(config_file)
- return config
- except FileNotFoundError:
- print("配置文件不存在,请检查文件路径。")
- return None
- except PermissionError:
- print("没有权限读取配置文件。")
- return None
- except json.JSONDecodeError:
- print("配置文件格式错误,请检查文件内容是否为有效的 JSON。")
- return None
-
- def write_Dailycfg(config):
- with open('daily.json', 'w') as config_file:
- json.dump(config, config_file, indent=4)
- def read_Dailycfg():
- try:
- with open('daily.json', 'r') as config_file:
- config = json.load(config_file)
- return config
- except FileNotFoundError:
- print("配置文件不存在,请检查文件路径。")
- return None
- except PermissionError:
- print("没有权限读取配置文件。")
- return None
- except json.JSONDecodeError:
- print("配置文件格式错误,请检查文件内容是否为有效的 JSON。")
- return None
- @socketio.on('begin_auto')
- def handle_auto(data):
- write_cfg(data)
- config = read_cfg()
- print("config", config)
- auto_task(config)
- def auto_task(data):
- global autoTask, g_cureNum
- if data == None:
- isMaxCollect = '4,3,2,1'
- isSimple = False
- isJina = 'jina'
- isAddStrengh = False
- activity = 'none'
- participateJijie = False
- auto_daily = False
- train_type = 'none'
- always = False
- cureNumber = 500
- else:
- isMaxCollect = data['maxCollect']
- isSimple = data['simple']
- isJina = data['jina']
- isAddStrengh = data['add_strength']
- activity = data['activity']
- participateJijie = data['participate_jijie']
- auto_daily = data['auto_daily']
- train_type = data['train']
- always = data['always']
- cureNumber = data['cureNumber']
- g_cureNum = cureNumber
- send_status(f'开始自动模式')
- executor.submit(add_auto_task, isMaxCollect, isJina, isSimple, isAddStrengh, activity, participateJijie, auto_daily, train_type, always)
- @socketio.on('begin_auto_participate')
- def handle_auto_participate():
- global autoTask
- send_status(f'开始自动集结模式')
- executor.submit(auto_participate)
- if __name__ == '__main__':
- init()
- if '--reset' in sys.argv:
- isReset = True
- print("需要重启游戏")
- runTask = threading.Thread(target=thread_runTask)#启动线程往里面添加任务
- runTask.daemon = True
- runTask.start()
-
- socketio.run(app, host= '0.0.0.0', debug=True)
|