Turns HolidayManager into an entity system, HolidaySystem.

This commit is contained in:
Vera Aguilera Puerto
2021-09-29 12:56:10 +02:00
parent 821fa87c3b
commit 77c93a5648
5 changed files with 29 additions and 31 deletions

View File

@@ -81,7 +81,6 @@ namespace Content.Server.Entry
{ {
base.PostInit(); base.PostInit();
IoCManager.Resolve<IHolidayManager>().Initialize();
IoCManager.Resolve<ISandboxManager>().Initialize(); IoCManager.Resolve<ISandboxManager>().Initialize();
IoCManager.Resolve<RecipeManager>().Initialize(); IoCManager.Resolve<RecipeManager>().Initialize();
IoCManager.Resolve<AlertManager>().Initialize(); IoCManager.Resolve<AlertManager>().Initialize();

View File

@@ -2,8 +2,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using Content.Server.Chat.Managers; using Content.Server.Chat.Managers;
using Content.Server.GameTicking; using Content.Server.GameTicking;
using Content.Server.Holiday.Interfaces;
using Content.Shared;
using Content.Shared.CCVar; using Content.Shared.CCVar;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
@@ -13,12 +11,10 @@ using Robust.Shared.ViewVariables;
namespace Content.Server.Holiday namespace Content.Server.Holiday
{ {
// ReSharper disable once ClassNeverInstantiated.Global public class HolidaySystem : EntitySystem
public class HolidayManager : IHolidayManager, IEntityEventSubscriber
{ {
[Dependency] private readonly IConfigurationManager _configManager = default!; [Dependency] private readonly IConfigurationManager _configManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IChatManager _chatManager = default!; [Dependency] private readonly IChatManager _chatManager = default!;
[ViewVariables] [ViewVariables]
@@ -27,19 +23,31 @@ namespace Content.Server.Holiday
[ViewVariables] [ViewVariables]
private bool _enabled = true; private bool _enabled = true;
public override void Initialize()
{
_configManager.OnValueChanged(CCVars.HolidaysEnabled, OnHolidaysEnableChange, true);
SubscribeLocalEvent<GameRunLevelChangedEvent>(OnRunLevelChanged);
}
public void RefreshCurrentHolidays() public void RefreshCurrentHolidays()
{ {
_currentHolidays.Clear(); _currentHolidays.Clear();
if (!_enabled) return; if (!_enabled)
return;
var now = DateTime.Now; var now = DateTime.Now;
foreach (var holiday in _prototypeManager.EnumeratePrototypes<HolidayPrototype>()) foreach (var holiday in _prototypeManager.EnumeratePrototypes<HolidayPrototype>())
{ {
if(holiday.ShouldCelebrate(now)) if (holiday.ShouldCelebrate(now))
{
_currentHolidays.Add(holiday); _currentHolidays.Add(holiday);
}
} }
RaiseLocalEvent(new HolidaysRefreshedEvent(_currentHolidays));
} }
public void DoGreet() public void DoGreet()
@@ -71,13 +79,6 @@ namespace Content.Server.Holiday
return _currentHolidays.Contains(prototype); return _currentHolidays.Contains(prototype);
} }
public void Initialize()
{
_configManager.OnValueChanged(CCVars.HolidaysEnabled, OnHolidaysEnableChange, true);
_entityManager.EventBus.SubscribeEvent<GameRunLevelChangedEvent>(EventSource.Local, this, OnRunLevelChanged);
}
private void OnHolidaysEnableChange(bool enabled) private void OnHolidaysEnableChange(bool enabled)
{ {
_enabled = enabled; _enabled = enabled;
@@ -103,4 +104,17 @@ namespace Content.Server.Holiday
} }
} }
} }
/// <summary>
/// Event for when the list of currently active holidays has been refreshed.
/// </summary>
public class HolidaysRefreshedEvent : EntityEventArgs
{
public readonly IEnumerable<HolidayPrototype> Holidays;
public HolidaysRefreshedEvent(IEnumerable<HolidayPrototype> holidays)
{
Holidays = holidays;
}
}
} }

View File

@@ -1,14 +0,0 @@
using System.Collections.Generic;
namespace Content.Server.Holiday.Interfaces
{
public interface IHolidayManager
{
void Initialize();
void RefreshCurrentHolidays();
void DoGreet();
void DoCelebrate();
IEnumerable<HolidayPrototype> GetCurrentHolidays();
bool IsCurrentlyHoliday(string holiday);
}
}

View File

@@ -52,7 +52,6 @@ namespace Content.Server.IoC
IoCManager.Register<IAdminManager, AdminManager>(); IoCManager.Register<IAdminManager, AdminManager>();
IoCManager.Register<IDeviceNetwork, DeviceNetwork.DeviceNetwork>(); IoCManager.Register<IDeviceNetwork, DeviceNetwork.DeviceNetwork>();
IoCManager.Register<EuiManager, EuiManager>(); IoCManager.Register<EuiManager, EuiManager>();
IoCManager.Register<IHolidayManager, HolidayManager>();
IoCManager.Register<IVoteManager, VoteManager>(); IoCManager.Register<IVoteManager, VoteManager>();
IoCManager.Register<INpcBehaviorManager, NpcBehaviorManager>(); IoCManager.Register<INpcBehaviorManager, NpcBehaviorManager>();
IoCManager.Register<IPlayerLocator, PlayerLocator>(); IoCManager.Register<IPlayerLocator, PlayerLocator>();

View File

@@ -27,7 +27,7 @@ namespace Content.Server.Jobs
if (string.IsNullOrEmpty(Holiday) || string.IsNullOrEmpty(Prototype)) if (string.IsNullOrEmpty(Holiday) || string.IsNullOrEmpty(Prototype))
return; return;
if (!IoCManager.Resolve<IHolidayManager>().IsCurrentlyHoliday(Holiday)) if (!EntitySystem.Get<HolidaySystem>().IsCurrentlyHoliday(Holiday))
return; return;
var entity = mob.EntityManager.SpawnEntity(Prototype, mob.Transform.Coordinates); var entity = mob.EntityManager.SpawnEntity(Prototype, mob.Transform.Coordinates);