Skip to content

Commit

Permalink
feat: Adapt to UIGF 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Joker committed Sep 19, 2024
1 parent 874cd2a commit d3aecd5
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 91 deletions.
9 changes: 9 additions & 0 deletions src/Starward.Language/Lang.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Starward.Language/Lang.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1582,4 +1582,7 @@ Do you accept the risk and continue to use it?</value>
<data name="GameLauncherPage_RemovableStorageDeviceNotConnected" xml:space="preserve">
<value>Removable storage device not connected.</value>
</data>
<data name="ZZZGachaService_ImportSignalSearchSuccessfully" xml:space="preserve">
<value>Import {0} signal search(es) successfully.</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/Starward.Language/Lang.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1582,4 +1582,7 @@
<data name="GameLauncherPage_RemovableStorageDeviceNotConnected" xml:space="preserve">
<value>可移动存储设备未连接</value>
</data>
<data name="ZZZGachaService_ImportSignalSearchSuccessfully" xml:space="preserve">
<value>成功导入调频记录 {0} 条</value>
</data>
</root>
5 changes: 4 additions & 1 deletion src/Starward.Language/Lang.zh-TW.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@
<value>驗證失敗</value>
</data>
<data name="AboutSettingPage_CurrentVersionIsNotTheLatestVersion" xml:space="preserve">
<value>目前版本不是最新版本({0})。</value>
<value>目前版本不是最新版本({0})。</value>
</data>
<data name="AboutSettingPage_ClickButtonToRepairFiles" xml:space="preserve">
<value>按下按鈕修復檔案</value>
Expand Down Expand Up @@ -1582,4 +1582,7 @@
<data name="GameLauncherPage_RemovableStorageDeviceNotConnected" xml:space="preserve">
<value>Removable storage device not connected.</value>
</data>
<data name="ZZZGachaService_ImportSignalSearchSuccessfully" xml:space="preserve">
<value>成功匯入 {0} 條調頻記錄</value>
</data>
</root>
10 changes: 9 additions & 1 deletion src/Starward/Pages/GachaLogPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button x:Name="Button_Export_Excel"
HorizontalAlignment="Stretch"
Expand All @@ -166,7 +167,14 @@
HorizontalAlignment="Stretch"
Command="{x:Bind ExportGachaLogCommand}"
CommandParameter="json"
Content="Json"
Content="UIGF4.0"
CornerRadius="0,0,0,0" />
<Button x:Name="Button_Export_JSON_old"
Grid.Column="2"
HorizontalAlignment="Stretch"
Command="{x:Bind ExportGachaLogCommand}"
CommandParameter="json_old"
Content="UIGF3.0"
CornerRadius="0,4,4,0" />
</Grid>
<Button x:Name="Button_Import"
Expand Down
7 changes: 4 additions & 3 deletions src/Starward/Pages/GachaLogPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
EnableStarRailGachaItemStats = true;
_gachaLogService = AppConfig.GetService<StarRailGachaService>();
Image_Emoji.Source = new BitmapImage(AppConfig.EmojiPom);
Button_Export_JSON_old.Content = "SRGF";
}
if (biz.ToGame() == GameBiz.nap)
{
IsZZZGachaStatsCardVisible = true;
_gachaLogService = AppConfig.GetService<ZZZGachaService>();
Image_Emoji.Source = new BitmapImage(AppConfig.EmojiBangboo);
MenuFlyoutItem_CloudGame.Visibility = Visibility.Collapsed;
Button_Export_Excel.IsEnabled = false;
Button_Export_JSON.IsEnabled = false;
Button_Import.IsEnabled = false;
Button_Export_JSON_old.Visibility = Visibility.Collapsed;
Button_Export_JSON.CornerRadius = new CornerRadius(0, 4, 4, 0);
}
if (biz.IsGlobalOfficial())
{
Expand Down Expand Up @@ -666,6 +666,7 @@ private async Task ExportGachaLogAsync(string format)
{
"excel" => "xlsx",
"json" => "json",
"json_old" => "json",
_ => "json"
};
var suggestName = $"Starward_Export_{CurrentGameBiz.ToGame()}_{uid}_{DateTime.Now:yyyyMMddHHmmss}.{ext}";
Expand Down
139 changes: 115 additions & 24 deletions src/Starward/Services/Gacha/GenshinGachaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,26 @@ INSERT OR REPLACE INTO GenshinGachaItem (Uid, Id, Name, Time, ItemId, ItemType,
public override async Task ExportGachaLogAsync(long uid, string file, string format)
{
if (format is "excel")
{
await ExportAsExcelAsync(uid, file);
}
else
{
else if (format is "json")
await ExportAsJsonAsync(uid, file);
}
else
await ExportAsJsonoldAsync(uid, file);
}



private async Task ExportAsJsonAsync(long uid, string output)
{
using var dapper = _database.CreateConnection();
var list = dapper.Query<UIGFItem>($"SELECT * FROM {GachaTableName} WHERE Uid = @uid ORDER BY Id;", new { uid }).ToList();
var obj = new UIGF40Obj(uid, list);
var str = JsonSerializer.Serialize(obj, AppConfig.JsonSerializerOptions);
await File.WriteAllTextAsync(output, str);
}


private async Task ExportAsJsonoldAsync(long uid, string output)
{
using var dapper = _database.CreateConnection();
var list = dapper.Query<UIGFItem>($"SELECT * FROM {GachaTableName} WHERE Uid = @uid ORDER BY Id;", new { uid }).ToList();
Expand All @@ -126,28 +134,40 @@ private async Task ExportAsExcelAsync(long uid, string output)
public override long ImportGachaLog(string file)
{
var str = File.ReadAllText(file);
var obj = JsonSerializer.Deserialize<UIGFObj>(str);
if (obj != null)
string lang;
long uid;
List<UIGFItem> list;
using (JsonDocument doc = JsonDocument.Parse(str))
{
string lang = obj.info.lang ?? "";
long uid = obj.info.uid;
foreach (var item in obj.list)
if (doc.RootElement.TryGetProperty("info", out JsonElement infoElement) && infoElement.TryGetProperty("version", out _))
{
if (item.Lang is null)
{
item.Lang = lang;
}
if (item.Uid == 0)
{
item.Uid = uid;
}
var obj = JsonSerializer.Deserialize<UIGF40Obj>(str)!.hk4e.FirstOrDefault();
if (obj is null)
return 0;
lang = obj.lang ?? "";
uid = obj.uid;
list = obj.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 count = InsertGachaLogItems(obj.list.ToList<GachaLogItem>());
// 成功导入祈愿记录 {count} 条
NotificationBehavior.Instance.Success($"Uid {obj.info.uid}", string.Format(Lang.GenshinGachaService_ImportWishRecordsSuccessfully, count), 5000);
return obj.info.uid;
else
return 0;
}
foreach (var item in list)
{
item.Lang ??= lang;
if (item.Uid == 0)
item.Uid = uid;
}
return 0;
var count = InsertGachaLogItems(list.ToList<GachaLogItem>());
// 成功导入祈愿记录 {count} 条
NotificationBehavior.Instance.Success($"Uid {uid}", string.Format(Lang.GenshinGachaService_ImportWishRecordsSuccessfully, count), 5000);
return uid;
}


Expand Down Expand Up @@ -235,7 +255,7 @@ private class UIAFInfo

public string export_app_version { get; set; } = AppConfig.AppVersion ?? "";

public string uigf_version { get; set; } = "v2.3";
public string uigf_version { get; set; } = "v3.0";

public int? region_time_zone { get; set; }

Expand All @@ -260,6 +280,77 @@ public UIAFInfo(long uid, List<UIGFItem> list)



private class UIGF40Obj
{
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)];
}

public UIGF40Info info { get; set; }

public List<UIGF40Game> hk4e { get; set; }
}


private class UIGF40Info
{
public string version { get; set; } = "v4.0";

public string export_app { get; set; } = "Starward";

public string export_app_version { get; set; } = AppConfig.AppVersion ?? "";

[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public long export_timestamp { get; set; }

public UIGF40Info()
{
export_timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
}
}


private class UIGF40Game
{
public UIGF40Game() { }

public UIGF40Game(long uid, List<UIGFItem> list)
{
this.uid = uid;
timezone = uid.ToString().FirstOrDefault() switch
{
'6' => -5,
'7' => 1,
_ => 8,
};
lang = list.FirstOrDefault()?.Lang ?? "";
this.list = list;
}

[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public long uid { get; set; }

public int timezone { get; set; }

public string lang { get; set; }

public List<UIGFItem> list { get; set; }
}



private class UIGFItem : GenshinGachaItem
{
public string uigf_gacha_type { get; set; }
Expand Down
Loading

0 comments on commit d3aecd5

Please sign in to comment.