using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ServerCore; using ServerBase; using ServerCommon; using ServerCommon.Cache; using Amazon.DynamoDBv2.Model; using META_ID = System.UInt32; using USER_GUID = System.String; using LAND_OWNER_GUID = System.String; namespace GameServer { public static class LandAuctionHelper { public static string getWinningBidValue(this LandAuction landAuction) { var land_auction_action = landAuction.getEntityAction(); NullReferenceCheckHelper.throwIfNull(land_auction_action, () => $"land_auction_action is null !!! - {landAuction.toBasicString()}"); return land_auction_action.getHighestBidPrice().ToString("N0"); // 천 단위 구분 기호 포함, 소숫점은 없다 !!! - kangms } public static Result copyDocToEntityAttributeForRefundBidPrice( this LandAuctionRefundBidPrice refundBidPrice , EntityAttributeBase toClonedAttributeBase, DynamoDbDocBase fromDoc ) { var result = new Result(); var err_msg = string.Empty; var refund_bid_price_attribute = toClonedAttributeBase as LandAuctionRefundBidPriceAttribute; if (null != refund_bid_price_attribute) { var is_success = refund_bid_price_attribute.copyEntityAttributeFromDoc(fromDoc); if (false == is_success) { err_msg = $"Failed to copyEntityAttributeFromDoc() !!! : docType:{fromDoc.getTypeName()} - {refundBidPrice.toBasicString()}"; result.setFail(ServerErrorCode.DynamoDbDocCopyToEntityAttributeFailed, err_msg); Log.getLogger().error(result.toBasicString()); return result; } var origin_attribute = refundBidPrice.getOriginEntityAttribute(); NullReferenceCheckHelper.throwIfNull(origin_attribute, () => $"origin_attribute is null !!! - {refundBidPrice.toBasicString()}"); origin_attribute.copyEntityAttributeFromDoc(fromDoc); return result; } ConditionValidCheckHelper.throwIfFalseWithCondition(() => false , () => $"Failed to copy EntityAttribute from Doc !!! : attributeType:{toClonedAttributeBase.getTypeName()} <= docType:{fromDoc.getTypeName()}" + $" - {refundBidPrice.toBasicString()}"); return result; } public static Result copyCacheToEntityAttributeForLandAuction( this LandAuction landAuction , EntityAttributeBase toClonedAttributeBase, CacheBase fromCache ) { var result = new Result(); var err_msg = string.Empty; var auction_action = landAuction.getEntityAction(); NullReferenceCheckHelper.throwIfNull(auction_action, () => $"auction_action is null !!! - {landAuction.toBasicString()}"); var registry_attribute = toClonedAttributeBase as LandAuctionRegistryAttribute; if (null != registry_attribute) { var origin_attribute = landAuction.getOriginEntityAttribute(); NullReferenceCheckHelper.throwIfNull(origin_attribute); var land_auction_cache = fromCache as LandAuctionCache; if (null != land_auction_cache) { registry_attribute.copyProcessInfoFromCache(land_auction_cache); origin_attribute.copyProcessInfoFromCache(land_auction_cache); auction_action.updateProcessVersion(land_auction_cache.ProcessVersionTime); } return result; } var highest_bid_user_attribute = toClonedAttributeBase as LandAuctionHighestBidUserAttribute; if (null != highest_bid_user_attribute) { var origin_attribute = landAuction.getOriginEntityAttribute(); NullReferenceCheckHelper.throwIfNull(origin_attribute); var normal_bid_highest_user_cache = fromCache as NormalBidHighestUserCache; if (null != normal_bid_highest_user_cache) { highest_bid_user_attribute.copyEntityAttributeFromCache(normal_bid_highest_user_cache); origin_attribute.copyEntityAttributeFromCache(normal_bid_highest_user_cache); auction_action.updateHighestRankVersion(normal_bid_highest_user_cache.HighestRankVersionTime); } var top_bidder_cache = fromCache as LandAuctionTopBidderCache; if (null != top_bidder_cache) { highest_bid_user_attribute.copyEntityAttributeFromCache(top_bidder_cache); origin_attribute.copyEntityAttributeFromCache(top_bidder_cache); } return result; } ConditionValidCheckHelper.throwIfFalseWithCondition( () => false , () => $"Failed to copy EntityAttribute from Cache !!! : attributeType:{toClonedAttributeBase.getTypeName()} <= cacheType:{fromCache.getTypeName()}" + $" - {landAuction.toBasicString()}"); return result; } public static async Task copyDocToEntityAttributeForLandAuction( this LandAuction landAuction , EntityAttributeBase toClonedAttributeBase, DynamoDbDocBase fromDoc , bool isWithLock ) { var result = new Result(); var err_msg = string.Empty; var auction_action = landAuction.getEntityAction(); NullReferenceCheckHelper.throwIfNull(auction_action, () => $"auction_action is null !!! - {landAuction.toBasicString()}"); var activity_attribute = toClonedAttributeBase as LandAuctionActivityAttribute; if(null != activity_attribute) { var is_success = activity_attribute.copyEntityAttributeFromDoc(fromDoc); if(false == is_success) { err_msg = $"Failed to copyEntityAttributeFromDoc() !!! : docType:{fromDoc.getTypeName()} - {landAuction.toBasicString()}"; result.setFail(ServerErrorCode.DynamoDbDocCopyToEntityAttributeFailed, err_msg); Log.getLogger().error(result.toBasicString()); return result; } var origin_attribute = landAuction.getOriginEntityAttribute(); NullReferenceCheckHelper.throwIfNull(origin_attribute, () => $"origin_attribute is null !!! - {landAuction.toBasicString()}"); origin_attribute.copyEntityAttributeFromDoc(fromDoc); return result; } var registry_attribute = toClonedAttributeBase as LandAuctionRegistryAttribute; if (null != registry_attribute) { var origin_attribute = landAuction.getOriginEntityAttribute(); NullReferenceCheckHelper.throwIfNull(origin_attribute); var registry_doc = fromDoc as LandAuctionRegistryDoc; NullReferenceCheckHelper.throwIfNull(registry_doc, () => $"registry_doc is null !!! : docType:{fromDoc.getTypeName()} - {landAuction.toBasicString()}"); var registry_attrib = registry_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(registry_attrib, () => $"registry_attrib is null !!! - {landAuction.toBasicString()}"); result = registry_attrib.checkValidLandAuctionRegistry(); if (result.isFail()) { return result; } var fn_copy_to_attribute = delegate () { if (origin_attribute.RegisteredVersionTime < registry_attrib.RegisteredVersionTime) { registry_attribute.copyRegisteredInfoFromDoc(registry_doc); origin_attribute.copyRegisteredInfoFromDoc(registry_doc); } if (origin_attribute.ProcessVersionTime < registry_attrib.ProcessVersionTime) { registry_attribute.copyProcessInfoFromDoc(registry_doc); origin_attribute.copyProcessInfoFromDoc(registry_doc); } auction_action.updateRegistryVersion( registry_attrib.RegisteredVersionTime, registry_attrib.ProcessVersionTime ); }; if (true == isWithLock) { using (var releaser = await landAuction.getAsyncLock()) { fn_copy_to_attribute(); } } else { fn_copy_to_attribute(); } return result; } var highest_bid_user_attribute = toClonedAttributeBase as LandAuctionHighestBidUserAttribute; if (null != highest_bid_user_attribute) { var origin_attribute = landAuction.getOriginEntityAttribute(); NullReferenceCheckHelper.throwIfNull(origin_attribute); var highest_bid_user_doc = fromDoc as LandAuctionHighestBidUserDoc; NullReferenceCheckHelper.throwIfNull(highest_bid_user_doc, () => $"highest_bid_user_doc is null !!! : docType:{fromDoc.getTypeName()} - {landAuction.toBasicString()}"); var highest_bid_user_attrib = highest_bid_user_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(highest_bid_user_attrib, () => $"highest_bid_user_attrib is null !!! - {landAuction.toBasicString()}"); if (origin_attribute.HighestRankVersionTime < highest_bid_user_attrib.HighestRankVersionTime) { highest_bid_user_attribute.copyEntityAttributeFromDoc(highest_bid_user_doc); origin_attribute.copyEntityAttributeFromDoc(highest_bid_user_doc); } auction_action.updateHighestRankVersion(highest_bid_user_attrib.HighestRankVersionTime); return result; } ConditionValidCheckHelper.throwIfFalseWithCondition( () => false , () => $"Failed to copy EntityAttribute from Doc !!! : attributeType:{toClonedAttributeBase.getTypeName()} <= docType:{fromDoc.getTypeName()}" + $" - {landAuction.toBasicString()}"); return result; } public static (bool, LandAuctionVersion?) checkChangedVersion( this LandAuctionVersion preVersion , DateTime registeredVersionTime, DateTime processVersionTime, DateTime highestRankVersionTime ) { var version = new LandAuctionVersion(registeredVersionTime, processVersionTime, highestRankVersionTime); if (true == preVersion.checkChangedVersion(version)) { return (true, version); } return (false, null); } public static bool checkChangedVersion(this LandAuctionVersion preVersion, LandAuctionVersion currVersion) { if( preVersion.RegisteredVersionTime < currVersion.RegisteredVersionTime || preVersion.ProcessVersionTime < currVersion.ProcessVersionTime || preVersion.HighestRankVersionTime < currVersion.HighestRankVersionTime ) { Log.getLogger().info( $"changed Version !!! " + $" : prevVersion.RegisteredVersion:{preVersion.RegisteredVersionTime} < currVersion.RegisteredVersion:{currVersion.RegisteredVersionTime}" + $", prevVersion.ProcessVersion:{preVersion.ProcessVersionTime} < currVersion.ProcessVersion:{currVersion.ProcessVersionTime}" + $", prevVersion.HighestRankVersion:{preVersion.HighestRankVersionTime} < currVersion.HighestRankVersion:{currVersion.HighestRankVersionTime}" ); if(preVersion.RegisteredVersionTime > currVersion.RegisteredVersionTime) { currVersion.RegisteredVersionTime = preVersion.RegisteredVersionTime; } if(preVersion.ProcessVersionTime > currVersion.ProcessVersionTime) { currVersion.ProcessVersionTime = preVersion.ProcessVersionTime; } if(preVersion.HighestRankVersionTime > currVersion.HighestRankVersionTime) { currVersion.HighestRankVersionTime = preVersion.HighestRankVersionTime; } currVersion.IsSyncRequried = true; return true; } return false; } public static bool isLandAuctionStateByTime(this LandAuctionCheckResult checkResult, LandAuctionState state) { var land_auction_info = checkResult.LandAuctionInfo; NullReferenceCheckHelper.throwIfNull(land_auction_info, () => $"land_auction_info is null !!!"); if (LandAuctionState.Waiting == state) { if (false == DateTimeHelper.hasTimeReached(land_auction_info.AuctionReservationNoticeStartTime.ToDateTime())) { return true; } } else if (LandAuctionState.Scheduled == state) { if ( true == DateTimeHelper.hasTimeReached(land_auction_info.AuctionReservationNoticeStartTime.ToDateTime()) && false == DateTimeHelper.hasTimeReached(land_auction_info.AuctionStartTime.ToDateTime())) { return true; } } else if (LandAuctionState.Started == state) { if (true == DateTimeHelper.isBetween(land_auction_info.AuctionStartTime.ToDateTime(), land_auction_info.AuctionEndTime.ToDateTime())) { return true; } } else if (LandAuctionState.Ended == state) { if (true == DateTimeHelper.hasTimeReached(land_auction_info.AuctionEndTime.ToDateTime())) { return true; } } else { var err_msg = $"Invalid LandAuctionState !!! : {state}"; Log.getLogger().warn(err_msg); } return false; } public static bool isLandAuctionState(this LandAuctionCheckResult checkResult, LandAuctionState state) { var land_auction_info = checkResult.LandAuctionInfo; NullReferenceCheckHelper.throwIfNull(land_auction_info, () => $"land_auction_info is null !!!"); if ( state == land_auction_info.LandAuctionState ) { return true; } return false; } public static string toBasicString(this LandAuctionCheckResult checkResult) { return $"{checkResult.getTypeName()}: LandAuctionInfo:{checkResult.LandAuctionInfo.toBasicString()}, BidType:{checkResult.LandAuctionBidType}" + $", CurrencyType:{checkResult.MyBidCurrencyType}, MyBidPrice:{checkResult.MyBidPrice}, {checkResult.Version.toBasicString()}"; } public static string toBasicString(this LandAuctionBidResult bidResult) { return $"{bidResult.getTypeName()}: TargetLandAuction:{bidResult.TargetLandAuction?.toBasicString()}, BidType:{bidResult.CurrentBidType}" + $", BidTime:{bidResult.BidTime}"; } public static bool isLandAuctionStateByTime(this LandAuctionRegistryAttribute registryAttribute, LandAuctionState state) { if (LandAuctionState.Waiting == state) { if (false == DateTimeHelper.hasTimeReached(registryAttribute.AuctionReservationNoticeStartTime)) { return true; } } else if (LandAuctionState.Scheduled == state) { if ( true == DateTimeHelper.hasTimeReached(registryAttribute.AuctionReservationNoticeStartTime) && false == DateTimeHelper.hasTimeReached(registryAttribute.AuctionStartTime) ) { return true; } } else if (LandAuctionState.Started == state) { if (true == DateTimeHelper.isBetween( registryAttribute.AuctionStartTime, registryAttribute.AuctionEndTime) ) { return true; } } else if (LandAuctionState.Ended == state) { if (true == DateTimeHelper.hasTimeReached(registryAttribute.AuctionEndTime)) { return true; } } else { var err_msg = $"Invalid LandAuctionState !!! : {state}"; Log.getLogger().warn(err_msg); } return false; } } }