From 6fa5c35c49b8718dbc98913c62c07e557e6b1302 Mon Sep 17 00:00:00 2001 From: Byte-Nova <159603018+Byte-Nova@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:20:28 +0100 Subject: [PATCH] Fixes to maps and caravans --- Source/Client/Managers/CaravanManager.cs | 2 + Source/Server/Managers/CaravanManager.cs | 53 +++++++++++------------- Source/Server/Managers/MapManager.cs | 17 +++++--- Source/Server/Managers/SaveManager.cs | 24 ++++++----- 4 files changed, 53 insertions(+), 43 deletions(-) diff --git a/Source/Client/Managers/CaravanManager.cs b/Source/Client/Managers/CaravanManager.cs index 4d392ec0..7d2b44e0 100644 --- a/Source/Client/Managers/CaravanManager.cs +++ b/Source/Client/Managers/CaravanManager.cs @@ -14,7 +14,9 @@ public static class CaravanManager //Variables public static WorldObjectDef onlineCaravanDef; + public static List activeCaravans = new List(); + public static Dictionary activePlayerCaravans = new Dictionary(); public static void ParsePacket(Packet packet) diff --git a/Source/Server/Managers/CaravanManager.cs b/Source/Server/Managers/CaravanManager.cs index f261de6d..721b4b1d 100644 --- a/Source/Server/Managers/CaravanManager.cs +++ b/Source/Server/Managers/CaravanManager.cs @@ -20,20 +20,20 @@ public static void ParsePacket(ServerClient client, Packet packet) switch (data._stepMode) { case CaravanStepMode.Add: - AddCaravan(client, data); + AddCaravan(client.userFile.Username, data); break; case CaravanStepMode.Remove: - RemoveCaravan(client, data); + RemoveCaravan(client.userFile.Username, data._caravanFile); break; case CaravanStepMode.Move: - MoveCaravan(client, data); + MoveCaravan(client, data._caravanFile); break; } } - private static void AddCaravan(ServerClient client, CaravanData data) + private static void AddCaravan(string username, CaravanData data) { data._caravanFile.ID = CaravanManagerHelper.GetNewCaravanID(); RefreshCaravanTimer(data._caravanFile); @@ -41,32 +41,40 @@ private static void AddCaravan(ServerClient client, CaravanData data) Packet packet = Packet.CreatePacketFromObject(nameof(CaravanManager), data); NetworkHelper.SendPacketToAllClients(packet); - Logger.Message($"[Add Caravan] > {data._caravanFile.ID} > {client.userFile.Username}"); + Logger.Message($"[Add Caravan] > {data._caravanFile.ID} > {username}"); } - private static void RemoveCaravan(ServerClient client, CaravanData data) + public static void RemoveCaravan(string username, CaravanFile file) { - CaravanFile toRemove = CaravanManagerHelper.GetCaravanFromID(client, data._caravanFile.ID); + CaravanFile toRemove = CaravanManagerHelper.GetCaravanFromID(username, file.ID); if (toRemove == null) return; else { - DeleteCaravan(data._caravanFile); + DeleteCaravan(file); + + CaravanData data = new CaravanData(); + data._stepMode = CaravanStepMode.Remove; + data._caravanFile = file; Packet packet = Packet.CreatePacketFromObject(nameof(CaravanManager), data); NetworkHelper.SendPacketToAllClients(packet); - Logger.Message($"[Remove Caravan] > {data._caravanFile.ID} > {client.userFile.Username}"); + Logger.Message($"[Remove Caravan] > {file.ID} > {username}"); } } - private static void MoveCaravan(ServerClient client, CaravanData data) + private static void MoveCaravan(ServerClient client, CaravanFile file) { - CaravanFile toMove = CaravanManagerHelper.GetCaravanFromID(client, data._caravanFile.ID); - if (toMove == null) return; + CaravanFile existingCaravan = CaravanManagerHelper.GetCaravanFromID(client.userFile.Username, file.ID); + if (existingCaravan == null) return; else { - UpdateCaravan(toMove, data._caravanFile); - RefreshCaravanTimer(data._caravanFile); + UpdateCaravan(existingCaravan, file); + RefreshCaravanTimer(file); + + CaravanData data = new CaravanData(); + data._stepMode = CaravanStepMode.Move; + data._caravanFile = file; Packet packet = Packet.CreatePacketFromObject(nameof(CaravanManager), data); NetworkHelper.SendPacketToAllClients(packet, client); @@ -140,13 +148,10 @@ public static CaravanFile[] GetActiveCaravans() return activeCaravans.ToArray(); } - public static CaravanFile GetCaravanFromID(ServerClient client, int caravanID) + public static CaravanFile GetCaravanFromID(string username, int caravanID) { - CaravanFile toGet = GetActiveCaravans().FirstOrDefault(fetch => fetch.ID == caravanID && - fetch.Owner == client.userFile.Username); - - if (toGet == null) return null; - else return toGet; + return GetActiveCaravans().FirstOrDefault(fetch => fetch.ID == caravanID && + fetch.Owner == username); } public static CaravanFile[] GetCaravansFromOwner(string userName) @@ -157,14 +162,6 @@ public static CaravanFile[] GetCaravansFromOwner(string userName) else return toGet; } - public static CaravanFile GetCaravanFromOwner(string userName) - { - CaravanFile toGet = GetActiveCaravans().FirstOrDefault(fetch => fetch.Owner == userName); - - if (toGet == null) return null; - else return toGet; - } - public static int GetNewCaravanID() { int maxID = 0; diff --git a/Source/Server/Managers/MapManager.cs b/Source/Server/Managers/MapManager.cs index 5c5bff09..67dfa9fe 100644 --- a/Source/Server/Managers/MapManager.cs +++ b/Source/Server/Managers/MapManager.cs @@ -18,16 +18,19 @@ public static void SaveUserMap(ServerClient client, MapFile file) { string savingDirectory = Path.Combine(Master.mapsPath, client.userFile.Username); if (!Directory.Exists(savingDirectory)) Directory.CreateDirectory(savingDirectory); + + file.Owner = client.userFile.Username; Serializer.ObjectBytesToFile(Path.Combine(savingDirectory, file.Tile + fileExtension), file); Logger.Message($"[Save map] > {client.userFile.Username} > {file.Tile}"); } - public static void DeleteMap(string path) + public static void DeleteMap(MapFile mapFile) { - File.Delete(path); + string filePath = Path.Combine(Master.mapsPath, mapFile.Owner, mapFile.Tile + fileExtension); - Logger.Warning($"[Remove map] > {Path.GetFileNameWithoutExtension(path)}"); + File.Delete(filePath); + Logger.Warning($"[Remove map] > {Path.GetFileNameWithoutExtension(filePath)}"); } public static string[] GetAllMaps() @@ -42,9 +45,13 @@ public static bool CheckIfMapExists(int mapTileToCheck) else return false; } - public static string[] GetAllMapsFromUsername(string username) + public static MapFile[] GetAllMapsFromUsername(string username) { - return Directory.GetFiles(Path.Combine(Master.mapsPath, username)); + List allUserMaps = new List(); + string[] allMapPaths = Directory.GetFiles(Path.Combine(Master.mapsPath, username)); + foreach (string str in allMapPaths) allUserMaps.Add(Serializer.FileBytesToObject(str)); + + return allUserMaps.ToArray(); } public static MapFile GetUserMapFromTile(string username, int mapTileToGet) diff --git a/Source/Server/Managers/SaveManager.cs b/Source/Server/Managers/SaveManager.cs index e6e892d3..7c652ad7 100644 --- a/Source/Server/Managers/SaveManager.cs +++ b/Source/Server/Managers/SaveManager.cs @@ -139,30 +139,34 @@ public static void ResetPlayerData(ServerClient client, string username) if (client != null) client.listener.disconnectFlag = true; - //Delete save file + // Delete save file try { File.Delete(Path.Combine(Master.savesPath, username + fileExtension)); } catch { Logger.Warning($"Failed to find {username}'s save"); } - //Delete map files - string[] userMaps = MapManager.GetAllMapsFromUsername(username); - foreach (string str in userMaps) MapManager.DeleteMap(str); + // Delete map files + MapFile[] userMaps = MapManager.GetAllMapsFromUsername(username); + foreach (MapFile map in userMaps) MapManager.DeleteMap(map); - //Delete site files + // Delete caravan files + CaravanFile[] userCaravans = CaravanManagerHelper.GetCaravansFromOwner(username); + foreach (CaravanFile caravan in userCaravans) CaravanManager.RemoveCaravan(username, caravan); + + // Delete site files SiteIdendityFile[] playerSites = SiteManagerHelper.GetAllSitesFromUsername(username); foreach (SiteIdendityFile site in playerSites) SiteManager.DestroySiteFromFile(site); - //Delete settlement files + // Delete settlement files SettlementFile[] playerSettlements = PlayerSettlementManager.GetAllSettlementsFromUsername(username); - foreach (SettlementFile settlementFile in playerSettlements) + foreach (SettlementFile settlement in playerSettlements) { PlayerSettlementData settlementData = new PlayerSettlementData(); - settlementData._settlementData.Tile = settlementFile.Tile; - settlementData._settlementData.Owner = settlementFile.Owner; + settlementData._settlementData.Tile = settlement.Tile; + settlementData._settlementData.Owner = settlement.Owner; PlayerSettlementManager.RemoveSettlement(client, settlementData); } - Logger.Warning($"[Reseted player data] > {username}"); + Logger.Warning($"[Reset player data] > {username}"); } } }