using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace JXHelper.Config;
///
/// Mô hình lưu trữ tài khoản game phục vụ Auto-Launch và quản lý danh sách tài khoản.
///
public class AccountEntry
{
public string Id { get; set; } = Guid.NewGuid().ToString("N");
public string Username { get; set; } = "";
public string Password { get; set; } = "";
public string CharacterName { get; set; } = "";
public string ServerName { get; set; } = "Trấn Vân Chi Tình";
public string Sect { get; set; } = "Cái Bang";
public string DefaultMap { get; set; } = "Nghiệt Long Động 7x";
public int TargetCoordX { get; set; } = 233;
public int TargetCoordY { get; set; } = 202;
public int CharacterSlot { get; set; } = 1; // Slot 1: Trái (250, 350), Slot 2: Giữa (400, 350), Slot 3: Phải (550, 350)
public bool AutoLoginOnLaunch { get; set; } = true;
public bool IsEnabled { get; set; } = true;
public string Notes { get; set; } = "";
public override string ToString()
{
return $"{CharacterName} ({Username}) - {Sect} [{ServerName}]";
}
}
///
/// Quản lý lưu trữ danh sách tài khoản (SSoT: config/accounts.json).
///
public static class AccountStorage
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
PropertyNameCaseInsensitive = true
};
public static string GetConfigPath()
{
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string configDir = Path.Combine(baseDir, "config");
if (!Directory.Exists(configDir))
{
try { Directory.CreateDirectory(configDir); } catch { }
}
return Path.Combine(configDir, "accounts.json");
}
public static List LoadAccounts()
{
string path = GetConfigPath();
if (File.Exists(path))
{
try
{
string json = File.ReadAllText(path);
var list = JsonSerializer.Deserialize>(json, JsonOptions);
if (list != null && list.Count > 0)
{
return list;
}
}
catch
{
// Fallback nếu file lỗi
}
}
// Tạo danh sách mặc định dựa trên thông tin người dùng cung cấp
var defaultList = new List
{
new()
{
Username = "nguvansi",
Password = "Ngu123@@",
CharacterName = "BoCuaCacCon",
Sect = "Cái Bang",
ServerName = "Trấn Vân Chi Tình",
DefaultMap = "Nghiệt Long Động 7x",
TargetCoordX = 233,
TargetCoordY = 202,
Notes = "Đội trưởng Cái Bang chưởng Kháng Long Hữu Hối (cấp 50)"
},
new()
{
Username = "nguvansi1",
Password = "Ngu123@@",
CharacterName = "ConCuaBoMe",
Sect = "Thiếu Lâm",
ServerName = "Trấn Vân Chi Tình",
DefaultMap = "Nghiệt Long Động 7x",
TargetCoordX = 235,
TargetCoordY = 205,
Notes = "Thiếu Lâm đao/côn/quyền Kim Cang Phục Ma"
},
new()
{
Username = "nguvansi2",
Password = "Ngu123@@",
CharacterName = "ConGailCung",
Sect = "Ngũ Độc",
ServerName = "Trấn Vân Chi Tình",
DefaultMap = "Nghiệt Long Động 7x",
TargetCoordX = 230,
TargetCoordY = 200,
Notes = "Ngũ Độc bùa chú Vạn Cổ & U Hồn"
},
new()
{
Username = "nguvansi3",
Password = "Ngu123@@",
CharacterName = "MeCuaCacCon",
Sect = "Nga My",
ServerName = "Trấn Vân Chi Tình",
DefaultMap = "Nghiệt Long Động 7x",
TargetCoordX = 232,
TargetCoordY = 204,
Notes = "Nga My Từ Hàng Phổ Độ & Hào quang Lưu Thủy"
}
};
SaveAccounts(defaultList);
return defaultList;
}
public static void SaveAccounts(List accounts)
{
string path = GetConfigPath();
try
{
string json = JsonSerializer.Serialize(accounts, JsonOptions);
File.WriteAllText(path, json);
}
catch (Exception ex)
{
Console.WriteLine($"[AccountStorage] Loi luu accounts.json: {ex.Message}");
}
}
///
/// Nhập nhanh danh sách tài khoản từ văn bản.
/// Hỗ trợ các định dạng phân cách: '|', ';', ',', tab.
/// Dạng: username|password|character_name|sect|server
///
public static List ImportFromText(string text)
{
var result = new List();
if (string.IsNullOrWhiteSpace(text)) return result;
var lines = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
char[] separators = { '|', ';', '\t', ',' };
foreach (var rawLine in lines)
{
string line = rawLine.Trim();
if (string.IsNullOrEmpty(line) || line.StartsWith("#") || line.StartsWith("//"))
continue;
string[] parts = line.Split(separators, StringSplitOptions.TrimEntries);
if (parts.Length >= 2)
{
var entry = new AccountEntry
{
Username = parts[0],
Password = parts[1],
CharacterName = parts.Length > 2 ? parts[2] : parts[0],
Sect = parts.Length > 3 ? parts[3] : "Cái Bang",
ServerName = parts.Length > 4 ? parts[4] : "Trấn Vân Chi Tình",
DefaultMap = parts.Length > 5 ? parts[5] : "Nghiệt Long Động 7x",
CharacterSlot = (parts.Length > 6 && int.TryParse(parts[6], out int slot)) ? Math.Clamp(slot, 1, 3) : 1,
IsEnabled = true
};
result.Add(entry);
}
}
return result;
}
///
/// Xuất danh sách tài khoản sang định dạng văn bản dễ đọc.
///
public static string ExportToText(List accounts)
{
var lines = new List();
foreach (var a in accounts)
{
lines.Add($"{a.Username}|{a.Password}|{a.CharacterName}|{a.Sect}|{a.ServerName}|{a.DefaultMap}|{a.CharacterSlot}");
}
return string.Join(Environment.NewLine, lines);
}
}