return parsed card when setting

This commit is contained in:
ppc
2024-09-02 16:23:48 +01:00
parent ec98003d2c
commit 14efbc4971
2 changed files with 11 additions and 12 deletions
+10 -5
View File
@@ -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)));
}
/// <summary>
@@ -44,10 +47,10 @@ public class CardPresenter
/// <param name="cardId">The card id to set</param>
/// <param name="validFor">How long the card should be presented to the game for in milliseconds</param>
/// <returns>Whether the card was set successfully</returns>
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;
}
}
+1 -7
View File
@@ -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;
}