From 24176ef9b4eedf59551f9e7345a971c61bf253ce Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Thu, 25 Apr 2024 11:44:27 +0800 Subject: [PATCH 1/5] perf(SafeValue): :sparkles: make the Timer SafeValue --- logic/GameClass/GameObj/Map/Map.cs | 4 +++ logic/GameClass/GameObj/Map/MapGameTimer.cs | 33 ------------------- logic/GameEngine/MoveEngine.cs | 3 +- logic/Gaming/Game.cs | 11 +------ logic/Preparation/Interface/IMap.cs | 2 +- logic/Preparation/Interface/ITimer.cs | 11 ------- .../Utility/Value/SafeValue/MyTimer.cs | 30 +++++++++++++++++ .../Utility/Value/ValueInterface/IMyTimer.cs | 11 +++++++ logic/Server/GameServer.cs | 2 +- 9 files changed, 49 insertions(+), 58 deletions(-) delete mode 100755 logic/GameClass/GameObj/Map/MapGameTimer.cs delete mode 100755 logic/Preparation/Interface/ITimer.cs create mode 100644 logic/Preparation/Utility/Value/SafeValue/MyTimer.cs create mode 100644 logic/Preparation/Utility/Value/ValueInterface/IMyTimer.cs diff --git a/logic/GameClass/GameObj/Map/Map.cs b/logic/GameClass/GameObj/Map/Map.cs index 201b0b21..e8ed31aa 100755 --- a/logic/GameClass/GameObj/Map/Map.cs +++ b/logic/GameClass/GameObj/Map/Map.cs @@ -19,6 +19,10 @@ public partial class Map : IMap public readonly PlaceType[,] protoGameMap; public PlaceType[,] ProtoGameMap => protoGameMap; + // xfgg说:爱因斯坦说,每个坐标系都有与之绑定的时钟,(x, y, z, ict) 构成四维时空坐标,在洛伦兹变换下满足矢量性(狗头) + private readonly MyTimer timer = new(); + public IMyTimer Timer => timer; + #region 大本营相关 public List Homes { get; } private readonly long currentHomeNum = 0; diff --git a/logic/GameClass/GameObj/Map/MapGameTimer.cs b/logic/GameClass/GameObj/Map/MapGameTimer.cs deleted file mode 100755 index f9a92db6..00000000 --- a/logic/GameClass/GameObj/Map/MapGameTimer.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Preparation.Utility; -using System; -using System.Threading; -using ITimer = Preparation.Interface.ITimer; - -namespace GameClass.GameObj -{ - public partial class Map - { - // xfgg说:爱因斯坦说,每个坐标系都有与之绑定的时钟,(x, y, z, ict) 构成四维时空坐标,在洛伦兹变换下满足矢量性(狗头) - private readonly GameTimer timer = new(); - public ITimer Timer => timer; - - public class GameTimer : ITimer - { - private long startTime; - public int nowTime() => (int)(Environment.TickCount64 - startTime); - - private readonly AtomicBool isGaming = new(false); - public AtomicBool IsGaming => isGaming; - - public bool StartGame(int timeInMilliseconds) - { - if (!IsGaming.TrySet(true)) - return false; - startTime = Environment.TickCount64; - Thread.Sleep(timeInMilliseconds); - IsGaming.SetROri(false); - return true; - } - } - } -} diff --git a/logic/GameEngine/MoveEngine.cs b/logic/GameEngine/MoveEngine.cs index 8d43c19b..a5b7e25a 100755 --- a/logic/GameEngine/MoveEngine.cs +++ b/logic/GameEngine/MoveEngine.cs @@ -3,7 +3,6 @@ using System; using System.Threading; using Timothy.FrameRateTask; -using ITimer = Preparation.Interface.ITimer; namespace GameEngine { @@ -35,7 +34,7 @@ public enum AfterCollision Destroyed = 2 // 物体已经毁坏 } - private readonly ITimer gameTimer = gameMap.Timer; + private readonly IMyTimer gameTimer = gameMap.Timer; private readonly Action EndMove = EndMove; public IGameObj? CheckCollision(IMovable obj, XY Pos) diff --git a/logic/Gaming/Game.cs b/logic/Gaming/Game.cs index be2506dc..aedda949 100755 --- a/logic/Gaming/Game.cs +++ b/logic/Gaming/Game.cs @@ -115,16 +115,7 @@ public bool StartGame(int milliSeconds) actionManager.AddMoneyNaturally(team); ActivateShip(team.TeamID, ShipType.CivilShip); } - new Thread - ( - () => - { - if (!gameMap.Timer.StartGame(milliSeconds)) - return; - EndGame(); // 游戏结束时要做的事 - } - ) - { IsBackground = true }.Start(); + gameMap.Timer.Start(() => { }, () => EndGame(), milliSeconds); return true; } public void EndGame() diff --git a/logic/Preparation/Interface/IMap.cs b/logic/Preparation/Interface/IMap.cs index 6a4cf884..84d8f166 100755 --- a/logic/Preparation/Interface/IMap.cs +++ b/logic/Preparation/Interface/IMap.cs @@ -5,7 +5,7 @@ namespace Preparation.Interface { public interface IMap { - ITimer Timer { get; } + IMyTimer Timer { get; } // the two dicts must have same keys Dictionary> GameObjDict { get; } diff --git a/logic/Preparation/Interface/ITimer.cs b/logic/Preparation/Interface/ITimer.cs deleted file mode 100755 index bbcaee20..00000000 --- a/logic/Preparation/Interface/ITimer.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Preparation.Utility; - -namespace Preparation.Interface -{ - public interface ITimer - { - AtomicBool IsGaming { get; } - public int nowTime(); - public bool StartGame(int timeInMilliseconds); - } -} diff --git a/logic/Preparation/Utility/Value/SafeValue/MyTimer.cs b/logic/Preparation/Utility/Value/SafeValue/MyTimer.cs new file mode 100644 index 00000000..1a7be738 --- /dev/null +++ b/logic/Preparation/Utility/Value/SafeValue/MyTimer.cs @@ -0,0 +1,30 @@ +using System; +using System.Threading; + +namespace Preparation.Utility +{ + public class MyTimer : IMyTimer + { + private readonly AtomicLong startTime = new(long.MaxValue); + public int NowTime() => (int)(Environment.TickCount64 - startTime); + public bool IsGaming => startTime != long.MaxValue; + + public bool Start(Action start, Action end, int timeInMilliseconds) + { + start(); + if (startTime.CompareExROri(Environment.TickCount64, long.MaxValue) != long.MaxValue) + return false; + new Thread + ( + () => + { + Thread.Sleep(timeInMilliseconds); + startTime.SetROri(long.MaxValue); + end(); + } + ) + { IsBackground = true }.Start(); + return true; + } + } +} diff --git a/logic/Preparation/Utility/Value/ValueInterface/IMyTimer.cs b/logic/Preparation/Utility/Value/ValueInterface/IMyTimer.cs new file mode 100644 index 00000000..7294083c --- /dev/null +++ b/logic/Preparation/Utility/Value/ValueInterface/IMyTimer.cs @@ -0,0 +1,11 @@ +using System; + +namespace Preparation.Utility +{ + public interface IMyTimer + { + bool IsGaming { get; } + public int NowTime(); + public bool Start(Action start, Action end, int timeInMilliseconds); + } +} diff --git a/logic/Server/GameServer.cs b/logic/Server/GameServer.cs index 4ee277b8..6bfd1717 100755 --- a/logic/Server/GameServer.cs +++ b/logic/Server/GameServer.cs @@ -155,7 +155,7 @@ public void ReportGame(GameState gameState, bool requiredGaming = true) currentNews.Clear(); } currentGameInfo.GameState = gameState; - currentGameInfo.AllMessage = GetMessageOfAll(game.GameMap.Timer.nowTime()); + currentGameInfo.AllMessage = GetMessageOfAll(game.GameMap.Timer.NowTime()); mwr?.WriteOne(currentGameInfo); break; default: From 5f34135c6f851c0212927b052ddd1ee913af7ecc Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Sat, 27 Apr 2024 20:59:40 +0800 Subject: [PATCH 2/5] feat(SafeValue): :sparkles: MyTimer add a method --- .../Utility/Value/SafeValue/MyTimer.cs | 59 +++++++++++++++---- .../Utility/Value/ValueInterface/IAddable.cs | 2 +- .../Utility/Value/ValueInterface/IDouble.cs | 2 +- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/logic/Preparation/Utility/Value/SafeValue/MyTimer.cs b/logic/Preparation/Utility/Value/SafeValue/MyTimer.cs index 1a7be738..921554b0 100644 --- a/logic/Preparation/Utility/Value/SafeValue/MyTimer.cs +++ b/logic/Preparation/Utility/Value/SafeValue/MyTimer.cs @@ -1,7 +1,8 @@ -using System; +using Preparation.Utility.Value.SafeValue.Atomic; +using System; using System.Threading; -namespace Preparation.Utility +namespace Preparation.Utility.Value.SafeValue { public class MyTimer : IMyTimer { @@ -9,21 +10,55 @@ public class MyTimer : IMyTimer public int NowTime() => (int)(Environment.TickCount64 - startTime); public bool IsGaming => startTime != long.MaxValue; + public bool Start(Action start, Action endBefore, Action endAfter, int timeInMilliseconds) + { + start(); + if (startTime.CompareExROri(Environment.TickCount64, long.MaxValue) != long.MaxValue) + return false; + try + { + new Thread + ( + () => + { + Thread.Sleep(timeInMilliseconds); + endBefore(); + startTime.SetROri(long.MaxValue); + endAfter(); + } + ) + { IsBackground = true }.Start(); + } + catch (Exception ex) + { + startTime.SetROri(long.MaxValue); + Console.WriteLine(ex.Message); + } + return true; + } public bool Start(Action start, Action end, int timeInMilliseconds) { start(); if (startTime.CompareExROri(Environment.TickCount64, long.MaxValue) != long.MaxValue) return false; - new Thread - ( - () => - { - Thread.Sleep(timeInMilliseconds); - startTime.SetROri(long.MaxValue); - end(); - } - ) - { IsBackground = true }.Start(); + try + { + new Thread + ( + () => + { + Thread.Sleep(timeInMilliseconds); + startTime.SetROri(long.MaxValue); + end(); + } + ) + { IsBackground = true }.Start(); + } + catch (Exception ex) + { + startTime.SetROri(long.MaxValue); + Console.WriteLine(ex.Message); + } return true; } } diff --git a/logic/Preparation/Utility/Value/ValueInterface/IAddable.cs b/logic/Preparation/Utility/Value/ValueInterface/IAddable.cs index 81c125cc..175eb6d8 100644 --- a/logic/Preparation/Utility/Value/ValueInterface/IAddable.cs +++ b/logic/Preparation/Utility/Value/ValueInterface/IAddable.cs @@ -1,4 +1,4 @@ -namespace Preparation.Interface +namespace Preparation.Utility { public interface IAddable { diff --git a/logic/Preparation/Utility/Value/ValueInterface/IDouble.cs b/logic/Preparation/Utility/Value/ValueInterface/IDouble.cs index f6ca8639..a2c2f206 100644 --- a/logic/Preparation/Utility/Value/ValueInterface/IDouble.cs +++ b/logic/Preparation/Utility/Value/ValueInterface/IDouble.cs @@ -1,4 +1,4 @@ -namespace Preparation.Interface +namespace Preparation.Utility { public interface IDouble { From 7ee04796a584c8b2aeda7e191e723d254c2b8dd3 Mon Sep 17 00:00:00 2001 From: Grange <2634070476@qq.com> Date: Sat, 27 Apr 2024 21:40:42 +0800 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20fix=20resource=20coor?= =?UTF-8?q?dination?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/Client/Util/UtilFunctions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logic/Client/Util/UtilFunctions.cs b/logic/Client/Util/UtilFunctions.cs index c88ec57e..01c6a66e 100644 --- a/logic/Client/Util/UtilFunctions.cs +++ b/logic/Client/Util/UtilFunctions.cs @@ -34,7 +34,7 @@ public static int getCellIndex(int i, int j) } public static int getGridIndex(float i, float j) { - return 50 * (49 - (int)i / 1000) + (49 - (int)j / 1000); + return 50 * ((int)i / 1000) + ((int)j / 1000); } public static PointF Grid2CellPoint(float i, float j) From 1bbda177a6204d1170ae6b47da40f12e973e7ddb Mon Sep 17 00:00:00 2001 From: Grange <2634070476@qq.com> Date: Sat, 27 Apr 2024 21:41:13 +0800 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20fix=20ship=20list=20c?= =?UTF-8?q?an't=20delete=20ships?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logic/Client/View/MainPage.xaml | 4 +- logic/Client/ViewModel/GeneralViewModel.cs | 97 +++++++++++++++++++--- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/logic/Client/View/MainPage.xaml b/logic/Client/View/MainPage.xaml index 59489b0e..53e26dd4 100644 --- a/logic/Client/View/MainPage.xaml +++ b/logic/Client/View/MainPage.xaml @@ -86,7 +86,7 @@