2022-09-09 14:31:45 +02:00
|
|
|
|
using System.Collections.Immutable;
|
2022-09-12 16:17:15 +02:00
|
|
|
|
using Swan.Mapping;
|
2022-09-09 14:31:45 +02:00
|
|
|
|
using TaikoWebUI.Shared.Models;
|
|
|
|
|
|
|
|
|
|
namespace TaikoWebUI.Services;
|
|
|
|
|
|
|
|
|
|
public class GameDataService : IGameDataService
|
|
|
|
|
{
|
2022-09-15 09:27:27 +02:00
|
|
|
|
private readonly string[] bodyTitles = new string[Constants.COSTUME_BODY_MAX];
|
2022-09-09 14:31:45 +02:00
|
|
|
|
private readonly HttpClient client;
|
2022-09-15 09:27:27 +02:00
|
|
|
|
private readonly string[] faceTitles = new string[Constants.COSTUME_FACE_MAX];
|
|
|
|
|
|
|
|
|
|
private readonly string[] headTitles = new string[Constants.COSTUME_HEAD_MAX];
|
|
|
|
|
private readonly string[] kigurumiMTitles = new string[Constants.COSTUME_KIGURUMI_MAX];
|
2022-09-09 14:31:45 +02:00
|
|
|
|
|
2022-09-10 17:45:18 +02:00
|
|
|
|
private readonly Dictionary<uint, MusicDetail> musicMap = new();
|
2022-09-15 09:27:27 +02:00
|
|
|
|
private readonly string[] puchiTitles = new string[Constants.COSTUME_PUCHI_MAX];
|
2022-09-09 14:31:45 +02:00
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
private ImmutableDictionary<uint, DanData> danMap = ImmutableDictionary<uint, DanData>.Empty;
|
2022-09-11 18:28:12 +02:00
|
|
|
|
|
2022-09-15 11:47:46 +02:00
|
|
|
|
private ImmutableHashSet<Title> titles = ImmutableHashSet<Title>.Empty;
|
2022-09-14 23:20:55 +02:00
|
|
|
|
|
2022-09-09 14:31:45 +02:00
|
|
|
|
public GameDataService(HttpClient client)
|
|
|
|
|
{
|
|
|
|
|
this.client = client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task InitializeAsync(string dataBaseUrl)
|
|
|
|
|
{
|
2022-09-09 19:27:20 +02:00
|
|
|
|
var musicInfo = await client.GetFromJsonAsync<MusicInfo>($"{dataBaseUrl}/data/musicinfo.json");
|
|
|
|
|
var wordList = await client.GetFromJsonAsync<WordList>($"{dataBaseUrl}/data/wordlist.json");
|
2022-09-10 17:45:18 +02:00
|
|
|
|
var musicOrder = await client.GetFromJsonAsync<MusicOrder>($"{dataBaseUrl}/data/music_order.json");
|
2022-09-11 18:28:12 +02:00
|
|
|
|
var danData = await client.GetFromJsonAsync<List<DanData>>($"{dataBaseUrl}/data/dan_data.json");
|
2022-09-09 14:31:45 +02:00
|
|
|
|
|
|
|
|
|
musicInfo.ThrowIfNull();
|
|
|
|
|
wordList.ThrowIfNull();
|
2022-09-10 17:45:18 +02:00
|
|
|
|
musicOrder.ThrowIfNull();
|
2022-09-11 18:28:12 +02:00
|
|
|
|
danData.ThrowIfNull();
|
|
|
|
|
|
|
|
|
|
danMap = danData.ToImmutableDictionary(data => data.DanId);
|
2022-09-15 09:27:27 +02:00
|
|
|
|
|
2022-09-10 17:45:18 +02:00
|
|
|
|
// To prevent duplicate entries in wordlist
|
2022-09-09 17:21:46 +02:00
|
|
|
|
var dict = wordList.WordListEntries.GroupBy(entry => entry.Key)
|
|
|
|
|
.ToImmutableDictionary(group => group.Key, group => group.First());
|
2022-09-15 09:27:27 +02:00
|
|
|
|
await Task.Run(() => InitializeMusicMap(musicInfo, dict, musicOrder));
|
2022-09-15 07:18:50 +02:00
|
|
|
|
|
|
|
|
|
await Task.Run(() => InitializeHeadTitles(dict));
|
|
|
|
|
await Task.Run(() => InitializeFaceTitles(dict));
|
|
|
|
|
await Task.Run(() => InitializeBodyTitles(dict));
|
|
|
|
|
await Task.Run(() => InitializePuchiTitles(dict));
|
|
|
|
|
await Task.Run(() => InitializeKigurumiTitles(dict));
|
|
|
|
|
await Task.Run(() => InitializeTitles(dict));
|
|
|
|
|
}
|
2022-09-14 20:17:46 +02:00
|
|
|
|
|
2022-09-15 09:27:27 +02:00
|
|
|
|
public string GetMusicNameBySongId(uint songId)
|
|
|
|
|
{
|
|
|
|
|
return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.SongName : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetMusicArtistBySongId(uint songId)
|
|
|
|
|
{
|
|
|
|
|
return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.ArtistName : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SongGenre GetMusicGenreBySongId(uint songId)
|
|
|
|
|
{
|
|
|
|
|
return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.Genre : SongGenre.Variety;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetMusicIndexBySongId(uint songId)
|
|
|
|
|
{
|
|
|
|
|
return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.Index : int.MaxValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DanData GetDanDataById(uint danId)
|
|
|
|
|
{
|
|
|
|
|
return danMap.GetValueOrDefault(danId, new DanData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetMusicStarLevel(uint songId, Difficulty difficulty)
|
|
|
|
|
{
|
|
|
|
|
var success = musicMap.TryGetValue(songId, out var musicDetail);
|
|
|
|
|
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(uint index)
|
|
|
|
|
{
|
|
|
|
|
return index < headTitles.Length ? headTitles[index] : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetKigurumiTitle(uint index)
|
|
|
|
|
{
|
|
|
|
|
return index < kigurumiMTitles.Length ? kigurumiMTitles[index] : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetBodyTitle(uint index)
|
|
|
|
|
{
|
|
|
|
|
return index < bodyTitles.Length ? bodyTitles[index] : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetFaceTitle(uint index)
|
|
|
|
|
{
|
|
|
|
|
return index < faceTitles.Length ? faceTitles[index] : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetPuchiTitle(uint index)
|
|
|
|
|
{
|
|
|
|
|
return index < puchiTitles.Length ? puchiTitles[index] : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 11:47:46 +02:00
|
|
|
|
public ImmutableHashSet<Title> GetTitles()
|
2022-09-15 09:27:27 +02:00
|
|
|
|
{
|
2022-09-15 11:47:46 +02:00
|
|
|
|
return titles;
|
2022-09-15 09:27:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
private void InitializeTitles(ImmutableDictionary<string, WordListEntry> dict)
|
|
|
|
|
{
|
2022-09-15 11:47:46 +02:00
|
|
|
|
var set = ImmutableHashSet.CreateBuilder<Title>();
|
2022-09-15 07:18:50 +02:00
|
|
|
|
for (var i = 1; i < Constants.PLAYER_TITLE_MAX; i++)
|
|
|
|
|
{
|
|
|
|
|
var key = $"syougou_{i}";
|
2022-09-10 17:45:18 +02:00
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
var titleWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
|
2022-09-10 17:45:18 +02:00
|
|
|
|
|
2022-09-15 11:47:46 +02:00
|
|
|
|
set.Add(new Title{
|
|
|
|
|
TitleName = titleWordlistItem.JapaneseText,
|
|
|
|
|
TitleId = i
|
|
|
|
|
});
|
2022-09-10 17:45:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
titles = set.ToImmutable();
|
|
|
|
|
}
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
private void InitializePuchiTitles(ImmutableDictionary<string, WordListEntry> dict)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < Constants.COSTUME_PUCHI_MAX; i++)
|
2022-09-14 21:36:12 +02:00
|
|
|
|
{
|
2022-09-15 07:18:50 +02:00
|
|
|
|
var key = $"costume_puchi_{i}";
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
|
|
|
|
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
|
2022-09-15 07:18:50 +02:00
|
|
|
|
puchiTitles[i] = costumeWordlistItem.JapaneseText;
|
2022-09-14 21:36:12 +02:00
|
|
|
|
}
|
2022-09-15 07:18:50 +02:00
|
|
|
|
}
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
private void InitializeKigurumiTitles(ImmutableDictionary<string, WordListEntry> dict)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < Constants.COSTUME_KIGURUMI_MAX; i++)
|
2022-09-14 21:36:12 +02:00
|
|
|
|
{
|
2022-09-15 07:18:50 +02:00
|
|
|
|
var key = $"costume_kigurumi_{i}";
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
|
|
|
|
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
|
2022-09-15 07:18:50 +02:00
|
|
|
|
kigurumiMTitles[i] = costumeWordlistItem.JapaneseText;
|
2022-09-14 21:36:12 +02:00
|
|
|
|
}
|
2022-09-15 07:18:50 +02:00
|
|
|
|
}
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
private void InitializeBodyTitles(ImmutableDictionary<string, WordListEntry> dict)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < Constants.COSTUME_BODY_MAX; i++)
|
2022-09-14 21:36:12 +02:00
|
|
|
|
{
|
2022-09-15 07:18:50 +02:00
|
|
|
|
var key = $"costume_body_{i}";
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
|
|
|
|
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
|
2022-09-15 07:18:50 +02:00
|
|
|
|
bodyTitles[i] = costumeWordlistItem.JapaneseText;
|
2022-09-14 21:36:12 +02:00
|
|
|
|
}
|
2022-09-15 07:18:50 +02:00
|
|
|
|
}
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
private void InitializeFaceTitles(ImmutableDictionary<string, WordListEntry> dict)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < Constants.COSTUME_FACE_MAX; i++)
|
2022-09-14 21:36:12 +02:00
|
|
|
|
{
|
2022-09-15 07:18:50 +02:00
|
|
|
|
var key = $"costume_face_{i}";
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
|
|
|
|
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
|
2022-09-15 07:18:50 +02:00
|
|
|
|
faceTitles[i] = costumeWordlistItem.JapaneseText;
|
2022-09-14 21:36:12 +02:00
|
|
|
|
}
|
2022-09-15 07:18:50 +02:00
|
|
|
|
}
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
private void InitializeHeadTitles(ImmutableDictionary<string, WordListEntry> dict)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < Constants.COSTUME_HEAD_MAX; i++)
|
2022-09-14 21:36:12 +02:00
|
|
|
|
{
|
2022-09-15 07:18:50 +02:00
|
|
|
|
var key = $"costume_head_{i}";
|
2022-09-14 21:36:12 +02:00
|
|
|
|
|
|
|
|
|
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
|
2022-09-15 07:18:50 +02:00
|
|
|
|
headTitles[i] = costumeWordlistItem.JapaneseText;
|
2022-09-14 21:36:12 +02:00
|
|
|
|
}
|
2022-09-15 07:18:50 +02:00
|
|
|
|
}
|
2022-09-14 23:20:55 +02:00
|
|
|
|
|
2022-09-15 09:27:27 +02:00
|
|
|
|
private void InitializeMusicMap(MusicInfo musicInfo, ImmutableDictionary<string, WordListEntry> dict,
|
|
|
|
|
MusicOrder musicOrder)
|
2022-09-15 07:18:50 +02:00
|
|
|
|
{
|
|
|
|
|
foreach (var music in musicInfo.Items)
|
2022-09-14 23:20:55 +02:00
|
|
|
|
{
|
2022-09-15 07:18:50 +02:00
|
|
|
|
var songNameKey = $"song_{music.Id}";
|
|
|
|
|
var songArtistKey = $"song_sub_{music.Id}";
|
2022-09-14 23:20:55 +02:00
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
var musicName = dict.GetValueOrDefault(songNameKey, new WordListEntry());
|
|
|
|
|
var musicArtist = dict.GetValueOrDefault(songArtistKey, new WordListEntry());
|
|
|
|
|
|
|
|
|
|
var musicSongId = music.SongId;
|
|
|
|
|
var musicDetail = music.CopyPropertiesToNew<MusicDetail>();
|
|
|
|
|
musicDetail.SongName = musicName.JapaneseText;
|
|
|
|
|
musicDetail.ArtistName = musicArtist.JapaneseText;
|
2022-09-14 23:20:55 +02:00
|
|
|
|
|
2022-09-15 07:18:50 +02:00
|
|
|
|
musicMap.TryAdd(musicSongId, musicDetail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var index = 0; index < musicOrder.Order.Count; index++)
|
|
|
|
|
{
|
|
|
|
|
var musicOrderEntry = musicOrder.Order[index];
|
|
|
|
|
var songId = musicOrderEntry.SongId;
|
|
|
|
|
if (musicMap.ContainsKey(songId))
|
2022-09-14 23:20:55 +02:00
|
|
|
|
{
|
2022-09-15 07:18:50 +02:00
|
|
|
|
musicMap[songId].Index = index;
|
2022-09-14 23:20:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-09 14:31:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|