2020-08-19 02:54:10 -04:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-12-23 16:59:50 +01:00
|
|
|
using System.Linq;
|
2020-08-19 02:54:10 -04:00
|
|
|
using System.Text;
|
2020-09-13 14:23:52 +02:00
|
|
|
using Content.Server.GameTicking;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.StationEvents.Events;
|
2020-11-06 04:04:07 +11:00
|
|
|
using Content.Shared;
|
2021-06-13 14:52:40 +02:00
|
|
|
using Content.Shared.CCVar;
|
2020-10-14 22:45:53 +02:00
|
|
|
using Content.Shared.GameTicking;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.StationEvents;
|
2020-08-14 06:52:17 +10:00
|
|
|
using JetBrains.Annotations;
|
2020-08-18 14:29:13 +02:00
|
|
|
using Robust.Server.Console;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Server.Player;
|
|
|
|
|
using Robust.Shared.Configuration;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
2020-08-14 06:52:17 +10:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Localization;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Network;
|
|
|
|
|
using Robust.Shared.Random;
|
|
|
|
|
using Robust.Shared.Reflection;
|
|
|
|
|
using Robust.Shared.Timing;
|
2020-08-14 06:52:17 +10:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.StationEvents
|
2020-08-14 06:52:17 +10:00
|
|
|
{
|
|
|
|
|
[UsedImplicitly]
|
2020-08-19 02:54:10 -04:00
|
|
|
// Somewhat based off of TG's implementation of events
|
2021-06-29 15:56:07 +02:00
|
|
|
public sealed class StationEventSystem : EntitySystem
|
2020-08-14 06:52:17 +10:00
|
|
|
{
|
2020-11-06 04:04:07 +11:00
|
|
|
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IServerNetManager _netManager = default!;
|
|
|
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
2020-12-23 16:59:50 +01:00
|
|
|
[Dependency] private readonly IConGroupController _conGroupController = default!;
|
|
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
2020-08-18 14:29:13 +02:00
|
|
|
|
2021-01-18 18:14:53 +08:00
|
|
|
public StationEvent? CurrentEvent { get; private set; }
|
2020-08-14 06:52:17 +10:00
|
|
|
public IReadOnlyCollection<StationEvent> StationEvents => _stationEvents;
|
2020-08-19 02:54:10 -04:00
|
|
|
|
2020-11-27 11:00:49 +01:00
|
|
|
private readonly List<StationEvent> _stationEvents = new();
|
2020-08-14 06:52:17 +10:00
|
|
|
|
2020-10-08 11:59:20 -04:00
|
|
|
private const float MinimumTimeUntilFirstEvent = 300;
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// How long until the next check for an event runs
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// Default value is how long until first event is allowed
|
|
|
|
|
private float _timeUntilNextEvent = MinimumTimeUntilFirstEvent;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether random events can run
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// If disabled while an event is running (even if admin run) it will disable it
|
|
|
|
|
public bool Enabled
|
|
|
|
|
{
|
|
|
|
|
get => _enabled;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_enabled == value)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_enabled = value;
|
|
|
|
|
CurrentEvent?.Shutdown();
|
|
|
|
|
CurrentEvent = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool _enabled = true;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Admins can get a list of all events available to run, regardless of whether their requirements have been met
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public string GetEventNames()
|
|
|
|
|
{
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
foreach (var stationEvent in _stationEvents)
|
|
|
|
|
{
|
|
|
|
|
result.Append(stationEvent.Name + "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.ToString();
|
|
|
|
|
}
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Admins can forcibly run events by passing in the Name
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">The exact string for Name, without localization</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public string RunEvent(string name)
|
|
|
|
|
{
|
|
|
|
|
// Could use a dictionary but it's such a minor thing, eh.
|
|
|
|
|
// Wasn't sure on whether to localize this given it's a command
|
|
|
|
|
var upperName = name.ToUpperInvariant();
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
foreach (var stationEvent in _stationEvents)
|
|
|
|
|
{
|
|
|
|
|
if (stationEvent.Name.ToUpperInvariant() != upperName)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
CurrentEvent?.Shutdown();
|
|
|
|
|
CurrentEvent = stationEvent;
|
2021-01-18 18:14:53 +08:00
|
|
|
stationEvent.Announce();
|
2021-06-21 02:13:54 +02:00
|
|
|
return Loc.GetString("station-event-system-run-event", ("eventName", stationEvent.Name));
|
2020-08-14 06:52:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// I had string interpolation but lord it made it hard to read
|
2021-06-21 02:13:54 +02:00
|
|
|
return Loc.GetString("station-event-system-run-event-no-event-name", ("eventName", name));
|
2020-08-14 06:52:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-01-18 18:14:53 +08:00
|
|
|
/// Randomly run a valid event <b>immediately</b>, ignoring earlieststart
|
2020-08-14 06:52:17 +10:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public string RunRandomEvent()
|
|
|
|
|
{
|
2021-01-18 18:14:53 +08:00
|
|
|
var randomEvent = PickRandomEvent();
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
if (randomEvent == null)
|
|
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
return Loc.GetString("station-event-system-run-random-event-no-valid-events");
|
2020-08-14 06:52:17 +10:00
|
|
|
}
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
CurrentEvent?.Shutdown();
|
|
|
|
|
CurrentEvent = randomEvent;
|
|
|
|
|
CurrentEvent.Startup();
|
|
|
|
|
|
2021-06-21 02:13:54 +02:00
|
|
|
return Loc.GetString("station-event-system-run-event",("eventName", randomEvent.Name));
|
2020-08-14 06:52:17 +10:00
|
|
|
}
|
|
|
|
|
|
2021-01-18 18:14:53 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Randomly picks a valid event.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public StationEvent? PickRandomEvent()
|
|
|
|
|
{
|
|
|
|
|
var availableEvents = AvailableEvents(true);
|
|
|
|
|
return FindEvent(availableEvents);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Admins can stop the currently running event (if applicable) and reset the timer
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public string StopEvent()
|
|
|
|
|
{
|
|
|
|
|
string resultText;
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
if (CurrentEvent == null)
|
|
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
resultText = Loc.GetString("station-event-system-stop-event-no-running-event");
|
2020-08-14 06:52:17 +10:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
resultText = Loc.GetString("station-event-system-stop-event", ("eventName", CurrentEvent.Name));
|
2020-08-14 06:52:17 +10:00
|
|
|
CurrentEvent.Shutdown();
|
|
|
|
|
CurrentEvent = null;
|
|
|
|
|
}
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
ResetTimer();
|
|
|
|
|
return resultText;
|
|
|
|
|
}
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
var reflectionManager = IoCManager.Resolve<IReflectionManager>();
|
|
|
|
|
var typeFactory = IoCManager.Resolve<IDynamicTypeFactory>();
|
|
|
|
|
|
|
|
|
|
foreach (var type in reflectionManager.GetAllChildren(typeof(StationEvent)))
|
|
|
|
|
{
|
|
|
|
|
if (type.IsAbstract) continue;
|
|
|
|
|
|
|
|
|
|
var stationEvent = (StationEvent) typeFactory.CreateInstance(type);
|
2021-08-23 13:28:47 +10:00
|
|
|
IoCManager.InjectDependencies(stationEvent);
|
2020-08-14 06:52:17 +10:00
|
|
|
_stationEvents.Add(stationEvent);
|
|
|
|
|
}
|
2020-08-18 14:29:13 +02:00
|
|
|
|
2020-11-06 04:04:07 +11:00
|
|
|
// Can't just check debug / release for a default given mappers need to use release mode
|
|
|
|
|
// As such we'll always pause it by default.
|
|
|
|
|
_configurationManager.OnValueChanged(CCVars.EventsEnabled, value => Enabled = value, true);
|
2020-12-23 16:59:50 +01:00
|
|
|
|
2021-06-20 22:43:54 -07:00
|
|
|
_netManager.RegisterNetMessage<MsgRequestStationEvents>(RxRequest);
|
|
|
|
|
_netManager.RegisterNetMessage<MsgStationEvents>();
|
2021-06-29 15:56:07 +02:00
|
|
|
|
|
|
|
|
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
|
2020-08-18 14:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-23 16:59:50 +01:00
|
|
|
private void RxRequest(MsgRequestStationEvents msg)
|
2020-08-18 14:29:13 +02:00
|
|
|
{
|
2020-12-23 16:59:50 +01:00
|
|
|
if (_playerManager.TryGetSessionByChannel(msg.MsgChannel, out var player))
|
|
|
|
|
SendEvents(player);
|
2020-08-18 14:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendEvents(IPlayerSession player)
|
|
|
|
|
{
|
2020-12-23 16:59:50 +01:00
|
|
|
if (!_conGroupController.CanCommand(player, "events"))
|
2020-08-18 14:29:13 +02:00
|
|
|
return;
|
|
|
|
|
|
2020-12-23 16:59:50 +01:00
|
|
|
var newMsg = _netManager.CreateNetMessage<MsgStationEvents>();
|
|
|
|
|
newMsg.Events = StationEvents.Select(e => e.Name).ToArray();
|
2020-08-18 14:29:13 +02:00
|
|
|
_netManager.ServerSendMessage(newMsg, player.ConnectedClient);
|
2020-08-14 06:52:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
{
|
|
|
|
|
base.Update(frameTime);
|
|
|
|
|
|
2021-01-09 09:58:42 +11:00
|
|
|
if (!Enabled && CurrentEvent == null)
|
2020-08-14 06:52:17 +10:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-19 02:54:10 -04:00
|
|
|
|
|
|
|
|
// Stop events from happening in lobby and force active event to end if the round ends
|
2021-06-20 10:09:24 +02:00
|
|
|
if (Get<GameTicker>().RunLevel != GameRunLevel.InRound)
|
2020-08-19 02:54:10 -04:00
|
|
|
{
|
|
|
|
|
if (CurrentEvent != null)
|
|
|
|
|
{
|
|
|
|
|
Enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
// Keep running the current event
|
|
|
|
|
if (CurrentEvent != null)
|
|
|
|
|
{
|
|
|
|
|
CurrentEvent.Update(frameTime);
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
// Shutdown the event and set the timer for the next event
|
|
|
|
|
if (!CurrentEvent.Running)
|
|
|
|
|
{
|
|
|
|
|
CurrentEvent.Shutdown();
|
|
|
|
|
CurrentEvent = null;
|
|
|
|
|
ResetTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_timeUntilNextEvent > 0)
|
|
|
|
|
{
|
|
|
|
|
_timeUntilNextEvent -= frameTime;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No point hammering this trying to find events if none are available
|
|
|
|
|
var stationEvent = FindEvent(AvailableEvents());
|
|
|
|
|
if (stationEvent == null)
|
|
|
|
|
{
|
|
|
|
|
ResetTimer();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CurrentEvent = stationEvent;
|
2021-01-18 18:14:53 +08:00
|
|
|
CurrentEvent.Announce();
|
2020-08-14 06:52:17 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reset the event timer once the event is done.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ResetTimer()
|
|
|
|
|
{
|
|
|
|
|
// 5 - 15 minutes. TG does 3-10 but that's pretty frequent
|
2020-12-23 16:59:50 +01:00
|
|
|
_timeUntilNextEvent = _random.Next(300, 900);
|
2020-08-14 06:52:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pick a random event from the available events at this time, also considering their weightings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2021-01-18 18:14:53 +08:00
|
|
|
private StationEvent? FindEvent(List<StationEvent> availableEvents)
|
2020-08-14 06:52:17 +10:00
|
|
|
{
|
|
|
|
|
if (availableEvents.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
var sumOfWeights = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var stationEvent in availableEvents)
|
|
|
|
|
{
|
|
|
|
|
sumOfWeights += (int) stationEvent.Weight;
|
|
|
|
|
}
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-12-23 16:59:50 +01:00
|
|
|
sumOfWeights = _random.Next(sumOfWeights);
|
2020-08-14 06:52:17 +10:00
|
|
|
|
|
|
|
|
foreach (var stationEvent in availableEvents)
|
|
|
|
|
{
|
|
|
|
|
sumOfWeights -= (int) stationEvent.Weight;
|
|
|
|
|
|
|
|
|
|
if (sumOfWeights <= 0)
|
|
|
|
|
{
|
|
|
|
|
return stationEvent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the events that have met their player count, time-until start, etc.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ignoreEarliestStart"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private List<StationEvent> AvailableEvents(bool ignoreEarliestStart = false)
|
|
|
|
|
{
|
|
|
|
|
TimeSpan currentTime;
|
2020-12-23 16:59:50 +01:00
|
|
|
var playerCount = _playerManager.PlayerCount;
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
// playerCount does a lock so we'll just keep the variable here
|
|
|
|
|
if (!ignoreEarliestStart)
|
|
|
|
|
{
|
2020-12-23 16:59:50 +01:00
|
|
|
currentTime = _gameTiming.CurTime;
|
2020-08-14 06:52:17 +10:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
currentTime = TimeSpan.Zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = new List<StationEvent>();
|
|
|
|
|
|
|
|
|
|
foreach (var stationEvent in _stationEvents)
|
|
|
|
|
{
|
|
|
|
|
if (CanRun(stationEvent, playerCount, currentTime))
|
|
|
|
|
{
|
|
|
|
|
result.Add(stationEvent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CanRun(StationEvent stationEvent, int playerCount, TimeSpan currentTime)
|
|
|
|
|
{
|
|
|
|
|
if (stationEvent.MaxOccurrences.HasValue && stationEvent.Occurrences >= stationEvent.MaxOccurrences.Value)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (playerCount < stationEvent.MinimumPlayers)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentTime != TimeSpan.Zero && currentTime.TotalMinutes < stationEvent.EarliestStart)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 22:45:53 +02:00
|
|
|
public override void Shutdown()
|
|
|
|
|
{
|
|
|
|
|
CurrentEvent?.Shutdown();
|
2021-01-18 18:14:53 +08:00
|
|
|
base.Shutdown();
|
2020-10-14 22:45:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-29 15:56:07 +02:00
|
|
|
public void Reset(RoundRestartCleanupEvent ev)
|
2020-08-14 06:52:17 +10:00
|
|
|
{
|
2021-01-18 18:14:53 +08:00
|
|
|
if (CurrentEvent?.Running == true)
|
2020-08-14 06:52:17 +10:00
|
|
|
{
|
|
|
|
|
CurrentEvent.Shutdown();
|
|
|
|
|
CurrentEvent = null;
|
|
|
|
|
}
|
2020-08-24 14:10:28 +02:00
|
|
|
|
2020-08-14 06:52:17 +10:00
|
|
|
foreach (var stationEvent in _stationEvents)
|
|
|
|
|
{
|
|
|
|
|
stationEvent.Occurrences = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timeUntilNextEvent = MinimumTimeUntilFirstEvent;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-18 14:29:13 +02:00
|
|
|
}
|