using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using StackExchange.Redis; using ServerCore; using SERVER_METRICS_TRIGGER_TYPE = System.UInt32; namespace ServerBase; public class ServerInfo : IComparable { public ServerInfo() { } public ServerInfo(ServerInfo serverInfo) { this.Name = serverInfo.Name; this.Address = serverInfo.Address; this.Port = serverInfo.Port; this.Sessions = serverInfo.Sessions; this.RoomCapacity = serverInfo.RoomCapacity; this.Capacity = serverInfo.Capacity; this.Reservation = serverInfo.Reservation; this.ReturnCount = serverInfo.ReturnCount; this.UgcNpcCount = serverInfo.UgcNpcCount; this.Inspection = serverInfo.Inspection; this.ReadyForDistroy = serverInfo.ReadyForDistroy; this.LastUpdateTime = serverInfo.LastUpdateTime; this.AwsInstanceID = serverInfo.AwsInstanceID; this.Channel = serverInfo.Channel; this.WorldId = serverInfo.WorldId; } public string Name = string.Empty; public string Address = string.Empty; public string AwsInstanceID = string.Empty; public int Port = 0; public int Sessions = 0; public int RoomCapacity = 0; public int Capacity = 0; public int Reservation = 0; public int ReturnCount = 0; public int UgcNpcCount = 0; public bool Inspection = false; public bool ReadyForDistroy = false; public DateTime LastUpdateTime; public int Channel = 0; public int WorldId = 0; public int CompareTo(ServerInfo? other) { if (other == null) return 1; return Sessions.CompareTo(other.Sessions); } } public class ServerMetricsCacheRequest : RedisRequestWithLambdaBase { public class CacheServerKey { public string key = string.Empty; public long tick = 0; public List redisValue = new(); } public delegate Task FnConditionCheck(); public delegate Task FnTriggerHandler( params object[] handlerParams ); // for Metrics public enum TriggerType { ServerMetrics_Init, //Return : ResultOnly, Params : { SERVER_NAME, ServerType, WORLD_META_ID } ServerMetrics_AllGetAndFill, //Return : ResultValue>, Params : { WORLD_META_ID List } with ServerType{ Login, Channel, Indun } ServerMetrics_ServerTypeGetAndFill, //Return : ResultValue>, Params : { ServerType, WORLD_META_ID } ServerMetrics_ChannelTargetGetAndFill, //Return : ResultValue, Params : { WORLD_META_ID, ChannelNo } with ServerType{ Channel } ServerMetrics_GetByServerName, //Return : ResultValue, Params : { SERVER_NAME } ServerMetrics_ExpiredAllRemove, //Return : ResultOnly, Params : { WORLD_META_ID List } with ServerType{ Login, Channel, Indun } ServerMetrics_RemoveByServerName, //Return : ResultOnly, Params : { SERVER_NAME } ServerMetrics_UpdateToCache, //Return : ResultOnly, Params : { SERVER_NAME, ServerInfo } } private ConcurrentDictionary m_trigger_handlers = new(); private ServerMetricsManager m_manager; public ServerMetricsCacheRequest(ServerMetricsManager manager, RedisConnector redisConnector) : base(redisConnector) { m_manager = manager; } public bool registerTriggerHandler(SERVER_METRICS_TRIGGER_TYPE triggerType, FnTriggerHandler handler) { return m_trigger_handlers.TryAdd(triggerType, handler); } public async Task tryRunTriggerHandler(SERVER_METRICS_TRIGGER_TYPE triggerType, params object[] handlerParams) { var result = new Result(); var err_msg = string.Empty; if (false == m_trigger_handlers.TryGetValue(triggerType, out var found_handler)) { err_msg = $"Failed to TryGetValue() !!!, in tryRunTriggerHandler(), Not found FnTriggerHandler : triggerType:{triggerType}"; result.setFail(ServerErrorCode.ServerMetricsTriggerHandlerNotFound, err_msg); return new ResultOnly(result); } return await found_handler.Invoke(handlerParams); } public ServerInfo makeServerInfo( string serverName , string address, int port , int sessions, int capacity, int reservation , int returnCount, int ugcNpcCount , int worldId = 0, int channel = 0, int roomCapacity = 0 , string awsInstanceId = "" ) { var server_info = new ServerInfo(); server_info.Name = serverName; server_info.Address = address; server_info.Port = port; server_info.Sessions = sessions; server_info.Capacity = capacity; server_info.Reservation = reservation; server_info.ReturnCount = returnCount; server_info.UgcNpcCount = ugcNpcCount; server_info.RoomCapacity = roomCapacity; //server_info.Inspection = AccountAuthorityManager.Instance.isInspection; //server_info.ReadyForDistroy = AccountAuthorityManager.Instance.isReadyForDistroy; server_info.LastUpdateTime = DateTime.Now; server_info.AwsInstanceID = awsInstanceId; server_info.Channel = channel; server_info.WorldId = worldId; return server_info; } public ServerMetricsManager getServerMetricsManager() => m_manager; }