1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-07 21:58:06 -05:00
IW4M-Admin/Application/Factories/GameScriptEventFactory.cs

40 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using SharedLibraryCore.Interfaces.Events;
namespace IW4MAdmin.Application.Factories;
public class GameScriptEventFactory : IGameScriptEventFactory
{
private readonly IServiceProvider _serviceProvider;
private readonly Dictionary<string, Type> _gameScriptEventMap;
public GameScriptEventFactory(IServiceProvider serviceProvider, IEnumerable<IGameScriptEvent> gameScriptEventTypes)
{
_serviceProvider = serviceProvider;
_gameScriptEventMap = gameScriptEventTypes
.ToLookup(kvp => kvp.EventName ?? kvp.GetType().Name.Replace("ScriptEvent", ""))
.ToDictionary(kvp => kvp.Key, kvp => kvp.First().GetType());
}
public IGameScriptEvent Create(string eventType, string logData)
{
if (string.IsNullOrEmpty(eventType) || !_gameScriptEventMap.TryGetValue(eventType, out var matchedType))
{
return null;
}
var newEvent = _serviceProvider.GetRequiredService(matchedType) as IGameScriptEvent;
if (newEvent is not null)
{
newEvent.ScriptData = logData;
}
return newEvent;
}
}