using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using JXHelper.Config; using JXHelper.Core; using JXHelper.Platform; namespace JXHelper.UI; /// /// Cửa sổ quản lý danh sách tài khoản, nhập hàng loạt và hỗ trợ khởi chạy Client & Auto. /// public partial class AccountManagerForm : Form { private List _accounts = new(); private AccountEntry? _selectedAccount; public AccountManagerForm() { InitializeComponent(); SetupSectCombo(); BindEvents(); LoadAccountData(); } private void SetupSectCombo() { cboSect.Items.Clear(); foreach (var sect in SkillDatabase.AllSects) { cboSect.Items.Add(sect); } if (cboSect.Items.Count > 0) { cboSect.SelectedIndex = 0; // Cái Bang } cboSlot.Items.Clear(); cboSlot.Items.Add("1 (Trái)"); cboSlot.Items.Add("2 (Giữa)"); cboSlot.Items.Add("3 (Phải)"); cboSlot.SelectedIndex = 0; } private void BindEvents() { lvwAccounts.SelectedIndexChanged += (_, _) => OnAccountSelected(); chkShowPass.CheckedChanged += (_, _) => { txtPassword.PasswordChar = chkShowPass.Checked ? '\0' : '•'; }; btnAdd.Click += (_, _) => OnAddAccount(); btnUpdate.Click += (_, _) => OnUpdateAccount(); btnDelete.Click += (_, _) => OnDeleteAccount(); btnImportBulk.Click += (_, _) => OnImportBulk(); btnExportBulk.Click += (_, _) => OnExportBulk(); btnSave.Click += (_, _) => { AccountStorage.SaveAccounts(_accounts); MessageBox.Show("Đã lưu danh sách tài khoản thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); }; btnLaunchGame.Click += (_, _) => OnLaunchSelectedGame(); btnSmartLoginAll.Click += (_, _) => OnSmartLoginAll(); btnLaunchAutoVLBS.Click += (_, _) => OnLaunchAutoVLBS(); btnClose.Click += (_, _) => { AccountStorage.SaveAccounts(_accounts); this.DialogResult = DialogResult.OK; this.Close(); }; } private void LoadAccountData() { _accounts = AccountStorage.LoadAccounts(); RefreshListView(); } private void RefreshListView() { var reconcileReport = AutoLoginEngine.ReconcileAccounts(_accounts); var onlineMap = reconcileReport.AccountStatuses.ToDictionary(s => s.Account.Id, s => s); lvwAccounts.BeginUpdate(); lvwAccounts.Items.Clear(); foreach (var acc in _accounts) { var item = new ListViewItem(acc.CharacterName) { Tag = acc, Checked = acc.IsEnabled }; item.SubItems.Add(acc.Username); item.SubItems.Add(new string('•', Math.Min(acc.Password.Length, 8))); item.SubItems.Add($"Slot {acc.CharacterSlot}"); item.SubItems.Add(acc.Sect); item.SubItems.Add(acc.DefaultMap); if (onlineMap.TryGetValue(acc.Id, out var st) && st.IsOnline && st.WindowInfo != null) { item.SubItems.Add($"🟢 Online (PID {st.WindowInfo.ProcessId})"); item.ForeColor = Color.DarkGreen; } else { item.SubItems.Add("⚪ Chưa log"); item.ForeColor = Color.DimGray; } if (string.Equals(acc.CharacterName, "BoCuaCacCon", StringComparison.OrdinalIgnoreCase)) { item.Font = new Font(lvwAccounts.Font, FontStyle.Bold); } lvwAccounts.Items.Add(item); } if (lvwAccounts.Items.Count > 0 && lvwAccounts.SelectedItems.Count == 0) { lvwAccounts.Items[0].Selected = true; } lvwAccounts.EndUpdate(); } private void OnAccountSelected() { if (lvwAccounts.SelectedItems.Count == 0) return; var item = lvwAccounts.SelectedItems[0]; if (item.Tag is AccountEntry acc) { _selectedAccount = acc; txtCharName.Text = acc.CharacterName; txtUsername.Text = acc.Username; txtPassword.Text = acc.Password; txtMap.Text = acc.DefaultMap; int sectIdx = cboSect.FindStringExact(acc.Sect); if (sectIdx >= 0) cboSect.SelectedIndex = sectIdx; cboSlot.SelectedIndex = Math.Clamp(acc.CharacterSlot - 1, 0, 2); } } private void OnAddAccount() { if (string.IsNullOrWhiteSpace(txtCharName.Text) || string.IsNullOrWhiteSpace(txtUsername.Text)) { MessageBox.Show("Vui lòng nhập Tên nhân vật và Tài khoản!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var newAcc = new AccountEntry { CharacterName = txtCharName.Text.Trim(), Username = txtUsername.Text.Trim(), Password = txtPassword.Text, CharacterSlot = cboSlot.SelectedIndex + 1, Sect = cboSect.SelectedItem?.ToString() ?? "Cái Bang", DefaultMap = string.IsNullOrWhiteSpace(txtMap.Text) ? "Nghiệt Long Động 7x" : txtMap.Text.Trim(), IsEnabled = true }; _accounts.Add(newAcc); AccountStorage.SaveAccounts(_accounts); RefreshListView(); } private void OnUpdateAccount() { if (_selectedAccount == null) return; _selectedAccount.CharacterName = txtCharName.Text.Trim(); _selectedAccount.Username = txtUsername.Text.Trim(); _selectedAccount.Password = txtPassword.Text; _selectedAccount.CharacterSlot = cboSlot.SelectedIndex + 1; _selectedAccount.Sect = cboSect.SelectedItem?.ToString() ?? "Cái Bang"; _selectedAccount.DefaultMap = txtMap.Text.Trim(); AccountStorage.SaveAccounts(_accounts); RefreshListView(); } private void OnDeleteAccount() { if (_selectedAccount == null) return; var confirm = MessageBox.Show($"Bạn có chắc muốn xóa tài khoản '{_selectedAccount.CharacterName}'?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirm == DialogResult.Yes) { _accounts.Remove(_selectedAccount); _selectedAccount = null; AccountStorage.SaveAccounts(_accounts); RefreshListView(); } } private void OnImportBulk() { string text = txtBulk.Text.Trim(); if (string.IsNullOrEmpty(text)) { MessageBox.Show("Vui lòng dán danh sách tài khoản vào ô nhập!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } var imported = AccountStorage.ImportFromText(text); if (imported.Count == 0) { MessageBox.Show("Không tìm thấy dòng tài khoản hợp lệ. Định dạng mẫu:\ntaikhoan|matkhau|ten_nhanvat|monphai", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } _accounts.AddRange(imported); AccountStorage.SaveAccounts(_accounts); RefreshListView(); MessageBox.Show($"Đã nhập thành công {imported.Count} tài khoản!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void OnExportBulk() { txtBulk.Text = AccountStorage.ExportToText(_accounts); } private void OnLaunchSelectedGame() { var target = _selectedAccount ?? (_accounts.Count > 0 ? _accounts[0] : null); if (target == null) { MessageBox.Show("Vui lòng chọn một tài khoản để đăng nhập!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } btnLaunchGame.Enabled = false; btnSmartLoginAll.Enabled = false; string originalText = btnLaunchGame.Text; btnLaunchGame.Text = "⏳ Đang chạy..."; _ = System.Threading.Tasks.Task.Run(async () => { try { bool success = await AutoLoginEngine.SmartLoginAccountAsync(target); if (!this.IsDisposed && this.IsHandleCreated) { this.Invoke(() => { btnLaunchGame.Enabled = true; btnSmartLoginAll.Enabled = true; btnLaunchGame.Text = originalText; RefreshListView(); if (success) { MessageBox.Show($"Đã hoàn tất chu trình đăng nhập thông minh cho '{target.Username}' ({target.CharacterName})!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show($"Quá trình tự động đăng nhập cho '{target.Username}' chưa hoàn tất. Vui lòng kiểm tra lại trạng thái cửa sổ game!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }); } } catch (Exception ex) { if (!this.IsDisposed && this.IsHandleCreated) { this.Invoke(() => { btnLaunchGame.Enabled = true; btnSmartLoginAll.Enabled = true; btnLaunchGame.Text = originalText; RefreshListView(); MessageBox.Show($"Lỗi trong quá trình khởi chạy: {ex.Message}", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); }); } } }); } private void OnSmartLoginAll() { btnLaunchGame.Enabled = false; btnSmartLoginAll.Enabled = false; string originalText = btnSmartLoginAll.Text; btnSmartLoginAll.Text = "⏳ Đang log..."; _ = System.Threading.Tasks.Task.Run(async () => { try { int processed = await AutoLoginEngine.SmartReconcileAllAccountsAsync(); if (!this.IsDisposed && this.IsHandleCreated) { this.Invoke(() => { btnLaunchGame.Enabled = true; btnSmartLoginAll.Enabled = true; btnSmartLoginAll.Text = originalText; RefreshListView(); MessageBox.Show($"Đã hoàn tất đối soát và tự động đăng nhập! Đã xử lý thành công {processed} tài khoản.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); }); } } catch (Exception ex) { if (!this.IsDisposed && this.IsHandleCreated) { this.Invoke(() => { btnLaunchGame.Enabled = true; btnSmartLoginAll.Enabled = true; btnSmartLoginAll.Text = originalText; RefreshListView(); MessageBox.Show($"Lỗi trong quá trình đối soát đăng nhập: {ex.Message}", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); }); } } }); } private void OnLaunchAutoVLBS() { bool success = GameLauncher.LaunchAutoVLBS(); if (success) { MessageBox.Show("Đã khởi chạy AutoVLBS thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Không tìm thấy AutoVLBS.exe tại C:\\JXVCTK\\JxVoCongTruyenKy\\_AutoVLBS\\VLBS19\\", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }