1
0
mirror of synced 2024-11-27 23:50:49 +01:00

Add card register, restructure CardServiceController.cs

This commit is contained in:
jiych1 2022-07-02 23:00:40 +08:00
parent c2258a5fd9
commit 95d9d0be5f
4 changed files with 118 additions and 97 deletions

View File

@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Keynum/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unlockable/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -1,5 +1,4 @@
using System.Diagnostics; using System.Net.Mime;
using System.Net.Mime;
using System.Text; using System.Text;
using System.Xml.Linq; using System.Xml.Linq;
using ChoETL; using ChoETL;
@ -28,18 +27,56 @@ public class CardServiceController : WebApiController
[Route(HttpVerbs.Post, "/cardn.cgi")] [Route(HttpVerbs.Post, "/cardn.cgi")]
// ReSharper disable once UnusedMember.Global // ReSharper disable once UnusedMember.Global
public string CardService([FormField] int gid, [FormField("mac_addr")] string mac, [FormField] int type, public string CardService([FormField] int gid, [FormField("mac_addr")] string mac, [FormField] int type,
[FormField("card_no")] long cardId, [FormField("data")] string xmlData) [FormField("card_no")] long cardId, [FormField("data")] string xmlData, [FormField("cmd_str")] int cmdType)
{ {
HttpContext.Response.ContentType = MediaTypeNames.Application.Octet; HttpContext.Response.ContentType = MediaTypeNames.Application.Octet;
HttpContext.Response.ContentEncoding = new UTF8Encoding(false); HttpContext.Response.ContentEncoding = new UTF8Encoding(false);
HttpContext.Response.KeepAlive = true; HttpContext.Response.KeepAlive = true;
return ProcessCommand(cmdType, mac, cardId, xmlData, type);
}
private string ProcessCommand(int cmdType, string mac, long cardId, string xmlData, int type)
{
if (!Enum.IsDefined(typeof(Command), cmdType))
{
throw new ArgumentOutOfRangeException(nameof(cmdType), cmdType, "Cmd type is unknown!");
}
var command = (Command)cmdType;
return command switch
{
Command.CardRequest => ProcessCardRequest(mac, cardId, xmlData, type),
Command.ReissueRequest => ProcessReissueRequest(),
Command.RegisterRequest => ProcessRegisterRequest(cardId, xmlData),
_ => throw new ArgumentOutOfRangeException(nameof(command), command, "Command unknown, should never happen!")
};
}
private string ProcessRegisterRequest(long cardId, string xmlData)
{
$"Get card register request, data is \n{xmlData}".Info();
Write<Card>(cardId, xmlData);
return ConstructResponse(xmlData);
}
private static string ProcessReissueRequest()
{
"Get reissue request, returning not reissue".Info();
return ConstructResponse("", ReturnCode.NotReissue);
}
private string ProcessCardRequest(string mac, long cardId, string xmlData, int type)
{
if (!Enum.IsDefined(typeof(CardRequestType), type)) if (!Enum.IsDefined(typeof(CardRequestType), type))
{ {
throw new ArgumentOutOfRangeException(nameof(type)); throw new ArgumentOutOfRangeException(nameof(type), type, "Card request type is unknown!");
} }
var requestType = (CardRequestType)type; var requestType = (CardRequestType)type;
$"Getting card request, type is {requestType}".Info();
switch (requestType) switch (requestType)
{ {
@ -47,136 +84,97 @@ public class CardServiceController : WebApiController
case CardRequestType.ReadCard: case CardRequestType.ReadCard:
{ {
$"Getting read request, type is {requestType}".Info(); var response = Card(cardId, out var returnCode);
return ConstructResponse(Card(cardId)); return ConstructResponse(response, returnCode);
} }
case CardRequestType.ReadCardDetail: case CardRequestType.ReadCardDetail:
{ {
$"Getting read request, type is {requestType}".Info(); var cardDetail = CardDetail(cardId, xmlData);
return ConstructResponse(cardDetail);
return ConstructResponse(CardDetail(cardId, xmlData));
} }
case CardRequestType.ReadCardDetails: case CardRequestType.ReadCardDetails:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(CardDetails(cardId)); return ConstructResponse(CardDetails(cardId));
} }
case CardRequestType.ReadCardBData: case CardRequestType.ReadCardBData:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(CardBData(cardId)); return ConstructResponse(CardBData(cardId));
} }
case CardRequestType.ReadAvatar: case CardRequestType.ReadAvatar:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse( return ConstructResponse(
GetStaticCount<Avatar>(cardId, Configs.SETTINGS.AvatarCount, Configs.AVATAR_XPATH)); GetStaticCount<Avatar>(cardId, Configs.SETTINGS.AvatarCount, Configs.AVATAR_XPATH));
} }
case CardRequestType.ReadItem: case CardRequestType.ReadItem:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse( return ConstructResponse(
GetStaticCount<Item>(cardId, Configs.SETTINGS.ItemCount, Configs.ITEM_XPATH)); GetStaticCount<Item>(cardId, Configs.SETTINGS.ItemCount, Configs.ITEM_XPATH));
} }
case CardRequestType.ReadSkin: case CardRequestType.ReadSkin:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse( return ConstructResponse(
GetStaticCount<Skin>(cardId, Configs.SETTINGS.SkinCount, Configs.SKIN_XPATH)); GetStaticCount<Skin>(cardId, Configs.SETTINGS.SkinCount, Configs.SKIN_XPATH));
} }
case CardRequestType.ReadTitle: case CardRequestType.ReadTitle:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse( return ConstructResponse(
GetStaticCount<Title>(cardId, Configs.SETTINGS.TitleCount, Configs.TITLE_XPATH)); GetStaticCount<Title>(cardId, Configs.SETTINGS.TitleCount, Configs.TITLE_XPATH));
} }
case CardRequestType.ReadMusic: case CardRequestType.ReadMusic:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(MusicUnlock()); return ConstructResponse(MusicUnlock());
} }
case CardRequestType.ReadEventReward: case CardRequestType.ReadEventReward:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse( return ConstructResponse(
GenerateEmptyXML(Configs.EVENT_REWARD)); GenerateEmptyXml(Configs.EVENT_REWARD));
} }
case CardRequestType.ReadNavigator: case CardRequestType.ReadNavigator:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse( return ConstructResponse(
GetStaticCount<Navigator>(cardId, Configs.SETTINGS.NavigatorCount, Configs.NAVIGATOR_XPATH)); GetStaticCount<Navigator>(cardId, Configs.SETTINGS.NavigatorCount, Configs.NAVIGATOR_XPATH));
} }
case CardRequestType.ReadMusicExtra: case CardRequestType.ReadMusicExtra:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(MusicExtra()); return ConstructResponse(MusicExtra());
} }
case CardRequestType.ReadMusicAou: case CardRequestType.ReadMusicAou:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(MusicAouUnlock()); return ConstructResponse(MusicAouUnlock());
} }
case CardRequestType.ReadCoin: case CardRequestType.ReadCoin:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(Coin(cardId)); return ConstructResponse(Coin(cardId));
} }
case CardRequestType.ReadUnlockReward: case CardRequestType.ReadUnlockReward:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(UnlockReward(cardId)); return ConstructResponse(UnlockReward(cardId));
} }
case CardRequestType.ReadUnlockKeynum: case CardRequestType.ReadUnlockKeynum:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(UnlockKeynum(cardId)); return ConstructResponse(UnlockKeynum(cardId));
} }
case CardRequestType.ReadSoundEffect: case CardRequestType.ReadSoundEffect:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse( return ConstructResponse(
GetStaticCount<SoundEffect>(cardId, Configs.SETTINGS.SeCount, Configs.SE_XPATH)); GetStaticCount<SoundEffect>(cardId, Configs.SETTINGS.SeCount, Configs.SE_XPATH));
} }
case CardRequestType.ReadGetMessage: case CardRequestType.ReadGetMessage:
{ {
$"Getting read request, type is {requestType}".Info(); return ConstructResponse(GenerateEmptyXml(Configs.GET_MESSAGE));
return ConstructResponse(GenerateEmptyXML(Configs.GET_MESSAGE));
} }
case CardRequestType.ReadCond: case CardRequestType.ReadCond:
{ {
$"Getting read request, type is {requestType}".Info(); return ConstructResponse(GenerateEmptyXml(Configs.COND));
return ConstructResponse(GenerateEmptyXML(Configs.COND));
} }
case CardRequestType.ReadTotalTrophy: case CardRequestType.ReadTotalTrophy:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(TotalTrophy(cardId)); return ConstructResponse(TotalTrophy(cardId));
} }
case CardRequestType.SessionStart: case CardRequestType.SessionStart:
case CardRequestType.SessionGet: case CardRequestType.SessionGet:
{ {
$"Getting read request, type is {requestType}".Info();
return ConstructResponse(GetSession(cardId, mac)); return ConstructResponse(GetSession(cardId, mac));
} }
@ -184,28 +182,26 @@ public class CardServiceController : WebApiController
#region WriteRequests #region WriteRequests
case CardRequestType.WriteCard:
{
$"Card Request data is {xmlData}".Info();
Write<Card>(cardId, xmlData);
return ConstructResponse(xmlData);
}
case CardRequestType.WriteCardDetail: case CardRequestType.WriteCardDetail:
{ {
$"Getting write request, type is {requestType}\n Data is {xmlData}".Info(); $"Card Request data is {xmlData}".Info();
WriteCardDetail(cardId, xmlData); WriteCardDetail(cardId, xmlData);
return ConstructResponse(xmlData); return ConstructResponse(xmlData);
} }
case CardRequestType.WriteCardBData: case CardRequestType.WriteCardBData:
{ {
$"Getting write request, type is {requestType}\n Data is {xmlData}".Info(); $"Card Request data is {xmlData}".Info();
Write<CardBData>(cardId, xmlData); Write<CardBData>(cardId, xmlData);
WriteCardPlayCount(cardId); WriteCardPlayCount(cardId);
return ConstructResponse(xmlData);
}
case CardRequestType.WriteCard:
{
$"Getting write request, type is {requestType}\n Data is {xmlData}".Info();
Write<Card>(cardId, xmlData);
return ConstructResponse(xmlData); return ConstructResponse(xmlData);
} }
// TODO: Maybe one day implement these
case CardRequestType.WriteAvatar: case CardRequestType.WriteAvatar:
case CardRequestType.WriteItem: case CardRequestType.WriteItem:
case CardRequestType.WriteTitle: case CardRequestType.WriteTitle:
@ -216,33 +212,32 @@ public class CardServiceController : WebApiController
case CardRequestType.WriteUnlockKeynum: case CardRequestType.WriteUnlockKeynum:
case CardRequestType.WriteSoundEffect: case CardRequestType.WriteSoundEffect:
{ {
$"Getting write request, type is {requestType}\n Data is {xmlData}".Info(); $"Card Request data is {xmlData}".Info();
return ConstructResponse(xmlData); return ConstructResponse(xmlData);
} }
#endregion #endregion
default: default:
#pragma warning disable CA2208 throw new ArgumentOutOfRangeException(nameof(requestType), requestType, "Request type not captured, should never happen!");
throw new ArgumentOutOfRangeException(nameof(requestType));
#pragma warning restore CA2208
} }
} }
#region ReadImplementation #region ReadImplementation
private string Card(long cardId) private string Card(long cardId, out ReturnCode returnCode)
{ {
var result = cardSqLiteConnection.Table<Card>().Where(card => card.CardId == cardId); var result = cardSqLiteConnection.Table<Card>().Where(card => card.CardId == cardId);
if (!result.Any()) if (!result.Any())
{ {
return GenerateEmptyXML(Configs.CARD); returnCode = ReturnCode.CardNotRegistered;
return string.Empty;
} }
var card = result.First(); var card = result.First();
returnCode = ReturnCode.Ok;
return GenerateSingleXml(card, Configs.CARD_XPATH); return GenerateSingleXml(card, Configs.CARD_XPATH);
} }
@ -258,11 +253,10 @@ public class CardServiceController : WebApiController
if (!result.Any()) if (!result.Any())
{ {
return GenerateEmptyXML(Configs.CARD_DETAIL); return GenerateEmptyXml(Configs.CARD_DETAIL);
} }
var cardDetail = result.First(); var cardDetail = result.First();
return GenerateSingleXml(cardDetail, Configs.CARD_DETAIL_SINGLE_XPATH); return GenerateSingleXml(cardDetail, Configs.CARD_DETAIL_SINGLE_XPATH);
} }
@ -273,7 +267,7 @@ public class CardServiceController : WebApiController
if (!result.Any()) if (!result.Any())
{ {
return GenerateEmptyXML(Configs.CARD_DETAIL); return GenerateEmptyXml(Configs.CARD_DETAIL);
} }
var cardDetails = result.ToList(); var cardDetails = result.ToList();
@ -288,7 +282,7 @@ public class CardServiceController : WebApiController
if (!result.Any()) if (!result.Any())
{ {
return GenerateEmptyXML(Configs.CARD_BDATA); return GenerateEmptyXml(Configs.CARD_BDATA);
} }
var cardBData = result.First(); var cardBData = result.First();
@ -394,7 +388,7 @@ public class CardServiceController : WebApiController
{ {
var result = musicSqLiteConnection.Table<MusicAou>().ToList(); var result = musicSqLiteConnection.Table<MusicAou>().ToList();
return !result.Any() ? GenerateEmptyXML(Configs.MUSIC_AOU) : GenerateRecordsXml(result, Configs.MUSIC_AOU_XPATH); return !result.Any() ? GenerateEmptyXml(Configs.MUSIC_AOU) : GenerateRecordsXml(result, Configs.MUSIC_AOU_XPATH);
} }
private string MusicExtra() private string MusicExtra()
@ -408,23 +402,28 @@ public class CardServiceController : WebApiController
#region HelperMethods #region HelperMethods
private static string ConstructResponse(string xml) private static string ConstructResponse(string xml, ReturnCode returnCode = ReturnCode.Ok)
{ {
return "1\n" + var returnCodeInt = (int)returnCode;
"10,10\n" + if (returnCodeInt == 1)
xml; {
return $"{returnCodeInt}\n" +
"1,1\n" +
xml;
}
return $"{returnCodeInt}";
} }
private static string GenerateEmptyXML(string fieldName) private static string GenerateEmptyXml(string fieldName)
{ {
var xml = new XDocument(new XElement("root", var xml = new XDocument(new XElement("root",
new XElement(fieldName))); new XElement(fieldName)));
xml.Declaration = new XDeclaration("1.0", "UTF-8", null); xml.Declaration = new XDeclaration("1.0", "UTF-8", null);
return xml.ToString(); return xml.ToString();
} }
private static string GenerateSingleXml<T>(T obj, string xpath) where T: class private static string GenerateSingleXml<T>(T obj, string xpath) where T : class
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
@ -439,7 +438,7 @@ public class CardServiceController : WebApiController
return sb.ToString(); return sb.ToString();
} }
private static string GenerateRecordsXml<T>(IReadOnlyList<T> list, string xpath) where T : Record private static string GenerateRecordsXml<T>(IReadOnlyList<T> list, string xpath) where T : Record
{ {
var stringBuilder = new StringBuilder(); var stringBuilder = new StringBuilder();
@ -549,7 +548,7 @@ public class CardServiceController : WebApiController
{ {
unlockableSongIds = Configs.DEFAULT_UNLOCKABLE_SONGS; unlockableSongIds = Configs.DEFAULT_UNLOCKABLE_SONGS;
} }
var detailList = unlockableSongIds!.Select(id => new CardDetail var detailList = unlockableSongIds.Select(id => new CardDetail
{ {
CardId = cardId, CardId = cardId,
Pcol1 = 10, Pcol1 = 10,
@ -559,10 +558,10 @@ public class CardServiceController : WebApiController
ScoreUi6 = 1 ScoreUi6 = 1
}) })
.ToList(); .ToList();
cardSqLiteConnection.InsertOrIgnoreAll(detailList); cardSqLiteConnection.InsertOrIgnoreAll(detailList);
} }
Write<CardDetail>(cardId, xmlData); Write<CardDetail>(cardId, xmlData);
} }
@ -610,4 +609,29 @@ public class CardServiceController : WebApiController
WriteUnlockKeynum = 1020, WriteUnlockKeynum = 1020,
WriteSoundEffect = 8969 WriteSoundEffect = 8969
} }
private enum Command
{
CardRequest = 256,
RegisterRequest = 512,
ReissueRequest = 1536
}
private enum ReturnCode
{
/// <summary>
/// 処理は正常に完了しました in debug string
/// </summary>
Ok = 1,
/// <summary>
/// 未登録のカードです in debug string
/// </summary>
CardNotRegistered = 23,
/// <summary>
/// 再発行予約がありません in debug string
/// </summary>
NotReissue = 27
}
} }

