using System.Collections.Concurrent; using Newtonsoft.Json; using ServerCore; using ServerBase; using USER_GUID = System.String; namespace ServerCommon; public class SwitchingPropAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute { [JsonProperty()] public ConcurrentDictionary m_switching_props { get; set; } = new(); public SwitchingPropAttribute(EntityBase owner) : base(owner) { } public override void onClear() { m_switching_props.Clear(); } public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true) { var player = getOwner(); NullReferenceCheckHelper.throwIfNull(player, () => $"player is null !!!"); var switching_prop_attribute = player.getEntityAttribute(); NullReferenceCheckHelper.throwIfNull(switching_prop_attribute, () => $"switching_prop_attribute is null !!! - {player.toBasicString()}"); var user_attribute = player.getRootParent().getEntityAttribute(); NullReferenceCheckHelper.throwIfNull(user_attribute, () => $"user_attribute is null !!! - {player.toBasicString()}"); USER_GUID user_guid = user_attribute.UserGuid; //===================================================================================== // Attribute => try pending Doc //===================================================================================== var try_pending_doc = getTryPendingDocBase() as SwitchingPropDoc; if (null == try_pending_doc) { var to_copy_doc = new SwitchingPropDoc(user_guid); 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_attrib = try_pending_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(to_copy_attrib, () => $"SwitchingPropAttrib is null !!! - {player.toBasicString()}"); to_copy_attrib.m_switching_props.Clear(); foreach (var prop in switching_prop_attribute.m_switching_props) { to_copy_attrib.m_switching_props.AddOrUpdate(prop.Key, prop.Value, (key, old) => prop.Value); } //===================================================================================== // Doc QueryType 반영 //===================================================================================== (var result, var to_query_doc) = await applyDoc4Query(try_pending_doc); if (result.isFail()) { return (result, null); } return (result, to_query_doc); } public override EntityAttributeBase onCloned() { var cloned = new SwitchingPropAttribute(getOwner()); cloned.deepCopyFromBase(this); foreach (var prop in m_switching_props) { Int32 prop_id = prop.Key; Int32 prop_state = prop.Value; if (false == cloned.m_switching_props.TryAdd(prop_id, prop_state)) { Log.getLogger().error($"SwitchingPropAttribute try add Fail!!! propid : {prop_id}, state : {prop_state}, player : {getOwner().toBasicString()}"); } } return cloned; } public Result onMerge(EntityAttributeBase otherEntityAttribute) { var result = new Result(); var err_msg = string.Empty; if (getAttributeState().hasFlag(StateType.Removed)) { return result; } var player = getOwner(); var other_attribute = otherEntityAttribute as SwitchingPropAttribute; if (null == other_attribute) { err_msg = $"Failed to cast SwitchingPropAttribute !!!, other_attribute is null"; result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } //===================================================================================== // OtherAttribute => Attribute //===================================================================================== foreach (var prop in other_attribute.m_switching_props) { m_switching_props.TryAdd(prop.Key, prop.Value); } //===================================================================================== // Attribute Try Pending Doc => Origin Doc //===================================================================================== var try_pending_doc = other_attribute.getTryPendingDocBase() as SwitchingPropDoc; if (null != try_pending_doc) { other_attribute.resetTryPendingDocBase(); syncOriginDocBaseWithNewDoc(try_pending_doc); } var origin_doc_base = getOriginDocBase(); if (null == origin_doc_base) { // DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!! return result; } var switching_prop_attrib = origin_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(switching_prop_attrib, () => $"switching_prop_attrib is null !!! - {player.toBasicString()}"); switching_prop_attrib.m_switching_props.Clear(); foreach (var prop in m_switching_props) { switching_prop_attrib.m_switching_props.AddOrUpdate(prop.Key, prop.Value, (key, old) => prop.Value); } return result; } public bool copyEntityAttributeFromDoc(DynamoDbDocBase customDoc) { var err_msg = string.Empty; var to_cast_string = typeof(SwitchingPropDoc).Name; var switching_prop_doc = customDoc as SwitchingPropDoc; if (null == switching_prop_doc) { err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, switching_prop_doc is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } //===================================================================================== // New Doc => Origin Doc //===================================================================================== syncOriginDocBaseWithNewDoc(switching_prop_doc); //===================================================================================== // Doc => Attribute //===================================================================================== var switching_prop_attrib = switching_prop_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(switching_prop_attrib, () => $"switching_prop_attrib is null !!!"); foreach (var prop in switching_prop_attrib.m_switching_props) { m_switching_props.AddOrUpdate(prop.Key, prop.Value, (key, old) => prop.Value); } return true; } public override IEntityAttributeTransactor onNewEntityAttributeTransactor() { return new SwitchingPropTransactor(getOwner()); } } public class SwitchingPropTransactor : EntityAttributeTransactorBase { public SwitchingPropTransactor(EntityBase owner) : base(owner) { } public bool copyEntityAttributeTransactorBaseFromEntityAttribute(EntityAttributeBase? entityAttributeBase) { return true; } }