237 lines
9.1 KiB
C#
237 lines
9.1 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
|
using Google.Protobuf;
|
|
using Google.Protobuf.WellKnownTypes;
|
|
|
|
|
|
using ServerCore;
|
|
using ServerBase;
|
|
using ServerCommon;
|
|
using ServerCommon.BusinessLogDomain;
|
|
using MetaAssets;
|
|
|
|
|
|
namespace GameServer;
|
|
|
|
public class SocialActionLoadAction : EntityActionBase
|
|
{
|
|
private ConcurrentDictionary<int, SocialAction> m_social_actions = new();
|
|
|
|
public SocialActionLoadAction(EntityBase owner)
|
|
: base(owner)
|
|
{ }
|
|
|
|
|
|
public override async Task<Result> onInit()
|
|
{
|
|
await Task.CompletedTask;
|
|
|
|
var result = new Result();
|
|
|
|
return result;
|
|
}
|
|
|
|
public override void onClear()
|
|
{
|
|
m_social_actions.Clear();
|
|
}
|
|
|
|
public bool isOwnedSocialAction(int socialActionMetaId)
|
|
{
|
|
return m_social_actions.ContainsKey(socialActionMetaId);
|
|
}
|
|
|
|
public bool tryGetSocialAction(int socialActionMetaId, [MaybeNullWhen(false)] out SocialAction socialAction)
|
|
{
|
|
return m_social_actions.TryGetValue(socialActionMetaId, out socialAction);
|
|
}
|
|
|
|
public List<SocialAction> getSocialActions()
|
|
{
|
|
return m_social_actions.Select(x => x.Value).ToList();
|
|
}
|
|
|
|
public async Task<Result> tryAddSocialActionFromDoc(SocialActionDoc socialActionDoc)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
var owner = getOwner();
|
|
|
|
var social_action = new SocialAction(owner);
|
|
result = await social_action.onInit();
|
|
if (result.isFail())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var found_social_action_attribute = social_action.getEntityAttribute<UserSocialActionAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(found_social_action_attribute, () => $"found_social_action_attribute is null !!! - {owner.toBasicString()}");
|
|
|
|
if (false == found_social_action_attribute.copyEntityAttributeFromDoc(socialActionDoc))
|
|
{
|
|
err_msg = $"Failed to copyEntityAttributeFromDoc() !!! to:{found_social_action_attribute.getTypeName()}, from:{socialActionDoc.getTypeName()} : {this.getTypeName()} - {owner.toBasicString()}";
|
|
result.setFail(ServerErrorCode.DynamoDbDocCopyToEntityAttributeFailed, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
if (false == m_social_actions.TryAdd((int)found_social_action_attribute.SocialActionMetaId, social_action))
|
|
{
|
|
err_msg = $"Failed to TryAdd() !!! : {social_action.toBasicString()} : {this.getTypeName()} - {owner.toBasicString()}";
|
|
result.setFail(ServerErrorCode.SocialActionDocLoadDuplicatedSocialAction, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> tryAddSocialActionFromMetaId(int socialActionMetaId)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
var owner = getOwner();
|
|
|
|
if (false == MetaData.Instance._SocialActionTable.TryGetValue(socialActionMetaId, out _))
|
|
{
|
|
err_msg = $"Failed to TryGetValue() !!! : socialActionMetaId:{socialActionMetaId} - {owner.toBasicString()}";
|
|
result.setFail(ServerErrorCode.SocialActionMetaDataNotFound, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
// 최대 획득 조건 추가
|
|
var social_action_slot_max = MetaHelper.GameConfigMeta.MaxSocialActionSlot;
|
|
if (m_social_actions.Count >= social_action_slot_max)
|
|
{
|
|
err_msg = $"Social Action Max count exceed !!! : {m_social_actions.Count} <= {social_action_slot_max} - socialActionMetaId:{socialActionMetaId}, {owner.toBasicString()}";
|
|
result.setFail(ServerErrorCode.SocialActionSlotOutOfRange, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
return result;
|
|
}
|
|
|
|
var social_action = new SocialAction(owner);
|
|
await social_action.onInit();
|
|
|
|
var social_action_attribute = social_action.getEntityAttribute<UserSocialActionAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(social_action_attribute, () => $"social_action_attribute is null !!! - {owner.toBasicString()}");
|
|
|
|
social_action_attribute.SocialActionMetaId = (uint)socialActionMetaId;
|
|
social_action_attribute.newEntityAttribute();
|
|
|
|
if (!m_social_actions.TryAdd((int)social_action_attribute.SocialActionMetaId, social_action))
|
|
{
|
|
err_msg = $"Failed to TryAdd() !!! : {social_action.toBasicString()} - {owner.toBasicString()}";
|
|
result.setFail(ServerErrorCode.SocialActionAlreadyExist, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public bool tryGetSocialActionByEquipedPos(UInt16 equipedPos, [MaybeNullWhen(false)] out SocialAction socialAction)
|
|
{
|
|
socialAction = null;
|
|
|
|
var owner = getOwner();
|
|
|
|
foreach (var social_action in m_social_actions.Values)
|
|
{
|
|
var origin_social_action_attribute = social_action.getOriginEntityAttribute<UserSocialActionAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(origin_social_action_attribute, () => $"origin_social_action_attribute is null !!! - {owner.toBasicString()}");
|
|
|
|
if (origin_social_action_attribute.EquipedPos == equipedPos)
|
|
{
|
|
socialAction = social_action;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public (Result, List<ILogInvoker>?) tryExchangeSocialActionSlot(int socialActionSlot, int socialActionMetaId)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
var business_logs = new List<ILogInvoker>();
|
|
|
|
var player = getOwner() as Player;
|
|
NullReferenceCheckHelper.throwIfNull(player, () => $"player is null !!!");
|
|
|
|
var user_guid = player.getUserGuid();
|
|
var old_social_action_meta_id = 0;
|
|
|
|
if (tryGetSocialActionByEquipedPos((UInt16)socialActionSlot, out var equipedSocialAction))
|
|
{
|
|
var equiped_social_action_action = equipedSocialAction.getEntityAction<SocialActionAction>();
|
|
NullReferenceCheckHelper.throwIfNull(equiped_social_action_action, () => $"equiped_social_action_action is null !!! - {player.toBasicString()}");
|
|
|
|
// 슬롯에서 제거
|
|
equiped_social_action_action.unequipSocialAction();
|
|
|
|
var equiped_social_action_attribute = equipedSocialAction.getEntityAttribute<UserSocialActionAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(equiped_social_action_attribute, () => $"equiped_social_action_action is null !!! - {player.toBasicString()}");
|
|
|
|
old_social_action_meta_id = (int)equiped_social_action_attribute.SocialActionMetaId;
|
|
}
|
|
|
|
if (socialActionMetaId == 0)
|
|
{
|
|
var social_action_slot_exchange_log_info = SocialActionBusinessLogHelper.toSocialActionSlotExchangeLogInfo(user_guid, socialActionSlot, old_social_action_meta_id, socialActionMetaId);
|
|
var social_action_slot_exchange_business_log = new SocialActionSlotExchangeBusinessLog(social_action_slot_exchange_log_info);
|
|
business_logs.Add(social_action_slot_exchange_business_log);
|
|
|
|
return (result, business_logs);
|
|
}
|
|
|
|
if (!MetaData.Instance._SocialActionTable.TryGetValue(socialActionMetaId, out var social_action_meta_data))
|
|
{
|
|
err_msg = $"Failed to TryGetValue() !!! : socialActionMetaId:{socialActionMetaId} - {player.toBasicString()}";
|
|
result.setFail(ServerErrorCode.SocialActionMetaDataNotFound, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return (result, null);
|
|
}
|
|
|
|
if (!tryGetSocialAction(socialActionMetaId, out var social_action))
|
|
{
|
|
err_msg = $"Failed to tryGetSocialAction() !!! : socialActionMetaId:{socialActionMetaId} - {player.toBasicString()}";
|
|
result.setFail(ServerErrorCode.SocialActionNotFound, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return (result, null);
|
|
}
|
|
|
|
var social_action_action = social_action.getEntityAction<SocialActionAction>();
|
|
NullReferenceCheckHelper.throwIfNull(social_action_action, () => $"social_action_action is null !!! - {player.toBasicString()}");
|
|
|
|
result = social_action_action.tryEquipSocialAction((UInt16)socialActionSlot);
|
|
if (result.isFail())
|
|
{
|
|
err_msg = $"Failed to tryEquipSocialAction() !!! : {result.toBasicString()} - {player.toBasicString()}";
|
|
Log.getLogger().error(err_msg);
|
|
|
|
return (result, null);
|
|
}
|
|
|
|
{
|
|
var social_action_slot_exchange_log_info = SocialActionBusinessLogHelper.toSocialActionSlotExchangeLogInfo(user_guid, socialActionSlot, old_social_action_meta_id, socialActionMetaId);
|
|
var social_action_slot_exchange_business_log = new SocialActionSlotExchangeBusinessLog(social_action_slot_exchange_log_info);
|
|
business_logs.Add(social_action_slot_exchange_business_log);
|
|
}
|
|
|
|
return (result, business_logs);
|
|
}
|
|
}
|