Skip to content

Commit

Permalink
Добавлен флаг рандомного интервала отправки сообщений в пределах 1-30…
Browse files Browse the repository at this point in the history
… секунд, тернарные операторы для упрощения кода и описания объявлённых полей в компоненте.
  • Loading branch information
Schrodinger71 committed Jun 17, 2024
1 parent 1d4c5de commit 3563f18
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
19 changes: 10 additions & 9 deletions Content.Server/ADT/AutoPostingChat/AutoPostingChatSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Timers;
using System.ComponentModel;
using System.Linq;
//using System.Random;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
public sealed class AutoPostingChatSystem : EntitySystem
Expand All @@ -28,6 +29,8 @@ public sealed class AutoPostingChatSystem : EntitySystem
[Dependency] private readonly ChatSystem _chat = default!;
private System.Timers.Timer _speakTimer = new();
private System.Timers.Timer _emoteTimer = new();
private readonly Random _random = new Random();
//private static readonly Random _random = new Random();

public override void Initialize()
{
Expand Down Expand Up @@ -58,34 +61,32 @@ private void OnMobState(EntityUid uid, AutoPostingChatComponent component, MobSt

private void OnComponentStartup(EntityUid uid, AutoPostingChatComponent component, ComponentStartup args)
{
// Проверяем наличие компонента AutoPostingChatComponent на сущности
if (component == null)
if (component == null)
{
Log.Debug("AutoPostingChatComponent отсутствует на сущности с UID: " + uid);
return;
}

_speakTimer.Interval = component.SpeakTimerRead; // 8000 миллисекунд = 8 секунд по умолчанию
_speakTimer.Interval = component.RandomIntervalSpeak ? _random.Next(1000, 30001) : component.SpeakTimerRead;
_speakTimer.Elapsed += (sender, eventArgs) =>
{
// Проверяем, что данные в компоненте были обновлены
if (component.PostingMessageSpeak != null)
{
_chat.TrySendInGameICMessage(uid, component.PostingMessageSpeak, InGameICChatType.Speak, ChatTransmitRange.Normal);
}
_speakTimer.Interval = component.SpeakTimerRead;
_speakTimer.Interval = component.RandomIntervalSpeak ? _random.Next(1000, 30001) : component.SpeakTimerRead;
};
_emoteTimer.Interval = component.EmoteTimerRead; // 9000 миллисекунд = 9 секунд по умолчанию

_emoteTimer.Interval = component.RandomIntervalEmote ? _random.Next(1000, 30001) : component.EmoteTimerRead;
_emoteTimer.Elapsed += (sender, eventArgs) =>
{
// Проверяем, что данные в компоненте были обновлены
if (component.PostingMessageEmote != null)
{
_chat.TrySendInGameICMessage(uid, component.PostingMessageEmote, InGameICChatType.Emote, ChatTransmitRange.Normal);
}
_emoteTimer.Interval = component.EmoteTimerRead;
_emoteTimer.Interval = component.RandomIntervalEmote ? _random.Next(1000, 30001) : component.EmoteTimerRead;
};
// Запускаем таймеры

_speakTimer.Start();
_emoteTimer.Start();
}
Expand Down
34 changes: 25 additions & 9 deletions Content.Shared/ADT/AutoPostingChat/AutoPostingChatComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,40 @@ namespace Content.Shared.ADT.AutoPostingChat;
public sealed partial class AutoPostingChatComponent : Component
{
/// <summary>
/// timings for giggles and knocks.
/// Whether this destination is shown in the gateway ui.
/// If you are making a gateway for an admeme set this once you are ready for players to select it.
/// </summary>
//[ViewVariables(VVAccess.ReadWrite)]
//public TimeSpan DamageGiggleCooldown = TimeSpan.FromSeconds(2);
//[ViewVariables(VVAccess.ReadWrite)]
//public float KnockChance = 0.05f;
//[ViewVariables(VVAccess.ReadWrite)]
//public float GiggleRandomChance = 0.3f;
[DataField, ViewVariables(VVAccess.ReadWrite)]
public bool RandomIntervalSpeak = false;

/// <summary>
/// Whether this destination is shown in the gateway ui.
/// If you are making a gateway for an admeme set this once you are ready for players to select it.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public bool RandomIntervalEmote = false;

/// <summary>
/// The interval in milliseconds between automatic speech messages.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public int SpeakTimerRead = 8000;

/// <summary>
/// The interval in milliseconds between automatic emote messages.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public int EmoteTimerRead = 9000;

/// <summary>
/// The message that will be automatically spoken by the entity.
/// </summary>
[DataField("speakMessage")]
public string? PostingMessageSpeak = "Вульп-вульп!";
public string? PostingMessageSpeak = "";

/// <summary>
/// The message that will be automatically emotes by the entity.
/// </summary>
[DataField("emoteMessage")]
public string? PostingMessageEmote = "Кхе";
public string? PostingMessageEmote = "";
}

0 comments on commit 3563f18

Please sign in to comment.