1
0
mirror of synced 2024-11-28 00:20:53 +01:00

Merge remote-tracking branch 'origin/master' into CostumeUI

# Conflicts:
#	TaikoLocalServer/Controllers/Game/InitialDataCheckController.cs
#	TaikoLocalServer/TaikoLocalServer.csproj
This commit is contained in:
asesidaa 2022-09-14 21:13:39 +08:00
commit 722cdafba2
14 changed files with 311 additions and 111 deletions

View File

@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
namespace SharedProject.Models;
public class SongIntroductionData
{
[JsonPropertyName("setId")]
public uint SetId { get; set; }
[JsonPropertyName("verupNo")]
public uint VerupNo { get; set; }
[JsonPropertyName("mainSongNo")]
public uint MainSongNo { get; set; }
[JsonPropertyName("subSongNo")]
public uint[]? SubSongNo { get; set; }
}

View File

@ -24,6 +24,8 @@ public static class Constants
public const string DAN_DATA_FILE_NAME = "dan_data.json";
public const string INTRO_DATA_FILE_NAME = "intro_data.json";
public const int MIN_DAN_ID = 1;
public const int MAX_DAN_ID = 19;
public const int GOT_DAN_BITS = MAX_DAN_ID * 4;

View File

@ -1,48 +0,0 @@
using System.Collections.Immutable;
using System.Text.Json;
using SharedProject.Models;
using Swan.Mapping;
namespace TaikoLocalServer.Common.Utils;
public class DanOdaiDataManager
{
public ImmutableDictionary<uint, GetDanOdaiResponse.OdaiData> OdaiDataList { get; }
static DanOdaiDataManager() {}
private DanOdaiDataManager()
{
var dataPath = PathHelper.GetDataPath();
var filePath = Path.Combine(dataPath, Constants.DAN_DATA_FILE_NAME);
var jsonString = File.ReadAllText(filePath);
var result = JsonSerializer.Deserialize<List<DanData>>(jsonString);
if (result is null)
{
throw new ApplicationException("Cannot parse dan data json!");
}
OdaiDataList = result.ToImmutableDictionary(data => data.DanId, ToResponseOdaiData);
}
private GetDanOdaiResponse.OdaiData ToResponseOdaiData(DanData data)
{
var responseOdaiData = new GetDanOdaiResponse.OdaiData
{
DanId = data.DanId,
Title = data.Title,
VerupNo = data.VerupNo
};
var odaiSongs = data.OdaiSongList.Select(song => song.CopyPropertiesToNew<GetDanOdaiResponse.OdaiData.OdaiSong>());
responseOdaiData.AryOdaiSongs.AddRange(odaiSongs);
var odaiBorders = data.OdaiBorderList.Select(border => border.CopyPropertiesToNew<GetDanOdaiResponse.OdaiData.OdaiBorder>());
responseOdaiData.AryOdaiBorders.AddRange(odaiBorders);
return responseOdaiData;
}
public static DanOdaiDataManager Instance { get; } = new();
}

View File

@ -1,42 +0,0 @@
using System.Text.Json;
namespace TaikoLocalServer.Common.Utils;
public class MusicAttributeManager
{
public readonly Dictionary<uint,MusicAttributeEntry> MusicAttributes;
static MusicAttributeManager()
{
}
private MusicAttributeManager()
{
var dataPath = PathHelper.GetDataPath();
var filePath = Path.Combine(dataPath, Constants.MUSIC_ATTRIBUTE_FILE_NAME);
var jsonString = File.ReadAllText(filePath);
var result = JsonSerializer.Deserialize<MusicAttributes>(jsonString);
if (result is null)
{
throw new ApplicationException("Cannot parse music attribute json!");
}
MusicAttributes = result.MusicAttributeEntries.ToDictionary(attribute => attribute.MusicId);
Musics = MusicAttributes.Select(pair => pair.Key)
.ToList();
Musics.Sort();
MusicsWithUra = MusicAttributes.Where(attribute => attribute.Value.HasUra)
.Select(pair => pair.Key)
.ToList();
MusicsWithUra.Sort();
}
public static MusicAttributeManager Instance { get; } = new();
public readonly List<uint> Musics;
public readonly List<uint> MusicsWithUra;
}

View File

