From a5fea98d5975ac1e86f47f4a0907e6cc2ec48451 Mon Sep 17 00:00:00 2001 From: asesidaa <1061472754@qq.com> Date: Sat, 16 Jul 2022 16:03:56 +0800 Subject: [PATCH] Add online matching testing code Add event download code --- GC-local-server-rewrite.sln.DotSettings.user | 3 +- .../GC-local-server-rewrite.csproj | 4 - .../GC-local-server-rewrite.exe.config | 13 --- GC-local-server-rewrite/Program.cs | 19 ++-- GC-local-server-rewrite/common/Configs.cs | 5 +- .../common/IAppSettings.cs | 3 + GC-local-server-rewrite/config.json | 7 +- .../controllers/CardServiceController.cs | 86 ++++++++++++++---- .../controllers/ServerController.cs | 7 +- .../models/OnlineMatchingEntry.cs | 87 +++++++++++++++++++ 10 files changed, 186 insertions(+), 48 deletions(-) delete mode 100644 GC-local-server-rewrite/GC-local-server-rewrite.exe.config create mode 100644 GC-local-server-rewrite/models/OnlineMatchingEntry.cs diff --git a/GC-local-server-rewrite.sln.DotSettings.user b/GC-local-server-rewrite.sln.DotSettings.user index 5fbb72c..46fd807 100644 --- a/GC-local-server-rewrite.sln.DotSettings.user +++ b/GC-local-server-rewrite.sln.DotSettings.user @@ -4,4 +4,5 @@ True True True - True \ No newline at end of file + True + True \ No newline at end of file diff --git a/GC-local-server-rewrite/GC-local-server-rewrite.csproj b/GC-local-server-rewrite/GC-local-server-rewrite.csproj index 5f2f0cc..3b528e2 100644 --- a/GC-local-server-rewrite/GC-local-server-rewrite.csproj +++ b/GC-local-server-rewrite/GC-local-server-rewrite.csproj @@ -50,10 +50,6 @@ true - - PreserveNewest - true - PreserveNewest diff --git a/GC-local-server-rewrite/GC-local-server-rewrite.exe.config b/GC-local-server-rewrite/GC-local-server-rewrite.exe.config deleted file mode 100644 index 9c441a1..0000000 --- a/GC-local-server-rewrite/GC-local-server-rewrite.exe.config +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/GC-local-server-rewrite/Program.cs b/GC-local-server-rewrite/Program.cs index 63466aa..ac16ac7 100644 --- a/GC-local-server-rewrite/Program.cs +++ b/GC-local-server-rewrite/Program.cs @@ -32,7 +32,7 @@ internal class Program Terminal.Flush(); Console.WriteLine("Press any key to exit."); - WaitForKeypress(); + Console.ReadKey(true); } private static void InitializeLogging() @@ -67,7 +67,7 @@ internal class Program // Be sure to run in parallel. await Task.Yield(); - "Press any key to stop the web server.".Info(nameof(Program)); + "Press x to stop the web server.".Info(nameof(Program)); WaitForKeypress(); "Stopping...".Info(nameof(Program)); cancel(); @@ -78,12 +78,17 @@ internal class Program /// private static void WaitForKeypress() { - while (Console.KeyAvailable) - { - Console.ReadKey(true); - } + ConsoleKeyInfo consoleKeyInfo; - Console.ReadKey(true); + do + { + while (Console.KeyAvailable == false) + { + Thread.Sleep(250); + } + + consoleKeyInfo = Console.ReadKey(true); + } while (consoleKeyInfo.Key != ConsoleKey.X); } private static void LogConfigValues() diff --git a/GC-local-server-rewrite/common/Configs.cs b/GC-local-server-rewrite/common/Configs.cs index bf5e76a..1999065 100644 --- a/GC-local-server-rewrite/common/Configs.cs +++ b/GC-local-server-rewrite/common/Configs.cs @@ -4,7 +4,7 @@ namespace GCLocalServerRewrite.common; public static class Configs { - public const bool USE_FILE_CACHE = false; + public const bool USE_FILE_CACHE = true; public const string ROOT_CA_CN = "Taito Arcade Machine CA"; @@ -126,6 +126,8 @@ public static class Configs public const string RANK_STATUS_XPATH = $"{ROOT_XPATH}/ranking_status"; + public const string ONLINE_MATCHING_XPATH = $"{ROOT_XPATH}/online_matching/record"; + public const int FIRST_CONFIG_PCOL1 = 0; public const int SECOND_CONFIG_PCOL1 = 1; public const int CONFIG_PCOL2 = 0; @@ -159,6 +161,7 @@ public static class Configs public const string DEFAULT_SERVER_IP = "127.0.0.1"; public const string DEFAULT_RELAY_SERVER = "127.0.0.1"; public const int DEFAULT_RELAY_PORT = 54321; + public const string DEFAULT_EVENT_FOLDER = "event"; public static readonly IReadOnlyList DEFAULT_UNLOCKABLE_SONGS = new[] { diff --git a/GC-local-server-rewrite/common/IAppSettings.cs b/GC-local-server-rewrite/common/IAppSettings.cs index 2e3f671..ee097e3 100644 --- a/GC-local-server-rewrite/common/IAppSettings.cs +++ b/GC-local-server-rewrite/common/IAppSettings.cs @@ -30,6 +30,9 @@ public interface IAppSettings [Option(DefaultValue = Configs.DEFAULT_SERVER_IP)] string ServerIp { get; } + + [Option(DefaultValue = Configs.DEFAULT_EVENT_FOLDER)] + string EventFolder { get; } [Option(DefaultValue = Configs.DEFAULT_RELAY_SERVER)] string RelayServer { get; } diff --git a/GC-local-server-rewrite/config.json b/GC-local-server-rewrite/config.json index cd11d2b..7b49c7b 100644 --- a/GC-local-server-rewrite/config.json +++ b/GC-local-server-rewrite/config.json @@ -6,9 +6,10 @@ "SeCount": 26, "TitleCount": 5530, "CardDbName": "card.db3", - "MusicDbName": "music471.db3", + "MusicDbName": "music471omni.db3", "ServerIp": "127.0.0.1", - "RelayServer": "26.56.10.224", + "EventFolder": "event", + "RelayServer": "127.0.0.1", "RelayPort": 54321, "UnlockableSongIds": [ 11, 13, 149, 273, 291, 320, 321, 371, 378, 384, 464, 471, 474, 475, 492, 494, 498, 520, @@ -69,7 +70,7 @@ "FileName": "/event_unlock_20201125.cmp", "NotBeforeUnixTime": 1335677127, "NotAfterUnixTime": 1966397127, - "Md5": "623b0f10125cbe19c5394d992930ae8c", + "Md5": "534a253e3de8360c2beff49a5f120105", "Index": 8 } ] diff --git a/GC-local-server-rewrite/controllers/CardServiceController.cs b/GC-local-server-rewrite/controllers/CardServiceController.cs index 26eecf1..fff3a7f 100644 --- a/GC-local-server-rewrite/controllers/CardServiceController.cs +++ b/GC-local-server-rewrite/controllers/CardServiceController.cs @@ -18,6 +18,8 @@ public class CardServiceController : WebApiController private readonly SQLiteConnection cardSqLiteConnection; private readonly SQLiteConnection musicSqLiteConnection; + private static Dictionary OnlineMatchingEntries = new(); + public CardServiceController() { cardSqLiteConnection = DatabaseHelper.ConnectDatabase(Configs.SETTINGS.CardDbName); @@ -42,7 +44,7 @@ public class CardServiceController : WebApiController { throw new ArgumentOutOfRangeException(nameof(cmdType), cmdType, $"Cmd type is unknown!\n Data is {xmlData}"); } - + var command = (Command)cmdType; return command switch @@ -73,9 +75,9 @@ public class CardServiceController : WebApiController { throw new ArgumentOutOfRangeException(nameof(type), type, "Card request type is unknown!"); } - + var requestType = (CardRequestType)type; - + $"Getting card request, type is {requestType}".Info(); switch (requestType) @@ -172,8 +174,8 @@ public class CardServiceController : WebApiController { return ConstructResponse(TotalTrophy(cardId)); } - case CardRequestType.SessionStart: - case CardRequestType.SessionGet: + case CardRequestType.StartSession: + case CardRequestType.GetSession: { return ConstructResponse(GetSession(cardId, mac)); } @@ -184,19 +186,19 @@ public class CardServiceController : WebApiController case CardRequestType.WriteCard: { - $"Card Request data is {xmlData}".Info(); + $"Card Write data is {xmlData}".Info(); Write(cardId, xmlData); return ConstructResponse(xmlData); } case CardRequestType.WriteCardDetail: { - $"Card Request data is {xmlData}".Info(); + $"Card Detail Write data is {xmlData}".Info(); WriteCardDetail(cardId, xmlData); return ConstructResponse(xmlData); } case CardRequestType.WriteCardBData: { - $"Card Request data is {xmlData}".Info(); + $"Card BData Write data is {xmlData}".Info(); Write(cardId, xmlData); WriteCardPlayCount(cardId); return ConstructResponse(xmlData); @@ -212,17 +214,26 @@ public class CardServiceController : WebApiController case CardRequestType.WriteUnlockKeynum: case CardRequestType.WriteSoundEffect: { - $"Card Request data is {xmlData}".Info(); + $"Card Write data is {xmlData}".Info(); return ConstructResponse(xmlData); } + #endregion + #region OnlineMatching + + case CardRequestType.StartOnlineMatching: + { + $"Start Online Matching, data is {xmlData}".Info(); + return ConstructResponse(StartOnlineMatching(cardId, xmlData)); + } + #endregion default: throw new ArgumentOutOfRangeException(nameof(requestType), requestType, "Request type not captured, should never happen!"); } } - + #region ReadImplementation private string Card(long cardId, out ReturnCode returnCode) @@ -404,7 +415,7 @@ public class CardServiceController : WebApiController private static string ConstructResponse(string xml, ReturnCode returnCode = ReturnCode.Ok) { - var returnCodeInt = (int)returnCode; + var returnCodeInt = (int)returnCode; if (returnCodeInt == 1) { return $"{returnCodeInt}\n" + @@ -534,7 +545,7 @@ public class CardServiceController : WebApiController $"Updated card play count, current count is {data.PlayCount}".Info(); } - private void WriteCardDetail(long cardId, string xmlData) + private void WriteCardDetail(long cardId, string xmlData) { var result = cardSqLiteConnection.Table() .Where(detail => detail.CardId == cardId); @@ -588,6 +599,44 @@ public class CardServiceController : WebApiController #endregion + #region OnlineMatchingImplementation + + private static string StartOnlineMatching(long cardId, string xmlData) + { + var reader = new ChoXmlReader(new StringReader(xmlData)).WithXPath(Configs.DATA_XPATH); + var entry = reader.Read(); + + /*var fake = entry.CopyPropertiesToNew(); + fake.MachineId--; + fake.MatchingId = 0xB16B00B5; + fake.EntryStart = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + fake.CardId = cardId - 1; + fake.AvatarId--; + fake.PlayerName = "FAKE"; + fake.EntryNo = 0x4B1D; + fake.TitleId--; + fake.MatchingTimeout = 90; + fake.MatchingRemainingTime = 89; + fake.MatchingWaitTime = 1; + fake.Status = 1; + OnlineMatchingEntries[fake.CardId] = fake;*/ + + entry.CardId = cardId; + entry.MatchingId = 0xB16B00B5; + entry.EntryNo = 0; + entry.EntryStart = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + entry.MatchingTimeout = 90 * 1000; + entry.MatchingRemainingTime = 89 * 1000; + entry.MatchingWaitTime = 1; + entry.Status = 1; + entry.Dump().Info(); + OnlineMatchingEntries[cardId] = entry; + + return GenerateRecordsXml(OnlineMatchingEntries.Values.ToList(), Configs.ONLINE_MATCHING_XPATH); + } + + #endregion + private enum CardRequestType { // Read data @@ -613,8 +662,8 @@ public class CardServiceController : WebApiController ReadTotalTrophy = 8468, // Sessions - SessionGet = 401, - SessionStart = 402, + GetSession = 401, + StartSession = 402, // Write data WriteCard = 771, @@ -628,7 +677,10 @@ public class CardServiceController : WebApiController WriteCoin = 980, WriteSkin = 933, WriteUnlockKeynum = 1020, - WriteSoundEffect = 8969 + WriteSoundEffect = 8969, + + // Online matching + StartOnlineMatching = 8705 } private enum Command @@ -645,12 +697,12 @@ public class CardServiceController : WebApiController /// 処理は正常に完了しました in debug string /// Ok = 1, - + /// /// 未登録のカードです in debug string /// CardNotRegistered = 23, - + /// /// 再発行予約がありません in debug string /// diff --git a/GC-local-server-rewrite/controllers/ServerController.cs b/GC-local-server-rewrite/controllers/ServerController.cs index eb429e9..dde2ef0 100644 --- a/GC-local-server-rewrite/controllers/ServerController.cs +++ b/GC-local-server-rewrite/controllers/ServerController.cs @@ -15,6 +15,8 @@ namespace GCLocalServerRewrite.controllers; public class ServerController : WebApiController { + private static readonly string DataUrl = $"https://{Configs.SETTINGS.ServerIp}/{Configs.STATIC_FOLDER}/{Configs.SETTINGS.EventFolder}"; + [Route(HttpVerbs.Get, "/certify.php")] public string Certify([QueryData] NameValueCollection parameters) { @@ -99,12 +101,13 @@ public class ServerController : WebApiController for (var i = 0; i < count; i++) { var data = responseList[i]; - dataString.Append($"{i},{data.FileName},{data.NotBeforeUnixTime},{data.NotAfterUnixTime},{data.Md5},{data.Index}"); + var fileUrl = data.FileName.StartsWith("/") ? $"{DataUrl}{data.FileName}" : $"{DataUrl}/{data.FileName}"; + dataString.Append($"{i},{fileUrl},{data.NotBeforeUnixTime},{data.NotAfterUnixTime},{data.Md5},{data.Index}"); dataString.Append('\n'); } return $"count={count}\n" + - "nexttime=0\n" + + "nexttime=1\n" + dataString.ToString().TrimEnd('\n'); } diff --git a/GC-local-server-rewrite/models/OnlineMatchingEntry.cs b/GC-local-server-rewrite/models/OnlineMatchingEntry.cs new file mode 100644 index 0000000..085f9dc --- /dev/null +++ b/GC-local-server-rewrite/models/OnlineMatchingEntry.cs @@ -0,0 +1,87 @@ +using System.Xml.Serialization; +using ChoETL; + +namespace GCLocalServerRewrite.models; + +public class OnlineMatchingEntry: Record +{ + [XmlElement(ElementName = "machine_id")] + [ChoXmlElementRecordField(FieldName = "machine_id")] + public long MachineId { get; set; } + + [XmlElement(ElementName = "event_id")] + [ChoXmlElementRecordField(FieldName = "event_id")] + public long EventId { get; set; } + + [XmlElement(ElementName = "matching_id")] + [ChoXmlElementRecordField(FieldName = "matching_id")] + public long MatchingId { get; set; } + + [XmlElement(ElementName = "entry_no")] + [ChoXmlElementRecordField(FieldName = "entry_no")] + public long EntryNo { get; set; } + + [XmlElement(ElementName = "entry_start")] + [ChoXmlElementRecordField(FieldName = "entry_start")] + public string EntryStart { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + + [XmlElement(ElementName = "status")] + [ChoXmlElementRecordField(FieldName = "status")] + public long Status { get; set; } = 1; + + [XmlElement(ElementName = "card_id")] + [ChoXmlElementRecordField(FieldName = "card_id")] + public long CardId { get; set; } + + [XmlElement(ElementName = "player_name")] + [ChoXmlElementRecordField(FieldName = "player_name")] + public string PlayerName { get; set; } = string.Empty; + + [XmlElement(ElementName = "avatar_id")] + [ChoXmlElementRecordField(FieldName = "avatar_id")] + public long AvatarId { get; set; } + + [XmlElement(ElementName = "title_id")] + [ChoXmlElementRecordField(FieldName = "title_id")] + public long TitleId { get; set; } + + [XmlElement(ElementName = "class_id")] + [ChoXmlElementRecordField(FieldName = "class_id")] + public long ClassId { get; set; } + + [XmlElement(ElementName = "group_id")] + [ChoXmlElementRecordField(FieldName = "group_id")] + public long GroupId { get; set; } + + [XmlElement(ElementName = "tenpo_id")] + [ChoXmlElementRecordField(FieldName = "tenpo_id")] + public long TenpoId { get; set; } + + [XmlElement(ElementName = "tenpo_name")] + [ChoXmlElementRecordField(FieldName = "tenpo_name")] + public string TenpoName { get; set; } = "1337"; + + [XmlElement(ElementName = "pref_id")] + [ChoXmlElementRecordField(FieldName = "pref_id")] + public long PrefId { get; set; } + + [XmlElement(ElementName = "pref")] + [ChoXmlElementRecordField(FieldName = "pref")] + public string Pref { get; set; } = "nesys"; + + [XmlElement(ElementName = "message_id")] + [ChoXmlElementRecordField(FieldName = "message_id")] + public long MessageId { get; set; } + + [XmlElement(ElementName = "matching_timeout")] + [ChoXmlElementRecordField(FieldName = "matching_timeout")] + public long MatchingTimeout { get; set; } = 99; + + [XmlElement(ElementName = "matching_wait_time")] + [ChoXmlElementRecordField(FieldName = "matching_wait_time")] + public long MatchingWaitTime { get; set; } = 10; + + [XmlElement(ElementName = "matching_remaining_time")] + [ChoXmlElementRecordField(FieldName = "matching_remaining_time")] + public long MatchingRemainingTime { get; set; } = 89; +} \ No newline at end of file