Files
OldThink/Content.Shared/Roles/SharedRoleSystem.cs
MJSailor a95fe13180 Goida revert (#600)
* Revert "- fix: YAML linter fixes. (#598)"

This reverts commit 012bf3c357.

* Revert "Automatic changelog update"

This reverts commit cf1c3a9af5.

* Revert "[Fix] Base Layer Prototype (#597)"

This reverts commit b000423999.

* Revert "Modules update (#596)"

This reverts commit 00fbdead77.

* Revert "Automatic changelog update"

This reverts commit 0d7a12b2a2.

* Revert "Fixes (#593)"

This reverts commit 943c77031c.

* Revert "minor loadout fixes (#594)"

This reverts commit 143c010a89.

* Revert "Update DryDock.yml (#595)"

This reverts commit 4cd0100ac7.

* Revert "Automatic changelog update"

This reverts commit 08eadc690f.

* Revert "fix: Maximum message size (#591)"

This reverts commit 343f3612eb.

* Revert "Черри пики 7 (#592)"

This reverts commit 3f97bdce2f.

* Revert "Automatic changelog update"

This reverts commit 0678eca250.

* Revert "Рандомфиксы (#590)"

This reverts commit 2b9e5e2437.

* Revert "Нижнее бельё в лодауты (#580)"

This reverts commit e01a47b089.

* Revert "add lathe sounds (#588)"

This reverts commit c80a2985f2.

* Revert "Добавил параметр группы для некоторых реагентов (#585)"

This reverts commit 713b16bb98.

* Revert "add hrp ++++ aspect (#587)"

This reverts commit a6a69cc60f.

* Revert "Новые амбиенты и пару песен (#586)"

This reverts commit 48c86bd846.

* Revert "Сообщения в ПДА 2 (#583)"

This reverts commit cced3cc98b.

* Revert "Automatic changelog update"

This reverts commit abf435b11d.

* Revert "Chem stuff and more (#584)"

This reverts commit 3608960f5c.

* Revert "JobRequiremet refactor (#579)"

This reverts commit 9a9c9598e0.

* Revert "Revert "Reapply "Нижнее бельё в лодауты"""

This reverts commit 44447d573f.

* Revert "Reapply "Нижнее бельё в лодауты""

This reverts commit 0c4d082ad3.

* Revert "Revert "Нижнее бельё в лодауты""

This reverts commit 56473c5492.

* Revert "Нижнее бельё в лодауты"

This reverts commit d1cb0cb364.

* Revert "DryDock and WhiteMoose update (#578)"

This reverts commit 14755808af.

* Revert "Automatic changelog update"

This reverts commit 0133f82722.

* Revert "Fixes (#576)"

This reverts commit b7cc49896c.

* Revert "порт системы регенерации солюшена цинки (#574)"

This reverts commit a22cf3d50b.

* Revert "Воровские перчатки (#573)"

This reverts commit bb7140f3d4.

* Revert "mood resprite (#572)"

This reverts commit 4db96dc569.

* Revert "fix missing letter (#571)"

This reverts commit 94ea756794.

* Revert "Сообщения в ПДА (#564)"

This reverts commit d023d29e54.

* Revert "- fix: No visible aghost."

This reverts commit 27e7f25f7e.

* Revert "- tweak: Nerf cult shield."

This reverts commit 6a384246b8.
2024-08-09 19:09:22 +03:00

190 lines
6.4 KiB
C#

using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.Mind;
using Content.Shared.Roles.Jobs;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Roles;
public abstract class SharedRoleSystem : EntitySystem
{
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedMindSystem _minds = default!;
// TODO please lord make role entities
private readonly HashSet<Type> _antagTypes = new();
public override void Initialize()
{
// TODO make roles entities
SubscribeLocalEvent<JobComponent, MindGetAllRolesEvent>(OnJobGetAllRoles);
}
private void OnJobGetAllRoles(EntityUid uid, JobComponent component, ref MindGetAllRolesEvent args)
{
var name = "game-ticker-unknown-role";
var prototype = "";
string? playTimeTracker = null;
if (component.Prototype != null && _prototypes.TryIndex(component.Prototype, out JobPrototype? job))
{
name = job.Name;
prototype = job.ID;
playTimeTracker = job.PlayTimeTracker;
}
name = Loc.GetString(name);
args.Roles.Add(new RoleInfo(component, name, false, playTimeTracker, prototype));
}
protected void SubscribeAntagEvents<T>() where T : AntagonistRoleComponent
{
SubscribeLocalEvent((EntityUid _, T component, ref MindGetAllRolesEvent args) =>
{
var name = "game-ticker-unknown-role";
var prototype = "";
if (component.PrototypeId != null && _prototypes.TryIndex(component.PrototypeId, out AntagPrototype? antag))
{
name = antag.Name;
prototype = antag.ID;
}
name = Loc.GetString(name);
args.Roles.Add(new RoleInfo(component, name, true, null, prototype));
});
SubscribeLocalEvent((EntityUid _, T _, ref MindIsAntagonistEvent args) => { args.IsAntagonist = true; args.IsExclusiveAntagonist |= typeof(T).TryGetCustomAttribute<ExclusiveAntagonistAttribute>(out _); });
_antagTypes.Add(typeof(T));
}
/// <summary>
/// Gives this mind a new role.
/// </summary>
/// <param name="mindId">The mind to add the role to.</param>
/// <param name="component">The role instance to add.</param>
/// <typeparam name="T">The role type to add.</typeparam>
/// <param name="silent">Whether or not the role should be added silently</param>
/// <returns>The instance of the role.</returns>
/// <exception cref="ArgumentException">
/// Thrown if we already have a role with this type.
/// </exception>
public void MindAddRole<T>(EntityUid mindId, T component, MindComponent? mind = null, bool silent = false) where T : IComponent, new()
{
if (!Resolve(mindId, ref mind))
return;
if (HasComp<T>(mindId))
{
throw new ArgumentException($"We already have this role: {typeof(T)}");
}
AddComp(mindId, component);
var antagonist = IsAntagonistRole<T>();
var mindEv = new MindRoleAddedEvent(silent);
RaiseLocalEvent(mindId, ref mindEv);
var message = new RoleAddedEvent(mindId, mind, antagonist, silent);
if (mind.OwnedEntity != null)
{
RaiseLocalEvent(mind.OwnedEntity.Value, message, true);
}
_adminLogger.Add(LogType.Mind, LogImpact.Low,
$"'Role {typeof(T).Name}' added to mind of {_minds.MindOwnerLoggingString(mind)}");
}
/// <summary>
/// Removes a role from this mind.
/// </summary>
/// <param name="mindId">The mind to remove the role from.</param>
/// <typeparam name="T">The type of the role to remove.</typeparam>
/// <exception cref="ArgumentException">
/// Thrown if we do not have this role.
/// </exception>
public void MindRemoveRole<T>(EntityUid mindId) where T : IComponent
{
if (!RemComp<T>(mindId))
{
throw new ArgumentException($"We do not have this role: {typeof(T)}");
}
var mind = Comp<MindComponent>(mindId);
var antagonist = IsAntagonistRole<T>();
var message = new RoleRemovedEvent(mindId, mind, antagonist);
if (mind.OwnedEntity != null)
{
RaiseLocalEvent(mind.OwnedEntity.Value, message, true);
}
_adminLogger.Add(LogType.Mind, LogImpact.Low,
$"'Role {typeof(T).Name}' removed from mind of {_minds.MindOwnerLoggingString(mind)}");
}
public bool MindTryRemoveRole<T>(EntityUid mindId) where T : IComponent
{
if (!MindHasRole<T>(mindId))
return false;
MindRemoveRole<T>(mindId);
return true;
}
public bool MindHasRole<T>(EntityUid mindId) where T : IComponent
{
return HasComp<T>(mindId);
}
public List<RoleInfo> MindGetAllRoles(EntityUid mindId)
{
var ev = new MindGetAllRolesEvent(new List<RoleInfo>());
RaiseLocalEvent(mindId, ref ev);
return ev.Roles;
}
public bool MindIsAntagonist(EntityUid? mindId)
{
if (mindId == null)
return false;
var ev = new MindIsAntagonistEvent();
RaiseLocalEvent(mindId.Value, ref ev);
return ev.IsAntagonist;
}
/// <summary>
/// Does this mind possess an exclusive antagonist role
/// </summary>
/// <param name="mindId">The mind entity</param>
/// <returns>True if the mind possesses an exclusive antag role</returns>
public bool MindIsExclusiveAntagonist(EntityUid? mindId)
{
if (mindId == null)
return false;
var ev = new MindIsAntagonistEvent();
RaiseLocalEvent(mindId.Value, ref ev);
return ev.IsExclusiveAntagonist;
}
public bool IsAntagonistRole<T>()
{
return _antagTypes.Contains(typeof(T));
}
/// <summary>
/// Play a sound for the mind, if it has a session attached.
/// Use this for role greeting sounds.
/// </summary>
public void MindPlaySound(EntityUid mindId, SoundSpecifier? sound, MindComponent? mind = null)
{
if (Resolve(mindId, ref mind) && mind.Session != null)
_audio.PlayGlobal(sound, mind.Session);
}
}