@ -1,9 +1,18 @@
namespace TaikoLocalServer.Controllers.Game;
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/getdanodai.php")]
[ApiController]
public class GetDanOdaiController : BaseController<GetDanOdaiController>
{
private readonly IGameDataService gameDataService;
public GetDanOdaiController(IGameDataService gameDataService)
{
this.gameDataService = gameDataService;
}
[HttpPost]
[Produces("application/protobuf")]
public IActionResult GetDanOdai([FromBody] GetDanOdaiRequest request)
@ -20,10 +29,9 @@ public class GetDanOdaiController : BaseController<GetDanOdaiController>
return Ok(response);
}
var manager = DanOdaiDataManager.Instance;
foreach (var danId in request.DanIds)
{
manager.OdaiDataList.TryGetValue(danId, out var odaiData);
gameDataService.GetDanDataDictionary().TryGetValue(danId, out var odaiData);
if (odaiData is null)
{
Logger.LogWarning("Requested dan id {Id} does not exist!", danId);

View File

@ -1,9 +1,18 @@
namespace TaikoLocalServer.Controllers.Game;
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/getsongintroduction.php")]
[ApiController]
public class GetSongIntroductionController : BaseController<GetSongIntroductionController>
{
private readonly IGameDataService gameDataService;
public GetSongIntroductionController(IGameDataService gameDataService)
{
this.gameDataService = gameDataService;
}
[HttpPost]
[Produces("application/protobuf")]
public IActionResult GetSongIntroduction([FromBody] GetSongIntroductionRequest request)
@ -17,13 +26,14 @@ public class GetSongIntroductionController : BaseController<GetSongIntroductionC
foreach (var setId in request.SetIds)
{
response.ArySongIntroductionDatas.Add(new GetSongIntroductionResponse.SongIntroductionData
gameDataService.GetSongIntroDictionary().TryGetValue(setId, out var introData);
if (introData is null)
{
MainSongNo = 2,
SubSongNoes = new uint[] {177,193,3,4},
SetId = setId,
VerupNo = 1
});
Logger.LogWarning("Requested set id {Id} does not exist!", setId);
continue;
}
response.ArySongIntroductionDatas.Add(introData);
}
return Ok(response);

View File

@ -1,4 +1,5 @@
using System.Collections;
using TaikoLocalServer.Services.Interfaces;
namespace TaikoLocalServer.Controllers.Game;
@ -6,6 +7,13 @@ namespace TaikoLocalServer.Controllers.Game;
[Route("/v12r03/chassis/initialdatacheck.php")]
public class InitialDataCheckController : BaseController<InitialDataCheckController>
{
private readonly IGameDataService gameDataService;
public InitialDataCheckController(IGameDataService gameDataService)
{
this.gameDataService = gameDataService;
}
[HttpPost]
[Produces("application/protobuf")]
public IActionResult InitialDataCheck([FromBody] InitialdatacheckRequest request)
@ -27,6 +35,16 @@ public class InitialDataCheckController : BaseController<InitialDataCheckControl
});
}
var introData = new List<InitialdatacheckResponse.InformationData>();
for (var setId = 1; setId <= gameDataService.GetSongIntroDictionary().Count; setId++)
{
introData.Add(new InitialdatacheckResponse.InformationData
{
InfoId = (uint)setId,
VerupNo = 1
});
}
var response = new InitialdatacheckResponse
{
Result = 1,
@ -95,6 +113,7 @@ public class InitialDataCheckController : BaseController<InitialDataCheckControl
});*/
};
response.AryDanOdaiDatas.AddRange(danData);
response.ArySongIntroductionDatas.AddRange(introData);
return Ok(response);
}

View File

