From 7afd41664c74286a5905256f226251ea76d17f4f Mon Sep 17 00:00:00 2001 From: Yuchen Ji Date: Sat, 18 Jun 2022 22:11:57 +0800 Subject: [PATCH] Try add stamp count for 3EX and before --- .../controllers/CardServiceController.cs | 51 +++++++++++++++++ .../controllers/OptionServiceController.cs | 55 ++++++++++++++++++- .../models/CardPlayCount.cs | 16 ++++++ GC-local-server-rewrite/models/Music.cs | 8 ++- GC-local-server-rewrite/server/Server.cs | 2 +- 5 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 GC-local-server-rewrite/models/CardPlayCount.cs diff --git a/GC-local-server-rewrite/controllers/CardServiceController.cs b/GC-local-server-rewrite/controllers/CardServiceController.cs index ba8c7a0..03b66f7 100644 --- a/GC-local-server-rewrite/controllers/CardServiceController.cs +++ b/GC-local-server-rewrite/controllers/CardServiceController.cs @@ -8,6 +8,7 @@ using EmbedIO.WebApi; using GCLocalServerRewrite.common; using GCLocalServerRewrite.models; using SQLite.Net2; +using Swan; using Swan.Logging; namespace GCLocalServerRewrite.controllers; @@ -193,6 +194,7 @@ public class CardServiceController : WebApiController { $"Getting write request, type is {requestType}\n Data is {xmlData}".Info(); Write(cardId, xmlData); + WriteCardPlayCount(cardId); return ConstructResponse(xmlData); } @@ -483,6 +485,55 @@ public class CardServiceController : WebApiController $"Updated {typeof(T)}".Info(); } + private void WriteCardPlayCount(long cardId) + { + var record = cardSqLiteConnection.Table().Where(count => count.CardId == cardId); + + if (!record.Any()) + { + $"Created new play count data for card {cardId}".Info(); + var playCount = new CardPlayCount + { + CardId = cardId, + PlayCount = 1, + LastPlayed = DateTime.Now + }; + cardSqLiteConnection.InsertOrReplace(playCount); + return; + } + + var data = record.First(); + var now = DateTime.Now; + var lastPlayedTime = data.LastPlayed; + + if (now <= lastPlayedTime) + { + $"Current time {now} is less than or equal to last played time! Clock skew detected!".Warn(); + data.PlayCount = 0; + data.LastPlayed = DateTime.Now; + cardSqLiteConnection.InsertOrReplace(data); + return; + } + + DateTime start; + DateTime end; + + if (now.Hour >= 8) + { + start = DateTime.Today.AddHours(8); + end = start.AddHours(24); + } + else + { + end = DateTime.Today.AddHours(8); + start = end.AddHours(-24); + } + + data.PlayCount = lastPlayedTime.IsBetween(start, end) ? data.PlayCount + 1 : 0; + cardSqLiteConnection.InsertOrReplace(data); + $"Updated card play count, current count is {data.PlayCount}".Info(); + } + private void WriteCardDetail(long cardId, string xmlData) { var result = cardSqLiteConnection.Table() diff --git a/GC-local-server-rewrite/controllers/OptionServiceController.cs b/GC-local-server-rewrite/controllers/OptionServiceController.cs index 16d8ad8..3667beb 100644 --- a/GC-local-server-rewrite/controllers/OptionServiceController.cs +++ b/GC-local-server-rewrite/controllers/OptionServiceController.cs @@ -3,19 +3,68 @@ using System.Text; using EmbedIO; using EmbedIO.Routing; using EmbedIO.WebApi; +using GCLocalServerRewrite.common; +using GCLocalServerRewrite.models; +using SQLite.Net2; +using Swan; +using Swan.Logging; namespace GCLocalServerRewrite.controllers; public class OptionServiceController : WebApiController { - [Route(HttpVerbs.Get, "/PlayInfo.php")] - public string OptionService() + private readonly SQLiteConnection cardSqLiteConnection; + + public OptionServiceController() { + cardSqLiteConnection = DatabaseHelper.ConnectDatabase(Configs.SETTINGS.CardDbName); + } + + [Route(HttpVerbs.Get, "/PlayInfo.php")] + public string OptionService([QueryField("card_id")] long cardId) + { + HttpContext.Response.ContentType = MediaTypeNames.Text.Plain; HttpContext.Response.ContentEncoding = new UTF8Encoding(false); HttpContext.Response.KeepAlive = true; - return "1"; + return "1\n" + + $"{GetPlayCount(cardId)}"; + } + + private int GetPlayCount(long cardId) + { + var record = cardSqLiteConnection.Table().Where(count => count.CardId == cardId); + + if (!record.Any()) + { + return 0; + } + + var now = DateTime.Now; + var data = record.First(); + var lastPlayedTime = data.LastPlayed; + + if (now <= lastPlayedTime) + { + $"Current time {now} is less than or equal to last played time! Clock skew detected!".Warn(); + return 0; + } + + DateTime start; + DateTime end; + if (now.Hour >= 8) + { + start = DateTime.Today.AddHours(8); + end = start.AddHours(24); + } + else + { + end = DateTime.Today.AddHours(8); + start = end.AddHours(-24); + } + + return lastPlayedTime.IsBetween(start, end) ? data.PlayCount : 0; } } \ No newline at end of file diff --git a/GC-local-server-rewrite/models/CardPlayCount.cs b/GC-local-server-rewrite/models/CardPlayCount.cs new file mode 100644 index 0000000..fa0f36f --- /dev/null +++ b/GC-local-server-rewrite/models/CardPlayCount.cs @@ -0,0 +1,16 @@ +using SQLite.Net2; + +namespace GCLocalServerRewrite.models; + +public class CardPlayCount +{ + [PrimaryKey] + [Column("card_id")] + public long CardId { get; set; } + + [Column("play_count")] + public int PlayCount { get; set; } + + [Column("last_played_time")] + public DateTime LastPlayed { get; set; } +} \ No newline at end of file diff --git a/GC-local-server-rewrite/models/Music.cs b/GC-local-server-rewrite/models/Music.cs index b538f2a..e4bcccd 100644 --- a/GC-local-server-rewrite/models/Music.cs +++ b/GC-local-server-rewrite/models/Music.cs @@ -17,7 +17,13 @@ public class Music : Record [Column("artist")] [XmlElement(ElementName = "artist")] - public string Artist { get; set; } = string.Empty; + public string? Artist + { + get => _artist; + set => _artist = value ?? string.Empty; + } + + private string? _artist = string.Empty; [Column("release_date")] [XmlElement(ElementName = "release_date")] diff --git a/GC-local-server-rewrite/server/Server.cs b/GC-local-server-rewrite/server/Server.cs index 67043c6..160c8c3 100644 --- a/GC-local-server-rewrite/server/Server.cs +++ b/GC-local-server-rewrite/server/Server.cs @@ -73,7 +73,7 @@ public class Server private static void InitializeDatabase() { DatabaseHelper.InitializeLocalDatabase(Configs.SETTINGS.CardDbName, - typeof(Card), typeof(CardBData), typeof(CardDetail)); + typeof(Card), typeof(CardBData), typeof(CardDetail), typeof(CardPlayCount)); DatabaseHelper.InitializeLocalDatabase(Configs.SETTINGS.MusicDbName, typeof(Music), typeof(MusicAou), typeof(MusicExtra)); }