From 66d2b277b96554dafcede27326dcf4e369c621c7 Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Mon, 10 Jul 2023 01:59:24 +0800 Subject: [PATCH] fix: :bug: fix the chest progress --- logic/GameClass/GameObj/Map/Chest.cs | 1 - logic/Preparation/Utility/SafeValue.cs | 29 ++++++++++++++++++++++++-- logic/Server/CopyInfo.cs | 7 +++---- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/logic/GameClass/GameObj/Map/Chest.cs b/logic/GameClass/GameObj/Map/Chest.cs index 8e450e5..0fc6979 100644 --- a/logic/GameClass/GameObj/Map/Chest.cs +++ b/logic/GameClass/GameObj/Map/Chest.cs @@ -1,6 +1,5 @@ using Preparation.Interface; using Preparation.Utility; -using System; namespace GameClass.GameObj { diff --git a/logic/Preparation/Utility/SafeValue.cs b/logic/Preparation/Utility/SafeValue.cs index dcbf502..934fbe2 100644 --- a/logic/Preparation/Utility/SafeValue.cs +++ b/logic/Preparation/Utility/SafeValue.cs @@ -84,7 +84,7 @@ namespace Preparation.Utility } public LongProgressByTime() { - this.needT = long.MaxValue; + this.needT = 0; } public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2); public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2); @@ -93,6 +93,7 @@ namespace Preparation.Utility { return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64; } + public bool IsOpened() => Interlocked.Read(ref endT) != long.MaxValue; /// /// GetProgress<0则表明未开始 /// @@ -102,6 +103,13 @@ namespace Preparation.Utility if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2); return Interlocked.CompareExchange(ref needT, -2, -2) - cutime; } + public long GetNonNegativeProgress() + { + long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; + if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2); + long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime; + return progress < 0 ? 0 : progress; + } /// /// GetProgress<0则表明未开始 /// @@ -111,6 +119,13 @@ namespace Preparation.Utility if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2); return Interlocked.CompareExchange(ref needT, -2, -2) - cutime; } + public long GetNonNegativeProgress(long time) + { + long cutime = Interlocked.Read(ref endT) - time; + if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2); + long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime; + return progress < 0 ? 0 : progress; + } /// /// <0则表明未开始 /// @@ -123,7 +138,17 @@ namespace Preparation.Utility { long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64; if (cutime <= 0) return 1; - return 1.0 - ((double)cutime / Interlocked.CompareExchange(ref needT, -2, -2)); + long needTime = Interlocked.CompareExchange(ref needT, -2, -2); + if (needTime == 0) return 0; + return 1.0 - ((double)cutime / needTime); + } + public double GetNonNegativeProgressDouble(long time) + { + long cutime = Interlocked.Read(ref endT) - time; + if (cutime <= 0) return 1; + long needTime = Interlocked.CompareExchange(ref needT, -2, -2); + if (needTime <= cutime) return 0; + return 1.0 - ((double)cutime / needTime); } public bool Start(long needTime) diff --git a/logic/Server/CopyInfo.cs b/logic/Server/CopyInfo.cs index 067eccc..7532cd6 100644 --- a/logic/Server/CopyInfo.cs +++ b/logic/Server/CopyInfo.cs @@ -269,16 +269,15 @@ namespace Server } private static MessageOfObj Chest(Chest chest, long time) { - MessageOfObj msg = new() + return new() { ChestMessage = new() { X = chest.Position.x, - Y = chest.Position.y + Y = chest.Position.y, + Progress = (int)(chest.OpenProgress.GetNonNegativeProgressDouble(time) * GameData.degreeOfOpenedChest) } }; - msg.ChestMessage.Progress = (int)chest.OpenProgress.GetProgress(time); - return msg; } } }