156 lines
5.3 KiB
C#
156 lines
5.3 KiB
C#
|
|
using ServerCore;
|
|
using ServerBase;
|
|
using ServerCommon;
|
|
using ServerCommon.BusinessLogDomain;
|
|
|
|
|
|
|
|
namespace BrokerCore.Services;
|
|
|
|
using Common;
|
|
|
|
public class EchoSystemService
|
|
{
|
|
private readonly IServerLogic m_server_logic;
|
|
private string m_user_guid = string.Empty;
|
|
CaliumEventRequest? m_calium_event_payload;
|
|
const int m_retry_count_max = 5;
|
|
const int m_retry_interval_ms = 1000;
|
|
|
|
public EchoSystemService(IServerLogic serverLogic)
|
|
{
|
|
m_server_logic = serverLogic;
|
|
}
|
|
|
|
private class CaliumEchoSystemFailBusinessLog : ILogInvokerEx
|
|
{
|
|
private readonly CaliumEchoSystemFailLogData m_data_to_log;
|
|
|
|
public CaliumEchoSystemFailBusinessLog(CaliumEchoSystemFailLogData log) : base(LogDomainType.CaliumEchoSystem)
|
|
{
|
|
m_data_to_log = log;
|
|
}
|
|
|
|
public override bool hasLog() => true;
|
|
|
|
protected override void fillup(ref BusinessLog.LogBody body)
|
|
{
|
|
body.append(new CaliumEchoSystemFailLogData(this, m_data_to_log));
|
|
}
|
|
}
|
|
|
|
public async Task createAndSetEventPayload(string accountId, string nickname, string userGuid,
|
|
long sapphireDelta, string serverType, string eventType, string subType = "BrokerApiServer")
|
|
{
|
|
m_user_guid = userGuid;
|
|
var calium_storage_attrib = await findCaliumStorage();
|
|
var calium_event_payload = new CaliumEventRequest
|
|
{
|
|
m_event_id = Guid.NewGuid().ToString("N"),
|
|
m_server_type = serverType,
|
|
m_event_type = eventType, // CaliumEventType.extra_get.ToString(),
|
|
// 확인용 문자열
|
|
m_sub_type = subType,
|
|
m_div_type = nickname,
|
|
m_div_id = accountId,
|
|
m_calium_delta = 0,
|
|
m_sapphire_delta = Convert.ToDouble(sapphireDelta),
|
|
m_current_epoch = calium_storage_attrib?.DailyEpoch ?? 0,
|
|
m_current_inflation_rate = calium_storage_attrib?.ExchangerStorage.DailyInflationRate ?? Convert.ToDouble(0)
|
|
};
|
|
m_calium_event_payload = calium_event_payload;
|
|
}
|
|
|
|
private async Task<CaliumStorageAttrib?> findCaliumStorage()
|
|
{
|
|
var dynamo_db_client = m_server_logic.getDynamoDbClient();
|
|
var (result_pk, primary_key_object) = await DynamoDBDocBaseHelper.makePrimaryKey<CaliumStorageDoc>(m_user_guid);
|
|
if (result_pk.isFail() || primary_key_object == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var config = dynamo_db_client.makeQueryConfigForReadByPKOnly(primary_key_object.PK);
|
|
var (result, doc) =
|
|
await dynamo_db_client.simpleQueryDocTypeWithQueryOperationConfig<CaliumStorageDoc>(config);
|
|
var attrib = doc?.getAttrib<CaliumStorageAttrib>();
|
|
return attrib;
|
|
}
|
|
|
|
public async Task<Result> processCaliumEvent(IWithLogActor logActor)
|
|
{
|
|
Guard.Against.isNull(m_calium_event_payload, ServerErrorCode.InternalServerError,
|
|
() => "m_calium_event_payload is null");
|
|
var server_config = m_server_logic.getServerConfig();
|
|
var url = server_config.EchoSystemConfig.BaseAddress;
|
|
var echo_system_requester = new EchoSystemRequester(url);
|
|
var result = new Result();
|
|
CaliumEventResponse? echo_system_response = null;
|
|
foreach (var _ in Enumerable.Range(0, m_retry_count_max))
|
|
{
|
|
(result, echo_system_response) = await echo_system_requester.postCaliumEvent(m_calium_event_payload);
|
|
|
|
if (result.isSuccess())
|
|
{
|
|
Log.getLogger().info("EchoSystemRequest Success");
|
|
break;
|
|
}
|
|
|
|
Log.getLogger().info("EchoSystemRequest Failed");
|
|
if (result.ErrorCode == ServerErrorCode.FailToGetEchoSystemHttpError)
|
|
{
|
|
await Task.Delay(m_retry_interval_ms);
|
|
}
|
|
}
|
|
|
|
if (result.isSuccess())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//========================================
|
|
// 여기서 부터 실패시 처리
|
|
//========================================
|
|
|
|
await insertEventFailDoc(m_user_guid);
|
|
|
|
Log.getLogger().info($"EchoSystemRequest Failed => Code: {echo_system_response?.m_code}");
|
|
|
|
BusinessLogger.collectLog(logActor, new CaliumEchoSystemFailBusinessLog(
|
|
new CaliumEchoSystemFailLogData
|
|
{
|
|
FailCode = echo_system_response?.m_code ?? string.Empty,
|
|
FailMessages = echo_system_response?.m_messages ?? [],
|
|
ReTry = false,
|
|
EventData = m_calium_event_payload
|
|
}));
|
|
return result;
|
|
}
|
|
|
|
// 칼리움 이벤트가 실패하면 CaliumEventDoc을 생성해서 채널 서버에 이 후에 처리하도록 유도한다.
|
|
private async Task insertEventFailDoc(string userGuid)
|
|
{
|
|
var dynamo_db_client = m_server_logic.getDynamoDbClient();
|
|
var request = m_calium_event_payload;
|
|
Guard.Against.isNull(request, ServerErrorCode.InternalServerError, () => "m_calium_event_payload is null");
|
|
var doc = new CaliumEventDoc(request.m_event_id);
|
|
var attrib = doc.getAttrib<CaliumEventAttrib>();
|
|
NullReferenceCheckHelper.throwIfNull(attrib, () => $"calium event attrib is null !!! - userGuid[{userGuid}]");
|
|
|
|
attrib.UserGuid = userGuid;
|
|
attrib.EventData.m_server_type = request.m_server_type;
|
|
attrib.EventData.m_event_type = request.m_event_type;
|
|
attrib.EventData.m_sub_type = request.m_sub_type;
|
|
attrib.EventData.m_div_type = request.m_div_type;
|
|
attrib.EventData.m_div_id = request.m_div_id;
|
|
attrib.EventData.m_calium_delta = request.m_calium_delta;
|
|
attrib.EventData.m_sapphire_delta = request.m_sapphire_delta;
|
|
attrib.EventData.m_current_epoch = request.m_current_epoch;
|
|
attrib.EventData.m_current_inflation_rate = request.m_current_inflation_rate;
|
|
attrib.Status = CaliumEventStatus.Regist;
|
|
|
|
await dynamo_db_client.simpleInsertDocumentWithDocType(doc);
|
|
}
|
|
}
|