diff --git a/tests/Aetheria.Shared.Tests/EconomyRescaleTests.cs b/tests/Aetheria.Shared.Tests/EconomyRescaleTests.cs deleted file mode 100644 index 9bb9ce8dd..000000000 --- a/tests/Aetheria.Shared.Tests/EconomyRescaleTests.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using GameCult.Caching; -using Newtonsoft.Json; -using Xunit; - -// The one-time price transform (aetheria-living-world, cut gear-money-rescale), on a fixture game folder. Deleted with the command. -public sealed class EconomyRescaleTests : IDisposable -{ - private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-rescale-" + Guid.NewGuid().ToString("N")); - private string Catalog => Path.Combine(_root, "GameData", "Aetheria.cc"); - private string Package => Path.Combine(_root, "GameData", "Mods", "aetheria.wren", "ship.cc"); - - public void Dispose() => Directory.Delete(_root, true); - - // A catalog with a commodity, a gun, a hull, a cargo bay and a consumable at prices that exercise rounding, and the Wren package (6000). - private void Build() - { - Directory.CreateDirectory(Path.GetDirectoryName(Package)); - Directory.CreateDirectory(Path.GetDirectoryName(Catalog)); - File.Copy(Path.Combine(RunStartTests.FindRepoRoot(), "tests", "fixtures", "mods", "aetheria.wren", "ship.cc"), Package); - using var f = EconomyFixture.Healthy(); - f.EditItem("commodity:ore", ore => ore.Price = 777); - f.EditItem("gear:laser", gear => gear.Price = 12345); - f.EditItem("hull:skiff", hull => hull.Price = 250); - f.Cache.Upsert(typeof(CargoBayData), new CargoBayData { Name = "Bay", Shape = new Shape(), InteriorShape = new Shape(), Price = 30 }, new CultRecordKey("bay:small")); - f.Cache.Upsert(typeof(ConsumableItemData), new ConsumableItemData { Name = "Patch", Shape = new Shape(), Price = 18000 }, new CultRecordKey("consumable:patch")); - f.Cache.FlushAsync().GetAwaiter().GetResult(); - f.Cache.Dispose(); - File.Copy(f.CatalogPath, Catalog); - } - - private (int Price, string Json) Item(string key) where T : ItemData - { - using var cache = AetheriaStores.Open(Catalog, registry: TestCatalog.Registry()); - var item = cache.Get(new CultRecordKey(key)); - return (item.Price, JsonConvert.SerializeObject(item)); - } - - [Fact] - public void GearConsumablesAndPackagesScaleAndCommoditiesDoNot() - { - Build(); - var catalog = File.ReadAllBytes(Catalog); - var package = File.ReadAllBytes(Package); - var output = new StringWriter(); - - Assert.Equal(0, EconomyRescale.Apply(_root, 100, null, false, output)); - Assert.Equal(catalog, File.ReadAllBytes(Catalog)); - Assert.Equal(package, File.ReadAllBytes(Package)); - Assert.Contains("gear:laser", output.ToString()); - - var ore = Item("commodity:ore"); - Assert.Equal(0, EconomyRescale.Apply(_root, 100, null, true, new StringWriter())); - Assert.Equal(123, Item("gear:laser").Price); // 123.45 - Assert.Equal(3, Item("hull:skiff").Price); // 2.5 rounds away from zero - Assert.Equal(1, Item("bay:small").Price); // 0.3 floors at 1 - Assert.Equal(180, Item("consumable:patch").Price); - Assert.Equal(ore, Item("commodity:ore")); // untouched, field for field - Assert.Equal(60, ShipAuthoringStore.Load(Package).Hull.Price); - } -} diff --git a/tools/AetherDb/EconomyCommand.cs b/tools/AetherDb/EconomyCommand.cs index 24743b12d..4470b25f1 100644 --- a/tools/AetherDb/EconomyCommand.cs +++ b/tools/AetherDb/EconomyCommand.cs @@ -53,7 +53,7 @@ public sealed class EconomyAuthor } } -// economy report | author ... [apply] | prices | rescale: the one writer of economy content and a reader of the catalog's economy report. +// economy report | author ... [apply] | prices: the one writer of economy content and a reader of the catalog's economy report. // EconomyChecks owns every rule; this command only calls it. public static class EconomyCommand { @@ -63,7 +63,7 @@ public static class EconomyCommand { EconomyChecks.LRecipe, EconomyChecks.LLore, EconomyChecks.LProductive, EconomyChecks.LOneKindList, EconomyChecks.LWeights }; // A batch is compiled code under tools/AetherDb/Economy/ and runs with the operator's authority: reviewed code, never an input. - private const string Usage = "economy report | author ... [apply] | " + EconomyPrices.Usage + " | " + EconomyRescale.Usage + "\nA batch is reviewed code: it runs on a trial copy and must write only through EconomyAuthor."; + private const string Usage = "economy report | author ... [apply] | " + EconomyPrices.Usage + "\nA batch is reviewed code: it runs on a trial copy and must write only through EconomyAuthor."; public static int Run(string[] args, TextWriter output) { @@ -73,7 +73,6 @@ public static class EconomyCommand return Report(db.Cache, output); } if (args.Length > 0 && args[0] == "prices") return EconomyPrices.Run(args.Skip(1).ToArray(), output); - if (args.Length > 0 && args[0] == "rescale") return EconomyRescale.Run(args.Skip(1).ToArray(), output); if (args.Length > 1 && args[0] == "author") { var apply = args.Contains("apply"); diff --git a/tools/AetherDb/EconomyRescale.cs b/tools/AetherDb/EconomyRescale.cs deleted file mode 100644 index ca04038d4..000000000 --- a/tools/AetherDb/EconomyRescale.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using GameCult.Caching; - -// economy rescale [--mods ] [apply]: one transform that divides every gear, consumable and package hull price by the -// divisor (aetheria-living-world, cut gear-money-rescale). Equippable and Consumable items are gear scale; commodities are already -// ore scale and are not touched. Each price becomes round(price / divisor), half away from zero, never below 1. The catalog goes -// through EconomyCommand.Author's trial copy and swap, the packages through ShipAuthoringStore Load and Write. The dry run prints -// every price that moves, read through EconomyPrices. Deleted in the commit after it applies. -public static class EconomyRescale -{ - public const string Usage = "economy rescale [--mods ] [apply]"; - - public static int Run(string[] args, TextWriter output) - { - var apply = args.Contains("apply"); - string mods = null; - var rest = new List(); - for (var i = 0; i < args.Length; i++) - { - if (args[i] == "apply") continue; - if (args[i] == "--mods" && i + 1 < args.Length) mods = args[++i]; - else rest.Add(args[i]); - } - if (rest.Count != 1 || !int.TryParse(rest[0], NumberStyles.None, CultureInfo.InvariantCulture, out var divisor) || divisor < 2) - { - output.WriteLine(Usage); - return 1; - } - return Apply(AetherDb.FindRoot(), divisor, mods, apply, output); - } - - // The transform on the game folder at root: dry run unless apply. - public static int Apply(string root, int divisor, string mods, bool apply, TextWriter output) - { - var catalogPath = Path.Combine(root, "GameData", "Aetheria.cc"); - mods ??= Path.Combine(root, "GameData", "Mods"); - - // The catalog: the transform runs on a scratch copy first, so the table below is what the real run writes. - var scratch = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "aetherdb-rescale-" + Guid.NewGuid().ToString("N"))).FullName; - try - { - var copy = Path.Combine(scratch, "Aetheria.cc"); - File.Copy(catalogPath, copy); - new FileInfo(copy).IsReadOnly = false; - var trialOutput = new StringWriter(); - if (EconomyCommand.Author(copy, new List { new Divide(divisor) }, true, trialOutput) != 0) - { - output.WriteLine(trialOutput.ToString()); - return 1; - } - using (var before = AetheriaStores.Open(catalogPath)) - using (var after = AetheriaStores.Open(copy)) - { - var was = EconomyPrices.Read(before, null); - var now = EconomyPrices.Read(after, null); - foreach (var (id, row) in was) - if (now[id].Price != row.Price) output.WriteLine($"{id}\t{row.Label}\t{row.Price}\t{now[id].Price}"); - } - } - finally { Directory.Delete(scratch, true); } - - var packages = Directory.Exists(mods) ? Directory.GetFiles(mods, "ship.cc", SearchOption.AllDirectories).OrderBy(p => p, StringComparer.Ordinal).ToArray() : new string[0]; - foreach (var path in packages) - { - var (hull, ship, _) = ShipAuthoringStore.Load(path); - output.WriteLine($"package:{ship.Id}\tPackage hull {hull.Name}\t{hull.Price}\t{Scale(hull.Price, divisor)}"); - } - if (!apply) - { - output.WriteLine("\nDry run. Pass \"apply\" to write Aetheria.cc and the packages. The purse is GameplaySettings.StartingCredits in Settings.cs."); - return 0; - } - var code = EconomyCommand.Author(catalogPath, new List { new Divide(divisor) }, true, TextWriter.Null); - if (code != 0) return code; - foreach (var path in packages) - { - var (hull, ship, _) = ShipAuthoringStore.Load(path); - hull.Price = Scale(hull.Price, divisor); - using var cache = ShipAuthoringStore.Open(path, writable: true); - ShipAuthoringStore.Write(cache, hull, ship); - cache.FlushAsync().Wait(); - } - output.WriteLine($"\nWrote Aetheria.cc and {packages.Length} package(s)."); - return 0; - } - - // Half away from zero, never below 1 for a priced item; an unpriced one stays unpriced. - private static int Scale(int price, int divisor) => - price <= 0 ? price : Math.Max(1, (int) Math.Round(price / (double) divisor, MidpointRounding.AwayFromZero)); - - private sealed class Divide : EconomyBatch - { - private readonly int _divisor; - public Divide(int divisor) => _divisor = divisor; - - public override void Author(EconomyAuthor author) - { - var catalog = author.Catalog; - foreach (var item in catalog.GetAll().Where(i => i is EquippableItemData || i is ConsumableItemData) - .OrderBy(i => catalog.RefOf(i).Key.Value, StringComparer.Ordinal).ToArray()) - { - item.Price = Scale(item.Price, _divisor); - // The record's own type, so the cache's write ownership by type holds. - author.Put((dynamic) item, catalog.RefOf(item).Key); - } - } - } -} diff --git a/tools/AetherDb/Program.cs b/tools/AetherDb/Program.cs index 92e7c5cdc..85e5b6088 100644 --- a/tools/AetherDb/Program.cs +++ b/tools/AetherDb/Program.cs @@ -81,7 +81,7 @@ public static class Program case "auto-guns": return AutoGunsCatalog.Run(AetherDb.Open(catalogWritable: args.Contains("apply")).Cache, args.Contains("apply"), Console.Out); case "doctrine-catalog": return DoctrineCatalog.Run(AetherDb.Open(catalogWritable: args.Contains("apply")).Cache, args.Contains("apply"), Console.Out); default: - Console.WriteLine("commands: census, factions, station-fit, hardpoint-fit, loadout [seed], loadout-commit [,,,[@]]... [--group ,...]... [--replace] [--boss-of ], save, settings, settings-dump, dangling [clear ]... [apply], shield-migrate [apply], brownout-migrate [apply], roles-migrate [apply], firing-arc-migrate [apply], range-falloff [apply] [--effective ] [--end ], targeting-catalog [apply], targeting-catalog-6c [apply], targeting-catalog-6d [apply], ship-authoring create|inspect|validate|compose, mine-launcher [apply], smoke-hull [apply], smoke-rounds [apply], roles-backfill [apply], pd-gear [apply], consumables-first-set [apply], launch-suppressor [apply], pirates-faction [apply], verse-grammar [apply], generic-designs [apply], sensor-stat-set [apply], inspection-scanner [apply], minor-power-factions [apply], place-names [apply], product-lines weapons|minor-powers|gear|tiers [apply], doctrine-catalog [apply], auto-guns [apply], assembly-from-maker [apply], station-kinds [hull \"\" ] [apply], economy report|prices|rescale|author ... [apply]"); + Console.WriteLine("commands: census, factions, station-fit, hardpoint-fit, loadout [seed], loadout-commit [,,,[@]]... [--group ,...]... [--replace] [--boss-of ], save, settings, settings-dump, dangling [clear ]... [apply], shield-migrate [apply], brownout-migrate [apply], roles-migrate [apply], firing-arc-migrate [apply], range-falloff [apply] [--effective ] [--end ], targeting-catalog [apply], targeting-catalog-6c [apply], targeting-catalog-6d [apply], ship-authoring create|inspect|validate|compose, mine-launcher [apply], smoke-hull [apply], smoke-rounds [apply], roles-backfill [apply], pd-gear [apply], consumables-first-set [apply], launch-suppressor [apply], pirates-faction [apply], verse-grammar [apply], generic-designs [apply], sensor-stat-set [apply], inspection-scanner [apply], minor-power-factions [apply], place-names [apply], product-lines weapons|minor-powers|gear|tiers [apply], doctrine-catalog [apply], auto-guns [apply], assembly-from-maker [apply], station-kinds [hull \"\" ] [apply], economy report|prices|author ... [apply]"); return 1; } }