using System.Globalization; using System.Xml.Linq; using Microsoft.Data.SqlClient; namespace XmlTool.Importers { public sealed class RewardProfileSummary { public int ServerGroupCount { get; init; } public int RewardCount { get; init; } public int DistinctRewardIdCount { get; init; } public int DuplicateRewardIdWithinServerCount { get; init; } public int DistinctItemCodeCount { get; init; } public int MissingItemInfoCount { get; init; } public int ActiveRewardCount { get; init; } public int FutureRewardCount { get; init; } public int ExpiredRewardCount { get; init; } public ushort MinRewardId { get; init; } public ushort MaxRewardId { get; init; } public string ServerIndexes { get; init; } = string.Empty; public string DateRange { get; init; } = string.Empty; } public static class RewardProfileInspector { private const string DateFormat = "yyyy-MM-dd HH:mm:ss"; public static RewardProfileSummary ValidateXml(string rewardXmlPath) { RewardPackage package = LoadPackage(rewardXmlPath); return BuildSummary(package, missingItemInfoCount: 0); } public static RewardProfileSummary AssessDatabase(string connectionString, string rewardXmlPath) { RewardPackage package = LoadPackage(rewardXmlPath); using SqlConnection connection = new(connectionString); connection.Open(); HashSet itemIds = ReadItemIds(connection); int missingItemInfoCount = package.Rewards .Select(x => x.ItemCode) .Distinct() .Count(x => !itemIds.Contains(x)); return BuildSummary(package, missingItemInfoCount); } private static RewardProfileSummary BuildSummary(RewardPackage package, int missingItemInfoCount) { DateTime now = DateTime.Now; ushort minRewardId = package.Rewards.Count == 0 ? (ushort)0 : package.Rewards.Min(x => x.Id); ushort maxRewardId = package.Rewards.Count == 0 ? (ushort)0 : package.Rewards.Max(x => x.Id); DateTime minStartDate = package.Rewards.Min(x => x.StartDate); DateTime maxExpiryDate = package.Rewards.Max(x => x.ExpiryDate); return new RewardProfileSummary { ServerGroupCount = package.ServerIndexes.Count, RewardCount = package.Rewards.Count, DistinctRewardIdCount = package.Rewards.Select(x => x.Id).Distinct().Count(), DuplicateRewardIdWithinServerCount = package.Rewards .GroupBy(x => new { x.ServerIndex, x.Id }) .Where(x => x.Count() > 1) .Sum(x => x.Count() - 1), DistinctItemCodeCount = package.Rewards.Select(x => x.ItemCode).Distinct().Count(), MissingItemInfoCount = missingItemInfoCount, ActiveRewardCount = package.Rewards.Count(x => x.StartDate <= now && x.ExpiryDate >= now), FutureRewardCount = package.Rewards.Count(x => x.StartDate > now), ExpiredRewardCount = package.Rewards.Count(x => x.ExpiryDate < now), MinRewardId = minRewardId, MaxRewardId = maxRewardId, ServerIndexes = string.Join(", ", package.ServerIndexes.OrderBy(x => x)), DateRange = $"{minStartDate:yyyy-MM-dd HH:mm:ss} to {maxExpiryDate:yyyy-MM-dd HH:mm:ss}" }; } private static RewardPackage LoadPackage(string rewardXmlPath) { if (!File.Exists(rewardXmlPath)) { throw new FileNotFoundException("Reward.xml not found.", rewardXmlPath); } XDocument document = XDocument.Load(rewardXmlPath); XElement root = document.Root ?? throw new InvalidOperationException("Reward.xml is missing a root node."); if (root.Name.LocalName != "RewardTable") { throw new InvalidOperationException("Reward.xml root node must be ."); } List serverIndexes = new(); List rewards = new(); int serverGroupRow = 0; foreach (XElement serverGroupElement in root.Elements("ServerGroup")) { serverGroupRow++; string groupContext = $"ServerGroup row {serverGroupRow}"; int serverIndex = ReadRequiredInt(serverGroupElement, "ServerIndex", groupContext); if (serverIndex <= 0) { throw new InvalidOperationException($"{groupContext} has invalid ServerIndex value: '{serverIndex}'."); } XElement rewardsElement = serverGroupElement.Element("Rewards") ?? throw new InvalidOperationException($"{groupContext} is missing required node."); int rewardRow = 0; foreach (XElement rewardElement in rewardsElement.Elements("Reward")) { rewardRow++; string rewardContext = $"{groupContext} Reward row {rewardRow}"; ushort id = ReadRequiredUShort(rewardElement, "ID", rewardContext); DateTime startDate = ReadRequiredDate(rewardElement, "StartDate", rewardContext); DateTime expiryDate = ReadRequiredDate(rewardElement, "ExpDate", rewardContext); uint itemCode = ReadRequiredUInt(rewardElement, "ItemCode1", rewardContext); ushort itemCount = ReadRequiredUShort(rewardElement, "ItemCount1", rewardContext); _ = ReadRequiredString(rewardElement, "Comment", rewardContext); if (expiryDate < startDate) { throw new InvalidOperationException($"{rewardContext} has ExpDate earlier than StartDate."); } if (itemCode == 0) { throw new InvalidOperationException($"{rewardContext} has invalid ItemCode1 value: '0'."); } if (itemCount == 0) { throw new InvalidOperationException($"{rewardContext} has invalid ItemCount1 value: '0'."); } rewards.Add(new RewardRecord(serverIndex, id, startDate, expiryDate, itemCode)); } if (rewardRow == 0) { throw new InvalidOperationException($"{groupContext} does not contain any rows."); } serverIndexes.Add(serverIndex); } if (serverIndexes.Count == 0) { throw new InvalidOperationException("Reward.xml does not contain any rows."); } return new RewardPackage(serverIndexes, rewards); } private static int ReadRequiredInt(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!int.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)) { throw new InvalidOperationException($"{recordContext} has invalid integer value in '{elementName}': '{element.Value}'."); } return value; } private static uint ReadRequiredUInt(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!uint.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out uint value)) { throw new InvalidOperationException($"{recordContext} has invalid unsigned integer value in '{elementName}': '{element.Value}'."); } return value; } private static ushort ReadRequiredUShort(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!ushort.TryParse(element.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out ushort value)) { throw new InvalidOperationException($"{recordContext} has invalid ushort value in '{elementName}': '{element.Value}'."); } return value; } private static DateTime ReadRequiredDate(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } if (!DateTime.TryParseExact( element.Value, DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime value)) { throw new InvalidOperationException($"{recordContext} has invalid date value in '{elementName}': '{element.Value}'. Expected format: {DateFormat}."); } return value; } private static string ReadRequiredString(XElement parent, string elementName, string recordContext) { XElement? element = parent.Element(elementName); if (element == null) { throw new InvalidOperationException($"{recordContext} is missing required field '{elementName}'."); } return element.Value; } private static HashSet ReadItemIds(SqlConnection connection) { using SqlCommand command = new("SELECT [ItemId] FROM [Asset].[ItemInfo];", connection); using SqlDataReader reader = command.ExecuteReader(); HashSet values = new(); while (reader.Read()) { values.Add(Convert.ToUInt32(reader.GetValue(0), CultureInfo.InvariantCulture)); } return values; } private sealed record RewardPackage(List ServerIndexes, List Rewards); private sealed record RewardRecord(int ServerIndex, ushort Id, DateTime StartDate, DateTime ExpiryDate, uint ItemCode); } }