122 lines
4.9 KiB
C#
122 lines
4.9 KiB
C#
using ServerCommon;
|
|
using ServerCore; using ServerBase;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GameServer
|
|
{
|
|
internal class ItemFirstPurchaseHistoryAgentAction : EntityActionBase
|
|
{
|
|
ConcurrentDictionary<int, ItemFirstPurchaseHistory> m_item_first_purchase_histories = new();
|
|
|
|
public ItemFirstPurchaseHistoryAgentAction(Player owner)
|
|
: base(owner)
|
|
{ }
|
|
|
|
public override async Task<Result> onInit()
|
|
{
|
|
await Task.CompletedTask;
|
|
|
|
var result = new Result();
|
|
|
|
return result;
|
|
}
|
|
|
|
public override void onClear()
|
|
{
|
|
m_item_first_purchase_histories.Clear();
|
|
}
|
|
|
|
public List<ItemFirstPurchaseHistory> getItemFirstPurchaseHistories()
|
|
{
|
|
return m_item_first_purchase_histories.Select(x => x.Value).ToList();
|
|
}
|
|
|
|
public bool isItemFirstPurchase(int itemMetaId)
|
|
{
|
|
return !m_item_first_purchase_histories.ContainsKey(itemMetaId);
|
|
}
|
|
|
|
public async Task<Result> tryAddItemFirstPurchaseHistoryFromDoc(ItemFirstPurchaseHistoryDoc itemFirstPurchaseHistoryDoc)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
var player = getOwner() as Player;
|
|
NullReferenceCheckHelper.throwIfNull(player, () => "player is null !!!");
|
|
|
|
ItemFirstPurchaseHistory item_first_purchase_history = new(player);
|
|
await item_first_purchase_history.onInit();
|
|
|
|
var item_first_purchase_history_attribute = item_first_purchase_history.getEntityAttribute<ItemFirstPurchaseHistoryAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(item_first_purchase_history_attribute, () => $"item_first_purchase_history_attribute is null !!! - {player.toBasicString()}");
|
|
|
|
if (!item_first_purchase_history_attribute.copyEntityAttributeFromDoc(itemFirstPurchaseHistoryDoc))
|
|
{
|
|
err_msg = $"Failed to copyEntityAttributeFromDoc() !!! to:{item_first_purchase_history_attribute.getTypeName()}, from:{itemFirstPurchaseHistoryDoc.getTypeName()} : {this.getTypeName()}";
|
|
result.setFail(ServerErrorCode.DynamoDbDocCopyToEntityAttributeFailed, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
if (!m_item_first_purchase_histories.TryAdd((int)item_first_purchase_history_attribute.ItemMetaId, item_first_purchase_history))
|
|
{
|
|
err_msg = $"Failed to TryAdd() !!! : {item_first_purchase_history.toBasicString()} : {this.getTypeName()}";
|
|
result.setFail(ServerErrorCode.ItemFirstPurchaseHistoryDocLoadDuplicatedItem, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<Result> tryAddItemFirstPurchaseHistoryFromMetaId(int itemMetaId)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
var player = getOwner() as Player;
|
|
NullReferenceCheckHelper.throwIfNull(player, () => "player is null !!!");
|
|
|
|
if (!MetaData.Instance._ItemTable.TryGetValue(itemMetaId, out var itemMetaData))
|
|
{
|
|
err_msg = $"Failed to TryGetValue() !!! : itemMetaId:{itemMetaId} : {this.getTypeName()}";
|
|
result.setFail(ServerErrorCode.ItemMetaDataNotFound, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
if (itemMetaData.Buy_Discount_Rate == 0)
|
|
return result;
|
|
|
|
ItemFirstPurchaseHistory item_first_purchase_history = new(player);
|
|
await item_first_purchase_history.onInit();
|
|
|
|
var item_first_purchase_history_attribute = item_first_purchase_history.getEntityAttribute<ItemFirstPurchaseHistoryAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(item_first_purchase_history_attribute, () => $"item_first_purchase_history_attribute is null !!! - {player.toBasicString()}");
|
|
|
|
item_first_purchase_history_attribute.ItemMetaId = (uint)itemMetaId;
|
|
item_first_purchase_history_attribute.FirstPurchaseTime = DateTime.UtcNow;
|
|
item_first_purchase_history_attribute.newEntityAttribute();
|
|
|
|
if (!m_item_first_purchase_histories.TryAdd((int)item_first_purchase_history_attribute.ItemMetaId, item_first_purchase_history))
|
|
{
|
|
err_msg = $"Failed to TryAdd() !!! : {item_first_purchase_history.toBasicString()} : {this.getTypeName()}";
|
|
result.setFail(ServerErrorCode.ItemFirstPurchaseHistoryAlreadyExist, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|