mirror of
https://gitea.tendokyu.moe/ppc/amnet.git
synced 2026-09-27 17:27:55 +03:00
return parsed card when setting
This commit is contained in:
@@ -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,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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user