using ServerCore; using MODULE_ID = System.UInt32; using WORLD_META_ID = System.UInt32; namespace ServerBase; public static class ServerMetricsHelper { public static async Task<(Result, ServerInfo?)> getServerInfoByChannel( this IWithServerMetrics serverMetrics , WORLD_META_ID worldMetaId, int channelNo ) { var result = new Result(); ServerInfo? found_server_info = null; var server_metrics_request = serverMetrics.getServerMetricsCacheRequest(); NullReferenceCheckHelper.throwIfNull(server_metrics_request, () => $"server_metrics_request is null - {serverMetrics.toBasicString()}"); var handler_type = (uint)ServerMetricsCacheRequest.TriggerType.ServerMetrics_ChannelTargetGetAndFill; var with_result = await server_metrics_request.tryRunTriggerHandler(handler_type, worldMetaId, channelNo); if (with_result.Result.isFail()) { return (with_result.Result, null); } var result_value_server_info = with_result as ResultValue; NullReferenceCheckHelper.throwIfNull(result_value_server_info, () => $"result_value_server_info is null - {serverMetrics.toBasicString()}"); if (null != result_value_server_info.ValueOfResult) { found_server_info = result_value_server_info.ValueOfResult; } if (null != found_server_info) { return (result, found_server_info); } return (result, null); } public static async Task<(Result, ServerInfo?)> getServerInfoByServerName( this IWithServerMetrics serverMetrics , string serverName ) { var result = new Result(); ServerInfo? found_server_info = null; var server_metrics_request = serverMetrics.getServerMetricsCacheRequest(); NullReferenceCheckHelper.throwIfNull(server_metrics_request, () => $"server_metrics_request is null - {serverMetrics.toBasicString()}"); var handler_type = (uint)ServerMetricsCacheRequest.TriggerType.ServerMetrics_GetByServerName; var with_result = await server_metrics_request.tryRunTriggerHandler(handler_type, serverName); if (with_result.Result.isFail()) { return (with_result.Result, null); } var result_value_server_info = with_result as ResultValue; NullReferenceCheckHelper.throwIfNull(result_value_server_info, () => $"result_value_server_info is null - {serverMetrics.toBasicString()}"); if (null != result_value_server_info.ValueOfResult) { found_server_info = result_value_server_info.ValueOfResult; } if (null != found_server_info) { return (result, found_server_info); } return (result, null); } public static async Task<(Result, List)> getServerInfosByServerType( this IWithServerMetrics serverMetrics , ServerType serverType, WORLD_META_ID worldMetaId = 0) { var result = new Result(); var server_metrics_request = serverMetrics.getServerMetricsCacheRequest(); NullReferenceCheckHelper.throwIfNull(server_metrics_request, () => $"server_metrics_request is null - {serverMetrics.toBasicString()}"); var handler_type = (uint)ServerMetricsCacheRequest.TriggerType.ServerMetrics_ServerTypeGetAndFill; var with_result = await server_metrics_request.tryRunTriggerHandler(handler_type, serverType, worldMetaId); if (with_result.Result.isFail()) { return (with_result.Result, new List()); } if (with_result.isResultOnly()) { return (result, new List()); } var result_value_server_info = with_result as ResultValue>; NullReferenceCheckHelper.throwIfNull(result_value_server_info, () => $"result_value_server_info is null - {serverMetrics.toBasicString()}"); return (result, result_value_server_info.ValueOfResult); } public static async Task syncServerInfoToCache( this IWithServerMetrics serverMetrics , string serverName , string listenIp, ushort listenPort , int currentConnectedUserCount , int maxUserCount , int reservationCount = 0, int returnUserCount = 0 , int ugcNpcCount = 0 , string awsInstanceId = "" , int worldId = 0, int channelNo = 0, int roomCapcity = 0 ) { var err_msg = string.Empty; var result = new Result(); try { var server_metrics_request = serverMetrics.getServerMetricsCacheRequest(); var server_info = server_metrics_request.makeServerInfo( serverName , listenIp, listenPort , currentConnectedUserCount, maxUserCount , reservationCount, returnUserCount , ugcNpcCount , worldId, channelNo, roomCapcity , awsInstanceId ); var handler_type = (uint)ServerMetricsCacheRequest.TriggerType.ServerMetrics_UpdateToCache; var with_result = await server_metrics_request.tryRunTriggerHandler(handler_type, serverName, server_info); if (with_result.Result.isFail()) { err_msg = $"Failed to sync ServerInfo with Cache !!! : {with_result.Result.toBasicString()} - {serverMetrics.toBasicString()}"; Log.getLogger().error(err_msg); return with_result.Result; } return result; } catch (Exception e) { var error_code = ServerErrorCode.TryCatchException; err_msg = $"Exception !!!, Failed to sync ServerInfo with Cache !!! : errorCode:{error_code}, exception:{e} - {serverMetrics.toBasicString()}"; result.setFail(error_code, err_msg); Log.getLogger().error(err_msg); return result; } } public static ServerMetricsCacheRequest getServerMetricsCacheRequest(this IWithServerMetrics serverMetrics) { return ServerMetricsManager.It.getServerMetricsCacheRequest(); } public static bool isSetupCompleted(this ServerLogicBase serverLogic) { return ServerMetricsManager.It.isSetupCompleted(); } public static async Task setupDefaultServerMetrics(this IWithServerMetrics serverMetrics, MODULE_ID toRedisConnectorModuleId) where TRedisConnector : RedisConnector { await Task.CompletedTask; var result = new Result(); var server_logic = serverMetrics as IServerLogic; NullReferenceCheckHelper.throwIfNull(server_logic, () => $"server_logic is null"); var found_redis_connector_module = server_logic.getModule(toRedisConnectorModuleId); var casted_redis_connector = found_redis_connector_module as RedisConnector; NullReferenceCheckHelper.throwIfNull(casted_redis_connector, () => $"casted_redis_connector is null - moudleId:{toRedisConnectorModuleId}, moduleType:{typeof(TRedisConnector)} - {server_logic.toBasicString()}"); return ServerMetricsManager.It.setup(casted_redis_connector); } }