1
0
mirror of synced 2024-12-19 01:46:09 +01:00
TaikoLocalServer/TaikoWebUI/Services/GameDataService.cs

179 lines
6.4 KiB
C#
Raw Normal View History

using System.Collections.Immutable;
2022-09-14 20:17:46 +02:00
using MudBlazor.Utilities;
2022-09-12 16:17:15 +02:00
using Swan.Mapping;
using TaikoWebUI.Shared.Models;
namespace TaikoWebUI.Services;
public class GameDataService : IGameDataService
{
private readonly HttpClient client;
private readonly Dictionary<uint, MusicDetail> musicMap = new();
2022-09-11 18:28:12 +02:00
private ImmutableDictionary<uint, DanData> danMap = null!;
2022-09-14 23:20:55 +02:00
public const int COSTUME_HEAD_MAX = 140;
2022-09-15 00:22:20 +02:00
public const int COSTUME_FACE_MAX = 58;
2022-09-14 23:20:55 +02:00
public const int COSTUME_BODY_MAX = 156;
public const int COSTUME_KIGURUMI_MAX = 154;
public const int COSTUME_PUCHI_MAX = 129;
public const int COSTUME_COLOR_MAX = 63;
2022-09-15 00:22:20 +02:00
public const int PLAYER_TITLE_MAX = 750;
2022-09-14 23:20:55 +02:00
2022-09-14 21:36:12 +02:00
public static string[] headMap = new string[COSTUME_HEAD_MAX];
public static string[] faceMap = new string[COSTUME_FACE_MAX];
public static string[] bodyMap = new string[COSTUME_BODY_MAX];
public static string[] kigurumiMap = new string[COSTUME_KIGURUMI_MAX];
public static string[] puchiMap = new string[COSTUME_PUCHI_MAX];
2022-09-14 20:17:46 +02:00
2022-09-14 23:20:55 +02:00
public static Dictionary<int, string> titleMap = new();
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");
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");
musicInfo.ThrowIfNull();
wordList.ThrowIfNull();
musicOrder.ThrowIfNull();
2022-09-11 18:28:12 +02:00
danData.ThrowIfNull();
danMap = danData.ToImmutableDictionary(data => data.DanId);
// 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());
foreach (var music in musicInfo.Items)
{
var songNameKey = $"song_{music.Id}";
var songArtistKey = $"song_sub_{music.Id}";
2022-09-14 20:17:46 +02:00
var musicName = dict.GetValueOrDefault(songNameKey, new WordListEntry());
var musicArtist = dict.GetValueOrDefault(songArtistKey, new WordListEntry());
var musicSongId = music.SongId;
2022-09-12 16:17:15 +02:00
var musicDetail = music.CopyPropertiesToNew<MusicDetail>();
musicDetail.SongName = musicName.JapaneseText;
musicDetail.ArtistName = musicArtist.JapaneseText;
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))
{
musicMap[songId].Index = index;
}
}
2022-09-14 21:36:12 +02:00
for (var i = 0; i < COSTUME_HEAD_MAX; i++)
{
var index = i;
var key = $"costume_head_{index}";
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
headMap[index] = costumeWordlistItem.JapaneseText;
}
for (var i = 0; i < COSTUME_FACE_MAX; i++)
{
var index = i;
var key = $"costume_face_{index}";
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
faceMap[index] = costumeWordlistItem.JapaneseText;
}
for (var i = 0; i < COSTUME_BODY_MAX; i++)
{
var index = i;
var key = $"costume_body_{index}";
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
bodyMap[index] = costumeWordlistItem.JapaneseText;
}
for (var i = 0; i < COSTUME_KIGURUMI_MAX; i++)
{
var index = i;
var key = $"costume_kigurumi_{index}";
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
kigurumiMap[index] = costumeWordlistItem.JapaneseText;
}
for (var i = 0; i < COSTUME_PUCHI_MAX; i++)
{
var index = i;
var key = $"costume_puchi_{index}";
var costumeWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
puchiMap[index] = costumeWordlistItem.JapaneseText;
}
2022-09-14 23:20:55 +02:00
for (var i = 1; i < PLAYER_TITLE_MAX; i++)
{
var index = i;
var key = $"syougou_{index}";
var titleWordlistItem = dict.GetValueOrDefault(key, new WordListEntry());
// prevent duplicate values with different keys
if (titleMap.ContainsValue(titleWordlistItem.JapaneseText))
{
continue;
}
titleMap.TryAdd(index, titleWordlistItem.JapaneseText);
}
}
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;
}
2022-09-11 18:28:12 +02:00
public DanData GetDanDataById(uint danId)
{
return danMap.GetValueOrDefault(danId, new DanData());
}
2022-09-12 16:17:15 +02:00
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)
};
}
}