248 lines
9.1 KiB
C#
248 lines
9.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Numerics;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
|
|
using Google.Protobuf.WellKnownTypes;
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using ServerCore;
|
|
using ServerBase;
|
|
|
|
|
|
using USER_GUID = System.String;
|
|
using META_ID = System.UInt32;
|
|
|
|
|
|
namespace ServerCommon;
|
|
|
|
public class CartAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute
|
|
{
|
|
[JsonProperty]
|
|
public Dictionary<CurrencyType, Dictionary<META_ID/*ItemMetaID*/, Int32/*Count*/>> CartItems { get; set; } = new();
|
|
|
|
public CartAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType)
|
|
: base(owner, entityOfOwnerEntityType)
|
|
{
|
|
var currencyTypes = EnumHelper.getValues<CurrencyType>();
|
|
foreach (var currencyType in currencyTypes)
|
|
{
|
|
CartItems.TryAdd(currencyType, new());
|
|
}
|
|
}
|
|
|
|
public override void onClear()
|
|
{
|
|
foreach (var value in CartItems.Values)
|
|
{
|
|
value.Clear();
|
|
}
|
|
|
|
getAttributeState().reset();
|
|
}
|
|
|
|
public override IEntityAttributeTransactor onNewEntityAttributeTransactor()
|
|
{
|
|
var player = getOwner().getRootParent() as UserBase;
|
|
NullReferenceCheckHelper.throwIfNull(player, () => $"player is null !!!");
|
|
|
|
return new CartAttributeTransactor(player);
|
|
}
|
|
|
|
public override EntityAttributeBase onCloned()
|
|
{
|
|
var owner = getOwner().getRootParent() as UserBase;
|
|
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
|
|
|
|
var entity_of_owner_entity_type = getEntityOfOwnerEntityType();
|
|
NullReferenceCheckHelper.throwIfNull(entity_of_owner_entity_type, () => $"entity_of_owner_entity_type is null !!!");
|
|
|
|
var cloned = new CartAttribute(owner, entity_of_owner_entity_type);
|
|
cloned.CartItems = CartItems.ToDictionary(x => x.Key, y => new Dictionary<META_ID, Int32>(y.Value.ToDictionary(x => x.Key, y => y.Value)));
|
|
|
|
return cloned;
|
|
}
|
|
|
|
public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
var owner = getOwner().getRootParent();
|
|
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
|
|
|
|
var acoount_attribute = owner.getEntityAttribute<AccountAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(acoount_attribute, () => $"acoount_attribute is null !!!");
|
|
|
|
USER_GUID user_guid = acoount_attribute.UserGuid;
|
|
|
|
//=====================================================================================
|
|
// Attribute => try pending Doc
|
|
//=====================================================================================
|
|
var try_pending_doc = getTryPendingDocBase() as CartDoc;
|
|
if (null == try_pending_doc)
|
|
{
|
|
var to_copy_doc = new CartDoc(user_guid);
|
|
|
|
var origin_doc = getOriginDocBase<CartAttribute>();
|
|
if (null != origin_doc)
|
|
{
|
|
to_copy_doc.copyTimestampsFromOriginDocBase(origin_doc);
|
|
}
|
|
|
|
try_pending_doc = to_copy_doc;
|
|
|
|
setTryPendingDocBase(try_pending_doc);
|
|
}
|
|
|
|
var to_copy_doc_attrib = try_pending_doc.getAttrib<CartAttrib>();
|
|
NullReferenceCheckHelper.throwIfNull(to_copy_doc_attrib, () => $"to_copy_doc_attrib is null !!!");
|
|
|
|
to_copy_doc_attrib.CartItems = CartItems.ToDictionary(x => x.Key, y => new Dictionary<META_ID, Int32>(y.Value.ToDictionary(x => x.Key, y => y.Value)));
|
|
|
|
if (false == isForQuery)
|
|
{
|
|
return (result, try_pending_doc);
|
|
}
|
|
|
|
//=====================================================================================
|
|
// Doc QueryType 반영
|
|
//=====================================================================================
|
|
(result, var to_query_doc) = await applyDoc4Query(try_pending_doc);
|
|
if (result.isFail())
|
|
{
|
|
return (result, null);
|
|
}
|
|
|
|
return (result, to_query_doc);
|
|
}
|
|
|
|
public bool copyEntityAttributeFromDoc(DynamoDbDocBase docBase)
|
|
{
|
|
var err_msg = string.Empty;
|
|
|
|
var to_cast_string = typeof(CartDoc).Name;
|
|
|
|
var cart_doc_base = docBase as CartDoc;
|
|
if (null == cart_doc_base)
|
|
{
|
|
err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, cart_doc_base is null :{to_cast_string}";
|
|
Log.getLogger().error(err_msg);
|
|
return false;
|
|
}
|
|
|
|
//=====================================================================================
|
|
// New Doc => Origin Doc
|
|
//=====================================================================================
|
|
syncOriginDocBaseWithNewDoc<CartAttribute>(cart_doc_base);
|
|
|
|
//=====================================================================================
|
|
// Doc => Attribute
|
|
//=====================================================================================
|
|
var doc_attrib = cart_doc_base.getAttrib<CartAttrib>();
|
|
NullReferenceCheckHelper.throwIfNull(doc_attrib, () => $"doc_attrib is null !!!");
|
|
|
|
CartItems = doc_attrib.CartItems.ToDictionary(x => x.Key, y => new Dictionary<META_ID, Int32>(y.Value.ToDictionary(x => x.Key, y => y.Value)));
|
|
|
|
return true;
|
|
}
|
|
|
|
public Result onMerge(EntityAttributeBase otherEntityAttribute)
|
|
{
|
|
var owner = getOwner();
|
|
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
|
|
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
if (null == otherEntityAttribute)
|
|
{
|
|
err_msg = $"Invalid Param !!!, otherEntityAttribute is null";
|
|
result.setFail(ServerErrorCode.FunctionParamNull, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
//=====================================================================================
|
|
// OtherAttribute => Attribute
|
|
//=====================================================================================
|
|
var cart_attribute = otherEntityAttribute as CartAttribute;
|
|
if (null == cart_attribute)
|
|
{
|
|
err_msg = $"Failed to cast CartAttribute !!!, cart_attribute is null";
|
|
result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
CartItems = cart_attribute.CartItems.ToDictionary(x => x.Key, y => new Dictionary<META_ID, Int32>(y.Value.ToDictionary(x => x.Key, y => y.Value)));
|
|
|
|
//=====================================================================================
|
|
// Attribute Try Pending Doc => Origin Doc
|
|
//=====================================================================================
|
|
var try_pending_doc = cart_attribute.getTryPendingDocBase() as CartDoc;
|
|
if (null != try_pending_doc)
|
|
{
|
|
cart_attribute.resetTryPendingDocBase();
|
|
|
|
syncOriginDocBaseWithNewDoc<CartAttribute>(try_pending_doc);
|
|
}
|
|
|
|
var origin_doc_base = getOriginDocBase<CartAttribute>();
|
|
if (null == origin_doc_base)
|
|
{
|
|
// DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!!
|
|
return result;
|
|
}
|
|
|
|
var cart_attrib = origin_doc_base.getAttrib<CartAttrib>();
|
|
NullReferenceCheckHelper.throwIfNull(cart_attrib, () => $"cart_attrib is null !!! - {owner.toBasicString()}");
|
|
|
|
cart_attrib.CartItems = CartItems.ToDictionary(x => x.Key, y => new Dictionary<META_ID, Int32>(y.Value.ToDictionary(x => x.Key, y => y.Value)));
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public class CartAttributeTransactor : EntityAttributeTransactorBase<CartAttribute>, ICopyEntityAttributeTransactorFromEntityAttribute
|
|
{
|
|
public CartAttributeTransactor(UserBase owner)
|
|
: base(owner)
|
|
{
|
|
}
|
|
|
|
public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase)
|
|
{
|
|
var err_msg = string.Empty;
|
|
|
|
var to_cast_string = typeof(CartAttribute).Name;
|
|
|
|
var copy_from_cart_attribute = entityAttributeBase as CartAttribute;
|
|
if (null == copy_from_cart_attribute)
|
|
{
|
|
err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_from_cart_attribute is null :{to_cast_string}";
|
|
Log.getLogger().error(err_msg);
|
|
return false;
|
|
}
|
|
|
|
var copy_to_cart_attribute = getClonedEntityAttribute() as CartAttribute;
|
|
if (null == copy_to_cart_attribute)
|
|
{
|
|
err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_to_cart_attribute is null :{to_cast_string}";
|
|
Log.getLogger().error(err_msg);
|
|
return false;
|
|
}
|
|
|
|
copy_to_cart_attribute.CartItems = copy_from_cart_attribute.CartItems.ToDictionary(x => x.Key, y => new Dictionary<META_ID, Int32>(y.Value.ToDictionary(x => x.Key, y => y.Value)));
|
|
return true;
|
|
}
|
|
}
|