Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zlh #24

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Zlh #24

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ backup.md
test*
*.json
*.log
*.txt
!requirements.txt
131 changes: 116 additions & 15 deletions FSM_action.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import random
import sys
import time

import zlh
import keyboard

import click
import get_screen
from strategy import StrategyState
from log_state import *

import threading
import strategy
FSM_state = ""
time_begin = 0.0
game_count = 0
Expand All @@ -17,6 +18,42 @@
log_state = LogState()
log_iter = log_iter_func(HEARTHSTONE_POWER_LOG_PATH)
choose_hero_count = 0
data_x = []
data_y = []


def dump_data_x(data_x):
with open('data_x.txt', 'w') as file:
for item in data_x:
file.write(json.dumps(item) + '\n')

def dump_data_y(data_y):
with open('data_y.txt', 'w') as file:
for item in data_y:
file.write(json.dumps(item) + '\n')

def read_data_x():
with open('data_x.txt', 'r') as file:
for line in file:
item = json.loads(line)
data_x.append(item)
return data_x


def read_data_y():
with open('data_y.txt', 'r') as file:
for line in file:
item = json.loads(line)
data_y.append(item)
return data_y

# 用于接收输入的线程函数
def input_thread_func():
keyboard.add_hotkey("ctrl+q", system_exit)
while True:
# 获取用户输入
strategy.user_input = input("请输入数字: ")
print("user_input is :", strategy.user_input)


def init():
Expand Down Expand Up @@ -44,11 +81,13 @@ def init():
log_state = LogState()
log_iter = log_iter_func(HEARTHSTONE_POWER_LOG_PATH)
choose_hero_count = 0

input_thread = threading.Thread(target=input_thread_func)
input_thread.start()

def update_log_state():
log_container = next(log_iter)
if log_container.log_type == LOG_CONTAINER_ERROR:
print("err in log_container.log_type")
return False

for log_line_container in log_container.message_list:
Expand All @@ -63,7 +102,8 @@ def update_log_state():
# 注意如果Power.log没有更新, 这个函数依然会返回. 应该考虑到game_state只是被初始化
# 过而没有进一步更新的可能
if log_state.game_entity_id == 0:
return False
print("err in log_state.game_entity_id")
# return False

return True

Expand Down Expand Up @@ -107,7 +147,7 @@ def print_out():

def ChoosingHeroAction():
global choose_hero_count

print("==========================in ChoosingHeroAction==========================")
print_out()

# 有时脚本会卡在某个地方, 从而在FSM_Matching
Expand All @@ -127,7 +167,7 @@ def ChoosingHeroAction():
def MatchingAction():
print_out()
loop_count = 0

print("==========================in MatchingAction==========================")
while True:
if quitting_flag:
sys.exit(0)
Expand All @@ -154,7 +194,7 @@ def MatchingAction():
def ChoosingCardAction():
global choose_hero_count
choose_hero_count = 0

print("===========================in ChoosingCardAction===========================")
print_out()
time.sleep(21)
loop_count = 0
Expand All @@ -165,12 +205,19 @@ def ChoosingCardAction():

if not ok:
return FSM_ERROR
strategy_state = StrategyState(log_state)
strategy_state.debug_print_out()
time.sleep(3)
if log_state.game_num_turns_in_play > 0:
return FSM_BATTLING
if log_state.is_end:
return FSM_QUITTING_BATTLE

strategy_state = StrategyState(log_state)
strategy_state.debug_print_out()
time.sleep(3)
return FSM_BATTLING

hand_card_num = strategy_state.my_hand_card_num

# 等待被替换的卡牌 ZONE=HAND
Expand Down Expand Up @@ -210,7 +257,7 @@ def Battling():
global log_state

print_out()

print("===========================in Battling===========================")
not_mine_count = 0
mine_count = 0
last_controller_is_me = False
Expand All @@ -221,6 +268,7 @@ def Battling():

ok = update_log_state()
if not ok:
print("===========================update_log_state err in Battling===========================")
return FSM_ERROR

if log_state.is_end:
Expand All @@ -230,7 +278,54 @@ def Battling():
else:
info_print("你输了")
return FSM_QUITTING_BATTLE

