Skip to content

Commit

Permalink
fix: 修复在和远端/本地备份文件同步时间时不合并时间的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
GoldenPotato137 committed Nov 30, 2024
1 parent 8c9ba97 commit 3cadbef
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 96 deletions.
22 changes: 11 additions & 11 deletions GalgameManager/Models/BgTasks/PvnSyncTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ private async Task PullUpdates(IPvnService pvnService, GalgameCollectionService
{
GalgameDto dto = changedGalgames[index];
Galgame? game = gameService.GetGalgameFromId(dto.id.ToString(), RssType.PotatoVn);
game ??= gameService.GetGalgameFromId(dto.bgmId, RssType.Bangumi);
game ??= gameService.GetGalgameFromId(dto.vndbId, RssType.Vndb);
game ??= gameService.GetGalgameFromName(dto.name);
game ??= gameService.GetGalgameFromName(dto.cnName);
game ??= gameService.GetGalgameFromUid(new GalgameUid
{
BangumiId = dto.bgmId,
VndbId = dto.vndbId,
Name = dto.name ?? string.Empty,
CnName = dto.cnName,
});

await UiThreadInvokeHelper.InvokeAsync(async Task() =>
{
Expand Down Expand Up @@ -115,13 +118,10 @@ await UiThreadInvokeHelper.InvokeAsync(async Task() =>

if (dto.playTime is not null)
{
dto.playTime.Sort((a, b) => a.dateTimeStamp.CompareTo(b.dateTimeStamp));
game.PlayedTime.Clear();
foreach (PlayLogDto time in dto.playTime)
game.PlayedTime[time.dateTimeStamp.ToDateTime().ToLocalTime().ToStringDefault()] = time.minute;
game.TotalPlayTime = game.PlayedTime.Values.Sum();
if(dto.playTime.Count > 0)
game.LastPlayTime = dto.playTime[^1].dateTimeStamp.ToDateTime().ToLocalTime();
Dictionary<string, int> playTime = new();
foreach(PlayLogDto time in dto.playTime)
playTime[time.dateTimeStamp.ToDateTime().ToLocalTime().ToStringDefault()] = time.minute;
game.MergeTime(new Galgame { PlayedTime = playTime });
}

game.PlayType = dto.playType;
Expand Down
104 changes: 22 additions & 82 deletions GalgameManager/Models/Galgame.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Collections.ObjectModel;
using System.Globalization;
using Windows.Foundation.Metadata;
using CommunityToolkit.Mvvm.ComponentModel;
using GalgameManager.Contracts;
using GalgameManager.Core.Contracts.Services;
using GalgameManager.Enums;
using GalgameManager.Helpers;
using GalgameManager.Helpers.Phrase;
Expand All @@ -21,7 +19,7 @@ public partial class Galgame : ObservableObject, IDisplayableGameObject
public static readonly int PhraserNumber = 6;

public event Action<Galgame, string, object>? GalPropertyChanged;
public event GenericDelegate<Exception>? ErrorOccurred; //非致命异常产生时触发
public event Action<Exception>? ErrorOccurred; //非致命异常产生时触发

public string Path
{
Expand Down Expand Up @@ -172,24 +170,6 @@ public void Delete()
{
new DirectoryInfo(Path).Delete(true);
}

/// <summary>
/// 时间转换
/// </summary>
/// <param name="time">年/月/日</param>
/// <returns></returns>
public static long GetTime(string time)
{
if (time == DefaultString)
return 0;
if (DateTime.TryParseExact(time, "yyyy/M/d", CultureInfo.InvariantCulture, DateTimeStyles.None,
out DateTime dateTime))
{
return (long)(dateTime - DateTime.MinValue).TotalDays;
}

return 0;
}

/// <summary>
/// 获取该游戏的本地文件夹路径,若其不是本地游戏则返回null
Expand Down Expand Up @@ -222,21 +202,6 @@ public List<string> GetSubFolders()
return result;
}

/// <summary>
/// 获取该游戏信息文件夹地址
/// </summary>
/// <returns></returns>
public string GetMetaPath()
{
return SourceType switch
{
GalgameSourceType.LocalFolder => SystemPath.Combine(Path, MetaPath),
GalgameSourceType.LocalZip when SystemPath.GetDirectoryName(Path) is { } p =>
SystemPath.Combine(p, MetaPath, SystemPath.GetFileNameWithoutExtension(Path)),
_ => ""
};
}

/// <summary>
/// 获取用来保存meta信息的galgame,用于序列化
/// </summary>
Expand Down Expand Up @@ -302,52 +267,6 @@ public Galgame GetMetaCopy(string metaPath, string gamePath)
return result;
}

/// <summary>
/// 从meta信息中恢复游戏信息
/// </summary>
/// <param name="meta">待恢复的数据</param>
/// <param name="metaFolderPath">meta文件夹路径</param>
/// <returns>恢复过后的信息</returns>
public static Galgame ResolveMetaFromLocalFolder(Galgame meta,string metaFolderPath)
{
if(meta.SourceType is not (GalgameSourceType.LocalFolder or GalgameSourceType.LocalZip))return meta;
meta = App.GetService<IFileService>().Read<Galgame>(metaFolderPath, "meta.json")!;
meta.Path = SystemPath.GetFullPath(SystemPath.Combine(metaFolderPath, meta.Path));
if (meta.Path.EndsWith('\\')) meta.Path = meta.Path[..^1];
if (meta.ImagePath.Value != DefaultImagePath)
{
meta.ImagePath.Value = SystemPath.GetFullPath(SystemPath.Combine(metaFolderPath, meta.ImagePath.Value!));
if(File.Exists(meta.ImagePath) == false)
meta.ImagePath.Value = DefaultImagePath;
}
foreach (GalgameCharacter character in meta.Characters)
{
character.ImagePath = SystemPath.GetFullPath(SystemPath.Combine(metaFolderPath, character.ImagePath));
if (!File.Exists(character.ImagePath))
character.ImagePath = DefaultImagePath;
character.PreviewImagePath = SystemPath.GetFullPath(SystemPath.Combine(metaFolderPath, character.PreviewImagePath));
if (!File.Exists(character.PreviewImagePath))
character.PreviewImagePath = DefaultImagePath;
}
meta.UpdateIdFromMixed();
if (meta.SourceType == GalgameSourceType.LocalFolder)
{
if (meta.ExePath != null)
{
meta.ExePath = SystemPath.GetFullPath(SystemPath.Combine(metaFolderPath, meta.ExePath));
if (!File.Exists(meta.ExePath))
meta.ExePath = null;
}
meta.SavePath = Directory.Exists(meta.SavePath) ? meta.SavePath : null; //检查存档路径是否存在并设置SavePosition字段
meta.FindSaveInPath();
}
else
{
meta.ExePath = null;
}
return meta;
}

/// <summary>
/// 从混合数据源的id更新其他数据源的id
/// </summary>
Expand Down Expand Up @@ -395,6 +314,27 @@ public void FindSaveInPath()

/// 检查是否所有的id都为空
public bool IsIdsEmpty() => Ids.All(string.IsNullOrEmpty);

/// <summary>
/// 合并各种时间信息<br/>
/// PlayedTime, LastPlayTime, ReleaseDate
/// </summary>
public void MergeTime(Galgame? other)
{
if (other is null) return;
// 合并PlayedTime
foreach (var (key, value) in other.PlayedTime)
{
if (!PlayedTime.TryAdd(key, value))
PlayedTime[key] = int.Max(value, PlayedTime[key]);
}
// 排序PlayedTime
PlayedTime = PlayedTime.OrderBy(pair => Utils.TryParseDateGuessCulture(pair.Key))
.ToDictionary(pair => pair.Key, pair => pair.Value);
TotalPlayTime = PlayedTime.Values.Sum();
LastPlayTime = PlayedTime.Keys.Select(Utils.TryParseDateGuessCulture).Max();
ReleaseDate.Value = other.ReleaseDate.Value > ReleaseDate.Value ? other.ReleaseDate.Value : ReleaseDate.Value;
}

public string GetLogName() => $"Galgame_{Url.ToBase64().Replace("/", "").Replace("=", "")}.txt";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task<Galgame> AddGameAsync(GalgameSourceType sourceType, string pat
// 检查该游戏是否已经存在
if (GetGalgameFromUid(meta.Uid) is { } existGame)
{
Galgame tmp = await DealWithExistGameAsync(sourceType, path, existGame);
Galgame tmp = await DealWithExistGameAsync(sourceType, path, existGame, meta);
GalgameChangedEvent?.Invoke(tmp);
return tmp;
}
Expand All @@ -56,7 +56,7 @@ public async Task<Galgame> AddGameAsync(GalgameSourceType sourceType, string pat

public async Task<Galgame> SetLocalPathAsync(Galgame galgame, string path)
{
Galgame result = await DealWithExistGameAsync(GalgameSourceType.LocalFolder, path, galgame);
Galgame result = await DealWithExistGameAsync(GalgameSourceType.LocalFolder, path, galgame, null);
GalgameChangedEvent?.Invoke(result);
await SaveGalgamesAsync(result);
return result;
Expand All @@ -80,8 +80,10 @@ private async Task<string> GetNameFromPath(GalgameSourceType sourceType, string
throw new PvnException(string.Empty);
}

private async Task<Galgame> DealWithExistGameAsync(GalgameSourceType type, string path, Galgame existGame)
private async Task<Galgame> DealWithExistGameAsync(GalgameSourceType type, string path, Galgame existGame,
Galgame? meta)
{
existGame.MergeTime(meta);
switch (type)
{
case GalgameSourceType.LocalFolder:
Expand Down

0 comments on commit 3cadbef

Please sign in to comment.