198 lines
6.4 KiB
C#
198 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Google.Protobuf.WellKnownTypes;
|
|
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
|
|
|
|
using ServerCore;
|
|
using ServerBase;
|
|
|
|
|
|
using META_ID = System.UInt32;
|
|
|
|
|
|
namespace ServerCommon;
|
|
|
|
public class BuffCacheInfo
|
|
{
|
|
public META_ID BuffMetaID = 0;
|
|
public DateTime BuffStartTime = new();
|
|
}
|
|
|
|
public class BuffCache : CacheBase, ICopyCacheFromEntityAttribute
|
|
{
|
|
public ConcurrentDictionary<MetaAssets.EBuffCategory, ConcurrentDictionary<int, BuffCacheInfo>> buff_cache_Infos = new();
|
|
|
|
public bool copyCacheFromEntityAttribute(EntityAttributeBase entityAttributeBase)
|
|
{
|
|
var err_msg = string.Empty;
|
|
buff_cache_Infos.Clear();
|
|
|
|
var to_cast_string = typeof(BuffAttribute).Name;
|
|
var buff_attribute = entityAttributeBase as BuffAttribute;
|
|
if (null == buff_attribute)
|
|
{
|
|
err_msg = $"Failed to copyCacheFromEntityAttribute() !!!, buff_attribute is null :{to_cast_string}";
|
|
Log.getLogger().error(err_msg);
|
|
return false;
|
|
}
|
|
|
|
//=====================================================================================
|
|
// Attribute => Cache
|
|
//=====================================================================================
|
|
|
|
foreach (var channel_buff in buff_attribute.BuffInfos)
|
|
{
|
|
foreach(var buff_info in channel_buff.Value)
|
|
{
|
|
if(buff_cache_Infos.TryGetValue(channel_buff.Key, out var cache_channel_buffs) == false)
|
|
{
|
|
cache_channel_buffs = new ConcurrentDictionary<int, BuffCacheInfo>();
|
|
buff_cache_Infos.TryAdd(channel_buff.Key, cache_channel_buffs);
|
|
}
|
|
|
|
var cache_buff = new BuffCacheInfo();
|
|
cache_buff.BuffMetaID = buff_info.Value.BuffMetaID;
|
|
cache_buff.BuffStartTime = buff_info.Value.BuffStartTime;
|
|
|
|
if(cache_channel_buffs.TryAdd(buff_info.Key, cache_buff) == false)
|
|
{
|
|
err_msg = $"Duplicated buff in channel !!! : buffMetaId:{buff_info.Value.BuffMetaID}";
|
|
Log.getLogger().warn(err_msg);
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public string toBasicString()
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public partial class BuffCacheRequest : RedisRequestPrivateBase
|
|
{
|
|
public static readonly double BUFFER_CACHE_EXPIRY_TIME = ConstValue.default_1_min_to_sec * ConstValue.default_1_sec_to_milisec;
|
|
public static readonly double SERVER_SWITCH_CACHE_EXPIRY_TIME = 5 * (ConstValue.default_1_min_to_sec * ConstValue.default_1_sec_to_milisec);
|
|
|
|
// In
|
|
private string m_user_guid;
|
|
|
|
// Out
|
|
private BuffCache? m_buff_cache_nullable;
|
|
|
|
public BuffCacheRequest(UserBase owner, RedisConnector redisConnector)
|
|
: base(owner, redisConnector)
|
|
{
|
|
var accountAttribute = owner.getOriginEntityAttribute<AccountAttribute>();
|
|
NullReferenceCheckHelper.throwIfNull(accountAttribute, () => $"accountAttribute is null !!!");
|
|
m_user_guid = accountAttribute.UserGuid;
|
|
}
|
|
|
|
public async Task<Result> UpsertBuff()
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
try
|
|
{
|
|
var buff_cache_json_string = getBuffCache()?.toJsonString();
|
|
|
|
var database = getDatabase();
|
|
NullReferenceCheckHelper.throwIfNull(database, () => $"database is null !!! ");
|
|
|
|
if (false == await database.StringSetAsync(getKey(), buff_cache_json_string, TimeSpan.FromMilliseconds(BUFFER_CACHE_EXPIRY_TIME)))
|
|
{
|
|
err_msg = $"Failed to set LoginCacheInfo to Redis !!! : Key:{getKey()} - {getOwner().toBasicString()}";
|
|
result.setFail(ServerErrorCode.RedisLoginCacheSetFailed, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
err_msg = $"Failed to set LoginCache in Redis !!! : : Exception:{e} - {getOwner().toBasicString()}";
|
|
result.setFail(ServerErrorCode.TryCatchException, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public async Task<Result> fetchBuff()
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
var owner = getOwner();
|
|
|
|
try
|
|
{
|
|
result = await onPrepareRequest();
|
|
if (result.isFail())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var database = getDatabase();
|
|
NullReferenceCheckHelper.throwIfNull(database, () => $"database is null !!!");
|
|
|
|
var redis_value = await database.StringGetAsync(getKey(), CommandFlags.PreferReplica);
|
|
if (true == redis_value.HasValue)
|
|
{
|
|
var curr_buff_cache = JsonConvert.DeserializeObject<BuffCache>(redis_value.ToString());
|
|
if (null == curr_buff_cache)
|
|
{
|
|
err_msg = $"Failed to convert DeserializeObject of Json !!! : {redis_value.ToString()} - {owner.toBasicString()}";
|
|
result.setFail(ServerErrorCode.JsonConvertDeserializeFailed, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
return result;
|
|
}
|
|
|
|
m_buff_cache_nullable = curr_buff_cache;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
err_msg = $"Failed to get LoginCache from Redis !!! : Exception:{e} - {owner.toBasicString()}";
|
|
result.setFail(ServerErrorCode.TryCatchException, err_msg);
|
|
Log.getLogger().error(err_msg);
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
protected override string onMakeKey()
|
|
{
|
|
return $"buff:{m_user_guid}";
|
|
}
|
|
|
|
protected string onMakeKeyWith(string combinationKey)
|
|
{
|
|
return $"buff:{combinationKey}";
|
|
}
|
|
|
|
public BuffCache? getBuffCache() => m_buff_cache_nullable;
|
|
public BuffCache newBuffCache() => m_buff_cache_nullable = new BuffCache();
|
|
|
|
public override string toBasicString()
|
|
{
|
|
return $"BuffCache: In:UserGuid:{m_user_guid}, Out:{m_buff_cache_nullable?.toBasicString()}";
|
|
}
|
|
}
|