CVars for MIDI instrument limits (#2632)

* CVars for MIDI instruments limits!

* Localize this

* Cache CVars in instrument systems

* better naming

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
Víctor Aguilera Puerto
2020-11-27 17:12:45 +01:00
committed by GitHub
parent 6bb1e9fa5d
commit cec722e19e
6 changed files with 114 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ using System.Linq;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Utility;
using Content.Shared;
using Content.Shared.GameObjects.Components.Instruments;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces;
@@ -16,9 +17,11 @@ using Robust.Server.Player;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.Configuration;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Players;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -40,6 +43,7 @@ namespace Content.Server.GameObjects.Components.Instruments
[Dependency] private readonly IGameTiming _gameTiming = default!;
private static readonly TimeSpan OneSecAgo = TimeSpan.FromSeconds(-1);
private InstrumentSystem _instrumentSystem = default!;
/// <summary>
/// The client channel currently playing the instrument, or null if there's none.
@@ -165,6 +169,8 @@ namespace Content.Server.GameObjects.Components.Instruments
{
UserInterface.OnClosed += UserInterfaceOnClosed;
}
_instrumentSystem = EntitySystem.Get<InstrumentSystem>();
}
public override void ExposeData(ObjectSerializer serializer)
@@ -186,6 +192,10 @@ namespace Content.Server.GameObjects.Components.Instruments
{
base.HandleNetworkMessage(message, channel, session);
var maxMidiLaggedBatches = _instrumentSystem.MaxMidiLaggedBatches;
var maxMidiEventsPerSecond = _instrumentSystem.MaxMidiEventsPerSecond;
var maxMidiEventsPerBatch = _instrumentSystem.MaxMidiEventsPerBatch;
switch (message)
{
case InstrumentMidiEventMessage midiEventMsg:
@@ -206,26 +216,25 @@ namespace Content.Server.GameObjects.Components.Instruments
}
_laggedBatches++;
switch (_laggedBatches)
if (_laggedBatches == (int) (maxMidiLaggedBatches * (1 / 3d) + 1))
{
case (int) (MaxMidiLaggedBatches * (1 / 3d)) + 1:
Owner.PopupMessage(InstrumentPlayer.AttachedEntity,
"Your fingers are beginning to a cramp a little!");
break;
case (int) (MaxMidiLaggedBatches * (2 / 3d)) + 1:
Owner.PopupMessage(InstrumentPlayer.AttachedEntity,
"Your fingers are seriously cramping up!");
break;
Owner.PopupMessage(InstrumentPlayer.AttachedEntity,
Loc.GetString("Your fingers are beginning to a cramp a little!"));
} else if (_laggedBatches == (int) (maxMidiLaggedBatches * (2 / 3d) + 1))
{
Owner.PopupMessage(InstrumentPlayer.AttachedEntity,
Loc.GetString("Your fingers are seriously cramping up!"));
}
if (_laggedBatches > MaxMidiLaggedBatches)
if (_laggedBatches > maxMidiLaggedBatches)
{
send = false;
}
}
if (++_midiEventCount > MaxMidiEventsPerSecond
|| midiEventMsg.MidiEvent.Length > MaxMidiEventsPerBatch)
if (++_midiEventCount > maxMidiEventsPerSecond
|| midiEventMsg.MidiEvent.Length > maxMidiEventsPerBatch)
{
var now = _gameTiming.RealTime;
var oneSecAGo = now.Add(OneSecAgo);
@@ -346,6 +355,9 @@ namespace Content.Server.GameObjects.Components.Instruments
{
base.Update(delta);
var maxMidiLaggedBatches = _instrumentSystem.MaxMidiLaggedBatches;
var maxMidiBatchDropped = _instrumentSystem.MaxMidiBatchesDropped;
if (_instrumentPlayer != null && !ActionBlockerSystem.CanInteract(_instrumentPlayer.AttachedEntity))
{
InstrumentPlayer = null;
@@ -353,8 +365,8 @@ namespace Content.Server.GameObjects.Components.Instruments
UserInterface?.CloseAll();
}
if ((_batchesDropped >= MaxMidiBatchDropped
|| _laggedBatches >= MaxMidiLaggedBatches)
if ((_batchesDropped >= maxMidiBatchDropped
|| _laggedBatches >= maxMidiLaggedBatches)
&& InstrumentPlayer != null)
{
var mob = InstrumentPlayer.AttachedEntity;