Try add stamp count for 3EX and before
This commit is contained in:
parent
04be44cff1
commit
7afd41664c
@ -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<CardBData>(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<CardPlayCount>().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<CardDetail>()
|
||||
|
@ -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<CardPlayCount>().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;
|
||||
}
|
||||
}
|
16
GC-local-server-rewrite/models/CardPlayCount.cs
Normal file
16
GC-local-server-rewrite/models/CardPlayCount.cs
Normal file
@ -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; }
|
||||
}
|
@ -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")]
|
||||
|
@ -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));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user