Refactor the handling of date time to no longer depend on a static
Build / Build (push) Successful in 10m13s
Build / Build (push) Successful in 10m13s
This commit is contained in:
11 files changed
+53
-39
No files matched your search
@@ -2,6 +2,7 @@
|
||||
using System.CommandLine.Parsing;
|
||||
using PkmnLib.Dynamic.AI;
|
||||
using PkmnLib.Dynamic.Libraries;
|
||||
using PkmnLib.Dynamic.Plugins;
|
||||
using PkmnLib.Plugin.Gen7;
|
||||
using PkmnLib.Static.Utils;
|
||||
using Serilog;
|
||||
@@ -24,7 +25,10 @@ internal static class Program
|
||||
AILogging.LogHandler = Log.Debug;
|
||||
var library = DynamicLibraryImpl.Create([
|
||||
new Gen7Plugin(),
|
||||
]);
|
||||
], new PluginEnvironmentHelpers
|
||||
{
|
||||
DateTimeProvider = () => DateTimeOffset.Now,
|
||||
});
|
||||
_availableAIs = PokemonAI.InstantiateAis(library);
|
||||
|
||||
var testCommand = new Command("test", "Run two AIs against each other")
|
||||
|
||||
@@ -68,9 +68,9 @@ public class DynamicLibraryImpl : IDynamicLibrary
|
||||
/// Initializes a new instance of the <see cref="DynamicLibraryImpl"/> class, with the given
|
||||
/// plugins and static library.
|
||||
/// </summary>
|
||||
public static IDynamicLibrary Create(IEnumerable<IPlugin> plugins)
|
||||
public static IDynamicLibrary Create(IEnumerable<IPlugin> plugins, PluginEnvironmentHelpers helpers)
|
||||
{
|
||||
var load = LibraryLoader.LoadPlugins(plugins);
|
||||
var load = LibraryLoader.LoadPlugins(plugins, helpers);
|
||||
|
||||
return new DynamicLibraryImpl(load.StaticLibrary, load.Registry.BattleStatCalculator!,
|
||||
load.Registry.DamageCalculator!, load.Registry.MiscLibrary!, load.Registry.CaptureLibrary!, load.Resolver,
|
||||
|
||||
@@ -19,14 +19,14 @@ public static class LibraryLoader
|
||||
/// <summary>
|
||||
/// Loads plugins and creates a static library from them.
|
||||
/// </summary>
|
||||
public static LoadResult LoadPlugins(IEnumerable<IPlugin> plugins)
|
||||
public static LoadResult LoadPlugins(IEnumerable<IPlugin> plugins, PluginEnvironmentHelpers helpers)
|
||||
{
|
||||
var registry = new ScriptRegistry();
|
||||
var orderedPlugins = plugins.OrderBy(x => x.LoadOrder).ToList();
|
||||
var staticLibrary = CreateStaticLibrary(orderedPlugins);
|
||||
foreach (var plugin in orderedPlugins)
|
||||
{
|
||||
plugin.Register(registry);
|
||||
plugin.Register(registry, helpers);
|
||||
}
|
||||
if (registry.DamageCalculator is null)
|
||||
throw new InvalidOperationException("Damage calculator not found in plugins.");
|
||||
|
||||
@@ -26,7 +26,7 @@ public interface IPlugin
|
||||
/// <summary>
|
||||
/// Run the registration of the plugin when we're building the library.
|
||||
/// </summary>
|
||||
void Register(ScriptRegistry registry);
|
||||
void Register(ScriptRegistry registry, PluginEnvironmentHelpers helpers);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IPlugin"/>
|
||||
@@ -59,7 +59,7 @@ public abstract class Plugin<TConfiguration> : IPlugin where TConfiguration : IP
|
||||
/// <summary>
|
||||
/// Run the registration of the plugin when we're building the library.
|
||||
/// </summary>
|
||||
public abstract void Register(ScriptRegistry registry);
|
||||
public abstract void Register(ScriptRegistry registry, PluginEnvironmentHelpers helpers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace PkmnLib.Dynamic.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Helper functions to get information about the environment we're running in, such as the current date time.
|
||||
/// </summary>
|
||||
public class PluginEnvironmentHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// A delegate that provides the current date and time.
|
||||
/// </summary>
|
||||
public delegate DateTimeOffset DateTimeProviderDelegate();
|
||||
|
||||
/// <summary>
|
||||
/// A function to get the current date and time. This can be replaced in cases where the date and time
|
||||
/// may not be the same as the system time.
|
||||
/// </summary>
|
||||
public required DateTimeProviderDelegate DateTimeProvider { get; init; }
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
namespace PkmnLib.Dynamic;
|
||||
|
||||
/// <summary>
|
||||
/// A static helper class that contains functions that are used in multiple places.
|
||||
/// </summary>
|
||||
public static class StaticHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// A delegate that provides the current date and time.
|
||||
/// </summary>
|
||||
public delegate DateTimeOffset DateTimeProviderDelegate();
|
||||
|
||||
/// <summary>
|
||||
/// A function to get the current date and time. This can be replaced in cases where the date and time
|
||||
/// may not be the same as the system time.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static DateTimeProviderDelegate DateTimeProvider { get; set; } = () => DateTimeOffset.Now;
|
||||
|
||||
/// <summary>
|
||||
/// Get the current date and time.
|
||||
/// </summary>
|
||||
public static DateTimeOffset GetCurrentDateTime() => DateTimeProvider();
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Text.Json;
|
||||
using EnumerableAsyncProcessor.Extensions;
|
||||
using PkmnLib.Dynamic.Libraries;
|
||||
using PkmnLib.Dynamic.Plugins;
|
||||
using PkmnLib.Dynamic.Models;
|
||||
using PkmnLib.Plugin.Gen7;
|
||||
using PkmnLib.Static.Species;
|
||||
@@ -45,7 +46,10 @@ public class IntegrationTestRunner
|
||||
{
|
||||
DamageCalculatorHasRandomness = true,
|
||||
}),
|
||||
]);
|
||||
], new PluginEnvironmentHelpers
|
||||
{
|
||||
DateTimeProvider = () => DateTimeOffset.Now,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -54,7 +58,10 @@ public class IntegrationTestRunner
|
||||
{
|
||||
DamageCalculatorHasRandomness = false,
|
||||
}),
|
||||
]);
|
||||
], new PluginEnvironmentHelpers
|
||||
{
|
||||
DateTimeProvider = () => DateTimeOffset.Now,
|
||||
});
|
||||
}
|
||||
|
||||
await TestContext.Current!.OutputWriter.WriteLineAsync("File: " + $"file://{test.FileName}");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using PkmnLib.Dynamic.Libraries;
|
||||
using PkmnLib.Dynamic.Plugins;
|
||||
using PkmnLib.Plugin.Gen7;
|
||||
|
||||
namespace PkmnLib.Tests.Integration;
|
||||
@@ -12,7 +13,10 @@ public static class LibraryHelpers
|
||||
{
|
||||
DamageCalculatorHasRandomness = false,
|
||||
}),
|
||||
]);
|
||||
], new PluginEnvironmentHelpers
|
||||
{
|
||||
DateTimeProvider = () => DateTimeOffset.Now,
|
||||
});
|
||||
return dynamicLibrary;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using PkmnLib.Dynamic.Libraries;
|
||||
using PkmnLib.Dynamic.Plugins;
|
||||
|
||||
namespace PkmnLib.Plugin.Gen7.Tests;
|
||||
|
||||
@@ -11,7 +12,10 @@ public static class LibraryHelpers
|
||||
{
|
||||
DamageCalculatorHasRandomness = false,
|
||||
}),
|
||||
]);
|
||||
], new PluginEnvironmentHelpers
|
||||
{
|
||||
DateTimeProvider = () => DateTimeOffset.Now,
|
||||
});
|
||||
return dynamicLibrary;
|
||||
}
|
||||
}
|
||||
@@ -42,12 +42,12 @@ public class Gen7Plugin : Plugin<Gen7PluginConfiguration>, IResourceProvider
|
||||
public override uint LoadOrder => 0;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Register(ScriptRegistry registry)
|
||||
public override void Register(ScriptRegistry registry, PluginEnvironmentHelpers helpers)
|
||||
{
|
||||
registry.RegisterAssemblyScripts(typeof(Gen7Plugin).Assembly);
|
||||
registry.RegisterBattleStatCalculator(new Gen7BattleStatCalculator());
|
||||
registry.RegisterDamageCalculator(new Gen7DamageCalculator(Configuration));
|
||||
registry.RegisterMiscLibrary(new Gen7MiscLibrary());
|
||||
registry.RegisterMiscLibrary(new Gen7MiscLibrary(helpers.DateTimeProvider));
|
||||
registry.RegisterCaptureLibrary(new Gen7CaptureLibrary(Configuration));
|
||||
registry.RegisterExperienceGainCalculator(new Gen7ExperienceGainCalculator());
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
using PkmnLib.Dynamic;
|
||||
using PkmnLib.Dynamic.Libraries;
|
||||
using PkmnLib.Dynamic.Plugins;
|
||||
using PkmnLib.Static.Moves;
|
||||
|
||||
namespace PkmnLib.Plugin.Gen7.Libraries.Battling;
|
||||
|
||||
public class Gen7MiscLibrary : IMiscLibrary
|
||||
public class Gen7MiscLibrary(PluginEnvironmentHelpers.DateTimeProviderDelegate dateTimeProvider) : IMiscLibrary
|
||||
{
|
||||
private readonly IMoveData _struggleData = new MoveDataImpl("struggle", new TypeIdentifier(0, "none"),
|
||||
MoveCategory.Physical, 50, 255, 255, MoveTarget.Any, 0,
|
||||
@@ -21,7 +22,7 @@ public class Gen7MiscLibrary : IMiscLibrary
|
||||
/// <inheritdoc />
|
||||
public TimeOfDay GetTimeOfDay()
|
||||
{
|
||||
var time = StaticHelpers.GetCurrentDateTime().LocalDateTime;
|
||||
var time = dateTimeProvider().LocalDateTime;
|
||||
var hour = time.Hour;
|
||||
return hour switch
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user