using Google.Protobuf; using Google.Protobuf.WellKnownTypes; using ServerCore; using ServerBase; using ServerCommon; using ServerCommon.BusinessLogDomain; using MetaAssets; namespace GameServer; public static class CaliumStorageHelper { public const double FixedExchangeRate = 0.01; public static DateTime CurrentPivotTimeDate(DateTime? customDate = null) { var current = customDate ?? DateTimeHelper.Current; if (current.Hour < ServerCommon.MetaHelper.GameConfigMeta.CaliumConverterPivotTime) { current = current.AddDays(-1); } return new DateTime(current.Year, current.Month, current.Day, ServerCommon.MetaHelper.GameConfigMeta.CaliumConverterPivotTime, 0, 0); } public static async Task calculateCaliumFromSapphire(double sapphire, bool isCeiling = false) { // 1. 기본 정보 조회 var calium_storage_entity = GameServerApp.getServerLogic().findGlobalEntity(); NullReferenceCheckHelper.throwIfNull(calium_storage_entity, () => "calium_storage_entity is null !!!"); var calium_storage_action = calium_storage_entity.getEntityAction(); NullReferenceCheckHelper.throwIfNull(calium_storage_action, () => $"calium_storage_action is null !!! - {calium_storage_entity.toBasicString()}"); var info = await calium_storage_action.getCaliumStorageInfo(); // 2. calium 교환비 계산 : (사파이어 * 고정 교환비) / 인플레이션 가중치(당일, % 처리) var calium = MultiplyDoubleByLong(sapphire, FixedExchangeRate); calium /= info.DailyInflationRate / 100; // 3. 소수점 2자리수에서 판매는 올림 / 교환은 버림 calium = isCeiling ? roundUpDefaultDigitsFromDouble(calium) : roundDownDefaultDigitsFromDouble(calium); Log.getLogger().debug($"calculateCaliumFromSapphire !! : sapphire[{sapphire}] / calium[{calium}] / isCeiling[{isCeiling}] / swap_rate[{info.DailyInflationRate}]"); return calium; } public static bool compareDoubleByLong(double first, double second) { var first_long = (long)(roundHalfUpDefaultDigitsFromDouble(first) * EchoSystemHelper.DoubleCalculateDigitsLong); var second_long = (long)(roundHalfUpDefaultDigitsFromDouble(second) * EchoSystemHelper.DoubleCalculateDigitsLong); return first_long == second_long; } public static double roundHalfUpDefaultDigitsFromDouble(double value) { var default_value = Math.Round(value * EchoSystemHelper.DoubleCalculateDigitsLong, 0); var value_long = (long)default_value; return value_long / (double)EchoSystemHelper.DoubleCalculateDigitsLong; } public static double roundDownDefaultDigitsFromDouble(double value) { var value_long = (long)(value * EchoSystemHelper.DoubleCalculateDigitsLong); return value_long / (double)EchoSystemHelper.DoubleCalculateDigitsLong; } public static double roundUpDefaultDigitsFromDouble(double value) { var default_value = Math.Ceiling(value * EchoSystemHelper.DoubleCalculateDigitsLong); var value_long = (long)default_value; return value_long / (double)EchoSystemHelper.DoubleCalculateDigitsLong; } public static double AddDoubleByLong(double first, double second) { var first_long = (long)(first * EchoSystemHelper.DoubleCalculateDigitsLong); var second_long = (long)(second * EchoSystemHelper.DoubleCalculateDigitsLong); var add = first_long + second_long; return add / (double)EchoSystemHelper.DoubleCalculateDigitsLong; } public static double SubTractDoubleByLong(double first, double second) { var first_long = (long)(first * EchoSystemHelper.DoubleCalculateDigitsLong); var second_long = (long)(second * EchoSystemHelper.DoubleCalculateDigitsLong); var add = first_long - second_long; return add / (double)EchoSystemHelper.DoubleCalculateDigitsLong; } public static double MultiplyDoubleByLong(double first, double second) { var first_long = (long)(first * EchoSystemHelper.DoubleCalculateDigitsLong); var second_long = (long)(second * EchoSystemHelper.DoubleCalculateDigitsLong); var mul = first_long * second_long; return mul / (double)(EchoSystemHelper.DoubleCalculateDigitsLong * EchoSystemHelper.DoubleCalculateDigitsLong); } public static void writeFailEchoSystemLog(IWithLogActor logActor, CaliumEchoSystemFailLogData log) { var log_action = new LogActionEx(LogActionType.FailCaliumEchoSystem); var invokers = new List(); invokers.Add(new CaliumEchoSystemFailBusinessLog(log)); BusinessLogger.collectLogs(log_action, logActor, invokers); } public static async Task makeCaliumEventRequest(CaliumStorageEntity owner, CaliumEventType type, string subType, string userNickname, double sapphireDelta, double caliumDelta) { var storage_action = owner.getEntityAction(); NullReferenceCheckHelper.throwIfNull(storage_action, () => $"calium_storage_action is null !!! - {owner.toBasicString()}"); var storage = await storage_action.getCaliumStorageInfo(); var request = new CaliumEventRequest(); request.m_server_type = CaliumWeb3Action.SERVER_TYPE; request.m_event_type = type.ToString(); request.m_sub_type = subType; request.m_div_id = userNickname; request.m_calium_delta = caliumDelta; request.m_sapphire_delta = sapphireDelta; request.m_current_epoch = storage.DailyApplyEpoch; request.m_current_inflation_rate = storage.DailyInflationRate; return request; } public static CaliumEventRequest makeCaliumEventRequest(CaliumEventDoc sendDoc) { var attrib = sendDoc.getAttrib(); NullReferenceCheckHelper.throwIfNull(attrib, () => $"calium_event_attrib is null !!! - {sendDoc.toBasicString()}"); // 2. echoSystem 전송 var request = new CaliumEventRequest(); request.m_event_id = sendDoc.getCombinationKeyForSK(); request.m_server_type = attrib.EventData.m_server_type; request.m_event_type = attrib.EventData.m_event_type; request.m_sub_type = attrib.EventData.m_sub_type; request.m_div_type = attrib.EventData.m_div_type; request.m_div_id = attrib.EventData.m_div_id; request.m_calium_delta = attrib.EventData.m_calium_delta; request.m_sapphire_delta = attrib.EventData.m_sapphire_delta; request.m_current_epoch = attrib.EventData.m_current_epoch; request.m_current_inflation_rate = attrib.EventData.m_current_inflation_rate; return request; } }