strategy_state = StrategyState(log_state)
input_info = strategy_state.debug_print_out()
print("input_info is :", input_info)
time.sleep(1)
# return FSM_BATTLING
# if len(input_info) == 3:
if len(input_info) >= 2 and len(input_info[-1]) == 3:
print("action from user_input: ", input_info)
get_index = input_info[-1][0]
put_index = input_info[-1][1]
point_index = input_info[-1][2]
if get_index == -1:
click.end_turn()
data_x.append(input_info[:-1])
data_y.append(input_info[-1])
dump_data_x(data_x)
dump_data_y(data_y)
return FSM_BATTLING
if get_index <= (strategy_state.my_hand_card_num) and get_index >= 1:
print(1)
# strategy_state.use_best_entity(get_index, [1, put_index, point_index])
click.choose_card(get_index - 1, strategy_state.my_hand_card_num)
if isinstance(strategy_state.my_hand_cards[get_index -1], StrategyMinion) and put_index >= 1 and put_index <= 7:
click.put_minion(put_index - 1, strategy_state.my_minion_num)
if point_index == 0 and isinstance(strategy_state.my_hand_cards[get_index -1], StrategySpell):
click.choose_my_minion(0, 1);
elif get_index == 0:
# zlh.test_battle_hero_power_click()
hero_power = strategy_state.my_detail_hero_power
hero_power.use_with_arg(strategy_state, -1, [])
else:
click.choose_my_minion(get_index - strategy_state.my_hand_card_num - 1, strategy_state.my_minion_num)

if point_index >= 1 and point_index <= 7:
click.choose_opponent_minion(point_index - 1, strategy_state.oppo_minion_num)
elif point_index == 8:

click.choose_oppo_hero()
# TODO mk func choose card to put && point
data_x.append(input_info[:-1])
data_y.append(input_info[-1])
dump_data_x(data_x)
dump_data_y(data_y)
# with open('data_x.txt', 'w') as file:
# json.dump(data_x, file)
# with open('data_y.txt', 'w') as file:
# json.dump(data_y, file)
return FSM_BATTLING
# 在对方回合等就行了
if not log_state.is_my_turn:
last_controller_is_me = False
Expand Down Expand Up @@ -295,10 +390,11 @@ def Battling():

def QuittingBattle():
print_out()

print("==========================in QuittingBattle==========================")
time.sleep(5)

loop_count = 0
return FSM_CHOOSING_CARD
while True:
if quitting_flag:
sys.exit(0)
Expand All @@ -319,7 +415,7 @@ def QuittingBattle():

def GoBackHSAction():
global FSM_state

print("==========================in GoBackHSAction==========================")
print_out()
time.sleep(3)

Expand All @@ -336,6 +432,7 @@ def GoBackHSAction():


def MainMenuAction():
print("==========================in MainMenuAction==========================")
print_out()

time.sleep(3)
Expand All @@ -359,6 +456,7 @@ def MainMenuAction():


def WaitMainMenu():
print("==========================in WaitMainMenu==========================")
print_out()
while get_screen.get_state() != FSM_MAIN_MENU:
click.click_middle()
Expand All @@ -367,6 +465,7 @@ def WaitMainMenu():


def HandleErrorAction():
print("==========================in HandleErrorAction==========================")
print_out()

if not get_screen.test_hs_available():
Expand Down Expand Up @@ -412,7 +511,9 @@ def AutoHS_automata():
hs_hwnd = get_screen.get_HS_hwnd()
get_screen.move_window_foreground(hs_hwnd)
time.sleep(0.5)

# FSM_state = FSM_CHOOSING_CARD
read_data_x()
read_data_y()
while 1:
if quitting_flag:
sys.exit(0)
Expand All @@ -421,7 +522,7 @@ def AutoHS_automata():
FSM_state = FSM_dispatch(FSM_state)


if __name__ == "__main__":
keyboard.add_hotkey("ctrl+q", system_exit)
# if __name__ == "__main__":
# keyboard.add_hotkey("ctrl+q", system_exit)

init()
# init()
2 changes: 1 addition & 1 deletion card/hero_power_card.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import click
# import click
from card.basic_card import *


Expand Down
Loading