diff --git a/logic/GameClass/GameObj/Map/Map.cs b/logic/GameClass/GameObj/Map/Map.cs index cf64fd2c..9460b26f 100755 --- a/logic/GameClass/GameObj/Map/Map.cs +++ b/logic/GameClass/GameObj/Map/Map.cs @@ -187,7 +187,6 @@ public Map(MapStruct mapResource) { for (int j = 0; j < width; ++j) { - bool hasWormhole = false; switch (mapResource.map[i, j]) { case PlaceType.Resource: diff --git a/logic/GameClass/GameObj/Map/MapGameTimer.cs b/logic/GameClass/GameObj/Map/MapGameTimer.cs index b8ed39b7..1adc30f6 100755 --- a/logic/GameClass/GameObj/Map/MapGameTimer.cs +++ b/logic/GameClass/GameObj/Map/MapGameTimer.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using Preparation.Interface; +using Preparation.Utility; using ITimer = Preparation.Interface.ITimer; namespace GameClass.GameObj @@ -16,30 +17,16 @@ public class GameTimer : ITimer private long startTime; public int nowTime() => (int)(Environment.TickCount64 - startTime); - private bool isGaming = false; - public bool IsGaming - { - get => isGaming; - set - { - lock (isGamingLock) - isGaming = value; - } - } - - readonly object isGamingLock = new(); + private AtomicBool isGaming = new(false); + public AtomicBool IsGaming => isGaming; public bool StartGame(int timeInMilliseconds) { - lock (isGamingLock) - { - if (isGaming) - return false; - isGaming = true; - startTime = Environment.TickCount64; - } + if (!IsGaming.TrySet(true)) + return false; + startTime = Environment.TickCount64; Thread.Sleep(timeInMilliseconds); - isGaming = false; + IsGaming.SetReturnOri(false); return true; } } diff --git a/logic/GameClass/GameObj/MoneyPool.cs b/logic/GameClass/GameObj/MoneyPool.cs index 22c1e36c..b1b9e021 100644 --- a/logic/GameClass/GameObj/MoneyPool.cs +++ b/logic/GameClass/GameObj/MoneyPool.cs @@ -5,12 +5,15 @@ namespace GameClass.GameObj; public class MoneyPool : IMoneyPool { - public AtomicLong Money { get; } = new AtomicLong(0); + public AtomicLongOnlyAddScore Money { get; } = new(0); public AtomicLong Score { get; } = new AtomicLong(0); + public MoneyPool() + { + Money.SetScore(Score); + } public long AddMoney(long add) { Money.Add(add); - Score.Add(add); return add; } public void SubMoney(long sub) diff --git a/logic/Preparation/Interface/IMoneyPool.cs b/logic/Preparation/Interface/IMoneyPool.cs index 9422587f..3e9ae255 100644 --- a/logic/Preparation/Interface/IMoneyPool.cs +++ b/logic/Preparation/Interface/IMoneyPool.cs @@ -4,7 +4,7 @@ namespace Preparation.Interface { public interface IMoneyPool { - public AtomicLong Money { get; } + public AtomicLongOnlyAddScore Money { get; } public AtomicLong Score { get; } public long AddMoney(long add); public void SubMoney(long sub); diff --git a/logic/Preparation/Interface/ITimer.cs b/logic/Preparation/Interface/ITimer.cs index af9c5cd2..bbcaee20 100755 --- a/logic/Preparation/Interface/ITimer.cs +++ b/logic/Preparation/Interface/ITimer.cs @@ -1,8 +1,10 @@ -namespace Preparation.Interface +using Preparation.Utility; + +namespace Preparation.Interface { public interface ITimer { - bool IsGaming { get; set; } + AtomicBool IsGaming { get; } public int nowTime(); public bool StartGame(int timeInMilliseconds); } diff --git a/logic/Preparation/Preparation.csproj b/logic/Preparation/Preparation.csproj index 2d7a27f2..2c3ec574 100755 --- a/logic/Preparation/Preparation.csproj +++ b/logic/Preparation/Preparation.csproj @@ -13,7 +13,7 @@ - + diff --git a/logic/Preparation/Utility/SafeValue/Atomic.cs b/logic/Preparation/Utility/SafeValue/Atomic.cs index a952d962..f0b2a680 100755 --- a/logic/Preparation/Utility/SafeValue/Atomic.cs +++ b/logic/Preparation/Utility/SafeValue/Atomic.cs @@ -39,33 +39,25 @@ public AtomicInt(int x) /// /// 参数要求倍率speed(默认1) /// 可(在构造时)使用SetScore以设定AtomicInt类的Score - /// 在发生正向的变化时,自动给Score加上正向变化的差乘以speed。 + /// 在发生正向的变化时,自动给Score加上正向变化的差乘以speed(取整)。 /// 注意:AtomicIntOnlyAddScore本身为AtomicInt,提供的Score可能构成环而死锁。 /// public class AtomicIntOnlyAddScore : AtomicInt { public AtomicInt Score { get; set; } public AtomicDouble speed; - public AtomicDouble remainder = new(0); public AtomicIntOnlyAddScore(int x, double speed = 1.0) : base(x) { this.speed = new(speed); this.Score = new(0); } public void SetScore(AtomicInt score) => Score = score; - private int DealWithRemainder(int v) - { - double q = speed * v + remainder; - int ans = (int)(q); - remainder.SetReturnOri(q - ans); - return ans; - } /// 返回操作前的值 public override int SetReturnOri(int value) { int previousV = Interlocked.Exchange(ref v, value); if (value - previousV > 0) - Score.AddPositive((DealWithRemainder(value - previousV))); + Score.AddPositive(Convert.ToInt32((value - previousV) * speed)); return previousV; } /// 返回操作前的值 @@ -75,7 +67,7 @@ public int SetReturnOriNotAddScore(int value) } public override int Add(int x) { - if (x > 0) Score.AddPositive(DealWithRemainder(x)); + if (x > 0) Score.AddPositive(Convert.ToInt32(x * speed)); return Interlocked.Add(ref v, x); } public int AddNotAddScore(int x) @@ -87,12 +79,12 @@ public int AddNotAddScore(int x) /// public override int AddPositive(int x) { - Score.AddPositive(DealWithRemainder(x)); + Score.AddPositive(Convert.ToInt32(x * speed)); return Interlocked.Add(ref v, x); } public override int Sub(int x) { - if (x < 0) Score.AddPositive(DealWithRemainder(-x)); + if (x < 0) Score.AddPositive(Convert.ToInt32((-x) * speed)); return Interlocked.Add(ref v, -x); } public int SubNotAddScore(int x) @@ -108,7 +100,7 @@ public override int SubPositive(int x) } public override int Inc() { - Score.AddPositive(DealWithRemainder(1)); + Score.AddPositive(Convert.ToInt32(speed)); return Interlocked.Increment(ref v); } public int IncNotAddScore() @@ -120,7 +112,7 @@ public override int CompareExReturnOri(int newV, int compareTo) { int previousV = Interlocked.CompareExchange(ref v, newV, compareTo); if (newV - previousV > 0) - Score.AddPositive(DealWithRemainder(newV - previousV)); + Score.AddPositive(Convert.ToInt32((newV - previousV) * speed)); return previousV; } /// 返回操作前的值 @@ -132,33 +124,25 @@ public int CompareExReturnOriNotAddScore(int newV, int compareTo) /// /// 参数要求倍率speed(默认1) - /// 可(在构造时)使用SetScore以设定AtomicLong类的Score + /// 可(在构造时)使用SetScore以设定AtomicLong类的Score(取整) /// 在发生正向的变化时,自动给Score加上正向变化的差乘以speed。 /// public class AtomicIntOnlyAddLongScore : AtomicInt { public AtomicLong Score { get; set; } public AtomicDouble speed; - public AtomicDouble remainder = new(0); public AtomicIntOnlyAddLongScore(int x, double speed = 1.0) : base(x) { this.Score = new(0); this.speed = new(speed); } public void SetScore(AtomicLong score) => Score = score; - private long DealWithRemainder(int v) - { - double q = speed * v + remainder; - long ans = (long)(q); - remainder.SetReturnOri(q - ans); - return ans; - } /// 返回操作前的值 public override int SetReturnOri(int value) { int previousV = Interlocked.Exchange(ref v, value); if (value - previousV > 0) - Score.AddPositive((DealWithRemainder(value - previousV))); + Score.AddPositive((Convert.ToInt32((value - previousV) * speed))); return previousV; } /// 返回操作前的值 @@ -168,7 +152,7 @@ public int SetReturnOriNotAddScore(int value) } public override int Add(int x) { - if (x > 0) Score.AddPositive(DealWithRemainder(x)); + if (x > 0) Score.AddPositive(Convert.ToInt32(x * speed)); return Interlocked.Add(ref v, x); } public int AddNotAddScore(int x) @@ -180,12 +164,12 @@ public int AddNotAddScore(int x) /// public override int AddPositive(int x) { - Score.AddPositive(DealWithRemainder(x)); + Score.AddPositive(Convert.ToInt32((x) * speed)); return Interlocked.Add(ref v, x); } public override int Sub(int x) { - if (x < 0) Score.AddPositive(DealWithRemainder(-x)); + if (x < 0) Score.AddPositive(Convert.ToInt32((-x) * speed)); return Interlocked.Add(ref v, -x); } public int SubNotAddScore(int x) @@ -194,7 +178,7 @@ public int SubNotAddScore(int x) } public override int Inc() { - Score.AddPositive(DealWithRemainder(1)); + Score.AddPositive(Convert.ToInt32(speed)); return Interlocked.Increment(ref v); } public int IncNotAddScore() @@ -206,7 +190,7 @@ public override int CompareExReturnOri(int newV, int compareTo) { int previousV = Interlocked.CompareExchange(ref v, newV, compareTo); if (newV - previousV > 0) - Score.AddPositive(DealWithRemainder(newV - previousV)); + Score.AddPositive(Convert.ToInt32((newV - previousV) * speed)); return previousV; } /// 返回操作前的值 @@ -226,25 +210,17 @@ public class AtomicIntChangeAffectScore : AtomicInt { public AtomicInt Score { get; set; } public AtomicDouble speed; - public AtomicDouble remainder = new(0); public AtomicIntChangeAffectScore(int x, double speed = 1.0) : base(x) { this.Score = new(0); this.speed = new(speed); } public void SetScore(AtomicInt score) => Score = score; - private int DealWithRemainder(int v) - { - double q = speed * v + remainder; - int ans = (int)(q); - remainder.SetReturnOri(q - ans); - return ans; - } /// 返回操作前的值 public override int SetReturnOri(int value) { int previousV = Interlocked.Exchange(ref v, value); - Score.Add(DealWithRemainder(value - previousV)); + Score.Add(Convert.ToInt32((value - previousV) * speed)); return previousV; } /// 返回操作前的值 @@ -254,7 +230,7 @@ public int SetReturnOriNotAddScore(int value) } public override int Add(int x) { - Score.Add(DealWithRemainder(x)); + Score.Add(Convert.ToInt32((x) * speed)); return Interlocked.Add(ref v, x); } public int AddNotAddScore(int x) @@ -266,12 +242,12 @@ public int AddNotAddScore(int x) /// public override int AddPositive(int x) { - Score.AddPositive(DealWithRemainder(x)); + Score.AddPositive(Convert.ToInt32((x) * speed)); return Interlocked.Add(ref v, x); } public override int Sub(int x) { - Score.Sub(DealWithRemainder(x)); + Score.Sub(Convert.ToInt32((x) * speed)); return Interlocked.Add(ref v, -x); } public int SubNotAddScore(int x) @@ -283,12 +259,12 @@ public int SubNotAddScore(int x) /// public override int SubPositive(int x) { - Score.SubPositive(DealWithRemainder(x)); + Score.SubPositive(Convert.ToInt32((x) * speed)); return Interlocked.Add(ref v, -x); } public override int Inc() { - Score.AddPositive(DealWithRemainder(1)); + Score.AddPositive(Convert.ToInt32(speed)); return Interlocked.Increment(ref v); } public int IncNotAddScore() @@ -297,7 +273,7 @@ public int IncNotAddScore() } public override int Dec() { - Score.SubPositive(DealWithRemainder(1)); + Score.SubPositive(Convert.ToInt32(speed)); return Interlocked.Decrement(ref v); } public int DecNotAddScore() @@ -308,7 +284,7 @@ public int DecNotAddScore() public override int CompareExReturnOri(int newV, int compareTo) { int previousV = Interlocked.CompareExchange(ref v, newV, compareTo); - Score.Add(DealWithRemainder(newV - previousV)); + Score.Add(Convert.ToInt32((newV - previousV) * speed)); return previousV; } /// 返回操作前的值 @@ -356,27 +332,19 @@ public class AtomicLongOnlyAddScore : AtomicLong { public AtomicLong Score { get; set; } public AtomicDouble speed; - public AtomicDouble remainder = new(0); public AtomicLongOnlyAddScore(long x, double speed = 1.0) : base(x) { this.Score = new(0); this.speed = new(speed); } public void SetScore(AtomicLong score) => Score = score; - private long DealWithRemainder(long v) - { - double q = speed * v + remainder; - long ans = (long)(q); - remainder.SetReturnOri(q - ans); - return ans; - } /// 返回操作前的值 public override long SetReturnOri(long value) { long previousV = Interlocked.Exchange(ref v, value); if (value - previousV > 0) - Score.AddPositive(DealWithRemainder(value - previousV)); + Score.AddPositive(Convert.ToInt32((value - previousV) * speed)); return previousV; } /// 返回操作前的值 @@ -386,7 +354,7 @@ public long SetReturnOriNotAddScore(long value) } public override long Add(long x) { - if (x > 0) Score.AddPositive(DealWithRemainder(x)); + if (x > 0) Score.AddPositive(Convert.ToInt32((x) * speed)); return Interlocked.Add(ref v, x); } public long AddNotAddScore(long x) @@ -398,12 +366,12 @@ public long AddNotAddScore(long x) /// public override long AddPositive(long x) { - Score.AddPositive(DealWithRemainder(x)); + Score.AddPositive(Convert.ToInt32((x) * speed)); return Interlocked.Add(ref v, x); } public override long Sub(long x) { - if (x < 0) Score.AddPositive(DealWithRemainder(-x)); + if (x < 0) Score.AddPositive(Convert.ToInt32((-x) * speed)); return Interlocked.Add(ref v, -x); } public long SubNotAddScore(long x) @@ -412,7 +380,7 @@ public long SubNotAddScore(long x) } public override long Inc() { - Score.AddPositive(DealWithRemainder(1)); + Score.AddPositive(Convert.ToInt32(speed)); return Interlocked.Increment(ref v); } public long IncNotAddScore() @@ -424,7 +392,7 @@ public override long CompareExReturnOri(long newV, long compareTo) { long previousV = Interlocked.CompareExchange(ref v, newV, compareTo); if (newV - previousV > 0) - Score.AddPositive(DealWithRemainder(newV - previousV)); + Score.AddPositive(Convert.ToInt32((newV - previousV) * speed)); return previousV; } /// 返回操作前的值 diff --git a/logic/Preparation/Utility/XY.cs b/logic/Preparation/Utility/Tools/XY.cs old mode 100755 new mode 100644 similarity index 100% rename from logic/Preparation/Utility/XY.cs rename to logic/Preparation/Utility/Tools/XY.cs