Skip to content

Commit

Permalink
feat: Gacha Log supports simultaneous import of multiple UIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Joker committed Sep 19, 2024
1 parent d3aecd5 commit 88a0b68
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 96 deletions.
21 changes: 9 additions & 12 deletions src/Starward/Pages/GachaLogPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Starward/Services/Gacha/GachaLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public virtual async Task<long> GetUidFromGachaLogUrl(string url)



public abstract List<GachaLogItem> CheckUIGFItems(List<GachaLogItem> list, long uid, string lang);



protected abstract int InsertGachaLogItems(List<GachaLogItem> items);


Expand Down Expand Up @@ -272,7 +276,7 @@ public virtual int DeleteUid(long uid)



public abstract long ImportGachaLog(string file);
public abstract List<long> ImportGachaLog(string file);



Expand Down
101 changes: 59 additions & 42 deletions src/Starward/Services/Gacha/GenshinGachaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,21 @@ public override List<GachaLogItemEx> GetGachaLogItemEx(long uid)
}


public Dictionary<int, GenshinGachaInfo> GetItemsInfo()
{
using var dapper = _database.CreateConnection();
return dapper.Query<GenshinGachaInfo>("SELECT Id, Name, Level FROM GenshinGachaInfo").ToDictionary(item => item.Id);
}


protected override int InsertGachaLogItems(List<GachaLogItem> items)
{
using var dapper = _database.CreateConnection();
using var t = dapper.BeginTransaction();
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;
Expand Down Expand Up @@ -131,43 +138,62 @@ private async Task ExportAsExcelAsync(long uid, string output)



public override long ImportGachaLog(string file)
public override List<GachaLogItem> CheckUIGFItems(List<GachaLogItem> 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<long> ImportGachaLog(string file)
{
var str = File.ReadAllText(file);
string lang;
long uid;
List<UIGFItem> list;
var count = 0;
List<long> uids = [];
using (JsonDocument doc = JsonDocument.Parse(str))
{
if (doc.RootElement.TryGetProperty("info", out JsonElement infoElement) && infoElement.TryGetProperty("version", out _))
{
var obj = JsonSerializer.Deserialize<UIGF40Obj>(str)!.hk4e.FirstOrDefault();
if (obj is null)
return 0;
lang = obj.lang ?? "";
uid = obj.uid;
list = obj.list;
var obj = JsonSerializer.Deserialize<UIGF40Obj>(str)!.hk4e;
foreach (var user in obj)
{
var lang = user.lang ?? "";
var uid = user.uid;
var list = CheckUIGFItems(user.list.ToList<GachaLogItem>(), uid, lang);
uids.Add(uid);
count += InsertGachaLogItems(list);
}
}
else if (infoElement.TryGetProperty("uigf_version", out _))
{
var obj = JsonSerializer.Deserialize<UIGFObj>(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<GachaLogItem>(), 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<GachaLogItem>());
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;
}


Expand Down Expand Up @@ -221,14 +247,6 @@ public UIGFObj() { }
public UIGFObj(long uid, List<UIGFItem> 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;
}

Expand Down Expand Up @@ -287,14 +305,6 @@ public UIGF40Obj() { }
public UIGF40Obj(long uid, List<UIGFItem> 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)];
}

Expand Down Expand Up @@ -353,7 +363,14 @@ public UIGF40Game(long uid, List<UIGFItem> 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(),
};
}
}


Expand Down
78 changes: 53 additions & 25 deletions src/Starward/Services/Gacha/StarRailGachaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public override List<GachaLogItemEx> GetGachaLogItemEx(long uid)



public Dictionary<int, StarRailGachaInfo> GetItemsInfo()
{
using var dapper = _database.CreateConnection();
return dapper.Query<StarRailGachaInfo>("SELECT ItemId, ItemName, Rarity FROM StarRailGachaInfo").ToDictionary(item => item.ItemId);
}



protected override int InsertGachaLogItems(List<GachaLogItem> items)
{
using var dapper = _database.CreateConnection();
Expand Down Expand Up @@ -131,43 +139,63 @@ private async Task ExportAsExcelAsync(long uid, string output)



public override long ImportGachaLog(string file)
public override List<GachaLogItem> CheckUIGFItems(List<GachaLogItem> 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<long> ImportGachaLog(string file)
{
var str = File.ReadAllText(file);
string lang;
long uid;
List<StarRailGachaItem> list;
var count = 0;
List<long> uids = [];
using (JsonDocument doc = JsonDocument.Parse(str))
{
if (doc.RootElement.TryGetProperty("info", out JsonElement infoElement) && infoElement.TryGetProperty("version", out _))
{
var obj = JsonSerializer.Deserialize<UIGF40Obj>(str)!.hkrpg.FirstOrDefault();
if (obj is null)
return 0;
lang = obj.lang ?? "";
uid = obj.uid;
list = obj.list;
var obj = JsonSerializer.Deserialize<UIGF40Obj>(str)!.hkrpg;
foreach (var user in obj)
{
var lang = user.lang ?? "";
var uid = user.uid;
var list = CheckUIGFItems(user.list.ToList<GachaLogItem>(), 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<SRGFObj>(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<GachaLogItem>(), 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<GachaLogItem>());
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;
}


Expand Down
64 changes: 48 additions & 16 deletions src/Starward/Services/Gacha/ZZZGachaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public override List<GachaLogItemEx> GetGachaLogItemEx(long uid)



// todo
public Dictionary<int, ZZZGachaInfo> GetItemsInfo()
{
throw new NotImplementedException();
//using var dapper = _database.CreateConnection();
//return dapper.Query<ZZZGachaInfo>("SELECT ItemId, ItemName, Rarity FROM ZZZGachaItem").ToDictionary(item => item.Id);
}



public override (List<GachaTypeStats> GachaStats, List<GachaLogItemEx> ItemStats) GetGachaTypeStats(long uid)
{
var statsList = new List<GachaTypeStats>();
Expand Down Expand Up @@ -206,26 +216,48 @@ private async Task ExportAsExcelAsync(long uid, string output)



public override long ImportGachaLog(string file)
// todo
public override List<GachaLogItem> CheckUIGFItems(List<GachaLogItem> 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<long> ImportGachaLog(string file)
{
var str = File.ReadAllText(file);
var obj = JsonSerializer.Deserialize<UIGF40Obj>(str)?.nap.FirstOrDefault();
if (obj != null)
var count = 0;
List<long> uids = [];
var obj = (JsonSerializer.Deserialize<UIGF40Obj>(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<GachaLogItem>());
// 成功导入调频记录 {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<GachaLogItem>(), 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;
}


Expand Down

0 comments on commit 88a0b68

Please sign in to comment.