Skip to content

Commit

Permalink
Merge pull request eesast#126 from shangfengh/dev
Browse files Browse the repository at this point in the history
refactor: 🎨 use the AtomicLongOnlyAddScore
  • Loading branch information
asdawej authored Mar 15, 2024
2 parents d29693c + 773dce8 commit 672fbad
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 87 deletions.
1 change: 0 additions & 1 deletion logic/GameClass/GameObj/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 7 additions & 20 deletions logic/GameClass/GameObj/Map/MapGameTimer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading;
using Preparation.Interface;
using Preparation.Utility;
using ITimer = Preparation.Interface.ITimer;

namespace GameClass.GameObj
Expand All @@ -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;
}
}
Expand Down
7 changes: 5 additions & 2 deletions logic/GameClass/GameObj/MoneyPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions logic/Preparation/Interface/ITimer.cs
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion logic/Preparation/Preparation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\dependency\proto\Protos.csproj"/>
<ProjectReference Include="..\..\dependency\proto\Protos.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 672fbad

Please sign in to comment.