446 lines
18 KiB
C#
446 lines
18 KiB
C#
// using Amazon.DynamoDBv2;
|
|
// using Amazon.DynamoDBv2.Model;
|
|
//
|
|
// using ServerCommon;
|
|
//
|
|
// using ServerCore; using ServerBase;
|
|
//
|
|
// namespace BrokerCore.Repository;
|
|
//
|
|
// using BrokerApiServer.Common;
|
|
//
|
|
// public class BrokerApiLogActor : ILogActor
|
|
// {
|
|
// public string getActorGuid() => "broker";
|
|
// }
|
|
//
|
|
// public class BrokerApiWithLogActor : EntityBase, IWithLogActor
|
|
// {
|
|
// public readonly EntityType m_entity_type = EntityType.Money;
|
|
//
|
|
// public BrokerApiWithLogActor() : base(EntityType.Money)
|
|
// {
|
|
// }
|
|
//
|
|
// //public BrokerApiWithLogActor(string masterGuid) : base(EntityType.Money, masterGuid)
|
|
// //{
|
|
// //}
|
|
//
|
|
// public ILogActor toLogActor()
|
|
// {
|
|
// return new BrokerApiLogActor();
|
|
// }
|
|
// }
|
|
//
|
|
// public class DbqChangeSapphire : QueryExecutorBase
|
|
// {
|
|
// private readonly double m_delta;
|
|
// private PrimaryKey? m_primary_key;
|
|
// private readonly string m_user_guid;
|
|
//
|
|
// public DbqChangeSapphire(string userGuid, double delta) : base(nameof(DbqChangeSapphire))
|
|
// {
|
|
// m_delta = delta;
|
|
// m_user_guid = userGuid;
|
|
// }
|
|
//
|
|
// public override async Task<Result> onPrepareQuery()
|
|
// {
|
|
// (var result, m_primary_key) = await DynamoDBDocBaseHelper.makePrimaryKey<MoneyDoc>(m_user_guid);
|
|
// return result;
|
|
// }
|
|
//
|
|
// public override async Task<Result> onQuery()
|
|
// {
|
|
// var owner = getOwner();
|
|
//
|
|
// // 부모 인스턴스의 정보를 사용하여 필요한 데이터를 가져온다. (여기서는 ...)
|
|
// var query_batch = getQueryBatch() as QueryBatch<QueryRunnerWithItemRequest>;
|
|
// NullReferenceCheckHelper.throwIfNull(query_batch, () => $"query_batch is null !!! - {owner.toBasicString()}");
|
|
//
|
|
// var query_runner_with_item_request = query_batch.getQueryRunner();
|
|
// NullReferenceCheckHelper.throwIfNull(query_runner_with_item_request,
|
|
// () => $"query_runner_with_item_request is null !!! - {owner.toBasicString()}");
|
|
//
|
|
// var db_connector = query_batch.getDynamoDbConnector();
|
|
// var (result, update_request) = makeUpdateItemRequest();
|
|
// if (result.isFail())
|
|
// {
|
|
// return result;
|
|
// }
|
|
//
|
|
// NullReferenceCheckHelper.throwIfNull(update_request,
|
|
// () => $"update_request is null !!! - {owner.toBasicString()}");
|
|
//
|
|
// var exception_handler = new DynamoDbQueryExceptionNotifier.ExceptionHandler(
|
|
// DynamoDbQueryExceptionNotifier.ConditionalCheckFailed, ServerErrorCode.LackOfTotalCalium);
|
|
//
|
|
// var query_context = update_request.createItemRequestQueryContext(QueryType.Update, exception_handler);
|
|
// return await query_runner_with_item_request.tryRegisterQueryContext(query_context);
|
|
// }
|
|
//
|
|
// //=====================================================================================
|
|
// // DB 쿼리를 성공하고, doFnCommit()가 QueryResultType.NotCalledQueryFunc를 반환할 경우 호출된다.
|
|
// //=====================================================================================
|
|
// public override Task onQueryResponseCommit()
|
|
// {
|
|
// return Task.CompletedTask;
|
|
// }
|
|
//
|
|
// //=====================================================================================
|
|
// // DB 쿼리를 실패하고, doFnRollback()가 QueryResultType.NotCalledQueryFunc를 반환할 경우 호출된다.
|
|
// //=====================================================================================
|
|
// public override Task onQueryResponseRollback(Result errorResult)
|
|
// {
|
|
// return Task.CompletedTask;
|
|
// }
|
|
//
|
|
// private (Result, UpdateItemRequest?) makeUpdateItemRequest()
|
|
// {
|
|
// Guard.Against.isNull(m_primary_key, (BrokerApiErrorCode)ServerErrorCode.DynamoDbPrimaryKeyNotFound, "primary key is null");
|
|
// var attribute_value_with_primary_key = m_primary_key.toKeyWithAttributeValue();
|
|
//
|
|
// var currency_name = MoneyAttribExtensions.getKeyNameFromType(CurrencyType.Sapphire);
|
|
// var result = new Result();
|
|
// var dynamo_db_client = getQueryBatch()?.getDynamoDbConnector();
|
|
// NullReferenceCheckHelper.throwIfNull(dynamo_db_client, () => $"dynamo_db_client is null !!!");
|
|
//
|
|
// var query_builder = new DynamoDbItemRequestHelper.UpdateItemRequestBuilder(dynamo_db_client.getTableName());
|
|
// query_builder.withKeys(attribute_value_with_primary_key);
|
|
//
|
|
// var target_doc = new MoneyDoc();
|
|
// var attrib_path_json_string = target_doc.toJsonStringOfAttribs();
|
|
// var target_key = JsonHelper.getJsonPropertyName<MoneyAttrib>(currency_name);
|
|
// var (is_success, attribute_expression) =
|
|
// DynamoDbClientHelper.toAttributeExpressionFromJson(attrib_path_json_string, target_key);
|
|
// if (false == is_success)
|
|
// {
|
|
// var err_msg =
|
|
// $"Failed to DynamoDbClientHelper.toAttributeExpressionFromJson() !!! : attribPath:{attrib_path_json_string}, targetKey:{target_key}";
|
|
// result.setFail(ServerErrorCode.AttribPathMakeFailed, err_msg);
|
|
// Log.getLogger().error(result.toBasicString());
|
|
//
|
|
// return (result, null);
|
|
// }
|
|
//
|
|
// query_builder.withExpressionAttributeNames(
|
|
// DynamoDbClientHelper.toExpressionAttributeNamesFromJson(attrib_path_json_string, target_key));
|
|
//
|
|
// var update_expression = (m_delta >= 0)
|
|
// ? $"SET {attribute_expression} = if_not_exists({attribute_expression}, :start) + :changeValue"
|
|
// : $"SET {attribute_expression} = if_not_exists({attribute_expression}, :start) - :changeValue";
|
|
// query_builder.withUpdateExpression(update_expression);
|
|
//
|
|
// var expression_attribute_values = new Dictionary<string, AttributeValue>
|
|
// {
|
|
// { ":changeValue", new AttributeValue { N = Math.Abs(m_delta).ToString() } },
|
|
// { ":start", new AttributeValue { N = "0" } }
|
|
// };
|
|
// query_builder.withExpressionAttributeValues(expression_attribute_values);
|
|
// query_builder.withReturnValues(ReturnValue.ALL_NEW);
|
|
// return query_builder.build();
|
|
// }
|
|
// }
|
|
//
|
|
// public class MoneyDocRepo
|
|
// {
|
|
// private readonly DynamoDbClient m_dynamo_db_client;
|
|
//
|
|
// public MoneyDocRepo(DynamoDbClient dynamoDbClient)
|
|
// {
|
|
// m_dynamo_db_client = dynamoDbClient;
|
|
// }
|
|
//
|
|
// public async Task<(Result, MoneyDoc?)> findMoneyDoc(string userGuid)
|
|
// {
|
|
// var (result, primary_key_object) = await DynamoDBDocBaseHelper.makePrimaryKey<MoneyDoc>(userGuid);
|
|
// if (result.isFail() || primary_key_object == null)
|
|
// {
|
|
// return (result, null);
|
|
// }
|
|
//
|
|
// var config = m_dynamo_db_client.makeQueryConfigForReadByPKOnly(primary_key_object.PK);
|
|
// return await m_dynamo_db_client.simpleQueryDocTypeWithQueryOperationConfig<MoneyDoc>(config);
|
|
// }
|
|
//
|
|
// public async Task<(Result, MoneyAttrib?)> findMoneyAttrib(string userGuid, CancellationToken cancellationToken)
|
|
// {
|
|
// var (result, doc) = await findMoneyDoc(userGuid);
|
|
// return (result, doc?.getAttrib<MoneyAttrib>());
|
|
// }
|
|
//
|
|
// public async Task<(Result, MoneyAttrib?)> changeSapphire(string userGuid, double delta)
|
|
// {
|
|
// var (result, make_primary_key) = await DynamoDBDocBaseHelper.makePrimaryKey<MoneyDoc>(userGuid);
|
|
// // 프라이머리 키 생성 실패
|
|
// if (result.isFail() || make_primary_key == null)
|
|
// {
|
|
// return (result, null);
|
|
// }
|
|
//
|
|
// (result, var request) = makeUpdateItemRequest(
|
|
// make_primary_key.toKeyWithAttributeValue(),
|
|
// MoneyAttribExtensions.getKeyNameFromType(CurrencyType.Sapphire),
|
|
// delta);
|
|
// // 업데이트 쿼리 생성 실패
|
|
// if (result.isFail() || request == null)
|
|
// {
|
|
// return (result, null);
|
|
// }
|
|
//
|
|
// (result, var update_doc) = await m_dynamo_db_client.simpleQueryDocTypesWithUpdateItemRequest<MoneyDoc>(request);
|
|
// // 업데이트 쿼리 실패
|
|
// if (result.isFail() || update_doc == null) return (result, null);
|
|
//
|
|
// var attrib = update_doc.getAttrib<MoneyAttrib>();
|
|
// // 업데이트 된 문서에서 MoneyAttrib을 찾을 수 없음
|
|
// if (attrib == null)
|
|
// {
|
|
// result.setFail(ServerErrorCode.AttribNotFound, $"attrib is null !!!");
|
|
// return (result, null);
|
|
// }
|
|
// return (result, attrib);
|
|
// }
|
|
//
|
|
// private async Task<Result> sendQuery(QueryBatchBase batch)
|
|
// {
|
|
// var result = new Result();
|
|
// var err_msg = string.Empty;
|
|
//
|
|
// if (batch == null)
|
|
// {
|
|
// err_msg = $"Failed to sendQuery !!!, QueryBatchBase is null";
|
|
// result.setFail(ServerErrorCode.FunctionInvalidParam, err_msg);
|
|
// return result;
|
|
// }
|
|
//
|
|
// if (false == batch.hasQuery())
|
|
// {
|
|
// err_msg = $"Not has DBQuery !!!";
|
|
// result.setFail(ServerErrorCode.DynamoDbQueryNoRequested, err_msg);
|
|
// return result;
|
|
// }
|
|
//
|
|
// result = await batch.prepareQueryWithStopwatch();
|
|
// if (result.isFail())
|
|
// {
|
|
// return result;
|
|
// }
|
|
//
|
|
// result = await batch.doQueryWithStopwatch();
|
|
// if (result.isFail())
|
|
// {
|
|
// return result;
|
|
// }
|
|
//
|
|
// return result;
|
|
// }
|
|
//
|
|
// private (Result, UpdateItemRequest?) makeUpdateItemRequest(
|
|
// Dictionary<string, AttributeValue> attributeValueWithPrimaryKey
|
|
// , string targetAttribName
|
|
// , double deltaCount)
|
|
// {
|
|
// var result = new Result();
|
|
// var query_builder = new DynamoDbItemRequestHelper.UpdateItemRequestBuilder(m_dynamo_db_client.getTableName());
|
|
// query_builder.withKeys(attributeValueWithPrimaryKey);
|
|
//
|
|
// var target_doc = new MoneyDoc();
|
|
// var attrib_path_json_string = target_doc.toJsonStringOfAttribs();
|
|
// var target_key = JsonHelper.getJsonPropertyName<MoneyAttrib>(targetAttribName);
|
|
// var (is_success, attribute_expression) =
|
|
// DynamoDbClientHelper.toAttributeExpressionFromJson(attrib_path_json_string, target_key);
|
|
// if (false == is_success)
|
|
// {
|
|
// var err_msg =
|
|
// $"Failed to DynamoDbClientHelper.toAttributeExpressionFromJson() !!! : attribPath:{attrib_path_json_string}, targetKey:{target_key}";
|
|
// result.setFail(ServerErrorCode.AttribPathMakeFailed, err_msg);
|
|
// Log.getLogger().error(result.toBasicString());
|
|
// return (result, null);
|
|
// }
|
|
//
|
|
// query_builder.withExpressionAttributeNames(
|
|
// DynamoDbClientHelper.toExpressionAttributeNamesFromJson(attrib_path_json_string, target_key));
|
|
//
|
|
// var update_expression = (deltaCount >= 0)
|
|
// ? $"SET {attribute_expression} = if_not_exists({attribute_expression}, :start) + :changeValue"
|
|
// : $"SET {attribute_expression} = if_not_exists({attribute_expression}, :start) - :changeValue";
|
|
// query_builder.withUpdateExpression(update_expression);
|
|
//
|
|
// var expression_attribute_values = new Dictionary<string, AttributeValue>
|
|
// {
|
|
// { ":changeValue", new AttributeValue { N = Math.Abs(deltaCount).ToString() } },
|
|
// { ":start", new AttributeValue { N = "0" } }
|
|
// };
|
|
// query_builder.withExpressionAttributeValues(expression_attribute_values);
|
|
// query_builder.withReturnValues(ReturnValue.ALL_NEW);
|
|
// return query_builder.build();
|
|
// }
|
|
//
|
|
// // //=============================================================================================
|
|
// // // delta 만큼 금전을 변경(음수, 양수) 한다.
|
|
// // //=============================================================================================
|
|
// // public async Task<Result> changeMoney(CurrencyType currencyType, double delta, bool isTrimExcess = false,
|
|
// // bool useCaliumEvent = true)
|
|
// // {
|
|
// // var result = new Result();
|
|
// //
|
|
// // // var owner = getOwner() as Player;
|
|
// // // NullReferenceCheckHelper.throwIfNull(owner, () => $"player is null !!!");
|
|
// //
|
|
// // // if (currencyType == CurrencyType.Beam)
|
|
// // // {
|
|
// // // var found_transaction_runner = owner.findTransactionRunner(TransactionIdType.PrivateContents);
|
|
// // // found_transaction_runner?.addRemoteChargeAIPoint(this, delta);
|
|
// // // return result;
|
|
// // // }
|
|
// //
|
|
// // result = changeMoneyFromCurrencyType(currencyType, delta, isTrimExcess);
|
|
// // if (result.isSuccess() && useCaliumEvent)
|
|
// // {
|
|
// // var found_transaction_runner = owner.findTransactionRunner(TransactionIdType.PrivateContents);
|
|
// // var event_name = found_transaction_runner?.getTransactionName() ?? "None";
|
|
// // found_transaction_runner?.addNotifyCaliumEvent(this, event_name, currencyType, delta);
|
|
// // }
|
|
// //
|
|
// // return await Task.FromResult(result);
|
|
// // }
|
|
// //
|
|
// // private Result changeMoneyFromCurrencyType(CurrencyType type, double delta, bool isTrimExcess = false)
|
|
// // {
|
|
// // var result = new Result();
|
|
// // var err_msg = string.Empty;
|
|
// //
|
|
// // // var owner = getOwner() as Player;
|
|
// // // NullReferenceCheckHelper.throwIfNull(owner, () => $"player is null !!!");
|
|
// //
|
|
// // if (MetaData.Instance._CurrencyMetaTableByCurrencyType.TryGetValue(type, out var currencyMetaData) == false)
|
|
// // {
|
|
// // // err_msg = $"Not found CurrencyMetaData !!! : currencyType:{type} - {owner.toBasicString()}";
|
|
// // result.setFail(ServerErrorCode.CurrencyMetaDataNotFound, err_msg);
|
|
// // Log.getLogger().error(result.toBasicString());
|
|
// //
|
|
// // return result;
|
|
// // }
|
|
// //
|
|
// // // var money_attribute = owner.getEntityAttribute<MoneyAttribute>();
|
|
// // // NullReferenceCheckHelper.throwIfNull(money_attribute,
|
|
// // // () => $"money_attribute is null !!! - {owner.toBasicString()}");
|
|
// //
|
|
// // var currency = money_attribute.getCurrencyFromType(type);
|
|
// // var change = currency + delta;
|
|
// // if (change < 0)
|
|
// // {
|
|
// // err_msg =
|
|
// // $"Failed to change getCurrencyFromType() !!!, not enough Money : deltaMoney:{delta}, changeCurrency:{change}, currencyType:{type} - {owner.toBasicString()}";
|
|
// // result.setFail(ServerErrorCode.MoneyNotEnough, err_msg);
|
|
// // Log.getLogger().error(result.toBasicString());
|
|
// //
|
|
// // return result;
|
|
// // }
|
|
// //
|
|
// // if (change >= currencyMetaData.MaxCount)
|
|
// // {
|
|
// // MoneyNotifyHelper.send_GS2C_NTF_CURRENCY_MAX_ALERT(owner, type, currencyMetaData.MaxCount);
|
|
// //
|
|
// // if (change > currencyMetaData.MaxCount
|
|
// // && false == isTrimExcess)
|
|
// // {
|
|
// // err_msg =
|
|
// // $"Money exceeded Max Count !!! : toDelta:{delta}, toChange:{change} <= MaxCount:{currencyMetaData.MaxCount}, currencyType:{type} - {owner.toBasicString()}";
|
|
// // result.setFail(ServerErrorCode.MoneyMaxCountExceeded, err_msg);
|
|
// // Log.getLogger().error(result.toBasicString());
|
|
// //
|
|
// // return result;
|
|
// // }
|
|
// //
|
|
// // var trim_money = change - currencyMetaData.MaxCount;
|
|
// // change = double.Min(change, currencyMetaData.MaxCount);
|
|
// //
|
|
// // err_msg =
|
|
// // $"Exceeded MaxCount of Money !!!, Trimming Money : currencyType:{type}, changeMoney:{change}, trimMoney:{trim_money}, deltaMoney:{delta}, maxMoney:{currencyMetaData.MaxCount} - {owner.toBasicString()}";
|
|
// // Log.getLogger().info(err_msg);
|
|
// // }
|
|
// //
|
|
// // money_attribute.setCurrencyFromType(type, change);
|
|
// //
|
|
// // money_attribute.modifiedEntityAttribute();
|
|
// //
|
|
// // return result;
|
|
// // }
|
|
// //
|
|
// //
|
|
// // public TransactionRunner? findTransactionRunner(TransactionIdType transactionIdType)
|
|
// // {
|
|
// // TransactionRunner? found_transaction_runner = null;
|
|
// //
|
|
// // var err_msg = string.Empty;
|
|
// //
|
|
// // if (true == hasMasterGuid())
|
|
// // {
|
|
// // var master_guid = getMasterGuid();
|
|
// // var found_master_entity = onGetMasterEntity();
|
|
// // if(null == found_master_entity)
|
|
// // {
|
|
// // return null;
|
|
// // }
|
|
// //
|
|
// // found_transaction_runner = found_master_entity.findTransactionRunner(transactionIdType);
|
|
// // }
|
|
// // else
|
|
// // {
|
|
// // if (false == m_transaction_runners.TryGetValue(transactionIdType, out found_transaction_runner))
|
|
// // {
|
|
// // return null;
|
|
// // }
|
|
// // }
|
|
// //
|
|
// // return found_transaction_runner;
|
|
// // }
|
|
//
|
|
// // private (Result, UpdateItemRequest?) makeUpdateItemRequest(
|
|
// // Dictionary<string, AttributeValue> attributeValueWithPrimaryKey
|
|
// // , string targetAttribName
|
|
// // , double deltaCount)
|
|
// // {
|
|
// // var result = new Result();
|
|
// //
|
|
// // var query_builder = new DynamoDbItemRequestHelper.UpdateItemRequestBuilder(m_dynamo_db_client.getTableName());
|
|
// // query_builder.withKeys(attributeValueWithPrimaryKey);
|
|
// //
|
|
// // var target_doc = new MoneyDoc();
|
|
// // var attrib_path_json_string = target_doc.toJsonStringOfAttribs();
|
|
// // var target_key = JsonHelper.getJsonPropertyName<MoneyAttrib>(targetAttribName);
|
|
// // (var is_success, var attribute_expression) =
|
|
// // DynamoDbClientHelper.toAttributeExpressionFromJson(attrib_path_json_string, target_key);
|
|
// // if (false == is_success)
|
|
// // {
|
|
// // var err_msg =
|
|
// // $"Failed to DynamoDbClientHelper.toAttributeExpressionFromJson() !!! : attribPath:{attrib_path_json_string}, targetKey:{target_key}";
|
|
// // result.setFail(ServerErrorCode.AttribPathMakeFailed, err_msg);
|
|
// // Log.getLogger().error(result.toBasicString());
|
|
// //
|
|
// // return (result, null);
|
|
// // }
|
|
// //
|
|
// // query_builder.withExpressionAttributeNames(
|
|
// // DynamoDbClientHelper.toExpressionAttributeNamesFromJson(attrib_path_json_string, target_key));
|
|
// //
|
|
// // var update_expression = (deltaCount >= 0)
|
|
// // ? $"SET {attribute_expression} = if_not_exists({attribute_expression}, :start) + :changeValue"
|
|
// // : $"SET {attribute_expression} = if_not_exists({attribute_expression}, :start) - :changeValue";
|
|
// // query_builder.withUpdateExpression(update_expression);
|
|
// //
|
|
// // var expression_attribute_values = new Dictionary<string, AttributeValue>
|
|
// // {
|
|
// // { ":changeValue", new AttributeValue { N = Math.Abs(deltaCount).ToString() } },
|
|
// // { ":start", new AttributeValue { N = "0" } }
|
|
// // };
|
|
// // query_builder.withExpressionAttributeValues(expression_attribute_values);
|
|
// //
|
|
// // query_builder.withReturnValues(ReturnValue.ALL_NEW);
|
|
// //
|
|
// // return query_builder.build();
|
|
// // }
|
|
// }
|