using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JXHelper.Common;
using JXHelper.Config;
using JXHelper.Platform;
namespace JXHelper.Core;
///
/// Động cơ thực thi chuỗi truyền tống an toàn bằng Thần Hành Phù (Shenxing Talisman).
/// Tuân thủ nghiêm ngặt bất biến: INV-NPC-TEXT-MATCH-FIRST và INV-BG-INPUT-NO-HIJACK.
/// Đảm bảo nhân vật trở về ngay đầu cổng Nghiệt Long Động 7x khi cần hồi phục vị trí.
///
public class ThanHanhPhuExecutor
{
// Từ khóa phân giải menu 3 cấp của Thần Hành Phù đến Nghiệt Long Động
public static readonly string[] Level1Keywords = { "Bản đồ luyện công", "Khu vực luyện công", "Luyện công", "Ban do luyen cong" };
public static readonly string[] Level2Keywords = { "Cấp 70-79", "70-79", "Cấp 7x", "Cap 70", "Cap 7x", "7x" };
public static readonly string[] Level3Keywords = { "Nghiệt Long Động", "Nghiệt long", "Nghiet Long Dong", "Nghiet long" };
// Từ khóa phân giải menu Thần Hành Phù về thành Dương Châu và Lưu điểm hồi sinh
public static readonly string[] CityKeywords = { "Thành thị / Thôn trang", "Thành thị", "Thon trang", "Thanh thi" };
public static readonly string[] DuongChauKeywords = { "Dương Châu", "Duong Chau", "Dương châu phủ" };
public static readonly string[] RespawnKeywords = { "Lưu điểm hồi sinh", "Luu diem hoi sinh", "điểm hồi sinh", "Lưu điểm tái sinh" };
private readonly IntPtr _hWnd;
private readonly BotConfig _config;
public ThanHanhPhuExecutor(IntPtr hWnd, BotConfig config)
{
_hWnd = hWnd;
_config = config;
}
///
/// Giả lập phân giải menu 3 cấp của Thần Hành Phù đi Nghiệt Long Động phục vụ kiểm thử mô phỏng (Mock QA Track).
///
public static (int Step1Option, int Step2Option, int Step3Option) SimulateMenuResolution(
string[] menu1Options,
string[] menu2Options,
string[] menu3Options)
{
int opt1 = NpcDialogTextMatcher.ResolveOptionIndex(menu1Options, Level1Keywords, 2);
int opt2 = NpcDialogTextMatcher.ResolveOptionIndex(menu2Options, Level2Keywords, 7);
int opt3 = NpcDialogTextMatcher.ResolveOptionIndex(menu3Options, Level3Keywords, 1);
return (opt1, opt2, opt3);
}
///
/// Giả lập phân giải menu Thần Hành Phù về thành Dương Châu và Lưu điểm hồi sinh (Mock QA Track).
///
public static (int Step1CityOption, int Step2DuongChauOption, int RespawnOption) SimulateDuongChauMenuResolution(
string[] menu1Options,
string[] menu2Options,
string[] respawnMenuOptions)
{
int opt1 = NpcDialogTextMatcher.ResolveOptionIndex(menu1Options, CityKeywords, 1);
int opt2 = NpcDialogTextMatcher.ResolveOptionIndex(menu2Options, DuongChauKeywords, 6);
int opt3 = NpcDialogTextMatcher.ResolveOptionIndex(respawnMenuOptions, RespawnKeywords, 3);
return (opt1, opt2, opt3);
}
///
/// Kịch bản về thành Dương Châu bằng Thần Hành Phù (kiểm tra hạn dùng, hỗ trợ lưu điểm hồi sinh, fallback Thổ Địa Phù).
///
public async Task ExecuteReturnToDuongChauAsync(bool saveRespawn = true, int itemBagX = 250, int itemBagY = 280, bool isTalismanExpired = false)
{
if (_hWnd == IntPtr.Zero)
{
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Lỗi: HWND không hợp lệ.");
return false;
}
if (isTalismanExpired)
{
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Thần hành phù đã hết hạn! Fallback sử dụng Thổ Địa Phù...");
await BackgroundInput.SendKeyAsync(_hWnd, _config.KeyPhuVirtualCode); // Phím 5
await Task.Delay(_config.MapLoadDelayMs);
return true;
}
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Bắt đầu chuỗi về thành Dương Châu...");
// Bước 1: Mở hành trang F4
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Mở hành trang F4...");
await BackgroundInput.SendKeyAsync(_hWnd, Constants.VK_F4);
await Task.Delay(500);
// Bước 2: Click chuột phải ngầm vào vị trí Thần Hành Phù
TelemetryLogger.LogAction($"[THẦN HÀNH PHÙ] Click chuột phải ngầm vào Thần Hành Phù ({itemBagX}, {itemBagY})...");
await BackgroundInput.SendRightClickAsync(_hWnd, itemBagX, itemBagY);
await Task.Delay(800);
// Bước 3: Cấp 1 - Chọn 'Thành thị / Thôn trang' (Dòng 1 / Phím 1)
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Chọn Cấp 1: 'Thành thị / Thôn trang' (Phím 1)...");
await BackgroundInput.SendKeyAsync(_hWnd, 0x31); // Phím 1
await Task.Delay(600);
// Bước 4: Cấp 2 - Chọn 'Dương Châu' (Dòng 6 / Phím 6)
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Chọn Cấp 2: 'Dương Châu' (Phím 6)...");
await BackgroundInput.SendKeyAsync(_hWnd, 0x36); // Phím 6
await Task.Delay(500);
// Bước 5: Đóng F4 & Chờ nạp map Dương Châu
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Đóng hành trang F4 & Chờ nạp map Dương Châu (2500ms)...");
await BackgroundInput.SendKeyAsync(_hWnd, Constants.VK_F4);
await Task.Delay(2500);
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Đã về đến thành Dương Châu!");
// Bước 6: Nếu yêu cầu Lưu điểm hồi sinh
if (saveRespawn)
{
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Bắt đầu Lưu điểm hồi sinh tại Dương Châu...");
await BackgroundInput.SendKeyAsync(_hWnd, Constants.VK_F4);
await Task.Delay(500);
await BackgroundInput.SendRightClickAsync(_hWnd, itemBagX, itemBagY);
await Task.Delay(800);
// Chọn 'Lưu điểm hồi sinh' (Tùy chọn số 3 trên menu THP)
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Chọn: 'Lưu điểm hồi sinh' (Phím 3)...");
await BackgroundInput.SendKeyAsync(_hWnd, 0x33); // Phím 3
await Task.Delay(600);
// Đóng F4
await BackgroundInput.SendKeyAsync(_hWnd, Constants.VK_F4);
await Task.Delay(300);
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Đã lưu thành công điểm hồi sinh tại thành Dương Châu!");
}
return true;
}
///
/// Thực hiện chuỗi kích hoạt Thần Hành Phù và chọn Nghiệt Long Động 7x qua Background Input.
///
public async Task ExecuteReturnToNghietLongGateAsync(int itemBagX = 250, int itemBagY = 280)
{
if (_hWnd == IntPtr.Zero)
{
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Lỗi: HWND không hợp lệ.");
return false;
}
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Bắt đầu chuỗi trở về đầu cổng Nghiệt Long Động 7x...");
// Bước 1: Mở hành trang F4 nếu chưa mở
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Mở hành trang F4...");
await BackgroundInput.SendKeyAsync(_hWnd, Constants.VK_F4);
await Task.Delay(500);
// Bước 2: Click chuột phải ngầm vào vị trí Thần Hành Phù trong rương
TelemetryLogger.LogAction($"[THẦN HÀNH PHÙ] Click chuột phải ngầm vào vị trí Thần Hành Phù ({itemBagX}, {itemBagY})...");
await BackgroundInput.SendRightClickAsync(_hWnd, itemBagX, itemBagY);
await Task.Delay(800); // Chờ menu hiển thị
// Bước 3: Cấp 1 - Chọn 'Bản đồ luyện công'
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Chọn Cấp 1: 'Bản đồ luyện công' (Phím 2)...");
await BackgroundInput.SendKeyAsync(_hWnd, 0x32); // Phím 2
await Task.Delay(600);
// Bước 4: Cấp 2 - Chọn 'Cấp 70-79'
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Chọn Cấp 2: 'Cấp 70-79' (Phím 7)...");
await BackgroundInput.SendKeyAsync(_hWnd, 0x37); // Phím 7
await Task.Delay(600);
// Bước 5: Cấp 3 - Chọn 'Nghiệt Long Động'
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Chọn Cấp 3: 'Nghiệt Long Động' (Phím 1)...");
await BackgroundInput.SendKeyAsync(_hWnd, 0x31); // Phím 1
await Task.Delay(500);
// Bước 6: Đóng rương F4
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Đóng hành trang F4...");
await BackgroundInput.SendKeyAsync(_hWnd, Constants.VK_F4);
// Bước 7: Chờ nạp bản đồ
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Đang dịch chuyển... Chờ nạp bản đồ (2500ms)...");
await Task.Delay(2500);
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Nhân vật đã trở về an toàn tại ĐẦU CỔNG NGHIỆT LONG ĐỘNG 7X!");
return true;
}
///
/// Tự động dịch chuyển đến bản đồ chỉ định (Nghiệt Long Động 182, Lão Hổ Động 123, Dương Châu 80) phục vụ bám sát Acc Chính.
///
public async Task ExecuteTeleportToMapAsync(int targetMapId, int itemBagX = 250, int itemBagY = 280)
{
if (targetMapId == 182)
{
return await ExecuteReturnToNghietLongGateAsync(itemBagX, itemBagY);
}
else if (targetMapId == 80)
{
return await ExecuteReturnToDuongChauAsync(saveRespawn: false, itemBagX, itemBagY);
}
else if (targetMapId == 123) // Lão Hổ Động
{
if (_hWnd == IntPtr.Zero) return false;
TelemetryLogger.LogAction("[THẦN HÀNH PHÙ] Dịch chuyển đến Lão Hổ Động (Map 123)...");
await BackgroundInput.SendKeyAsync(_hWnd, Constants.VK_F4);
await Task.Delay(500);
await BackgroundInput.SendRightClickAsync(_hWnd, itemBagX, itemBagY);
await Task.Delay(800);
await BackgroundInput.SendKeyAsync(_hWnd, 0x32); // Phím 2: Luyện công
await Task.Delay(600);
await BackgroundInput.SendKeyAsync(_hWnd, 0x35); // Phím 5: Cấp 50-59
await Task.Delay(600);
await BackgroundInput.SendKeyAsync(_hWnd, 0x34); // Phím 4: Lão Hổ Động
await Task.Delay(500);
await BackgroundInput.SendKeyAsync(_hWnd, Constants.VK_F4);
await Task.Delay(2500);
return true;
}
return false;
}
}