diff --git a/GC-local-server-rewrite/controllers/RankController.cs b/GC-local-server-rewrite/controllers/RankController.cs index 8efb610..0ce0ae6 100644 --- a/GC-local-server-rewrite/controllers/RankController.cs +++ b/GC-local-server-rewrite/controllers/RankController.cs @@ -7,6 +7,7 @@ using EmbedIO.Routing; using EmbedIO.WebApi; using GCLocalServerRewrite.common; using GCLocalServerRewrite.models; +using SQLite.Net2; using Swan; using Swan.Logging; // ReSharper disable UnusedMember.Global @@ -15,6 +16,16 @@ namespace GCLocalServerRewrite.controllers; public class RankController : WebApiController { + private readonly SQLiteConnection cardSqLiteConnection; + private readonly SQLiteConnection musicSqLiteConnection; + + public RankController() + { + cardSqLiteConnection = DatabaseHelper.ConnectDatabase(Configs.SETTINGS.CardDbName); + musicSqLiteConnection = DatabaseHelper.ConnectDatabase(Configs.SETTINGS.MusicDbName); + } + + [Route(HttpVerbs.Get, "/ranking.php")] public string Rank([QueryField("cmd_type")] int type) { @@ -24,7 +35,7 @@ public class RankController : WebApiController if (!Enum.IsDefined(typeof(RankType), type)) { - throw new ArgumentOutOfRangeException(nameof(type)); + throw new ArgumentOutOfRangeException(nameof(type), type, "Rank type out of range"); } var requestType = (RankType)type; @@ -41,7 +52,7 @@ public class RankController : WebApiController case RankType.PlayNumRank: $"Getting rank request, type is {requestType}".Info(); - return ConstructResponse(RankTemp("play_num_rank")); + return ConstructResponse(PlayNumRank()); case RankType.EventRank: $"Getting rank request, type is {requestType}".Info(); @@ -97,6 +108,63 @@ public class RankController : WebApiController return stringWriter.ToString(); } + private string PlayNumRank() + { + var rankStatus = new RankStatus + { + Rows = 30, + TableName = "play_num_rank", + Status = 1 + }; + + var playNumRankContainer = new PlayNumRankContainer + { + PlayNumRankRecords = GetPlayNumRankRecords(), + RankStatus = rankStatus + }; + + var sb = new StringBuilder(); + + using (var writer = new ChoXmlWriter(sb)) + { + writer.Configuration.UseXmlSerialization = true; + writer.Configuration.OmitXmlDeclaration = false; + writer.Configuration.IgnoreRootName = true; + writer.Write(playNumRankContainer); + } + + return sb.ToString(); + } + + private List GetPlayNumRankRecords() + { + var records = new List(); + var musics = musicSqLiteConnection.Table().ToList().OrderBy(arg => Guid.NewGuid()).Take(30).ToList(); + + for (var i = 0; i < musics.Count; i++) + { + var music = musics[i]; + var index = i + 1; + var record = new PlayNumRankRecord + { + Id = index, + Rank = index, + Rank2 = index + 1, + PrevRank = musics.Count - i, + PrevRank2 = index + 1, + Artist = music.Artist, + Pcol1 = music.MusicId, + ScoreBi1 = index, + Title = music.Title + }; + records.Add(record); + } + + + return records; + } + + private enum RankType { GlobalRank = 4119, diff --git a/GC-local-server-rewrite/models/PlayNumRankContainer.cs b/GC-local-server-rewrite/models/PlayNumRankContainer.cs new file mode 100644 index 0000000..ba764ba --- /dev/null +++ b/GC-local-server-rewrite/models/PlayNumRankContainer.cs @@ -0,0 +1,13 @@ +using System.Xml.Serialization; + +namespace GCLocalServerRewrite.models; + +[XmlRoot("root")] +public class PlayNumRankContainer +{ + [XmlArray("play_num_rank")] + public List PlayNumRankRecords { get; set; } = new(); + + [XmlElement("ranking_status")] + public RankStatus? RankStatus { get; set; } +} \ No newline at end of file diff --git a/GC-local-server-rewrite/models/PlayNumRankRecord.cs b/GC-local-server-rewrite/models/PlayNumRankRecord.cs new file mode 100644 index 0000000..bb0bd67 --- /dev/null +++ b/GC-local-server-rewrite/models/PlayNumRankRecord.cs @@ -0,0 +1,34 @@ +using System.Xml.Serialization; + +namespace GCLocalServerRewrite.models; + +[XmlType("record")] +public class PlayNumRankRecord +{ + [XmlAttribute(AttributeName = "id")] + public int Id { get; set; } + + [XmlElement("rank")] + public int Rank { get; set; } + + [XmlElement("rank2")] + public int Rank2 { get; set; } + + [XmlElement("prev_rank")] + public int PrevRank { get; set; } + + [XmlElement("prev_rank2")] + public int PrevRank2 { get; set; } + + [XmlElement("pcol1")] + public int Pcol1 { get; set; } + + [XmlElement("score_bi1")] + public int ScoreBi1 { get; set; } + + [XmlElement("title")] + public string? Title { get; set; } + + [XmlElement("artist")] + public string? Artist { get; set; } +} \ No newline at end of file diff --git a/GC-local-server-rewrite/models/RankStatus.cs b/GC-local-server-rewrite/models/RankStatus.cs index 8b0fd67..09d9570 100644 --- a/GC-local-server-rewrite/models/RankStatus.cs +++ b/GC-local-server-rewrite/models/RankStatus.cs @@ -2,16 +2,17 @@ namespace GCLocalServerRewrite.models; +[XmlType("ranking_status")] public class RankStatus { [XmlElement("table_name")] public string? TableName { get; set; } [XmlElement("start_date")] - public string StartDate { get; set; } = "2017-05-30"; + public string StartDate { get; set; } = "2021-05-30 08:00:00"; [XmlElement("end_date")] - public string EndDate { get; set; } = "2018-05-30"; + public string EndDate { get; set; } = "2022-06-08 08:00:00"; [XmlElement("status")] public int Status { get; set; }