Skip to content

Commit

Permalink
326 - DI for ResearchCommand (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekolis authored Oct 20, 2024
1 parent aa2e3c5 commit 35ba3a9
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 36 deletions.
2 changes: 1 addition & 1 deletion FrEee.Core.Domain/Gameplay/Commands/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface ICommand : IPromotable
/// <summary>
/// The empire issuing the command.
/// </summary>
Empire Issuer { get; }
Empire Issuer { get; set; }

/// <summary>
/// Any new (from the client) objects referred to by this command.
Expand Down
16 changes: 0 additions & 16 deletions FrEee.Core.Domain/Gameplay/Commands/ICommandFactory.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Builds commands for empire wide projects such as research and espionage.
/// </summary>
public interface IProjectCommandFactory
{
/// <summary>
/// Creates an <see cref="IResearchCommand"/> to assign research spending.
/// </summary>
/// <returns></returns>
IResearchCommand Research();
}
28 changes: 28 additions & 0 deletions FrEee.Core.Domain/Gameplay/Commands/Projects/IResearchCommand.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Command which assigns research spending for an empire.
/// </summary>
public interface IResearchCommand
: ICommand<Empire>
{
/// <summary>
/// List of technologies to research in order.
/// If percentage spending weights total to less than 100, the queue will get the remaining points.
/// </summary>
ModReferenceList<Technology> Queue { get; }

/// <summary>
/// Percentage spending weights for technologies.
/// </summary>
ModReferenceKeyedDictionary<Technology, int> Spending { get; }
}
5 changes: 3 additions & 2 deletions FrEee.Core.Domain/Objects/Civilization/Empire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -582,11 +583,11 @@ public ResourceQuantity RemoteMiningIncome
}
}

public ResearchCommand ResearchCommand
public IResearchCommand ResearchCommand
{
get
{
return Commands.OfType<ResearchCommand>().SingleOrDefault();
return Commands.OfType<IResearchCommand>().SingleOrDefault();
}
set
{
Expand Down
6 changes: 6 additions & 0 deletions FrEee.Core.Domain/Utility/DIRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,11 @@ public static class DIRoot
/// </summary>
public static IOrderCommandFactory OrderCommands => DI.Get<IOrderCommandFactory>();

/// <summary>
/// Allows players to manage empire wide projects such as research and espionage.
/// </summary>
public static IProjectCommandFactory ProjectCommands => DI.Get<IProjectCommandFactory>();

/// <summary>
/// Allows players to manage waypoints.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions FrEee.Gameplay/Commands/Projects/ProjectCommandFactory.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Command to set an empire's research priorities.
/// </summary>
public class ResearchCommand : Command<Empire>
public class ResearchCommand
: Command<Empire>, IResearchCommand
{
public ResearchCommand()
: base(Empire.Current)
{
Spending = new ModReferenceKeyedDictionary<Tech, int>();
Queue = new ModReferenceList<Tech>();
}

public ModReferenceList<Tech> Queue { get; private set; }
public ModReferenceKeyedDictionary<Tech, int> Spending { get; private set; }
public ModReferenceList<Tech> Queue { get; private set; } = new();
public ModReferenceKeyedDictionary<Tech, int> Spending { get; private set; } = new();

public override void Execute()
{
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions FrEee.Root/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +39,7 @@ public static void ConfigureDI()
DI.RegisterSingleton<IMinisterCommandFactory, MinisterCommandFactory>();
DI.RegisterSingleton<INoteCommandFactory, NoteCommandFactory>();
DI.RegisterSingleton<IOrderCommandFactory, OrderCommandFactory>();
DI.RegisterSingleton<IProjectCommandFactory, ProjectCommandFactory>();
DI.RegisterSingleton<IWaypointCommandFactory, WaypointCommandFactory>();

// run this in the background, without awaiting it
Expand Down
1 change: 1 addition & 0 deletions FrEee.Tests/FrEee.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ProjectReference Include="..\FrEee.Core.Domain\FrEee.Core.Domain.csproj" />
<ProjectReference Include="..\FrEee.Core.Utility\FrEee.Core.Utility.csproj" />
<ProjectReference Include="..\FrEee.Processes\FrEee.Processes.csproj" />
<ProjectReference Include="..\FrEee.Root\FrEee.Root.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
Expand Down
15 changes: 7 additions & 8 deletions FrEee.Tests/Objects/Technology/TechnologyTest.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,8 +24,7 @@ public class TechnologyTest
[OneTimeSetUp]
public static void ClassInit()
{
DI.RegisterSingleton<ITurnProcessor, TurnProcessor>();
DI.Run();
Configuration.ConfigureDI();
processor = DIRoot.TurnProcessor;
new ModLoader().Load(null);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion FrEee.UI.WinForms/Forms/ResearchForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 35ba3a9

Please sign in to comment.