Files
2025-05-01 07:20:41 +09:00

169 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ServerCore;
using ServerBase;
using SESSION_ID = System.Int32;
using META_ID = System.UInt32;
using ENTITY_GUID = System.String;
using ACCOUNT_ID = System.String;
using OWNER_GUID = System.String;
using USER_GUID = System.String;
using CHARACTER_GUID = System.String;
using ITEM_GUID = System.String;
namespace ServerCommon;
public static class MoneyAttribExtensions
{
public static double getCurrencyFromType(this MoneyAttrib attrib, CurrencyType type)
{
var currency = type switch
{
CurrencyType.Gold => attrib.Gold,
CurrencyType.Sapphire => attrib.Sapphire,
CurrencyType.Calium => attrib.Calium,
CurrencyType.Ruby => attrib.Ruby,
_ => 0
};
return currency;
}
public static void setCurrencyFromType(this MoneyAttrib attrib, CurrencyType type, double currency)
{
var result = type switch
{
CurrencyType.Gold => attrib.Gold = currency,
CurrencyType.Sapphire => attrib.Sapphire = currency,
CurrencyType.Calium => attrib.Calium = currency,
CurrencyType.Ruby => attrib.Ruby = currency,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
}
public static string getKeyNameFromType(CurrencyType type)
{
var result = type switch
{
CurrencyType.Gold => nameof(MoneyAttrib.Gold),
CurrencyType.Sapphire => nameof(MoneyAttrib.Sapphire),
CurrencyType.Calium => nameof(MoneyAttrib.Calium),
CurrencyType.Ruby => nameof(MoneyAttrib.Ruby),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
return result;
}
}
public class MoneyAttrib : AttribBase
{
[JsonProperty("owner_guid")]
public OWNER_GUID OwnerGuid { get; set; } = string.Empty;
[JsonProperty("owner_entity_type")]
public OwnerEntityType OwnerEntityType { get; set; } = OwnerEntityType.None;
[JsonProperty("gold")]
public double Gold { get; set; } = 0;
[JsonProperty("sapphire")]
public double Sapphire { get; set; } = 0;
[JsonProperty("calium")]
public double Calium { get; set; } = 0;
[JsonProperty("ruby")]
public double Ruby { get; set; } = 0;
public MoneyAttrib()
: base(typeof(MoneyAttrib).Name, false)
{ }
}
//=============================================================================================
// Primary Key
// PK(Partition Key) : "money#owner_guid" [owner_guid : user_guid]
// SK(Sort Key) : ""
// DocType : MoneyDoc
// MoneyAttrib : {}
// ...
//=============================================================================================
public class MoneyDoc : DynamoDbDocBase
{
private static string getPrefixOfPK() { return "money#"; }
private static string getPrefixOfSK() { return ""; }
public MoneyDoc()
: base(typeof(MoneyDoc).Name)
{
appendAttribWrapperAll();
}
public MoneyDoc(OwnerEntityType ownerEntityType, string ownerGuid)
: base(typeof(MoneyDoc).Name)
{
setCombinationKeyForPK(ownerGuid);
appendAttribWrapperAll();
fillUpPrimaryKey(onMakePK(), onMakeSK());
var doc_attrib = getAttrib<MoneyAttrib>();
NullReferenceCheckHelper.throwIfNull(doc_attrib, () => "MoneyAttrib is null !!");
doc_attrib.OwnerGuid = ownerGuid;
doc_attrib.OwnerEntityType = ownerEntityType;
}
protected override string onGetPrefixOfPK()
{
return getPrefixOfPK();
}
private void appendAttribWrapperAll()
{
appendAttribWrapper(new AttribWrapper<MoneyAttrib>());
}
public static async Task<(Result result, MoneyAttrib? attrib)> findMoneyFromGuid(DynamoDbClient dynamoDbClient, string ownerGuid)
{
var result = new Result();
var money_docs = new List<MoneyDoc>();
(result, var make_primary_key) = await DynamoDBDocBaseHelper.makePrimaryKey<MoneyDoc>(ownerGuid);
if (result.isFail() || null == make_primary_key)
{
return (result, null);
}
var query_config = dynamoDbClient.makeQueryConfigForReadByPKSK(make_primary_key.PK, make_primary_key.SK);
(result, money_docs) = await dynamoDbClient.simpleQueryDocTypesWithQueryOperationConfig<MoneyDoc>(query_config);
if (result.isFail())
{
return (result, null);
}
var attrib = money_docs[0].getAttrib<MoneyAttrib>();
if (attrib == null) return (result, null);
return (result, attrib);
}
}