-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
171 lines (134 loc) · 3.98 KB
/
main.py
File metadata and controls
171 lines (134 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
Puzzles & Survival Bot - Main Entry Point
A game automation bot for Puzzles & Survival.
"""
import argparse
import sys
from typing import Optional
from src.bot import PuzzlesSurvivalBot
from src.logger import get_logger
def create_parser() -> argparse.ArgumentParser:
"""
Create command-line argument parser.
Returns:
Configured ArgumentParser
"""
parser = argparse.ArgumentParser(
description="Puzzles & Survival Bot - Automate game actions",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py --action train --times 5
python main.py --action help --times 3
python main.py --action heal --times 2
python main.py --debug
Actions:
train - Train troops with speedup
heal - Heal wounded troops
help - Help alliance members
gather - Gather resources (food)
check - Check if game window is available
"""
)
parser.add_argument(
'--action',
type=str,
choices=['train', 'heal', 'help', 'gather', 'check'],
help='Action to perform'
)
parser.add_argument(
'--times',
type=int,
default=1,
help='Number of times to repeat the action (default: 1)'
)
parser.add_argument(
'--debug',
action='store_true',
help='Enable debug logging'
)
parser.add_argument(
'--window-title',
type=str,
default=None,
help='Game window title (default: "Puzzles & Survival")'
)
return parser
def run_action(bot: PuzzlesSurvivalBot, action: str, times: int) -> Optional[dict]:
"""
Execute a bot action.
Args:
bot: Bot instance
action: Action to perform
times: Number of times to repeat
Returns:
Result dictionary or None
"""
action_map = {
'train': bot.train_troops,
'heal': bot.heal_troops,
'help': bot.help_alliance,
'gather': bot.gather_resources,
}
if action == 'check':
bot.check_game_window()
return None
if action in action_map:
return action_map[action](times)
return None
def print_results(action: str, results: Optional[dict]):
"""
Print action results.
Args:
action: Action that was performed
results: Results dictionary
"""
if results is None:
return
print("\n" + "=" * 50)
print(f"Action: {action.upper()}")
print("=" * 50)
print(f"Status: {'SUCCESS' if results.get('success') else 'FAILED'}")
print(f"Completed: {results.get('completed', 0)}")
print(f"Failed: {results.get('failed', 0)}")
print("=" * 50 + "\n")
def main():
"""Main entry point for the bot."""
parser = create_parser()
args = parser.parse_args()
# If no action specified, print help
if not args.action:
parser.print_help()
return 0
logger = get_logger()
try:
# Initialize bot
logger.info("Starting Puzzles & Survival Bot")
bot = PuzzlesSurvivalBot(
window_title=args.window_title,
debug=args.debug
)
# Check window availability first
if not bot.check_game_window():
logger.error("Cannot proceed - game window not found")
print("\nError: Game window not found. Please ensure Puzzles & Survival is running.")
return 1
# Run the action
results = run_action(bot, args.action, args.times)
# Print results
print_results(args.action, results)
# Shutdown
bot.shutdown()
logger.info("Bot execution completed")
return 0
except KeyboardInterrupt:
logger.info("Bot stopped by user")
print("\n\nBot stopped by user (Ctrl+C)")
return 0
except Exception as e:
logger.error(f"Fatal error: {e}", exc_info=True)
print(f"\n\nFatal error: {e}")
print("Check bot.log for details")
return 1
if __name__ == '__main__':
sys.exit(main())