From 88a0b6818518fd4ccdd4bcc7fb6df9fe9dc83c10 Mon Sep 17 00:00:00 2001 From: Eric-Joker <1277243150@qq.com> Date: Thu, 19 Sep 2024 13:53:42 +0800 Subject: [PATCH] feat: Gacha Log supports simultaneous import of multiple UIDs --- src/Starward/Pages/GachaLogPage.xaml.cs | 21 ++-- .../Services/Gacha/GachaLogService.cs | 6 +- .../Services/Gacha/GenshinGachaService.cs | 101 ++++++++++-------- .../Services/Gacha/StarRailGachaService.cs | 78 +++++++++----- .../Services/Gacha/ZZZGachaService.cs | 64 ++++++++--- 5 files changed, 174 insertions(+), 96 deletions(-) diff --git a/src/Starward/Pages/GachaLogPage.xaml.cs b/src/Starward/Pages/GachaLogPage.xaml.cs index cdee0ad7e..bc948b1e9 100644 --- a/src/Starward/Pages/GachaLogPage.xaml.cs +++ b/src/Starward/Pages/GachaLogPage.xaml.cs @@ -698,19 +698,16 @@ private async Task ImportGachaLogAsync() var file = await FileDialogHelper.PickSingleFileAsync(MainWindow.Current.WindowHandle, ("Json", ".json")); if (File.Exists(file)) { - var uid = _gachaLogService.ImportGachaLog(file); - if (uid == SelectUid) + var uids = _gachaLogService.ImportGachaLog(file); + foreach (var uid in uids) { - UpdateGachaTypeStats(uid); - } - else if (UidList.Contains(uid)) - { - SelectUid = uid; - } - else - { - UidList.Add(uid); - SelectUid = uid; + if (!UidList.Contains(uid)) + UidList.Add(uid); + if (uid == uids.First()) + if (uid == SelectUid) + UpdateGachaTypeStats(uid); + else + SelectUid = uid; } } } diff --git a/src/Starward/Services/Gacha/GachaLogService.cs b/src/Starward/Services/Gacha/GachaLogService.cs index 3abad8e77..3a99a06d9 100644 --- a/src/Starward/Services/Gacha/GachaLogService.cs +++ b/src/Starward/Services/Gacha/GachaLogService.cs @@ -120,6 +120,10 @@ public virtual async Task GetUidFromGachaLogUrl(string url) + public abstract List CheckUIGFItems(List list, long uid, string lang); + + + protected abstract int InsertGachaLogItems(List items); @@ -272,7 +276,7 @@ public virtual int DeleteUid(long uid) - public abstract long ImportGachaLog(string file); + public abstract List ImportGachaLog(string file); diff --git a/src/Starward/Services/Gacha/GenshinGachaService.cs b/src/Starward/Services/Gacha/GenshinGachaService.cs index a822cf3f4..2a5422d13 100644 --- a/src/Starward/Services/Gacha/GenshinGachaService.cs +++ b/src/Starward/Services/Gacha/GenshinGachaService.cs @@ -71,6 +71,13 @@ public override List GetGachaLogItemEx(long uid) } + public Dictionary GetItemsInfo() + { + using var dapper = _database.CreateConnection(); + return dapper.Query("SELECT Id, Name, Level FROM GenshinGachaInfo").ToDictionary(item => item.Id); + } + + protected override int InsertGachaLogItems(List items) { using var dapper = _database.CreateConnection(); @@ -78,7 +85,7 @@ protected override int InsertGachaLogItems(List items) var affect = dapper.Execute(""" INSERT OR REPLACE INTO GenshinGachaItem (Uid, Id, Name, Time, ItemId, ItemType, RankType, GachaType, Count, Lang) VALUES (@Uid, @Id, @Name, @Time, @ItemId, @ItemType, @RankType, @GachaType, @Count, @Lang); - """, items, t); + """, items, t); t.Commit(); UpdateGachaItemId(); return affect; @@ -131,43 +138,62 @@ private async Task ExportAsExcelAsync(long uid, string output) - public override long ImportGachaLog(string file) + public override List CheckUIGFItems(List list, long uid, string lang) + { + var infos = GetItemsInfo(); + foreach (var item in list) + { + infos.TryGetValue(item.ItemId, out GenshinGachaInfo? info); + if (item.GachaType == 0 || item.ItemId == 0 || item.Id == 0) + throw new JsonException("Missing required properties."); + item.Uid = uid; + if (item.Count == 0) + item.Count = 1; + item.Name ??= info?.Name ?? ""; + item.ItemType ??= ""; + if (item.RankType == 0) + item.RankType = info?.Level ?? 0; + item.Lang ??= lang; + } + return list; + } + + + + public override List ImportGachaLog(string file) { var str = File.ReadAllText(file); - string lang; - long uid; - List list; + var count = 0; + List uids = []; using (JsonDocument doc = JsonDocument.Parse(str)) { if (doc.RootElement.TryGetProperty("info", out JsonElement infoElement) && infoElement.TryGetProperty("version", out _)) { - var obj = JsonSerializer.Deserialize(str)!.hk4e.FirstOrDefault(); - if (obj is null) - return 0; - lang = obj.lang ?? ""; - uid = obj.uid; - list = obj.list; + var obj = JsonSerializer.Deserialize(str)!.hk4e; + foreach (var user in obj) + { + var lang = user.lang ?? ""; + var uid = user.uid; + var list = CheckUIGFItems(user.list.ToList(), uid, lang); + uids.Add(uid); + count += InsertGachaLogItems(list); + } } else if (infoElement.TryGetProperty("uigf_version", out _)) { var obj = JsonSerializer.Deserialize(str)!; - lang = obj.info.lang ?? ""; - uid = obj.info.uid; - list = obj.list; + var lang = obj.info.lang ?? ""; + var uid = obj.info.uid; + uids.Add(uid); + var list = CheckUIGFItems(obj.list.ToList(), uid, lang); + count += InsertGachaLogItems(list); } - else - return 0; - } - foreach (var item in list) - { - item.Lang ??= lang; - if (item.Uid == 0) - item.Uid = uid; } - var count = InsertGachaLogItems(list.ToList()); + if (uids.Count == 0) + throw new JsonException("Unsupported Json Structures."); // 成功导入祈愿记录 {count} 条 - NotificationBehavior.Instance.Success($"Uid {uid}", string.Format(Lang.GenshinGachaService_ImportWishRecordsSuccessfully, count), 5000); - return uid; + NotificationBehavior.Instance.Success($"Uid {string.Join(" ", uids)}", string.Format(Lang.GenshinGachaService_ImportWishRecordsSuccessfully, count), 5000); + return uids; } @@ -221,14 +247,6 @@ public UIGFObj() { } public UIGFObj(long uid, List list) { this.info = new UIAFInfo(uid, list); - foreach (var item in list) - { - item.uigf_gacha_type = item.GachaType switch - { - GenshinGachaType.CharacterEventWish_2 => GenshinGachaType.CharacterEventWish.ToString(), - _ => item.GachaType.ToString(), - }; - } this.list = list; } @@ -287,14 +305,6 @@ public UIGF40Obj() { } public UIGF40Obj(long uid, List list) { this.info = new UIGF40Info(); - foreach (var item in list) - { - item.uigf_gacha_type = item.GachaType switch - { - GenshinGachaType.CharacterEventWish_2 => GenshinGachaType.CharacterEventWish.ToString(), - _ => item.GachaType.ToString(), - }; - } this.hk4e = [new UIGF40Game(uid, list)]; } @@ -353,7 +363,14 @@ public UIGF40Game(long uid, List list) private class UIGFItem : GenshinGachaItem { - public string uigf_gacha_type { get; set; } + public string uigf_gacha_type + { + get => GachaType switch + { + GenshinGachaType.CharacterEventWish_2 => GenshinGachaType.CharacterEventWish.ToString(), + _ => GachaType.ToString(), + }; + } } diff --git a/src/Starward/Services/Gacha/StarRailGachaService.cs b/src/Starward/Services/Gacha/StarRailGachaService.cs index 4f041dc1e..9d40b88f0 100644 --- a/src/Starward/Services/Gacha/StarRailGachaService.cs +++ b/src/Starward/Services/Gacha/StarRailGachaService.cs @@ -70,6 +70,14 @@ public override List GetGachaLogItemEx(long uid) + public Dictionary GetItemsInfo() + { + using var dapper = _database.CreateConnection(); + return dapper.Query("SELECT ItemId, ItemName, Rarity FROM StarRailGachaInfo").ToDictionary(item => item.ItemId); + } + + + protected override int InsertGachaLogItems(List items) { using var dapper = _database.CreateConnection(); @@ -131,43 +139,63 @@ private async Task ExportAsExcelAsync(long uid, string output) - public override long ImportGachaLog(string file) + public override List CheckUIGFItems(List list, long uid, string lang) + { + var infos = GetItemsInfo(); + foreach (var item in list) + { + infos.TryGetValue(item.ItemId, out StarRailGachaInfo? info); + if (item.GachaType == 0 || item.ItemId == 0 || item.Id == 0) + throw new JsonException("Missing required properties."); + item.Uid = uid; + if (item.Count == 0) + item.Count = 1; + item.Name ??= info?.ItemName ?? ""; + item.ItemType ??= ""; + if (item.RankType == 0) + item.RankType = info?.Rarity ?? 0; + item.Lang ??= lang; + } + return list; + } + + + + + public override List ImportGachaLog(string file) { var str = File.ReadAllText(file); - string lang; - long uid; - List list; + var count = 0; + List uids = []; using (JsonDocument doc = JsonDocument.Parse(str)) { if (doc.RootElement.TryGetProperty("info", out JsonElement infoElement) && infoElement.TryGetProperty("version", out _)) { - var obj = JsonSerializer.Deserialize(str)!.hkrpg.FirstOrDefault(); - if (obj is null) - return 0; - lang = obj.lang ?? ""; - uid = obj.uid; - list = obj.list; + var obj = JsonSerializer.Deserialize(str)!.hkrpg; + foreach (var user in obj) + { + var lang = user.lang ?? ""; + var uid = user.uid; + var list = CheckUIGFItems(user.list.ToList(), uid, lang); + uids.Add(uid); + count += InsertGachaLogItems(list); + } } - else if (infoElement.TryGetProperty("srgf_version", out _)) + else if (infoElement.TryGetProperty("uigf_version", out _)) { var obj = JsonSerializer.Deserialize(str)!; - lang = obj.info.lang ?? ""; - uid = obj.info.uid; - list = obj.list; + var lang = obj.info.lang ?? ""; + var uid = obj.info.uid; + uids.Add(uid); + var list = CheckUIGFItems(obj.list.ToList(), uid, lang); + count += InsertGachaLogItems(list); } - else - return 0; - } - foreach (var item in list) - { - item.Lang ??= lang; - if (item.Uid == 0) - item.Uid = uid; } - var count = InsertGachaLogItems(list.ToList()); + if (uids.Count == 0) + throw new JsonException("Unsupported Json Structures."); // 成功导入跃迁记录 {count} 条 - NotificationBehavior.Instance.Success($"Uid {uid}", string.Format(Lang.StarRailGachaService_ImportWarpRecordsSuccessfully, count), 5000); - return uid; + NotificationBehavior.Instance.Success($"Uid {string.Join(" ", uids)}", string.Format(Lang.StarRailGachaService_ImportWarpRecordsSuccessfully, count), 5000); + return uids; } diff --git a/src/Starward/Services/Gacha/ZZZGachaService.cs b/src/Starward/Services/Gacha/ZZZGachaService.cs index c5e7f23d3..4646c60fd 100644 --- a/src/Starward/Services/Gacha/ZZZGachaService.cs +++ b/src/Starward/Services/Gacha/ZZZGachaService.cs @@ -71,6 +71,16 @@ public override List GetGachaLogItemEx(long uid) + // todo + public Dictionary GetItemsInfo() + { + throw new NotImplementedException(); + //using var dapper = _database.CreateConnection(); + //return dapper.Query("SELECT ItemId, ItemName, Rarity FROM ZZZGachaItem").ToDictionary(item => item.Id); + } + + + public override (List GachaStats, List ItemStats) GetGachaTypeStats(long uid) { var statsList = new List(); @@ -206,26 +216,48 @@ private async Task ExportAsExcelAsync(long uid, string output) - public override long ImportGachaLog(string file) + // todo + public override List CheckUIGFItems(List list, long uid, string lang) + { + // var infos = GetItemsInfo(); + // foreach (var item in list) + // { + // infos.TryGetValue(item.ItemId, out ZZZGachaInfo? info); + // if (item.GachaType == 0 || item.ItemId == 0 || item.Id == 0) + // throw new JsonException("Missing required properties."); + // item.Uid = uid; + // if (item.Count == 0) + // item.Count = 1; + // item.Name ??= info?.Name ?? ""; + // item.ItemType ??= ""; + // if (item.RankType == 0) + // item.RankType = info?.Level ?? 0; + // item.Lang ??= lang; + // } + return list; + } + + + + public override List ImportGachaLog(string file) { var str = File.ReadAllText(file); - var obj = JsonSerializer.Deserialize(str)?.nap.FirstOrDefault(); - if (obj != null) + var count = 0; + List uids = []; + var obj = (JsonSerializer.Deserialize(str)?.nap) ?? throw new JsonException("Unsupported Json Structures."); + foreach (var user in obj) { - var lang = obj.lang ?? ""; - long uid = obj.uid; - foreach (var item in obj.list) - { - item.Lang ??= lang; - if (item.Uid == 0) - item.Uid = uid; - } - var count = InsertGachaLogItems(obj.list.ToList()); - // 成功导入调频记录 {count} 条 - NotificationBehavior.Instance.Success($"Uid {obj.uid}", string.Format(Lang.ZZZGachaService_ImportSignalSearchSuccessfully, count), 5000); - return obj.uid; + var lang = user.lang ?? ""; + var uid = user.uid; + var list = CheckUIGFItems(user.list.ToList(), uid, lang); + uids.Add(uid); + count += InsertGachaLogItems(list); } - return 0; + if (uids.Count == 0) + throw new JsonException("Unsupported Json Structures."); + // 成功导入调频记录 {count} 条 + NotificationBehavior.Instance.Success($"Uid {string.Join(" ", uids)}", string.Format(Lang.ZZZGachaService_ImportSignalSearchSuccessfully, count), 5000); + return uids; }