fix: refactor GoGump, moves LocationTree to GoLocations (#1804)

This commit is contained in:
Daniel Dias Rodrigues 2024-06-08 23:59:31 -03:00 • committed by GitHub
parent 75a3f0a92c
commit 9847efbb1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 350 additions and 335 deletions

View file

@ -1,65 +1,43 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Server.Json;
namespace Server.Gumps
namespace Server.Gumps;
public class LocationTree
{
public class LocationTree
public LocationTree(Map map, Dictionary<Mobile, GoCategory> lastBranch, GoCategory root)
{
public LocationTree(string fileName, Map map)
LastBranch = lastBranch;
Map = map;
Root = root;
if (root != null)
{
LastBranch = new Dictionary<Mobile, GoCategory>();
Map = map;
SetParents(root);
}
}
var path = Path.Combine($"Data/Locations/{fileName}.json");
public Dictionary<Mobile, GoCategory> LastBranch { get; }
if (!File.Exists(path))
{
Console.WriteLine("Go Locations: {0} does not exist", path);
return;
}
public Map Map { get; }
try
{
Root = JsonConfig.Deserialize<GoCategory>(path);
if (Root == null)
{
throw new JsonException($"Failed to deserialize {path}.");
}
SetParents(Root);
}
catch (Exception e)
{
Console.WriteLine("Go Locations: Error in deserializing {0}", path);
Console.WriteLine(e);
}
public GoCategory Root { get; }
private static void SetParents(GoCategory parent)
{
// Deserialization may leave these null
parent.Categories ??= [];
parent.Locations ??= [];
for (var i = 0; i < parent.Categories.Length; i++)
{
var category = parent.Categories[i];
category.Parent = parent;
SetParents(category);
}
public Dictionary<Mobile, GoCategory> LastBranch { get; }
public Map Map { get; }
public GoCategory Root { get; }
private static void SetParents(GoCategory parent)
for (var j = 0; j < parent.Locations.Length; j++)
{
// Deserialization may leave these null
parent.Categories ??= Array.Empty<GoCategory>();
parent.Locations ??= Array.Empty<GoLocation>();
for (var i = 0; i < parent.Categories.Length; i++)
{
var category = parent.Categories[i];
category.Parent = parent;
SetParents(category);
}
for (var j = 0; j < parent.Locations.Length; j++)
{
parent.Locations[j].Parent = parent;
}
parent.Locations[j].Parent = parent;
}
}
}