using Amazon.DynamoDBv2.Model; using Amazon.DynamoDBv2; using Amazon.Runtime.Internal.Transform; using ServerCommon; using ServerCore; using ServerBase; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Org.BouncyCastle.Asn1.Ocsp; namespace GameServer { internal static class BuildingProfitHelper { public static FloorProfitInfo toFloorProfitInfo(this BuildingProfit buildingProfit) { var building_profit_attribute = buildingProfit.getEntityAttribute(); NullReferenceCheckHelper.throwIfNull(building_profit_attribute, () => $"building_profit_history_attribute is null !!!"); var floor_profit_info = new FloorProfitInfo(); foreach (var (currency_type, currency_amount) in building_profit_attribute.Profits) { var money = new Money(); money.Amount = currency_amount; floor_profit_info.Profits.Add((int)currency_type, money); } return floor_profit_info; } public static (Result, DynamoDbItemRequestQueryContext?) tryMakeUpdateItemRequestFromBuildingProfit(int buildingMetaId, int floor, CurrencyType currencyType, double deltaAmount) { var result = new Result(); var err_msg = string.Empty; var server_logic = GameServerApp.getServerLogic(); var db_client = server_logic.getDynamoDbClient(); var doc = new BuildingProfitDoc(buildingMetaId, floor); var primary_key = doc.getPrimaryKey(); var query_builder = new DynamoDbItemRequestHelper.UpdateItemRequestBuilder(db_client.getTableFullName(doc.TableName)); query_builder.withKeys(primary_key.toKeyWithAttributeValue()); var attrib_path_json_string = doc.toJsonStringOfAttribs(); var target_key = JsonHelper.getJsonPropertyName(nameof(BuildingProfitAttrib.Profits)); (var is_success, var attribute_expression) = DynamoDbClientHelper.toAttributeExpressionFromJson(attrib_path_json_string, target_key); if (false == is_success) { 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); } var attributeNames = DynamoDbClientHelper.toExpressionAttributeNamesFromJson(attrib_path_json_string, target_key); attributeNames.Add($"#{currencyType}", currencyType.ToString()); query_builder.withExpressionAttributeNames(attributeNames); attribute_expression += $".#{currencyType}"; var update_expression = (deltaAmount >= 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 { { ":changeValue", new AttributeValue { N = Math.Abs(deltaAmount).ToString() } }, { ":start", new AttributeValue { N = "0" } } }; query_builder.withExpressionAttributeValues(expression_attribute_values); query_builder.withReturnValues(ReturnValue.ALL_NEW); (result, var update_item_request) = query_builder.build(); if (result.isFail()) { err_msg = $"Failed to DynamoDbItemRequestHelper.build() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); return (result, null); } NullReferenceCheckHelper.throwIfNull(update_item_request, () => $"update_item_request is null !!! - {primary_key.toBasicString()}"); return (result, update_item_request.createItemRequestQueryContext(QueryType.Update)); } public static async Task<(Result, BuildingProfit?, DynamoDbDocBase?)> tryMakeBuildingProfit(Building building, int buildingMetaId, int floor, CurrencyType currencyType, double currencyAmount) { var result = new Result(); var err_msg = string.Empty; var building_profit = new BuildingProfit(building); await building_profit.onInit(); var new_building_profit_attribute = building_profit.getEntityAttribute(); NullReferenceCheckHelper.throwIfNull(new_building_profit_attribute, () => $"new_building_profit_attribute is null !!!"); new_building_profit_attribute.BuildingMetaId = buildingMetaId; new_building_profit_attribute.Floor = floor; new_building_profit_attribute.Profits[currencyType] = currencyAmount; new_building_profit_attribute.newEntityAttribute(); (result, var building_profit_doc) = await new_building_profit_attribute.toDocBase(); if (result.isFail()) { err_msg = $"Failed to toDocBase() !!! : {result.toBasicString()}"; Log.getLogger().error(err_msg); return (result, null, null); } NullReferenceCheckHelper.throwIfNull(building_profit_doc, () => $"building_profit_doc is null !!!"); return (result, building_profit, building_profit_doc); } } }