using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using Microsoft.AspNetCore.DataProtection.KeyManagement; using ServerCore; using ServerBase; using SESSION_ID = System.Int32; using WORLD_ID = System.UInt32; 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; using UI_KEY = System.String; using DATA_KEY = System.String; namespace ServerCommon { //============================================================================================= // // 클라이언트 Data 전용 DB 저장 정보 // // - 서버측에서 로직 관여 하지 않는다. (읽기, 쓰기 only) // - 추후 String 타입으로 하나의 변수로 저장 하도록 수정 한다. (전용 패킷 추가, 클라이언트와 협의) // //============================================================================================= public class CustomDefinedDataAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute { public DATA_KEY DataKey { get; set; } = string.Empty; public string CustomDefinedData { get; set; } = string.Empty; public CustomDefinedDataAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType) : base(owner, entityOfOwnerEntityType) { } public override IEntityAttributeTransactor onNewEntityAttributeTransactor() { return new CustomDefinedDataAttributeTransactor(getOwner().getRootParent()); } public override void onClear() { DataKey = string.Empty; CustomDefinedData = string.Empty; getAttributeState().reset(); } public override EntityAttributeBase onCloned() { var owner = getOwner(); var entity_of_owner_entity_type = getEntityOfOwnerEntityType(); NullReferenceCheckHelper.throwIfNull(entity_of_owner_entity_type, () => $"entity_of_owner_entity_type is null !!!"); var cloned = new CustomDefinedDataAttribute(owner, entity_of_owner_entity_type); cloned.deepCopyFromBase(this); cloned.DataKey = DataKey; cloned.CustomDefinedData = CustomDefinedData; return cloned; } public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true) { var owner = getOwner(); var parent = owner.getRootParent(); var result = new Result(); var user_attribute = parent.getOriginEntityAttribute(); NullReferenceCheckHelper.throwIfNull(user_attribute, () => $"user_attribute is null !!! - {parent.toBasicString()}"); var user_guid = user_attribute.UserGuid; //===================================================================================== // Attribute => try pending Doc //===================================================================================== var try_pending_doc = getTryPendingDocBase() as CustomDefinedDataDoc ; if (null == try_pending_doc) { var to_copy_doc = new CustomDefinedDataDoc(OwnerEntityType.User, user_guid, DataKey); var origin_doc = getOriginDocBase(); if (null != origin_doc) { to_copy_doc.copyTimestampsFromOriginDocBase(origin_doc); } try_pending_doc = to_copy_doc; setTryPendingDocBase(try_pending_doc); } var to_copy_doc_custom_defined_data_attrib = try_pending_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(to_copy_doc_custom_defined_data_attrib, () => $"to_copy_doc_custom_defined_data_attrib is null !!! - {parent.toBasicString()}"); to_copy_doc_custom_defined_data_attrib.CustomDefinedData = CustomDefinedData; 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 owner = getOwner(); var parent = owner.getRootParent(); var err_msg = string.Empty; var to_cast_string = typeof(CustomDefinedUiDoc).Name; var custom_defined_data_doc = docBase as CustomDefinedDataDoc; if (null == custom_defined_data_doc) { err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, custom_defined_ui_doc is null :{to_cast_string} - {parent.toBasicString()}"; Log.getLogger().error(err_msg); return false; } //===================================================================================== // New Doc => Origin Doc //===================================================================================== syncOriginDocBaseWithNewDoc(custom_defined_data_doc); //===================================================================================== // Doc => Doc, Attribute //===================================================================================== var doc_custom_defined_data_attrib = custom_defined_data_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(doc_custom_defined_data_attrib, () => $"doc_custom_defined_data_attrib is null !!! - {parent.toBasicString()}"); DataKey = doc_custom_defined_data_attrib.DataKey; CustomDefinedData = doc_custom_defined_data_attrib.CustomDefinedData; return true; } public Result onMerge(EntityAttributeBase otherEntityAttribute) { var owner = getOwner(); var parent = owner.getRootParent(); var result = new Result(); var err_msg = string.Empty; if (null == otherEntityAttribute) { err_msg = $"Invalid Param !!!, otherEntityAttribute is null - {parent.toBasicString()}"; result.setFail(ServerErrorCode.FunctionParamNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } //===================================================================================== // OtherAttribute => Attribute //===================================================================================== var custom_defined_data_attribute = otherEntityAttribute as CustomDefinedDataAttribute; if (null == custom_defined_data_attribute) { err_msg = $"Failed to cast CustomDefinedDataAttribute !!!, custom_defined_data_attribute is null - {parent.toBasicString()}"; result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } CustomDefinedData = custom_defined_data_attribute.CustomDefinedData; //===================================================================================== // Attribute Try Pending Doc => Origin Doc //===================================================================================== var try_pending_doc = custom_defined_data_attribute.getTryPendingDocBase() as CustomDefinedDataDoc ; if (null != try_pending_doc) { custom_defined_data_attribute.resetTryPendingDocBase(); syncOriginDocBaseWithNewDoc(try_pending_doc); } var origin_doc_base = getOriginDocBase(); if (null == origin_doc_base) { // DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!! return result; } var custom_defined_data_attrib = origin_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(custom_defined_data_attrib, () => $"custom_defined_data_attrib is null !!! - {owner.toBasicString()}"); custom_defined_data_attrib.CustomDefinedData = CustomDefinedData; return result; } } public class CustomDefinedDataAttributeTransactor : EntityAttributeTransactorBase, ICopyEntityAttributeTransactorFromEntityAttribute { public CustomDefinedDataAttributeTransactor(EntityBase owner) : base(owner) { } public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase) { var err_msg = string.Empty; var to_cast_string = typeof(CustomDefinedUiAttribute).Name; var copy_from_custom_defined_data_attribute = entityAttributeBase as CustomDefinedDataAttribute; if (null == copy_from_custom_defined_data_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_from_custom_defined_data_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } var copy_to_custom_defined_data_attribute = getClonedEntityAttribute() as CustomDefinedDataAttribute; if (null == copy_to_custom_defined_data_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_to_custom_defined_data_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } copy_to_custom_defined_data_attribute.DataKey = copy_from_custom_defined_data_attribute.DataKey; copy_to_custom_defined_data_attribute.CustomDefinedData = copy_from_custom_defined_data_attribute.CustomDefinedData; return true; } } }