From 14efbc4971877c863b3857970f3cbfaa6bac4283 Mon Sep 17 00:00:00 2001 From: ppc Date: Mon, 2 Sep 2024 16:23:48 +0100 Subject: [PATCH] return parsed card when setting --- AMNet.Server/CardPresenter.cs | 15 ++++++++++----- AMNet.Server/WebServer.cs | 8 +------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/AMNet.Server/CardPresenter.cs b/AMNet.Server/CardPresenter.cs index d1ef0f2..4e1fc41 100644 --- a/AMNet.Server/CardPresenter.cs +++ b/AMNet.Server/CardPresenter.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.Linq; namespace AMNet.Server; @@ -14,6 +15,8 @@ public class CardPresenter public record StoredCard(byte[] Value, string OriginalValue, long ExpiresAt) { public bool Expired => Environment.TickCount64 >= ExpiresAt; + + public override string ToString() => string.Join(" ", Enumerable.Range(0, 5).Select(x => OriginalValue.Substring(x * 4, 4))); } /// @@ -44,10 +47,10 @@ public class CardPresenter /// The card id to set /// How long the card should be presented to the game for in milliseconds /// Whether the card was set successfully - public bool SetCard(string cardId, long validFor = 5000) + public StoredCard SetCard(string cardId, long validFor = 5000) { // ensure the matrix code is 20-digits long, otherwise pad with zeros - var matrixCode = cardId.PadLeft(20, '0'); + var matrixCode = cardId.Replace(" ", "").PadLeft(20, '0'); var bytes = new byte[10]; for (var i = 0; i < bytes.Length; i++) @@ -59,15 +62,17 @@ public class CardPresenter } else { - return false; + return null; } } + + var card = new StoredCard(bytes, matrixCode, Environment.TickCount64 + validFor); lock (_cardLock) { - _currentCard = new StoredCard(bytes, matrixCode, Environment.TickCount64 + validFor); + _currentCard = card; } - return true; + return card; } } \ No newline at end of file diff --git a/AMNet.Server/WebServer.cs b/AMNet.Server/WebServer.cs index 88a1b02..b0d5427 100644 --- a/AMNet.Server/WebServer.cs +++ b/AMNet.Server/WebServer.cs @@ -1,11 +1,5 @@ using System; -using System.Collections; -using System.Collections.Generic; using System.Globalization; -using System.Linq; -using System.Net; -using System.Net.NetworkInformation; -using System.Net.Sockets; using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; @@ -107,7 +101,7 @@ internal static class WebServer return; } - if (cardPresenter.SetCard(request.MatrixCode)) + if (cardPresenter.SetCard(request.MatrixCode) != null) { ctx.Response.StatusCode = 202; }