1
0
mirror of synced 2025-01-18 22:24:06 +01:00

Update server for better query performance

This commit is contained in:
Yuchen Ji 2022-06-20 02:10:04 +08:00
parent 7afd41664c
commit 003f655585
6 changed files with 219 additions and 22 deletions

View File

@ -11,13 +11,13 @@
<ItemGroup>
<PackageReference Include="CertificateManager" Version="1.0.8" />
<PackageReference Include="ChoETL" Version="1.2.1.45" />
<PackageReference Include="ChoETL" Version="1.2.1.47" />
<PackageReference Include="Config.Net" Version="4.19.0" />
<PackageReference Include="Config.Net.Json" Version="4.19.0" />
<PackageReference Include="EmbedIO" Version="3.4.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="sqlite-net2" Version="2.0.7" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.0.7" />
<PackageReference Include="sqlite-net2" Version="2.1.0-preB" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.0" />
<PackageReference Include="System.Security.Cryptography.X509Certificates" Version="4.3.2" />
</ItemGroup>

View File

@ -1,4 +1,5 @@
using EmbedIO;
using ChoETL;
using EmbedIO;
using EmbedIO.Routing;
using EmbedIO.WebApi;
using GCLocalServerRewrite.common;
@ -6,6 +7,7 @@ using GCLocalServerRewrite.models;
using SharedProject.enums;
using SharedProject.models;
using SQLite.Net2;
using Swan.Logging;
namespace GCLocalServerRewrite.controllers;
@ -13,24 +15,102 @@ public class ApiController : WebApiController
{
private readonly SQLiteConnection cardSqLiteConnection;
private readonly Dictionary<int, Music> musics;
private readonly Dictionary<int, MusicExtra> musicExtras;
public ApiController()
{
cardSqLiteConnection = DatabaseHelper.ConnectDatabase(Configs.SETTINGS.CardDbName);
var musicSqLiteConnection = DatabaseHelper.ConnectDatabase(Configs.SETTINGS.MusicDbName);
musics = musicSqLiteConnection.Table<Music>().ToDictionary(music => music.MusicId);
musicExtras = musicSqLiteConnection.Table<MusicExtra>().ToDictionary(music => music.MusicId);
}
[Route(HttpVerbs.Get, "/PlayOption")]
public PlayOption? GetPlayOption([QueryField] long cardId)
{
var result = cardSqLiteConnection.Table<CardDetail>()
.Where(detail => detail.CardId == cardId &&
detail.Pcol1 == 0 && detail.Pcol2 == 0 && detail.Pcol3 == 0);
if (!result.Any())
[Route(HttpVerbs.Get, "/Users")]
// ReSharper disable once UnusedMember.Global
public List<User> GetUsers()
{
var result = cardSqLiteConnection.Table<Card>().ToList().ConvertAll(card => new User
{
CardId = card.CardId,
PlayerName = card.PlayerName
});
return result;
}
[Route(HttpVerbs.Get, "/UserDetail/{cardId}")]
// ReSharper disable once UnusedMember.Global
public UserDetail? GetUserDetail(long cardId)
{
var cardResult = cardSqLiteConnection.Table<Card>().Where(card => card.CardId == cardId);
if (!cardResult.Any())
{
return null;
}
var cardDetail = result.First();
var card = cardResult.First();
return ToUserDetail(card);
}
private UserDetail ToUserDetail(Card card)
{
var userDetail = new UserDetail
{
CardId = card.CardId,
PlayerName = card.PlayerName
};
var songPlayDataDict = new Dictionary<int, SongPlayData>();
ProcessCardDetail(userDetail, songPlayDataDict);
userDetail.SongPlayDataList = songPlayDataDict.Values.ToList();
userDetail.TotalSongCount = musics.Count;
userDetail.TotalStageCount = userDetail.TotalSongCount * 3 + musicExtras.Count;
userDetail.AverageScore = (int)(userDetail.TotalScore / userDetail.PlayedStageCount);
userDetail.PlayedSongCount = songPlayDataDict.Count;
return userDetail;
}
private void ProcessCardDetail(UserDetail userDetail, IDictionary<int, SongPlayData> songPlayDataDict)
{
var option = cardSqLiteConnection.Table<CardDetail>()
.FirstOrDefault(detail => detail.CardId == userDetail.CardId && detail.Pcol1 == 0, new CardDetail
{
CardId = userDetail.CardId
});
SetOptions(option, userDetail);
var songCounts = cardSqLiteConnection.Table<CardDetail>()
.Where(detail => detail.CardId == userDetail.CardId && detail.Pcol1 == 20);
foreach (var detail in songCounts)
{
SetCounts(detail, songPlayDataDict, userDetail);
}
var songScores = cardSqLiteConnection.Table<CardDetail>()
.Where(detail => detail.CardId == userDetail.CardId && detail.Pcol1 == 21);
foreach (var detail in songScores)
{
SetDetails(detail, songPlayDataDict, userDetail);
}
var favorites = cardSqLiteConnection.Table<CardDetail>()
.Where(detail => detail.CardId == userDetail.CardId && detail.Pcol1 == 10)
.ToDictionary(detail => detail.Pcol2);
foreach (var (musicId, songPlayData) in songPlayDataDict)
{
songPlayData.IsFavorite = favorites[musicId].Fcol1 != 0;
}
}
private static void SetOptions(CardDetail cardDetail, UserDetail userDetail)
{
var fastSlow = (int)cardDetail.ScoreUi1;
var feverTrance = (int)cardDetail.ScoreUi2;
@ -41,14 +121,121 @@ public class ApiController : WebApiController
if (!Enum.IsDefined(typeof(PlayOptions.FeverTranceShow), feverTrance))
{
feverTrance = (int)PlayOptions.FeverTranceShow.NotUsed;
feverTrance = (int)PlayOptions.FeverTranceShow.Show;
}
return new PlayOption
userDetail.PlayOption = new PlayOption
{
CardId = cardId,
CardId = cardDetail.CardId,
FastSlowIndicator = (PlayOptions.FastSlowIndicator)fastSlow,
FeverTrance = (PlayOptions.FeverTranceShow)feverTrance
};
}
}
private void SetDetails(CardDetail cardDetail, IDictionary<int, SongPlayData> songPlayDataDict,
UserDetail userDetail)
{
var musicId = cardDetail.Pcol2;
AddSongPlayDataIfNotExist(songPlayDataDict, musicId);
for (var i = 0; i < 4; i++)
{
var songPlayDetailData = songPlayDataDict[musicId].SongPlaySubDataList[i];
songPlayDetailData.Difficulty = (Difficulty)i;
if (i != cardDetail.Pcol3)
{
continue;
}
songPlayDetailData.Score = (int)cardDetail.ScoreUi1;
songPlayDetailData.MaxChain = (int)cardDetail.ScoreUi3;
userDetail.TotalScore += cardDetail.ScoreUi1;
if (cardDetail.ScoreUi1 >= 900000)
{
userDetail.SAboveStageCount++;
}
if (cardDetail.ScoreUi1 >= 950000)
{
userDetail.SPlusAboveStageCount++;
}
if (cardDetail.ScoreUi1 >= 990000)
{
userDetail.SPlusPlusAboveStageCount++;
}
}
}
private void SetCounts(CardDetail cardDetail, IDictionary<int, SongPlayData> songPlayDataDict, UserDetail userDetail)
{
var musicId = cardDetail.Pcol2;
AddSongPlayDataIfNotExist(songPlayDataDict, musicId);
for (var i = 0; i < 4; i++)
{
var songPlayDetailData = songPlayDataDict[musicId].SongPlaySubDataList[i];
songPlayDetailData.Difficulty = (Difficulty)i;
if (i != cardDetail.Pcol3)
{
continue;
}
songPlayDetailData.PlayCount = (int)cardDetail.ScoreUi1;
songPlayDetailData.ClearState = ClearState.Failed;
userDetail.PlayedStageCount++;
if (cardDetail.ScoreUi2 > 0)
{
userDetail.ClearedStageCount++;
songPlayDetailData.ClearState = ClearState.Clear;
}
if (cardDetail.ScoreUi3 > 0)
{
userDetail.NoMissStageCount++;
songPlayDetailData.ClearState = ClearState.NoMiss;
}
if (cardDetail.ScoreUi4 > 0)
{
userDetail.FullChainStageCount++;
songPlayDetailData.ClearState = ClearState.FullChain;
}
if (cardDetail.ScoreUi6 > 0)
{
userDetail.PerfectStageCount++;
songPlayDetailData.ClearState = ClearState.Perfect;
}
}
}
private void AddSongPlayDataIfNotExist(IDictionary<int, SongPlayData> songPlayDataDict, int musicId)
{
if (songPlayDataDict.ContainsKey(musicId))
{
return;
}
var musicData = musics[musicId];
var songPlayData = new SongPlayData
{
Artist = musicData.Artist ?? string.Empty,
Title = musicData.Title ?? string.Empty,
SongPlaySubDataList = new SongPlayDetailData[4]
};
for (var i = 0; i < 4; i++)
{
songPlayData.SongPlaySubDataList[i] = new SongPlayDetailData();
}
songPlayDataDict[musicId] = songPlayData;
}
}

