Skip to content

Commit

Permalink
Fixes to maps and caravans
Browse files Browse the repository at this point in the history
  • Loading branch information
Byte-Nova committed Nov 2, 2024
1 parent 4f61c59 commit 6fa5c35
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 43 deletions.
2 changes: 2 additions & 0 deletions Source/Client/Managers/CaravanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public static class CaravanManager
//Variables

public static WorldObjectDef onlineCaravanDef;

public static List<CaravanFile> activeCaravans = new List<CaravanFile>();

public static Dictionary<Caravan, int> activePlayerCaravans = new Dictionary<Caravan, int>();

public static void ParsePacket(Packet packet)
Expand Down
53 changes: 25 additions & 28 deletions Source/Server/Managers/CaravanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,61 @@ 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);

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);
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down
17 changes: 12 additions & 5 deletions Source/Server/Managers/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<MapFile> allUserMaps = new List<MapFile>();
string[] allMapPaths = Directory.GetFiles(Path.Combine(Master.mapsPath, username));
foreach (string str in allMapPaths) allUserMaps.Add(Serializer.FileBytesToObject<MapFile>(str));

return allUserMaps.ToArray();
}

public static MapFile GetUserMapFromTile(string username, int mapTileToGet)
Expand Down
24 changes: 14 additions & 10 deletions Source/Server/Managers/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
}

0 comments on commit 6fa5c35

Please sign in to comment.