@ -9,9 +9,12 @@ public class SelfBestController : BaseController<SelfBestController>
{
private readonly ISongBestDatumService songBestDatumService;
public SelfBestController(ISongBestDatumService songBestDatumService)
private readonly IGameDataService gameDataService;
public SelfBestController(ISongBestDatumService songBestDatumService, IGameDataService gameDataService)
{
this.songBestDatumService = songBestDatumService;
this.gameDataService = gameDataService;
}
[HttpPost]
@ -26,8 +29,6 @@ public class SelfBestController : BaseController<SelfBestController>
Level = request.Level
};
var manager = MusicAttributeManager.Instance;
var requestDifficulty = (Difficulty)request.Level;
requestDifficulty.Throw().IfOutOfRange();
@ -38,7 +39,7 @@ public class SelfBestController : BaseController<SelfBestController>
.ToList();
foreach (var songNo in request.ArySongNoes)
{
if (!manager.MusicAttributes.ContainsKey(songNo))
if (!gameDataService.GetMusicAttributes().ContainsKey(songNo))
{
Logger.LogWarning("Music no {No} is missing!", songNo);
continue;

View File

@ -13,10 +13,13 @@ public class UserDataController : BaseController<UserDataController>
private readonly ISongPlayDatumService songPlayDatumService;
public UserDataController(IUserDatumService userDatumService, ISongPlayDatumService songPlayDatumService)
private readonly IGameDataService gameDataService;
public UserDataController(IUserDatumService userDatumService, ISongPlayDatumService songPlayDatumService, IGameDataService gameDataService)
{
this.userDatumService = userDatumService;
this.songPlayDatumService = songPlayDatumService;
this.gameDataService = gameDataService;
}
[HttpPost]
@ -25,13 +28,11 @@ public class UserDataController : BaseController<UserDataController>
{
Logger.LogInformation("UserData request : {Request}", request.Stringify());
var musicAttributeManager = MusicAttributeManager.Instance;
var releaseSongArray =
FlagCalculator.GetBitArrayFromIds(musicAttributeManager.Musics, Constants.MUSIC_ID_MAX, Logger);
FlagCalculator.GetBitArrayFromIds(gameDataService.GetMusicList(), Constants.MUSIC_ID_MAX, Logger);
var uraSongArray =
FlagCalculator.GetBitArrayFromIds(musicAttributeManager.MusicsWithUra, Constants.MUSIC_ID_MAX, Logger);
FlagCalculator.GetBitArrayFromIds(gameDataService.GetMusicWithUraList(), Constants.MUSIC_ID_MAX, Logger);
var userData = await userDatumService.GetFirstUserDatumOrDefault(request.Baid);

View File

@ -6,6 +6,7 @@ using TaikoLocalServer.Services;
using TaikoLocalServer.Services.Extentions;
using TaikoLocalServer.Services.Interfaces;
using TaikoLocalServer.Settings;
using Throw;
var builder = WebApplication.CreateBuilder(args);
// Manually enable tls 1.0
@ -19,6 +20,7 @@ builder.WebHost.UseKestrel(kestrelOptions =>
// Add services to the container.
builder.Services.AddOptions();
builder.Services.AddSingleton<IGameDataService, GameDataService>();
builder.Services.Configure<UrlSettings>(builder.Configuration.GetSection(nameof(UrlSettings)));
builder.Services.AddControllers().AddProtoBufNet();
builder.Services.AddDbContext<TaikoDbContext>(option =>
@ -58,6 +60,10 @@ using (var scope = app.Services.CreateScope())
db.Database.Migrate();
}
var gameDataService = app.Services.GetService<IGameDataService>();
gameDataService.ThrowIfNull();
await gameDataService.InitializeAsync();
// For reverse proxy
app.UseForwardedHeaders(new ForwardedHeadersOptions
{

View File

@ -0,0 +1,130 @@
using System.Collections.Immutable;
using System.Text.Json;
using SharedProject.Models;
using Swan.Mapping;
using TaikoLocalServer.Services.Interfaces;
using Throw;
namespace TaikoLocalServer.Services;
public class GameDataService : IGameDataService
{
private ImmutableDictionary<uint, GetDanOdaiResponse.OdaiData> danDataDictionary =
ImmutableDictionary<uint, GetDanOdaiResponse.OdaiData>.Empty;
private ImmutableDictionary<uint, GetSongIntroductionResponse.SongIntroductionData> introDataDictionary =
ImmutableDictionary<uint, GetSongIntroductionResponse.SongIntroductionData>.Empty;
private ImmutableDictionary<uint, MusicAttributeEntry> musicAttributes =
ImmutableDictionary<uint, MusicAttributeEntry>.Empty;
private List<uint> musics = new();
private List<uint> musicsWithUra = new();
public List<uint> GetMusicList()
{
return musics;
}
public List<uint> GetMusicWithUraList()
{
return musicsWithUra;
}
public ImmutableDictionary<uint, MusicAttributeEntry> GetMusicAttributes()
{
return musicAttributes;
}
public ImmutableDictionary<uint, GetDanOdaiResponse.OdaiData> GetDanDataDictionary()
{
return danDataDictionary;
}
public ImmutableDictionary<uint, GetSongIntroductionResponse.SongIntroductionData> GetSongIntroDictionary()
{
return introDataDictionary;
}
public async Task InitializeAsync()
{
var dataPath = PathHelper.GetDataPath();
var musicAttributePath = Path.Combine(dataPath, Constants.MUSIC_ATTRIBUTE_FILE_NAME);
var danDataPath = Path.Combine(dataPath, Constants.DAN_DATA_FILE_NAME);
var songIntroDataPath = Path.Combine(dataPath, Constants.INTRO_DATA_FILE_NAME);
await using var musicAttributeFile = File.OpenRead(musicAttributePath);
await using var danDataFile = File.OpenRead(danDataPath);
await using var songIntroDataFile = File.OpenRead(songIntroDataPath);
var attributesData = await JsonSerializer.DeserializeAsync<MusicAttributes>(musicAttributeFile);
var danData = await JsonSerializer.DeserializeAsync<List<DanData>>(danDataFile);
var introData = await JsonSerializer.DeserializeAsync<List<SongIntroductionData>>(songIntroDataFile);
InitializeMusicAttributes(attributesData);
InitializeDanData(danData);
InitializeIntroData(introData);
}
private void InitializeIntroData(List<SongIntroductionData>? introData)
{
introData.ThrowIfNull("Shouldn't happen!");
introDataDictionary = introData.ToImmutableDictionary(data => data.SetId, ToResponseIntroData);
}
private void InitializeDanData(List<DanData>? danData)
{
danData.ThrowIfNull("Shouldn't happen!");
danDataDictionary = danData.ToImmutableDictionary(data => data.DanId, ToResponseOdaiData);
}
private void InitializeMusicAttributes(MusicAttributes? attributesData)
{
attributesData.ThrowIfNull("Shouldn't happen!");
musicAttributes = attributesData.MusicAttributeEntries.ToImmutableDictionary(attribute => attribute.MusicId);
musics = musicAttributes.Select(pair => pair.Key)
.ToList();
musics.Sort();
musicsWithUra = musicAttributes.Where(attribute => attribute.Value.HasUra)
.Select(pair => pair.Key)
.ToList();
musicsWithUra.Sort();
}
private static GetDanOdaiResponse.OdaiData ToResponseOdaiData(DanData data)
{
var responseOdaiData = new GetDanOdaiResponse.OdaiData
{
DanId = data.DanId,
Title = data.Title,
VerupNo = data.VerupNo
};
var odaiSongs = data.OdaiSongList.Select(song => song.CopyPropertiesToNew<GetDanOdaiResponse.OdaiData.OdaiSong>());
responseOdaiData.AryOdaiSongs.AddRange(odaiSongs);
var odaiBorders = data.OdaiBorderList.Select(border => border.CopyPropertiesToNew<GetDanOdaiResponse.OdaiData.OdaiBorder>());
responseOdaiData.AryOdaiBorders.AddRange(odaiBorders);
return responseOdaiData;
}
private static GetSongIntroductionResponse.SongIntroductionData ToResponseIntroData(SongIntroductionData data)
{
var responseOdaiData = new GetSongIntroductionResponse.SongIntroductionData
{
SetId = data.SetId,
VerupNo = data.VerupNo,
MainSongNo = data.MainSongNo,
SubSongNoes = data.SubSongNo
};
return responseOdaiData;
}
}

View File

@ -0,0 +1,18 @@
using System.Collections.Immutable;
namespace TaikoLocalServer.Services.Interfaces;
public interface IGameDataService
{
public Task InitializeAsync();
public List<uint> GetMusicList();
public List<uint> GetMusicWithUraList();
public ImmutableDictionary<uint, MusicAttributeEntry> GetMusicAttributes();
public ImmutableDictionary<uint, GetDanOdaiResponse.OdaiData> GetDanDataDictionary();
public ImmutableDictionary<uint, GetSongIntroductionResponse.SongIntroductionData> GetSongIntroDictionary();
}

View File

@ -41,6 +41,9 @@
<Content Update="wwwroot\data\dan_data.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="wwwroot\data\intro_data.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,74 @@
[
{
"setId":1,
"verupNo":1,
"mainSongNo":895,
"subSongNo":[894,732,44,921]
},
{
"setId":2,
"verupNo":1,
"mainSongNo":912,
"subSongNo":[827,871,36,227]
},
{
"setId":3,
"verupNo":1,
"mainSongNo":913,
"subSongNo":[460,916,430,872]
},
{
"setId":4,
"verupNo":1,
"mainSongNo":842,
"subSongNo":[7,233,256,831]
},
{
"setId":5,
"verupNo":1,
"mainSongNo":947,
"subSongNo":[926,882,730,695]
},
{
"setId":6,
"verupNo":1,
"mainSongNo":937,
"subSongNo":[828,925,474,924]
},
{
"setId":7,
"verupNo":1,
"mainSongNo":956,
"subSongNo":[839,255,285,187]
},
{
"setId":8,
"verupNo":1,
"mainSongNo":923,
"subSongNo":[729,873,789,893]
},
{
"setId":9,
"verupNo":1,
"mainSongNo":915,
"subSongNo":[726,811,711,303]
},
{
"setId":10,
"verupNo":1,
"mainSongNo":885,
"subSongNo":[837,464,801,18]
},
{
"setId":11,
"verupNo":1,
"mainSongNo":898,
"subSongNo":[47,135,374,792]
},
{
"setId":12,
"verupNo":1,
"mainSongNo":948,
"subSongNo":[412,538,411,413]
}
]