Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New component AutoPostingChat(no Ready) #545

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions Content.Server/ADT/AutoPostingChat/AutoPostingChatSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using Content.Server.Administration.Commands;
using Content.Shared.Mobs;
using Content.Server.Chat;
using Content.Server.Chat.Systems;
using Content.Shared.Chat.Prototypes;
using Content.Shared.Damage.Prototypes;
using Content.Shared.Damage;
using Robust.Shared.Prototypes;
using Content.Server.Emoting.Systems;
using Content.Server.Speech.EntitySystems;
using Content.Shared.ADT.AutoPostingChat;
using Content.Shared.Interaction.Components;
using System.Timers;
using System.ComponentModel;
using System.Linq;
using Robust.Shared.Timing;
public sealed class AutoPostingChatSystem : EntitySystem
{
// [Dependency] private readonly DamageableSystem _damageableSystem = default!;
// [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Таймеры в системе? Обычно они находятся в компоненте, и не являются System.Timers.Timer, как и рандом.
Рандом используется как Dependency IRobustRandom, или чего-то такого. Посмотри примеры, пожалуйста


public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AutoPostingChatComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<AutoPostingChatComponent, MobStateChangedEvent>(OnMobState);
SubscribeLocalEvent<AutoPostingChatComponent, ComponentShutdown>(ComponentRemove);
}

private void ComponentRemove(EntityUid uid, AutoPostingChatComponent component, ComponentShutdown args)
{
_speakTimer.Stop();
_emoteTimer.Stop();
_speakTimer.Dispose(); // освобождаем ресурсы
_emoteTimer.Dispose();
}

/// <summary>
/// On death removes active comps.
/// </summary>
private void OnMobState(EntityUid uid, AutoPostingChatComponent component, MobStateChangedEvent args)
{
if (args.NewMobState == MobState.Dead || component == null)
Schrodinger71 marked this conversation as resolved.
Show resolved Hide resolved
{
RemComp<AutoPostingChatComponent>(uid);
}
}

private void OnComponentStartup(EntityUid uid, AutoPostingChatComponent component, ComponentStartup args)
{
if (component == null)
{
Log.Debug("AutoPostingChatComponent отсутствует на сущности с UID: " + uid);
return;
Schrodinger71 marked this conversation as resolved.
Show resolved Hide resolved
}

SetupSpeakTimer(uid, component);
SetupEmoteTimer(uid, component);
}

private void SetupSpeakTimer(EntityUid uid, AutoPostingChatComponent component)
{
_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.RandomIntervalSpeak ? _random.Next(1000, 30001) : component.SpeakTimerRead;
Schrodinger71 marked this conversation as resolved.
Show resolved Hide resolved
};

_speakTimer.Start();
}

private void SetupEmoteTimer(EntityUid uid, AutoPostingChatComponent component)
{
_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.RandomIntervalEmote ? _random.Next(1000, 30001) : component.EmoteTimerRead;
};

_emoteTimer.Start();
}

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

// _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.RandomIntervalSpeak ? _random.Next(1000, 30001) : component.SpeakTimerRead;
// };

// _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.RandomIntervalEmote ? _random.Next(1000, 30001) : component.EmoteTimerRead;
// };

// _speakTimer.Start();
// _emoteTimer.Start();
// }
}
47 changes: 47 additions & 0 deletions Content.Shared/ADT/AutoPostingChat/AutoPostingChatComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Robust.Shared.Audio;
using Content.Shared.Chat.Prototypes;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.ADT.AutoPostingChat;
[RegisterComponent]
[NetworkedComponent]
public sealed partial class AutoPostingChatComponent : Component
{
/// <summary>
///Sets a random interval after each iteration of spoken messages
/// </summary>
[DataField("randomIntervalSpeak"), ViewVariables(VVAccess.ReadWrite)]
public bool RandomIntervalSpeak = false;

/// <summary>
/// Sets a random interval after each iteration of spoken emotions
/// </summary>
[DataField("randomIntervalEmote"), ViewVariables(VVAccess.ReadWrite)]
public bool RandomIntervalEmote = false;
Schrodinger71 marked this conversation as resolved.
Show resolved Hide resolved

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

/// <summary>
/// The interval in milliseconds between automatic emote messages.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("emoteTimer")]
public int EmoteTimerRead = 9000;
Schrodinger71 marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The message that will be automatically spoken by the entity.
/// </summary>
[DataField("speakMessage")]
public string? PostingMessageSpeak = "";

/// <summary>
/// The message that will be automatically emotes by the entity.
/// </summary>
[DataField("emoteMessage")]
public string? PostingMessageEmote = "";
Schrodinger71 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading