1
0
mirror of synced 2024-12-18 17:35:55 +01:00
TaikoLocalServer/TaikoWebUI/Services/GameDataService.cs

230 lines
12 KiB
C#
Raw Normal View History

2023-09-09 14:58:20 +02:00
using System.Collections.Immutable;
namespace TaikoWebUI.Services;
public class GameDataService : IGameDataService
{
private readonly HttpClient client;
private ImmutableDictionary<uint, DanData> danMap = ImmutableDictionary<uint, DanData>.Empty;
private Dictionary<uint, MusicDetail>? musicDetailDictionary = new();
private List<Costume>? costumeList;
private Dictionary<uint,Title>? titleDictionary = new();
private bool musicDetailInitialized;
private bool costumesInitialized;
private bool titlesInitialized;
private Dictionary<string, List<uint>>? lockedCostumeDataDictionary = new();
private Dictionary<string, List<uint>>? lockedTitleDataDictionary = new();
2023-09-09 14:58:20 +02:00
public GameDataService(HttpClient client)
{
this.client = client;
}
public async Task InitializeAsync(string dataBaseUrl)
{
dataBaseUrl = dataBaseUrl.TrimEnd('/');
var danData = await client.GetFromJsonAsync<List<DanData>>($"{dataBaseUrl}/data/dan_data.json");
danData.ThrowIfNull();
danMap = danData.ToImmutableDictionary(data => data.DanId);
}
public async Task<Dictionary<uint, MusicDetail>> GetMusicDetailDictionary()
{
if (!musicDetailInitialized)
{
await InitializeMusicDetailAsync();
}
2023-09-09 14:58:20 +02:00
return musicDetailDictionary ?? new Dictionary<uint, MusicDetail>();
}
public async Task<List<Costume>> GetCostumeList()
{
if (!costumesInitialized)
{
await InitializeCostumesAsync();
}
return costumeList ?? new List<Costume>();
}
public async Task<Dictionary<uint, Title>> GetTitleDictionary()
{
if (!titlesInitialized)
{
await InitializeTitlesAsync();
}
return titleDictionary ?? new Dictionary<uint, Title>();
2023-09-09 14:58:20 +02:00
}
public async Task<Dictionary<string, List<uint>>> GetLockedCostumeDataDictionary()
2023-09-09 14:58:20 +02:00
{
if (!costumesInitialized)
{
await InitializeCostumesAsync();
}
return lockedCostumeDataDictionary ?? new Dictionary<string, List<uint>>();
2023-09-09 14:58:20 +02:00
}
public async Task<Dictionary<string, List<uint>>> GetLockedTitleDataDictionary()
2024-03-14 03:27:55 +01:00
{
if (!titlesInitialized)
{
await InitializeTitlesAsync();
}
return lockedTitleDataDictionary ?? new Dictionary<string, List<uint>>();
2024-03-14 03:27:55 +01:00
}
public string GetMusicNameBySongId(Dictionary<uint, MusicDetail> musicDetails, uint songId, string? language = "ja")
2023-09-09 14:58:20 +02:00
{
return musicDetails.TryGetValue(songId, out var musicDetail) ? language switch
{
"ja" => musicDetail.SongName,
"en-US" => musicDetail.SongNameEN,
"fr-FR" => musicDetail.SongNameEN,
"zh-Hans" => musicDetail.SongNameCN,
"zh-Hant" => musicDetail.SongNameCN,
"ko" => musicDetail.SongNameKO,
_ => musicDetail.SongName
} : string.Empty;
2023-09-09 14:58:20 +02:00
}
public string GetMusicArtistBySongId(Dictionary<uint, MusicDetail> musicDetails, uint songId, string? language = "ja")
2023-09-09 14:58:20 +02:00
{
return musicDetails.TryGetValue(songId, out var musicDetail) ? language switch
{
"jp" => musicDetail.ArtistName,
"en-US" => musicDetail.ArtistNameEN,
"fr-FR" => musicDetail.ArtistNameEN,
"zh-Hans" => musicDetail.ArtistNameCN,
"zh-Hant" => musicDetail.ArtistNameCN,
"ko" => musicDetail.ArtistNameKO,
_ => musicDetail.ArtistName
} : string.Empty;
2023-09-09 14:58:20 +02:00
}
public SongGenre GetMusicGenreBySongId(Dictionary<uint, MusicDetail> musicDetails, uint songId)
2023-09-09 14:58:20 +02:00
{
return musicDetails.TryGetValue(songId, out var musicDetail) ? musicDetail.Genre : SongGenre.Variety;
2023-09-09 14:58:20 +02:00
}
public int GetMusicIndexBySongId(Dictionary<uint, MusicDetail> musicDetails, uint songId)
2023-09-09 14:58:20 +02:00
{
return musicDetails.TryGetValue(songId, out var musicDetail) ? musicDetail.Index : int.MaxValue;
2023-09-09 14:58:20 +02:00
}
public ImmutableDictionary<uint, DanData> GetDanMap()
2023-09-09 14:58:20 +02:00
{
return danMap;
2023-09-09 14:58:20 +02:00
}
public int GetMusicStarLevel(Dictionary<uint, MusicDetail> musicDetails, uint songId, Difficulty difficulty)
2023-09-09 14:58:20 +02:00
{
var success = musicDetails.TryGetValue(songId, out var musicDetail);
2023-09-09 14:58:20 +02:00
return difficulty switch
{
Difficulty.None => throw new ArgumentException("Difficulty cannot be none"),
Difficulty.Easy => success ? musicDetail!.StarEasy : 0,
Difficulty.Normal => success ? musicDetail!.StarNormal : 0,
Difficulty.Hard => success ? musicDetail!.StarHard : 0,
Difficulty.Oni => success ? musicDetail!.StarOni : 0,
Difficulty.UraOni => success ? musicDetail!.StarUra : 0,
_ => throw new ArgumentOutOfRangeException(nameof(difficulty), difficulty, null)
};
}
public string GetHeadTitle(IEnumerable<Costume> costumes, uint index, string? language = "ja")
2023-09-09 14:58:20 +02:00
{
return language switch
{
"jp" => costumes.FirstOrDefault(costume => costume.CostumeType == "head" && costume.CostumeId == index)?.CostumeName ?? string.Empty,
"en-US" => costumes.FirstOrDefault(costume => costume.CostumeType == "head" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"fr-FR" => costumes.FirstOrDefault(costume => costume.CostumeType == "head" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"zh-Hans" => costumes.FirstOrDefault(costume => costume.CostumeType == "head" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"zh-Hant" => costumes.FirstOrDefault(costume => costume.CostumeType == "head" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"ko" => costumes.FirstOrDefault(costume => costume.CostumeType == "head" && costume.CostumeId == index)?.CostumeNameKO ?? string.Empty,
_ => costumes.FirstOrDefault(costume => costume.CostumeType == "head" && costume.CostumeId == index)?.CostumeName ?? string.Empty
};
2023-09-09 14:58:20 +02:00
}
public string GetKigurumiTitle(IEnumerable<Costume> costumes, uint index, string? language = "ja")
2023-09-09 14:58:20 +02:00
{
return language switch
{
"jp" => costumes.FirstOrDefault(costume => costume.CostumeType == "kigurumi" && costume.CostumeId == index)?.CostumeName ?? string.Empty,
"en-US" => costumes.FirstOrDefault(costume => costume.CostumeType == "kigurumi" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"fr-FR" => costumes.FirstOrDefault(costume => costume.CostumeType == "kigurumi" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"zh-Hans" => costumes.FirstOrDefault(costume => costume.CostumeType == "kigurumi" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"zh-Hant" => costumes.FirstOrDefault(costume => costume.CostumeType == "kigurumi" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"ko" => costumes.FirstOrDefault(costume => costume.CostumeType == "kigurumi" && costume.CostumeId == index)?.CostumeNameKO ?? string.Empty,
_ => costumes.FirstOrDefault(costume => costume.CostumeType == "kigurumi" && costume.CostumeId == index)?.CostumeName ?? string.Empty
};
2023-09-09 14:58:20 +02:00
}
public string GetBodyTitle(IEnumerable<Costume> costumes, uint index, string? language = "ja")
2023-09-09 14:58:20 +02:00
{
return language switch
{
"jp" => costumes.FirstOrDefault(costume => costume.CostumeType == "body" && costume.CostumeId == index)?.CostumeName ?? string.Empty,
"en-US" => costumes.FirstOrDefault(costume => costume.CostumeType == "body" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"fr-FR" => costumes.FirstOrDefault(costume => costume.CostumeType == "body" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"zh-Hans" => costumes.FirstOrDefault(costume => costume.CostumeType == "body" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"zh-Hant" => costumes.FirstOrDefault(costume => costume.CostumeType == "body" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"ko" => costumes.FirstOrDefault(costume => costume.CostumeType == "body" && costume.CostumeId == index)?.CostumeNameKO ?? string.Empty,
_ => costumes.FirstOrDefault(costume => costume.CostumeType == "body" && costume.CostumeId == index)?.CostumeName ?? string.Empty
};
2023-09-09 14:58:20 +02:00
}
public string GetFaceTitle(IEnumerable<Costume> costumes, uint index, string? language = "ja")
2023-09-09 14:58:20 +02:00
{
return language switch
{
"jp" => costumes.FirstOrDefault(costume => costume.CostumeType == "face" && costume.CostumeId == index)?.CostumeName ?? string.Empty,
"en-US" => costumes.FirstOrDefault(costume => costume.CostumeType == "face" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"fr-FR" => costumes.FirstOrDefault(costume => costume.CostumeType == "face" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"zh-Hans" => costumes.FirstOrDefault(costume => costume.CostumeType == "face" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"zh-Hant" => costumes.FirstOrDefault(costume => costume.CostumeType == "face" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"ko" => costumes.FirstOrDefault(costume => costume.CostumeType == "face" && costume.CostumeId == index)?.CostumeNameKO ?? string.Empty,
_ => costumes.FirstOrDefault(costume => costume.CostumeType == "face" && costume.CostumeId == index)?.CostumeName ?? string.Empty
};
2023-09-09 14:58:20 +02:00
}
public string GetPuchiTitle(IEnumerable<Costume> costumes, uint index, string? language = "ja")
{
return language switch
{
"jp" => costumes.FirstOrDefault(costume => costume.CostumeType == "puchi" && costume.CostumeId == index)?.CostumeName ?? string.Empty,
"en-US" => costumes.FirstOrDefault(costume => costume.CostumeType == "puchi" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"fr-FR" => costumes.FirstOrDefault(costume => costume.CostumeType == "puchi" && costume.CostumeId == index)?.CostumeNameEN ?? string.Empty,
"zh-Hans" => costumes.FirstOrDefault(costume => costume.CostumeType == "puchi" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"zh-Hant" => costumes.FirstOrDefault(costume => costume.CostumeType == "puchi" && costume.CostumeId == index)?.CostumeNameCN ?? string.Empty,
"ko" => costumes.FirstOrDefault(costume => costume.CostumeType == "puchi" && costume.CostumeId == index)?.CostumeNameKO ?? string.Empty,
_ => costumes.FirstOrDefault(costume => costume.CostumeType == "puchi" && costume.CostumeId == index)?.CostumeName ?? string.Empty
};
}
private async Task InitializeMusicDetailAsync()
{
musicDetailDictionary = await client.GetFromJsonAsync<Dictionary<uint, MusicDetail>>("api/GameData/MusicDetails");
musicDetailInitialized = true;
}
private async Task InitializeCostumesAsync()
{
costumeList = await client.GetFromJsonAsync<List<Costume>>("api/GameData/Costumes");
lockedCostumeDataDictionary = await client.GetFromJsonAsync<Dictionary<string, List<uint>>>("api/GameData/LockedCostumes");
costumesInitialized = true;
}
private async Task InitializeTitlesAsync()
{
titleDictionary = await client.GetFromJsonAsync<Dictionary<uint, Title>>("api/GameData/Titles");
lockedTitleDataDictionary = await client.GetFromJsonAsync<Dictionary<string, List<uint>>>("api/GameData/LockedTitles");
titlesInitialized = true;
}
2023-09-09 14:58:20 +02:00
}