Skip to content

Commit

Permalink
Merge pull request #108 from shangfengh/dev
Browse files Browse the repository at this point in the history
fix(SafeValue): 🐛 fix the bug about the AtomicIntOnlyAddScore and so on
  • Loading branch information
asdawej authored Mar 9, 2024
2 parents 438cefb + 05feb11 commit 3d56ef0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
3 changes: 1 addition & 2 deletions logic/GameClass/GameObj/Areas/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public void BeAttacked(Bullet bullet)
{
return;
}
long subHP = bullet.AP;
this.HP.SubPositiveV(subHP);
this.HP.SubPositiveV(bullet.AP);
}
}
7 changes: 1 addition & 6 deletions logic/GameClass/GameObj/Areas/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ public class Resource : Immovable
public AtomicInt ProduceNum { get; } = new AtomicInt(0);
public bool Produce(int produceSpeed, Ship ship)
{
if (HP.SubV(produceSpeed) > 0)
{
ship.MoneyPool.AddMoney(produceSpeed);
return true;
}
return false;
return ship.MoneyPool.AddMoney(HP.SubV(produceSpeed)) > 0;
}
public void AddProduceNum(int add = 1)
{
Expand Down
3 changes: 2 additions & 1 deletion logic/GameClass/GameObj/MoneyPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ public class MoneyPool : IMoneyPool
{
public AtomicLong Money { get; } = new AtomicLong(0);
public AtomicLong Score { get; } = new AtomicLong(0);
public void AddMoney(long add)
public long AddMoney(long add)
{
Money.Add(add);
Score.Add(add);
return add;
}
public void SubMoney(long sub)
{
Expand Down
2 changes: 1 addition & 1 deletion logic/Preparation/Interface/IMoneyPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface IMoneyPool
{
public AtomicLong Money { get; }
public AtomicLong Score { get; }
public void AddMoney(long add);
public long AddMoney(long add);
public void SubMoney(long sub);
}
}
32 changes: 20 additions & 12 deletions logic/Preparation/Utility/SafeValue/Atomic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public AtomicInt(int x)
}

/// <summary>
/// 参数要求倍率speed(默认1)以及AtomicInt类的Score,
/// 参数要求倍率speed(默认1)
/// 可(在构造时)使用SetScore以设定AtomicInt类的Score
/// 在发生正向的变化时,自动给Score加上正向变化的差乘以speed。
/// 注意:AtomicIntOnlyAddScore本身为AtomicInt,提供的Score可能构成环而死锁。
/// </summary>
Expand All @@ -46,11 +47,12 @@ public class AtomicIntOnlyAddScore : AtomicInt
public AtomicInt Score { get; set; }
public AtomicDouble speed;
public AtomicDouble remainder = new(0);
public AtomicIntOnlyAddScore(int x, AtomicInt Score, double speed = 1.0) : base(x)
public AtomicIntOnlyAddScore(int x, double speed = 1.0) : base(x)
{
this.Score = Score;
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;
Expand Down Expand Up @@ -129,19 +131,21 @@ public int CompareExReturnOriNotAddScore(int newV, int compareTo)
}

/// <summary>
/// 参数要求倍率speed(默认1)以及AtomicLong类的Score,
/// 参数要求倍率speed(默认1)
/// 可(在构造时)使用SetScore以设定AtomicLong类的Score
/// 在发生正向的变化时,自动给Score加上正向变化的差乘以speed。
/// </summary>
public class AtomicIntOnlyAddLongScore : AtomicInt
{
public AtomicLong Score { get; set; }
public AtomicDouble speed;
public AtomicDouble remainder = new(0);
public AtomicIntOnlyAddLongScore(int x, AtomicLong Score, double speed = 1.0) : base(x)
public AtomicIntOnlyAddLongScore(int x, double speed = 1.0) : base(x)
{
this.Score = Score;
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;
Expand Down Expand Up @@ -213,7 +217,8 @@ public int CompareExReturnOriNotAddScore(int newV, int compareTo)
}

/// <summary>
/// 参数要求倍率speed(默认1)以及AtomicInt类的Score,
/// 参数要求倍率speed(默认1)
/// 可(在构造时)使用SetScore以设定AtomicInt类的Score
/// 在发生变化时,自动给Score加上变化的差乘以speed取整。
/// 注意:AtomicIntChangeAffectScore本身为AtomicInt,提供的Score可能构成环而死锁。
/// </summary>
Expand All @@ -222,11 +227,12 @@ public class AtomicIntChangeAffectScore : AtomicInt
public AtomicInt Score { get; set; }
public AtomicDouble speed;
public AtomicDouble remainder = new(0);
public AtomicIntChangeAffectScore(int x, AtomicInt Score, double speed = 1.0) : base(x)
public AtomicIntChangeAffectScore(int x, double speed = 1.0) : base(x)
{
this.Score = Score;
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;
Expand Down Expand Up @@ -341,7 +347,8 @@ public AtomicLong(long x)
}

/// <summary>
/// 参数要求倍率speed(默认1)以及AtomicLong类的Score,
/// 参数要求倍率speed(默认1)
/// 可(在构造时)使用SetScore以设定AtomicLong类的Score
/// 在发生正向的变化时,自动给Score加上正向变化的差乘以speed取整。
/// 注意:AtomicLongOnlyAddScore本身为AtomicLong,提供的Score可能构成环而死锁。
/// </summary>
Expand All @@ -350,11 +357,12 @@ public class AtomicLongOnlyAddScore : AtomicLong
public AtomicLong Score { get; set; }
public AtomicDouble speed;
public AtomicDouble remainder = new(0);
public AtomicLongOnlyAddScore(long x, AtomicLong score, double speed = 1.0) : base(x)
public AtomicLongOnlyAddScore(long x, double speed = 1.0) : base(x)
{
this.Score = score;
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;
Expand Down

0 comments on commit 3d56ef0

Please sign in to comment.