2022-09-09 14:31:45 +02:00
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
|
using System.Net.Http.Json;
|
2022-09-10 17:45:18 +02:00
|
|
|
|
using SharedProject.Enums;
|
2022-09-09 14:31:45 +02:00
|
|
|
|
using TaikoWebUI.Shared.Models;
|
|
|
|
|
using Throw;
|
|
|
|
|
|
|
|
|
|
namespace TaikoWebUI.Services;
|
|
|
|
|
|
|
|
|
|
public class GameDataService : IGameDataService
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpClient client;
|
|
|
|
|
|
2022-09-10 17:45:18 +02:00
|
|
|
|
private readonly Dictionary<uint, MusicDetail> musicMap = new();
|
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-09 14:31:45 +02:00
|
|
|
|
|
|
|
|
|
musicInfo.ThrowIfNull();
|
|
|
|
|
wordList.ThrowIfNull();
|
2022-09-10 17:45:18 +02:00
|
|
|
|
musicOrder.ThrowIfNull();
|
2022-09-09 14:31:45 +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-09 14:31:45 +02:00
|
|
|
|
foreach (var music in musicInfo.Items)
|
|
|
|
|
{
|
|
|
|
|
var songNameKey = $"song_{music.Id}";
|
|
|
|
|
var songArtistKey = $"song_sub_{music.Id}";
|
|
|
|
|
|
|
|
|
|
var musicName = dict.GetValueOrDefault(songNameKey, new WordListEntry());
|
|
|
|
|
var musicArtist = dict.GetValueOrDefault(songArtistKey, new WordListEntry());
|
2022-09-10 17:45:18 +02:00
|
|
|
|
|
|
|
|
|
var musicSongId = music.SongId;
|
|
|
|
|
var musicDetail = new MusicDetail
|
|
|
|
|
{
|
|
|
|
|
SongId = musicSongId,
|
|
|
|
|
SongName = musicName.JapaneseText,
|
|
|
|
|
ArtistName = musicArtist.JapaneseText,
|
|
|
|
|
Genre = music.Genre
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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-09 14:31:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetMusicNameBySongId(uint songId)
|
|
|
|
|
{
|
2022-09-10 17:45:18 +02:00
|
|
|
|
return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.SongName : string.Empty;
|
2022-09-09 14:31:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetMusicArtistBySongId(uint songId)
|
|
|
|
|
{
|
2022-09-10 17:45:18 +02:00
|
|
|
|
return musicMap.TryGetValue(songId, out var musicDetail) ? musicDetail.ArtistName : string.Empty;
|
2022-09-09 14:31:45 +02:00
|
|
|
|
}
|
2022-09-10 17:45:18 +02:00
|
|
|
|
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-09 14:31:45 +02:00
|
|
|
|
}
|