View File

@ -7,4 +7,10 @@
<RootNamespace>SharedProject</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ChoETL" Version="1.2.1.47" />
<PackageReference Include="sqlite-net2" Version="2.1.0-preB" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.0" />
</ItemGroup>
</Project>

View File

@ -2,8 +2,8 @@
public enum Difficulty
{
Simple,
Normal,
Hard,
Extra
Simple = 0,
Normal = 1,
Hard = 2,
Extra = 3
}

View File

@ -6,7 +6,7 @@ public class SongPlayData
public string Artist { get; set; } = string.Empty;
public List<SongPlayDetailData> SongPlaySubDataList { get; set; } = new List<SongPlayDetailData>(4);
public SongPlayDetailData[] SongPlaySubDataList { get; set; } = new SongPlayDetailData[4];
public bool IsFavorite { get; set; }

View File

@ -12,6 +12,8 @@ public class UserDetail: User
public int PlayedSongCount { get; set; }
public int PlayedStageCount { get; set; }
public int TotalStageCount { get; set; }
public int ClearedStageCount { get; set; }
@ -27,4 +29,6 @@ public class UserDetail: User
public int SPlusAboveStageCount { get; set; }
public int SPlusPlusAboveStageCount { get; set; }
public List<SongPlayData>? SongPlayDataList { get; set; }
}