Skip to content

Commit 6f904fb

Browse files
committed
Complete Event-Driven Architecture Migration
Updated `REFACTORING_PROGRESS.md` to reflect 100% completion of the Event-Driven Architecture migration, highlighting key achievements and metrics. Registered `IGameCoordinatorService` in `ServiceCollectionExtensions.cs` to coordinate complex operations between managers, along with a factory pattern for manager retrieval. Refactored `Form1.cs` to utilize `IGameCoordinatorService`, adding new menu items for advanced features like game state validation and manager synchronization. Enhanced `GameEngine.cs` to implement dependency injection for managers, improving modularity and testability. Updated `IBaseManager.cs` and `IGameManager.cs` interfaces to include shutdown and unsaved changes tracking methods for better resource management. Improved `BaseManager.cs` with shutdown logic and cleanup methods to ensure proper resource release. Refined `GameManager.cs` to track unsaved changes and manage game state transitions more effectively, delegating player-related commands to `PlayerManager`. Created `IGameCoordinatorService.cs` interface and implemented `GameCoordinatorService.cs` to encapsulate advanced game logic, including methods for coordinated actions, player initialization, and game state validation. These changes significantly enhance the architecture, maintainability, and extensibility of the codebase.
1 parent 237b5fb commit 6f904fb

File tree

10 files changed

+1639
-1919
lines changed

10 files changed

+1639
-1919
lines changed

docs/REFACTORING_PROGRESS.md

Lines changed: 123 additions & 511 deletions
Large diffs are not rendered by default.

src/Extensions/ServiceCollectionExtensions.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Logging;
33
using WinFormsApp1.Interfaces;
44
using WinFormsApp1.Managers;
5+
using WinFormsApp1.Services;
56

67
namespace WinFormsApp1.Extensions
78
{
@@ -26,6 +27,9 @@ public static IServiceCollection AddGameServices(this IServiceCollection service
2627
services.AddSingleton<ILocationManager, LocationManager>();
2728
services.AddSingleton<ISkillManager, SkillManager>();
2829

30+
// Register advanced coordination service
31+
services.AddSingleton<IGameCoordinatorService, GameCoordinatorService>();
32+
2933
// Register forms as transient (new instance each time)
3034
services.AddTransient<Form1>();
3135
services.AddTransient<InventoryForm>();
@@ -68,5 +72,99 @@ public static IServiceCollection AddGameConfiguration(this IServiceCollection se
6872
// This method is here for future configuration needs
6973
return services;
7074
}
75+
76+
/// <summary>
77+
/// Add advanced game services that demonstrate complex dependency injection patterns
78+
/// </summary>
79+
/// <param name="services">The service collection</param>
80+
/// <returns>The service collection for chaining</returns>
81+
public static IServiceCollection AddAdvancedGameServices(this IServiceCollection services)
82+
{
83+
// Register factory patterns
84+
services.AddSingleton<Func<string, IBaseManager>>(serviceProvider => managerType =>
85+
{
86+
return managerType.ToLowerInvariant() switch
87+
{
88+
"game" => serviceProvider.GetRequiredService<IGameManager>(),
89+
"player" => serviceProvider.GetRequiredService<IPlayerManager>(),
90+
"combat" => serviceProvider.GetRequiredService<ICombatManager>(),
91+
"inventory" => serviceProvider.GetRequiredService<IInventoryManager>(),
92+
"location" => serviceProvider.GetRequiredService<ILocationManager>(),
93+
"skill" => serviceProvider.GetRequiredService<ISkillManager>(),
94+
_ => throw new ArgumentException($"Unknown manager type: {managerType}")
95+
};
96+
});
97+
98+
// Register decorator pattern example
99+
//services.Decorate<IGameManager, GameManagerWithLogging>();
100+
101+
return services;
102+
}
103+
}
104+
105+
/// <summary>
106+
/// Example decorator that adds logging to GameManager operations
107+
/// Demonstrates decorator pattern with dependency injection
108+
/// </summary>
109+
public class GameManagerWithLogging : IGameManager
110+
{
111+
private readonly IGameManager _inner;
112+
private readonly ILogger<GameManagerWithLogging> _logger;
113+
114+
public GameManagerWithLogging(IGameManager inner, ILogger<GameManagerWithLogging> logger)
115+
{
116+
_inner = inner;
117+
_logger = logger;
118+
}
119+
120+
public GameState CurrentGameState => _inner.CurrentGameState;
121+
public bool IsGameRunning => _inner.IsGameRunning;
122+
public bool HasUnsavedChanges => _inner.HasUnsavedChanges;
123+
public string ManagerName => _inner.ManagerName;
124+
public bool IsInitialized => _inner.IsInitialized;
125+
126+
public void StartNewGame(Player player)
127+
{
128+
_logger.LogInformation("Starting new game for player: {PlayerName}", player.Name);
129+
_inner.StartNewGame(player);
130+
_logger.LogInformation("New game started successfully");
131+
}
132+
133+
public bool LoadGame(int saveSlot)
134+
{
135+
_logger.LogInformation("Loading game from slot: {SaveSlot}", saveSlot);
136+
var result = _inner.LoadGame(saveSlot);
137+
_logger.LogInformation("Load game result: {Success}", result);
138+
return result;
139+
}
140+
141+
public bool SaveGame(int saveSlot)
142+
{
143+
_logger.LogInformation("Saving game to slot: {SaveSlot}", saveSlot);
144+
var result = _inner.SaveGame(saveSlot);
145+
_logger.LogInformation("Save game result: {Success}", result);
146+
return result;
147+
}
148+
149+
public CommandResult ProcessCommand(string command)
150+
{
151+
_logger.LogDebug("Processing command: {Command}", command);
152+
var result = _inner.ProcessCommand(command);
153+
_logger.LogDebug("Command result: {Success} - {Message}", result.Success, result.Message);
154+
return result;
155+
}
156+
157+
public List<string> GetAvailableCommands() => _inner.GetAvailableCommands();
158+
public void PauseGame() => _inner.PauseGame();
159+
public void ResumeGame() => _inner.ResumeGame();
160+
public void EndGame() => _inner.EndGame();
161+
public bool ProcessCheat(string cheatCode) => _inner.ProcessCheat(cheatCode);
162+
public GameStatistics GetGameStatistics() => _inner.GetGameStatistics();
163+
public void UpdateGameTime(double deltaTime) => _inner.UpdateGameTime(deltaTime);
164+
public bool IsFeatureEnabled(string featureName) => _inner.IsFeatureEnabled(featureName);
165+
public void SetFeatureEnabled(string featureName, bool enabled) => _inner.SetFeatureEnabled(featureName, enabled);
166+
public void Initialize() => _inner.Initialize();
167+
public void Shutdown() => _inner.Shutdown();
168+
public void Cleanup() => _inner.Cleanup();
71169
}
72170
}

0 commit comments

Comments
 (0)