View File

@ -16,7 +16,7 @@ public class Card : ICardIdModel
[Column("player_name")] [Column("player_name")]
[ChoXmlElementRecordField(FieldName = "player_name")] [ChoXmlElementRecordField(FieldName = "player_name")]
[XmlElement(ElementName = "player_name")] [XmlElement(ElementName = "player_name")]
public string PlayerName { get; set; } = "player"; public string PlayerName { get; set; } = string.Empty;
[Column("score_i1")] [Column("score_i1")]
[ChoXmlElementRecordField(FieldName = "score_i1")] [ChoXmlElementRecordField(FieldName = "score_i1")]
@ -46,12 +46,12 @@ public class Card : ICardIdModel
[Column("created")] [Column("created")]
[ChoXmlElementRecordField(FieldName = "created")] [ChoXmlElementRecordField(FieldName = "created")]
[XmlElement("created")] [XmlElement("created")]
public string Created { get; set; } = "1"; public string Created { get; set; } = "2017-01-01 08:00:00";
[Column("updated")] [Column("modified")]
[ChoXmlElementRecordField(FieldName = "updated")] [ChoXmlElementRecordField(FieldName = "modified")]
[XmlElement("updated")] [XmlElement("modified")]
public string Updated { get; set; } = "1"; public string Modified { get; set; } = "2017-01-01 08:00:00";
public void SetCardId(long cardId) public void SetCardId(long cardId)
{ {

View File

@ -1,6 +0,0 @@
namespace GCLocalServerRewrite.models;
public class EmptyCard
{
}