From 35ba3a9cbeb529aa5b4b3024e51fe70338acf66f Mon Sep 17 00:00:00 2001 From: Ed Kolis Date: Sun, 20 Oct 2024 15:10:10 -0400 Subject: [PATCH] 326 - DI for ResearchCommand (#328) --- .../Gameplay/Commands/ICommand.cs | 2 +- .../Gameplay/Commands/ICommandFactory.cs | 16 ----------- .../Projects/IProjectCommandFactory.cs | 19 +++++++++++++ .../Commands/Projects/IResearchCommand.cs | 28 +++++++++++++++++++ .../Objects/Civilization/Empire.cs | 5 ++-- FrEee.Core.Domain/Utility/DIRoot.cs | 6 ++++ .../Projects/ProjectCommandFactory.cs | 21 ++++++++++++++ .../Commands/Projects}/ResearchCommand.cs | 14 ++++------ FrEee.Root/Configuration.cs | 2 ++ FrEee.Tests/FrEee.Tests.csproj | 1 + .../Objects/Technology/TechnologyTest.cs | 15 +++++----- FrEee.UI.WinForms/Forms/ResearchForm.cs | 2 +- 12 files changed, 95 insertions(+), 36 deletions(-) delete mode 100644 FrEee.Core.Domain/Gameplay/Commands/ICommandFactory.cs create mode 100644 FrEee.Core.Domain/Gameplay/Commands/Projects/IProjectCommandFactory.cs create mode 100644 FrEee.Core.Domain/Gameplay/Commands/Projects/IResearchCommand.cs create mode 100644 FrEee.Gameplay/Commands/Projects/ProjectCommandFactory.cs rename {FrEee.Core.Domain/Gameplay/Commands => FrEee.Gameplay/Commands/Projects}/ResearchCommand.cs (79%) diff --git a/FrEee.Core.Domain/Gameplay/Commands/ICommand.cs b/FrEee.Core.Domain/Gameplay/Commands/ICommand.cs index 8aec9959..0c9a051a 100644 --- a/FrEee.Core.Domain/Gameplay/Commands/ICommand.cs +++ b/FrEee.Core.Domain/Gameplay/Commands/ICommand.cs @@ -17,7 +17,7 @@ public interface ICommand : IPromotable /// /// The empire issuing the command. /// - Empire Issuer { get; } + Empire Issuer { get; set; } /// /// Any new (from the client) objects referred to by this command. diff --git a/FrEee.Core.Domain/Gameplay/Commands/ICommandFactory.cs b/FrEee.Core.Domain/Gameplay/Commands/ICommandFactory.cs deleted file mode 100644 index f7da85f1..00000000 --- a/FrEee.Core.Domain/Gameplay/Commands/ICommandFactory.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FrEee.Gameplay.Commands; - -/// -/// Builds various types of used by the game. -/// -public interface ICommandFactory -{ - // TODO: split these out into separate factories for each category and add parameters - ICommand Research(); -} \ No newline at end of file diff --git a/FrEee.Core.Domain/Gameplay/Commands/Projects/IProjectCommandFactory.cs b/FrEee.Core.Domain/Gameplay/Commands/Projects/IProjectCommandFactory.cs new file mode 100644 index 00000000..22b623c3 --- /dev/null +++ b/FrEee.Core.Domain/Gameplay/Commands/Projects/IProjectCommandFactory.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FrEee.Gameplay.Commands.Projects; + +/// +/// Builds commands for empire wide projects such as research and espionage. +/// +public interface IProjectCommandFactory +{ + /// + /// Creates an to assign research spending. + /// + /// + IResearchCommand Research(); +} \ No newline at end of file diff --git a/FrEee.Core.Domain/Gameplay/Commands/Projects/IResearchCommand.cs b/FrEee.Core.Domain/Gameplay/Commands/Projects/IResearchCommand.cs new file mode 100644 index 00000000..79b5a491 --- /dev/null +++ b/FrEee.Core.Domain/Gameplay/Commands/Projects/IResearchCommand.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FrEee.Modding; +using FrEee.Objects.Civilization; +using FrEee.Objects.Technology; + +namespace FrEee.Gameplay.Commands.Projects; + +/// +/// Command which assigns research spending for an empire. +/// +public interface IResearchCommand + : ICommand +{ + /// + /// List of technologies to research in order. + /// If percentage spending weights total to less than 100, the queue will get the remaining points. + /// + ModReferenceList Queue { get; } + + /// + /// Percentage spending weights for technologies. + /// + ModReferenceKeyedDictionary Spending { get; } +} diff --git a/FrEee.Core.Domain/Objects/Civilization/Empire.cs b/FrEee.Core.Domain/Objects/Civilization/Empire.cs index 03fa144d..d33f09e5 100644 --- a/FrEee.Core.Domain/Objects/Civilization/Empire.cs +++ b/FrEee.Core.Domain/Objects/Civilization/Empire.cs @@ -23,6 +23,7 @@ using FrEee.Processes.Setup; using FrEee.Gameplay.Commands; using FrEee.Gameplay.Commands.Orders; +using FrEee.Gameplay.Commands.Projects; namespace FrEee.Objects.Civilization; @@ -582,11 +583,11 @@ public ResourceQuantity RemoteMiningIncome } } - public ResearchCommand ResearchCommand + public IResearchCommand ResearchCommand { get { - return Commands.OfType().SingleOrDefault(); + return Commands.OfType().SingleOrDefault(); } set { diff --git a/FrEee.Core.Domain/Utility/DIRoot.cs b/FrEee.Core.Domain/Utility/DIRoot.cs index f94728a5..dace8242 100644 --- a/FrEee.Core.Domain/Utility/DIRoot.cs +++ b/FrEee.Core.Domain/Utility/DIRoot.cs @@ -9,6 +9,7 @@ using FrEee.Gameplay.Commands.Ministers; using FrEee.Gameplay.Commands.Notes; using FrEee.Gameplay.Commands.Orders; +using FrEee.Gameplay.Commands.Projects; using FrEee.Gameplay.Commands.Waypoints; using FrEee.Processes; using FrEee.Processes.Combat; @@ -60,6 +61,11 @@ public static class DIRoot /// public static IOrderCommandFactory OrderCommands => DI.Get(); + /// + /// Allows players to manage empire wide projects such as research and espionage. + /// + public static IProjectCommandFactory ProjectCommands => DI.Get(); + /// /// Allows players to manage waypoints. /// diff --git a/FrEee.Gameplay/Commands/Projects/ProjectCommandFactory.cs b/FrEee.Gameplay/Commands/Projects/ProjectCommandFactory.cs new file mode 100644 index 00000000..63a56edb --- /dev/null +++ b/FrEee.Gameplay/Commands/Projects/ProjectCommandFactory.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FrEee.Gameplay.Commands.Notes; +using FrEee.Objects.Civilization; +using FrEee.Objects.Civilization.Orders; +using FrEee.Objects.GameState; +using FrEee.Objects.Vehicles; + +namespace FrEee.Gameplay.Commands.Projects; + +public class ProjectCommandFactory + : IProjectCommandFactory +{ + public IResearchCommand Research() + { + return new ResearchCommand(); + } +} diff --git a/FrEee.Core.Domain/Gameplay/Commands/ResearchCommand.cs b/FrEee.Gameplay/Commands/Projects/ResearchCommand.cs similarity index 79% rename from FrEee.Core.Domain/Gameplay/Commands/ResearchCommand.cs rename to FrEee.Gameplay/Commands/Projects/ResearchCommand.cs index f803db2b..1dc91794 100644 --- a/FrEee.Core.Domain/Gameplay/Commands/ResearchCommand.cs +++ b/FrEee.Gameplay/Commands/Projects/ResearchCommand.cs @@ -3,24 +3,22 @@ using System.Linq; using Tech = FrEee.Objects.Technology.Technology; using FrEee.Modding; -using FrEee.Objects.Technology; -namespace FrEee.Gameplay.Commands; +namespace FrEee.Gameplay.Commands.Projects; /// /// Command to set an empire's research priorities. /// -public class ResearchCommand : Command +public class ResearchCommand + : Command, IResearchCommand { public ResearchCommand() : base(Empire.Current) { - Spending = new ModReferenceKeyedDictionary(); - Queue = new ModReferenceList(); } - public ModReferenceList Queue { get; private set; } - public ModReferenceKeyedDictionary Spending { get; private set; } + public ModReferenceList Queue { get; private set; } = new(); + public ModReferenceKeyedDictionary Spending { get; private set; } = new(); public override void Execute() { @@ -40,7 +38,7 @@ public override void Execute() if (!Executor.HasUnlocked(kvp.Key)) Spending[kvp.Key] = 0; } - foreach (Technology tech in Queue.ToArray()) + foreach (Tech tech in Queue.ToArray()) { if (!Executor.HasUnlocked(tech)) Queue.Remove(tech); diff --git a/FrEee.Root/Configuration.cs b/FrEee.Root/Configuration.cs index 7ec60d0f..51dfc4c8 100644 --- a/FrEee.Root/Configuration.cs +++ b/FrEee.Root/Configuration.cs @@ -9,6 +9,7 @@ using FrEee.Gameplay.Commands.Ministers; using FrEee.Gameplay.Commands.Notes; using FrEee.Gameplay.Commands.Orders; +using FrEee.Gameplay.Commands.Projects; using FrEee.Gameplay.Commands.Waypoints; using FrEee.Processes; using FrEee.Processes.Combat; @@ -38,6 +39,7 @@ public static void ConfigureDI() DI.RegisterSingleton(); DI.RegisterSingleton(); DI.RegisterSingleton(); + DI.RegisterSingleton(); DI.RegisterSingleton(); // run this in the background, without awaiting it diff --git a/FrEee.Tests/FrEee.Tests.csproj b/FrEee.Tests/FrEee.Tests.csproj index e2d7912f..9905432a 100644 --- a/FrEee.Tests/FrEee.Tests.csproj +++ b/FrEee.Tests/FrEee.Tests.csproj @@ -15,6 +15,7 @@ + diff --git a/FrEee.Tests/Objects/Technology/TechnologyTest.cs b/FrEee.Tests/Objects/Technology/TechnologyTest.cs index ce84330d..c0de20f0 100644 --- a/FrEee.Tests/Objects/Technology/TechnologyTest.cs +++ b/FrEee.Tests/Objects/Technology/TechnologyTest.cs @@ -1,10 +1,12 @@ using FrEee.Extensions; using FrEee.Gameplay.Commands; +using FrEee.Gameplay.Commands.Projects; using FrEee.Modding; using FrEee.Modding.Loaders; using FrEee.Objects.Civilization; using FrEee.Objects.GameState; using FrEee.Processes; +using FrEee.Root; using FrEee.Utility; using NUnit.Framework; @@ -22,8 +24,7 @@ public class TechnologyTest [OneTimeSetUp] public static void ClassInit() { - DI.RegisterSingleton(); - DI.Run(); + Configuration.ConfigureDI(); processor = DIRoot.TurnProcessor; new ModLoader().Load(null); } @@ -46,7 +47,7 @@ public void PercentageResearch() emp.ResearchedTechnologies[tech] = 0; emp.BonusResearch = tech.GetBaseLevelCost(1) + tech.GetBaseLevelCost(2); - var cmd = new ResearchCommand(); + var cmd = DIRoot.ProjectCommands.Research(); cmd.Issuer = emp; cmd.Executor = emp; cmd.Spending[tech] = 100; @@ -85,11 +86,9 @@ public void QueuedResearch() emp.BonusResearch = t1.GetBaseLevelCost(1) + 1; // create research command - var cmd = new ResearchCommand - { - Issuer = emp, - Executor = emp, - }; + var cmd = DIRoot.ProjectCommands.Research(); + cmd.Issuer = emp; + cmd.Executor = emp; cmd.Queue.Add(t1); cmd.Queue.Add(t2); diff --git a/FrEee.UI.WinForms/Forms/ResearchForm.cs b/FrEee.UI.WinForms/Forms/ResearchForm.cs index 10e6ec38..22b4f38a 100644 --- a/FrEee.UI.WinForms/Forms/ResearchForm.cs +++ b/FrEee.UI.WinForms/Forms/ResearchForm.cs @@ -418,7 +418,7 @@ private void ResearchForm_MouseEnter(object sender, EventArgs e) private void Save() { - var cmd = new ResearchCommand(); + var cmd = DIRoot.ProjectCommands.Research(); cmd.Spending.Clear(); foreach (var kvp in Empire.Current.ResearchSpending) cmd.Spending[kvp.Key] = kvp.Value;