-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbattle.py
More file actions
1352 lines (1162 loc) · 49.7 KB
/
Copy pathbattle.py
File metadata and controls
1352 lines (1162 loc) · 49.7 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
洛克王国战斗模拟器 — 主入口
菜单:
1. 开始对战 — 从队伍列表各选一支,AI 自战并输出日志
2. 新建队伍 — 交互式组队并保存到列表
3. 管理队伍 — 查看详情 / 删除 / 重命名
4. 批量模拟 — 选两支队伍,跑 N 场,输出胜率统计
0. 返回
"""
import sys
import os
import random
import time
from typing import List, Optional, Callable
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from sim.pokemon import Pokemon
from sim.pokemon_db import load_pokemon_db
from sim.skill_db import load_skills, get_learnable_skills
from sim.battle_state import BattleState
from sim.battle_engine import BattleEngine
from sim.team_builder_interactive import build_team_interactive
from sim.team_roster import (
list_teams, build_team, add_team, delete_team, rename_team, get_team_def
)
from sim.mcts_agent import MCTSAgent
from sim.strategy import get_starter_idx
from sim.human_agent import HumanAgent
# MCTS 每回合迭代次数(可调整:20 快速 / 100 标准 / 200 强力)
_MCTS_ITERS_BATTLE = 100 # 单局对战
_MCTS_ITERS_BATCH = 20 # 批量模拟(优先速度,仍有学习效果)
_IMPORT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "import_images")
_IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp", ".gif"}
SEP = "=" * 56
LINE = "─" * 56
# ============================================================
# 显示工具
# ============================================================
def _hp_bar(current: int, maximum: int, width: int = 10) -> str:
if maximum <= 0:
return f"[{'?' * width}] ---/---"
pct = max(0.0, min(100.0, current / maximum * 100))
filled = int(pct / (100 / width))
return f"[{'#' * filled}{'.' * (width - filled)}] {current:4}/{maximum}"
def _status_flags(p: Pokemon) -> str:
parts = []
if p.burn_stacks: parts.append(f"烧{p.burn_stacks}")
if p.poison_stacks: parts.append(f"毒{p.poison_stacks}")
if p.freeze_stacks: parts.append(f"冻{p.freeze_stacks}")
return " ".join(parts)
def _ability_tag(p: Pokemon) -> str:
"""精灵特性标签,无特性则返回空串"""
return f" [{p.ability}]" if p.ability else ""
def _print_field(state: BattleState, label_a: str, label_b: str) -> None:
pa, pb = state.get_current("a"), state.get_current("b")
weather_str = f" 天气:{state.weather.value}" if state.weather.value != "none" else ""
print(f"\n{LINE} 回合 {state.turn}{weather_str}")
print(f" {label_a}: {pa.name:<10}{_ability_tag(pa)} {_hp_bar(pa.current_hp, pa.hp)} 能量:{pa.energy:2} {_status_flags(pa)}")
print(f" {label_b}: {pb.name:<10}{_ability_tag(pb)} {_hp_bar(pb.current_hp, pb.hp)} 能量:{pb.energy:2} {_status_flags(pb)}")
def _print_team_summary(label: str, team: List[Pokemon]) -> None:
print(f"\n {label}:")
for p in team:
s = "FAINTED" if p.is_fainted else f"HP {p.current_hp}/{p.hp}"
print(f" {p.name:<12} {s}")
# ============================================================
# 队伍列表显示
# ============================================================
def _print_roster(header: str = "队伍列表") -> None:
teams = list_teams()
print(f"\n {header}(共 {len(teams)} 支):")
for i, t in enumerate(teams, 1):
tag = "[预设]" if t.get("preset") else "[自定]"
members = " ".join(m["pokemon"] for m in t["members"])
print(f" {i:2}. {tag} {t['name']:<14} {members}")
def _pick_team(prompt: str = "选择队伍序号") -> Optional[str]:
"""显示名册,让用户选一支队伍,返回队伍名;输入 0 取消返回 None"""
_print_roster()
teams = list_teams()
print(f"\n {prompt}(0 取消):", end="")
raw = input().strip()
if raw == "0" or raw == "":
return None
if raw.isdigit():
idx = int(raw) - 1
if 0 <= idx < len(teams):
return teams[idx]["name"]
print(" [!] 无效序号")
return None
# ============================================================
# 核心:单场对战 — 接受任意 AgentProtocol 组合
# ============================================================
def run_battle(
team_a: List[Pokemon],
team_b: List[Pokemon],
label_a: str = "A队",
label_b: str = "B队",
verbose: bool = True,
agent_a=None, # AgentProtocol 实例;None=自动创建 MCTSAgent
agent_b=None, # AgentProtocol 实例;None=自动创建 MCTSAgent
) -> Optional[str]:
"""
运行一场对战,支持任意智能体组合。
Parameters
----------
agent_a / agent_b : AgentProtocol — 自定义智能体(MCTS/Human/LLM)
None 时自动创建 MCTSAgent
Returns
-------
"a" / "b" / None(平局/超时)
"""
from sim.agent_base import AgentProtocol
if agent_a is None:
agent_a = MCTSAgent("a", label_a, iterations=_MCTS_ITERS_BATTLE)
if agent_b is None:
agent_b = MCTSAgent("b", label_b, iterations=_MCTS_ITERS_BATTLE)
starter_a = get_starter_idx(getattr(agent_a, 'strategy', None), team_a) or 0
starter_b = get_starter_idx(getattr(agent_b, 'strategy', None), team_b) or 0
state = BattleState(team_a=team_a, team_b=team_b,
current_a=starter_a, current_b=starter_b)
engine = BattleEngine(state, verbose=True) # 引擎日志始终打开
engine.label_a = label_a
engine.label_b = label_b
history = []
# 判断是否有人类玩家(人类参与时引擎日志关闭,由 HumanAgent 打印)
has_human = (
isinstance(agent_a, AgentProtocol) and getattr(agent_a, 'show_team_status', False)
or isinstance(agent_b, AgentProtocol) and getattr(agent_b, 'show_team_status', False)
)
if verbose and not has_human:
print(f"\n{SEP}")
ab_a = f" [{team_a[starter_a].ability}]" if team_a[starter_a].ability else ""
ab_b = f" [{team_b[starter_b].ability}]" if team_b[starter_b].ability else ""
print(f" {label_a}({agent_a.__class__.__name__}) VS {label_b}({agent_b.__class__.__name__})")
skills_a = [s.name for s in team_a[starter_a].skills]
skills_b = [s.name for s in team_b[starter_b].skills]
print(f" 先锋: {team_a[starter_a].name}{ab_a}[{', '.join(skills_a)}]")
print(f" vs {team_b[starter_b].name}{ab_b}[{', '.join(skills_b)}]")
print(SEP)
winner = None
for _ in range(BattleEngine.MAX_TURNS):
winner = engine.check_winner()
if winner:
break
if verbose and not has_human:
_print_field(state, label_a, label_b)
snap = state.deep_copy()
action_a = agent_a.choose_action(engine)
action_b = agent_b.choose_action(engine)
history.append((snap, action_a, action_b))
engine.execute_turn(action_a, action_b)
if not winner:
winner = engine.check_winner()
# 记录经验 — 通过统一协议调用
agent_a.on_game_end(history, winner)
agent_b.on_game_end(history, winner)
if verbose:
tag = f"{label_a} 赢!" if winner == "a" else (f"{label_b} 赢!" if winner == "b" else "平局/超时")
print(f"\n{SEP}")
print(f" 结果:{tag} (共 {state.turn} 回合)")
_print_team_summary(label_a, team_a)
_print_team_summary(label_b, team_b)
print(SEP)
return winner
# ============================================================
# 批量模拟
# ============================================================
def run_batch(
factory_a: Callable[[], List[Pokemon]],
factory_b: Callable[[], List[Pokemon]],
label_a: str,
label_b: str,
n: int,
) -> None:
results = {"a": 0, "b": 0, "draw": 0}
total_turns = 0
t0 = time.time()
agent_a = MCTSAgent("a", label_a, iterations=_MCTS_ITERS_BATCH)
agent_b = MCTSAgent("b", label_b, iterations=_MCTS_ITERS_BATCH)
for i in range(n):
ta, tb = factory_a(), factory_b()
sa = get_starter_idx(agent_a.strategy, ta) or 0
sb = get_starter_idx(agent_b.strategy, tb) or 0
state = BattleState(team_a=ta, team_b=tb, current_a=sa, current_b=sb)
engine = BattleEngine(state, verbose=False)
history = []
winner = None
for _ in range(BattleEngine.MAX_TURNS):
winner = engine.check_winner()
if winner:
break
snap = state.deep_copy()
action_a = agent_a.choose_action(engine)
action_b = agent_b.choose_action(engine)
history.append((snap, action_a, action_b))
engine.execute_turn(action_a, action_b)
if not winner:
winner = engine.check_winner()
agent_a.experience_db.record_game(history, winner)
agent_b.experience_db.record_game(history, winner)
total_turns += state.turn
results[winner or "draw"] += 1
elapsed_so_far = time.time() - t0
rate = elapsed_so_far / (i + 1)
eta = rate * (n - i - 1)
bar_filled = int(20 * (i + 1) / n)
bar = "#" * bar_filled + "." * (20 - bar_filled)
print(
f"\r [{bar}] {i+1:4}/{n} "
f"A:{results['a']} B:{results['b']} 平:{results['draw']} "
f"ETA:{eta:.0f}s ",
end="", flush=True,
)
agent_a.save()
agent_b.save()
elapsed = time.time() - t0
print(f"\n{SEP}")
print(f" 批量模拟结果({n} 场,MCTS×{_MCTS_ITERS_BATCH})")
print(f" {label_a} 胜: {results['a']:4} 场 ({results['a']/n*100:.1f}%)")
print(f" {label_b} 胜: {results['b']:4} 场 ({results['b']/n*100:.1f}%)")
print(f" 平局: {results['draw']:4} 场 ({results['draw']/n*100:.1f}%)")
print(f" 平均回合数: {total_turns/n:.1f}")
print(f" 总耗时: {elapsed:.2f}s ({elapsed/n*1000:.1f}ms/场)")
print(SEP)
# ============================================================
# 菜单:1. 开始对战
# ============================================================
def _menu_battle() -> None:
print(f"\n{SEP}")
print(" 选择对战模式")
print(f" 1. AI vs AI(MCTS自战)")
print(f" 2. 人 vs AI(你控制A队)")
print(f" 3. 人对人(你选A队,对方键盘输入B队)")
print(f" 4. LLM vs AI(大模型对战MCTS)")
print(f" 5. LLM vs 人类(大模型对战你)")
raw = input(" 选择 [1-5](默认 2):").strip()
mode = int(raw) if raw.isdigit() and 1 <= int(raw) <= 5 else 2
print(f"\n 开始对战 — 选择 A 队")
name_a = _pick_team("A 队序号")
if name_a is None:
return
print(f"\n A 队:{name_a}")
print(f" 选择 B 队")
name_b = _pick_team("B 队序号")
if name_b is None:
return
team_a = build_team(name_a)
team_b = build_team(name_b)
# ── 根据模式创建 Agent ───────────────────────
agent_a = None
agent_b = None
if mode == 1:
# AI vs AI — 默认行为,不传 agent
pass
elif mode == 2:
# 人 vs AI
agent_a = HumanAgent("a", name_a)
elif mode == 3:
# 人对人
agent_a = HumanAgent("a", name_a)
agent_b = HumanAgent("b", name_b)
elif mode == 4:
# LLM vs AI
try:
from sim.llm_agent import LLMAgent
agent_a = LLMAgent("a", name_a)
except EnvironmentError as e:
print(f"\n [!] {e}")
return
elif mode == 5:
# LLM vs 人类
try:
from sim.llm_agent import LLMAgent
agent_a = LLMAgent("a", name_a)
except EnvironmentError as e:
print(f"\n [!] {e}")
return
agent_b = HumanAgent("b", name_b)
run_battle(
team_a, team_b,
label_a=name_a, label_b=name_b,
verbose=True,
agent_a=agent_a,
agent_b=agent_b,
)
# ============================================================
# 菜单:2. 新建队伍
# ============================================================
def _menu_new_team() -> None:
print(f"\n{SEP}")
print(" 新建队伍")
print(" 输入队伍名称(留空取消):", end="")
name = input().strip()
if not name:
print(" 已取消")
return
# 检查是否与预设同名
existing = get_team_def(name)
if existing and existing.get("preset"):
print(f" [!] 「{name}」是内置预设名,请换一个名称")
return
if existing:
print(f" 队伍「{name}」已存在,继续将覆盖原内容。确认?(y/N):", end="")
if input().strip().lower() != "y":
print(" 已取消")
return
# 交互组队
pokemon_list = build_team_interactive(name)
# 从 Pokemon 对象提取成员定义
members = [
{"pokemon": p.name, "skills": [s.name for s in p.skills]}
for p in pokemon_list
]
result = add_team(name, members)
verb = "已覆盖" if result == "replaced" else "已保存"
print(f"\n 队伍「{name}」{verb}!(共 {len(members)} 只精灵)")
# ============================================================
# 菜单:3. 管理队伍
# ============================================================
def _edit_team(team_name: str) -> None:
"""交互式编辑队伍中任意精灵的技能和性格,完成后保存。"""
from sim.skill_db import get_learnable_skills, get_all_skills
from sim.team_roster import get_team_def
from sim.pokemon_db import (get_nature, nature_display, NATURES,
compute_stats_with_nature, _STAT_KEY_DISPLAY)
team_def = get_team_def(team_name)
if team_def is None:
print(f" [!] 找不到队伍「{team_name}」")
return
# 深复制成员列表(保留 nature 字段)
members = [
{"pokemon": m["pokemon"], "skills": list(m["skills"]),
"nature": m.get("nature")}
for m in team_def["members"]
]
all_skill_names = set(get_all_skills().keys())
# 25 种性格有序列表,方便按序号选择
_NATURE_LIST = list(NATURES.keys()) # 固定顺序
def _nat_str(m: dict) -> str:
"""返回该成员当前显示的性格字符串"""
custom = m.get("nature")
if custom:
return f"[{nature_display(custom)}*]" # * = 自定义
auto = get_nature(m["pokemon"])
return f"[{nature_display(auto)}]" if auto else ""
def _print_nature_table() -> None:
"""打印 25 种性格表(带序号,4 列)"""
print(" 25 种性格:")
cols = 4
for i, name in enumerate(_NATURE_LIST, 1):
pair = NATURES[name]
if pair[0]:
tag = f"{name}({_STAT_KEY_DISPLAY[pair[0]]}↑{_STAT_KEY_DISPLAY[pair[1]]}↓)"
else:
tag = f"{name}(无变化)"
print(f" {i:2}. {tag:<18}", end="\n" if i % cols == 0 else "")
if len(_NATURE_LIST) % cols != 0:
print()
while True:
print(f"\n{LINE}")
print(f" 编辑精灵配置 · 队伍「{team_name}」")
print(f" <序号>=编辑技能和性格 n<序号>=仅改性格 0=保存退出")
print(LINE)
for i, m in enumerate(members, 1):
ns = _nat_str(m)
skills_str = " | ".join(f"{j}:{s}" for j, s in enumerate(m["skills"], 1))
print(f" {i}. {m['pokemon']:<12}{ns} {skills_str}")
print(LINE)
print(" 输入:", end="")
raw = input().strip()
if raw == "0" or raw == "":
add_team(team_name, members)
print(f" 队伍「{team_name}」已保存。")
return
# ── 改性格 n<序号> ──────────────────────────────────────
if raw.lower().startswith("n") and raw[1:].isdigit():
poke_idx = int(raw[1:]) - 1
if not (0 <= poke_idx < len(members)):
print(" [!] 序号超出范围")
continue
member = members[poke_idx]
pname = member["pokemon"]
auto_nat = get_nature(pname) or "认真"
cur_nat = member.get("nature") or auto_nat
print(f"\n {pname} 当前性格:{nature_display(cur_nat)}"
+ ("(自动)" if not member.get("nature") else "(自定义*)"))
_print_nature_table()
print(f"\n 输入序号或性格名(回车=恢复自动 [{nature_display(auto_nat)}]):", end="")
cmd = input().strip()
if not cmd:
member["nature"] = None
print(f" 已恢复自动性格:{nature_display(auto_nat)}")
continue
# 尝试序号
new_nat = None
if cmd.isdigit() and 1 <= int(cmd) <= len(_NATURE_LIST):
new_nat = _NATURE_LIST[int(cmd) - 1]
elif cmd in NATURES:
new_nat = cmd
else:
# 模糊匹配
fuzzy = [n for n in NATURES if cmd in n]
if len(fuzzy) == 1:
new_nat = fuzzy[0]
print(f" → 模糊匹配:{new_nat}")
elif len(fuzzy) > 1:
print(f" [!] 匹配多个:{', '.join(fuzzy)},请更精确")
continue
else:
print(f" [!] 未找到性格「{cmd}」")
continue
member["nature"] = new_nat
new_stats = compute_stats_with_nature(pname, new_nat)
if new_stats:
print(f" {pname} 性格已设为 {nature_display(new_nat)} "
f"→ HP={new_stats['生命值']} 物攻={new_stats['物攻']} "
f"魔攻={new_stats['魔攻']} 物防={new_stats['物防']} "
f"魔防={new_stats['魔防']} 速度={new_stats['速度']}")
continue
# ── 改技能 <序号> ───────────────────────────────────────
if not raw.isdigit() or not (1 <= int(raw) <= len(members)):
print(" [!] 无效输入")
continue
poke_idx = int(raw) - 1
member = members[poke_idx]
pname = member["pokemon"]
learnable = get_learnable_skills(pname)
# 显示可学技能列表(仅显示一次)
print(f"\n {pname} 的可学技能(共 {len(learnable)} 个):")
cols = 3
for i, sname in enumerate(learnable, 1):
in_team = "✓" if sname in member["skills"] else " "
print(f" {i:3}.{in_team}{sname:<12}", end="\n" if i % cols == 0 else "")
if len(learnable) % cols != 0:
print()
# 技能编辑子循环:持续编辑直到回车空行返回
while True:
print(f"\n 当前技能: 1:{member['skills'][0]} 2:{member['skills'][1]}"
f" 3:{member['skills'][2]} 4:{member['skills'][3]}")
print(f" 输入替换(支持多个,如 1:喷火, 2:水刃,3:冰晶)回车=完成:", end="")
cmd = input().strip()
if not cmd:
break # 回到外层精灵选择循环
# 规范化:中文冒号→英文,中文逗号→英文
normalized = cmd.replace(":", ":").replace(",", ",")
# 拆分成多个 "槽位:技能" 对
pairs = [p.strip() for p in normalized.split(",") if p.strip()]
# 单个输入且不含冒号 → 可能是纯技能名(补全当前第一个空格或直接报错)
if len(pairs) == 1 and ":" not in pairs[0]:
print(" [!] 格式:槽位:技能,如 2:水刃 或 1:喷火, 2:冰晶")
continue
applied = []
errors = []
for pair in pairs:
if ":" not in pair:
errors.append(f"「{pair}」缺少冒号,跳过")
continue
slot_str, skill_input = pair.split(":", 1)
slot_str = slot_str.strip()
skill_input = skill_input.strip()
if not slot_str.isdigit() or not (1 <= int(slot_str) <= 4):
errors.append(f"「{slot_str}」槽位须为 1-4,跳过")
continue
slot = int(slot_str) - 1
# 解析技能:序号 → 技能名 → 精确名 → 模糊匹配
new_skill = None
if skill_input.isdigit():
si = int(skill_input) - 1
if 0 <= si < len(learnable):
new_skill = learnable[si]
else:
errors.append(f"槽{slot_str}:序号 {skill_input} 超出范围,跳过")
continue
elif skill_input in all_skill_names:
new_skill = skill_input
else:
fuzzy = [s for s in learnable if skill_input in s]
if len(fuzzy) == 1:
new_skill = fuzzy[0]
elif len(fuzzy) > 1:
errors.append(f"槽{slot_str}:「{skill_input}」匹配多个({', '.join(fuzzy[:4])}…),请更精确")
continue
else:
errors.append(f"槽{slot_str}:未找到技能「{skill_input}」")
continue
old = member["skills"][slot]
member["skills"][slot] = new_skill
match_note = f"(模糊→{new_skill})" if new_skill != skill_input and not skill_input.isdigit() else ""
applied.append(f" 槽{slot_str} {old} → {new_skill}{match_note}")
if applied:
print(" 已更新:")
for line in applied:
print(line)
for e in errors:
print(f" [!] {e}")
def _menu_manage() -> None:
while True:
_print_roster("队伍管理")
print(f"\n 操作:e<序号>=编辑精灵配置 r<序号>=重命名 d<序号>=删除 <序号>=查看详情 0=返回")
print(" 输入:", end="")
raw = input().strip()
if raw == "0" or raw == "":
return
# 编辑: e3
if raw.startswith("e") and raw[1:].isdigit():
idx = int(raw[1:]) - 1
teams = list_teams()
if 0 <= idx < len(teams):
t = teams[idx]
if t.get("preset"):
print(f" [!] 「{t['name']}」是内置预设,不可编辑")
else:
_edit_team(t["name"])
else:
print(" [!] 序号超出范围")
continue
# 删除: d3
if raw.startswith("d") and raw[1:].isdigit():
idx = int(raw[1:]) - 1
teams = list_teams()
if 0 <= idx < len(teams):
t = teams[idx]
if t.get("preset"):
print(f" [!] 「{t['name']}」是内置预设,不可删除")
else:
print(f" 确认删除「{t['name']}」?(y/N):", end="")
if input().strip().lower() == "y":
delete_team(t["name"])
print(f" 已删除「{t['name']}」")
else:
print(" [!] 序号超出范围")
continue
# 重命名: r3
if raw.startswith("r") and raw[1:].isdigit():
idx = int(raw[1:]) - 1
teams = list_teams()
if 0 <= idx < len(teams):
t = teams[idx]
if t.get("preset"):
print(f" [!] 「{t['name']}」是内置预设,不可重命名")
else:
print(f" 新名称(留空取消):", end="")
new_name = input().strip()
if new_name:
try:
rename_team(t["name"], new_name)
print(f" 已重命名为「{new_name}」")
except ValueError as e:
print(f" [!] {e}")
else:
print(" [!] 序号超出范围")
continue
# 查看详情
if raw.isdigit():
idx = int(raw) - 1
teams = list_teams()
if 0 <= idx < len(teams):
t = teams[idx]
tag = "[预设]" if t.get("preset") else "[自定]"
print(f"\n {tag} 队伍:{t['name']}")
from sim.pokemon_db import get_nature, nature_display
for i, m in enumerate(t["members"], 1):
custom = m.get("nature")
if custom:
nat_str = f" {nature_display(custom)}*"
else:
auto = get_nature(m["pokemon"])
nat_str = f" {nature_display(auto)}" if auto else ""
print(f" {i}. {m['pokemon']:<12}{nat_str}"
f" 技能:{', '.join(m['skills'])}")
input("\n 按 Enter 返回...") # ← 修复:暂停后再刷新列表
else:
print(" [!] 序号超出范围")
continue
print(" [!] 无效输入")
# ============================================================
# 菜单:4. 批量模拟
# ============================================================
def _menu_batch() -> None:
print(f"\n{SEP}")
print(" 批量模拟 — 选择 A 队")
name_a = _pick_team("A 队序号")
if name_a is None:
return
print(f" A 队:{name_a}")
print(" 选择 B 队")
name_b = _pick_team("B 队序号")
if name_b is None:
return
raw = input(" 模拟场数 N(默认 100):").strip()
n = int(raw) if raw.isdigit() and int(raw) > 0 else 100
# 选择模拟模式
print(f"\n 选择模拟模式:")
print(f" 1. 单线程批量模拟(兼容旧版)")
print(f" 2. 并发批量模拟+经验合并(推荐,更快)")
mode = input(" 选择 [1-2](默认 2):").strip()
if mode == "1":
# 旧版单线程模式
run_batch(
lambda: build_team(name_a),
lambda: build_team(name_b),
name_a, name_b, n,
)
else:
# 并发模拟模式
import multiprocessing as mp
workers = input(" 工作进程数(默认 CPU核心数):").strip()
if not workers.isdigit() or int(workers) < 1:
workers = mp.cpu_count()
else:
workers = min(int(workers), n)
from sim.batch_concurrent import run_concurrent_batch_with_experience
run_concurrent_batch_with_experience(
team_a_name=name_a,
team_b_name=name_b,
n=n,
workers=workers,
)
# ============================================================
# 菜单:5. 导入队伍(图片 / Wiki)
# ============================================================
def _menu_import() -> None:
"""导入队伍入口 — 让用户选择来源(图片识别 或 BWIKI 玩家配队)。"""
print(f"\n{SEP}")
print(" 导入队伍 — 选择来源")
print(SEP)
print(" 1. 从图片导入 (识别 import_images/ 下的标准组队分享图)")
print(" 2. 从 BWIKI 导入 (选用 data/lineups.json 中的玩家配队)")
print(" 0. 返回")
print(SEP)
try:
choice = input(" 选择 [0-2]: ").strip()
except (EOFError, KeyboardInterrupt):
return
if choice == "1":
_menu_import_from_image()
elif choice == "2":
_menu_import_from_wiki()
def _pick_parse_method() -> Optional[str]:
"""让用户选择解析方式,返回 'api' | 'ocr_rapid' | 'ocr_easy' | None"""
from sim.team_image_parser_ocr import get_available_engines
has_api_key = bool(os.environ.get("ANTHROPIC_API_KEY", ""))
ocr_engines = get_available_engines()
print(f"\n 选择识别方式:")
options = []
# API 选项
api_note = "(已配置 API Key)" if has_api_key else "(需配置 ANTHROPIC_API_KEY)"
print(f" 1. Claude Vision API ★精度最高{api_note}")
options.append("api")
# OCR 选项
if "rapid" in ocr_engines:
print(f" 2. 本地 OCR(rapidocr) 免费,有一定错误率,建议导入后核对")
options.append("ocr_rapid")
else:
print(f" 2. 本地 OCR(rapidocr) [未安装] pip install rapidocr-onnxruntime")
options.append(None)
if "easy" in ocr_engines:
print(f" 3. 本地 OCR(easyocr) 免费,精度略高于 rapid,依赖 PyTorch")
options.append("ocr_easy")
else:
print(f" 3. 本地 OCR(easyocr) [未安装] pip install easyocr")
options.append(None)
print(f" 0. 取消")
print(f" 选择 [0-3]:", end="")
raw = input().strip()
if raw == "0" or not raw:
return None
if raw.isdigit() and 1 <= int(raw) <= 3:
chosen = options[int(raw) - 1]
if chosen is None:
print(f" [!] 该 OCR 引擎未安装,请先安装后再使用")
return None
return chosen
print(" [!] 无效选择")
return None
def _do_parse(img_path: str, method: str) -> Optional[dict]:
"""执行解析,返回校验后的结果 dict,失败返回 None"""
from sim.team_image_parser import parse_and_validate as api_parse
from sim.team_image_parser import validate_and_fix
from sim.team_image_parser_ocr import parse_team_image_ocr
if method == "api":
print(" 正在调用 Claude Vision API(约 3-8 秒)...")
try:
return api_parse(img_path)
except EnvironmentError as e:
print(f"\n [!] {e}")
return None
except Exception as e:
print(f"\n [!] API 解析失败:{e}")
return None
# OCR 方式
engine = "rapid" if method == "ocr_rapid" else "easy"
engine_name = "rapidocr" if engine == "rapid" else "easyocr"
print(f" 正在用 {engine_name} 识别(首次运行会下载模型)...")
print(" ⚠ OCR 识别游戏字体存在一定错误率,建议导入后人工核对技能名称")
try:
raw = parse_team_image_ocr(img_path, engine=engine)
return validate_and_fix(raw)
except ImportError as e:
print(f"\n [!] {e}")
return None
except Exception as e:
print(f"\n [!] OCR 解析失败:{e}")
return None
def _menu_import_from_image() -> None:
os.makedirs(_IMPORT_DIR, exist_ok=True)
# 扫描 import_images/ 下的图片文件
images = sorted(
p for p in os.listdir(_IMPORT_DIR)
if os.path.splitext(p)[1].lower() in _IMAGE_EXTS
)
if not images:
print(f"\n [!] import_images/ 文件夹中没有图片。")
print(f" 请将队伍配置截图放入以下目录:")
print(f" {_IMPORT_DIR}")
return
print(f"\n{SEP}")
print(f" import_images/ 中的图片(共 {len(images)} 张):")
for i, name in enumerate(images, 1):
print(f" {i:2}. {name}")
print(" 输入序号选择图片(0 取消):", end="")
raw = input().strip()
if raw == "0" or not raw:
return
if not raw.isdigit() or not (1 <= int(raw) <= len(images)):
print(" [!] 无效序号")
return
img_path = os.path.join(_IMPORT_DIR, images[int(raw) - 1])
print(f"\n 已选:{images[int(raw)-1]}")
# 选择解析方式
method = _pick_parse_method()
if method is None:
return
result = _do_parse(img_path, method)
if result is None:
return
# 显示解析结果
print(f"\n{SEP}")
print(f" 识别结果:队伍名「{result['team_name']}」")
for i, m in enumerate(result["members"], 1):
skills_str = ", ".join(s for s in m["skills"] if s)
print(f" {i}. {m['pokemon']:<12} 技能:{skills_str}")
if result["warnings"]:
print(f"\n ⚠ 校验提示({len(result['warnings'])} 条):")
for w in result["warnings"]:
print(f" · {w}")
# 确认队伍名
print(f"\n 队伍名称(直接回车使用「{result['team_name']}」,或输入新名称):", end="")
custom_name = input().strip()
final_name = custom_name if custom_name else result["team_name"]
# 检查是否与预设同名
existing = get_team_def(final_name)
if existing and existing.get("preset"):
print(f" [!] 「{final_name}」是内置预设名,请重新输入队伍名称:", end="")
final_name = input().strip()
if not final_name:
print(" 已取消")
return
# 过滤掉技能为空的槽位
valid_members = [
{"pokemon": m["pokemon"], "skills": [s for s in m["skills"] if s]}
for m in result["members"]
if m["pokemon"]
]
if not valid_members:
print(" [!] 没有有效的精灵数据,已取消")
return
print(f"\n 确认保存队伍「{final_name}」({len(valid_members)} 只精灵)?(Y/n):", end="")
confirm = input().strip().lower()
if confirm == "n":
print(" 已取消")
return
save_result = add_team(final_name, valid_members)
verb = "已覆盖" if save_result == "replaced" else "已保存"
print(f"\n 队伍「{final_name}」{verb}!可在对战菜单中选用。")
def _menu_import_from_wiki() -> None:
"""从 BWIKI 玩家配队导入 — 浏览 data/lineups.json,选一支保存到名册。"""
from sim.lineups_loader import load_lineups, lineup_to_members
lineups = load_lineups()
if not lineups:
print("\n [!] data/lineups.json 不存在或为空。")
print(" 请先运行 BWIKI 抓取脚本生成数据。")
return
# 类型筛选
print(f"\n{SEP}")
print(f" BWIKI 玩家配队(共 {len(lineups)} 支)")
print(SEP)
print(" 1. 全部")
print(" 2. 仅 PVP")
print(" 3. 仅 PVE")
print(" 0. 返回")
try:
flt = input(" 选择 [0-3]: ").strip()
except (EOFError, KeyboardInterrupt):
return
if flt == "0":
return
if flt == "2":
filtered = [x for x in lineups if x.get("type") == "pvp"]
elif flt == "3":
filtered = [x for x in lineups if x.get("type") == "pve"]
else:
filtered = list(lineups)
if not filtered:
print(" [!] 无符合条件的配队")
return
# 分页浏览
page_size = 20
page = 0
total_pages = (len(filtered) + page_size - 1) // page_size
while True:
start = page * page_size
end = min(start + page_size, len(filtered))
print(f"\n{LINE} 第 {page + 1}/{total_pages} 页(共 {len(filtered)} 支)")
for i in range(start, end):
lp = filtered[i]
tag = lp.get("type", "?").upper()
title = lp.get("title", "(无名)")
author = lp.get("author") or "佚名"
print(f" {i + 1:3}. [{tag}] {title} — by {author}")
print(f" 输入序号选用;n=下一页, p=上一页, 0=取消:", end="")
try:
raw = input().strip().lower()
except (EOFError, KeyboardInterrupt):
return
if raw == "0" or not raw:
return
if raw == "n":
if page + 1 < total_pages:
page += 1
continue
if raw == "p":
if page > 0:
page -= 1
continue
if not raw.isdigit() or not (1 <= int(raw) <= len(filtered)):
print(" [!] 无效输入")
continue
chosen = filtered[int(raw) - 1]
break
# 转换 + 预览
members = lineup_to_members(chosen)
if not members:
print(" [!] 该配队无有效成员,已取消")
return
print(f"\n{SEP}")
print(f" 已选:{chosen.get('title', '(无名)')}({chosen.get('type', '?').upper()})")
print(f" 作者:{chosen.get('author') or '佚名'}")
if chosen.get("intro"):
print(f" 简介:{chosen['intro']}")
print(LINE)
for i, m in enumerate(members, 1):
skills_str = ", ".join(m["skills"])
nature_str = f" 性格:{m['nature']}" if m.get("nature") else ""
print(f" {i}. {m['pokemon']:<12}{nature_str}")
print(f" 技能:{skills_str}")
print(f"\n ⚠ BWIKI 数据中的「血脉」「个体值」会被忽略(sim 引擎不实现)")
# 队伍命名
default_name = chosen.get("title") or f"BWIKI-{chosen.get('id', '?')}"
print(f"\n 队伍名称(直接回车使用「{default_name}」,或输入新名称):", end="")
try: