Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,18 @@
using JetBrains.Annotations;
namespace Content.Server.GameTicking.Rules
{
[PublicAPI]
public abstract class GameRule
{
public virtual void Added()
{
}
public virtual void Removed()
{
}
}
}

View File

@@ -0,0 +1,112 @@
using System;
using System.Threading;
using Content.Server.Chat.Managers;
using Content.Shared;
using Content.Shared.Damage;
using Content.Shared.MobState;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.GameTicking.Rules
{
/// <summary>
/// Simple GameRule that will do a free-for-all death match.
/// Kill everybody else to win.
/// </summary>
public sealed class RuleDeathMatch : GameRule, IEntityEventSubscriber
{
private static readonly TimeSpan DeadCheckDelay = TimeSpan.FromSeconds(5);
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly IGameTicker _gameTicker = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
private CancellationTokenSource? _checkTimerCancel;
public override void Added()
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("The game is now a death match. Kill everybody else to win!"));
_entityManager.EventBus.SubscribeEvent<DamageChangedEventArgs>(EventSource.Local, this, OnHealthChanged);
_playerManager.PlayerStatusChanged += PlayerManagerOnPlayerStatusChanged;
}
public override void Removed()
{
base.Removed();
_entityManager.EventBus.UnsubscribeEvent<DamageChangedEventArgs>(EventSource.Local, this);
_playerManager.PlayerStatusChanged -= PlayerManagerOnPlayerStatusChanged;
}
private void OnHealthChanged(DamageChangedEventArgs message)
{
_runDelayedCheck();
}
private void _checkForWinner()
{
_checkTimerCancel = null;
if (!_cfg.GetCVar(CCVars.GameLobbyEnableWin))
return;
IPlayerSession? winner = null;
foreach (var playerSession in _playerManager.GetAllPlayers())
{
var playerEntity = playerSession.AttachedEntity;
if (playerEntity == null
|| !playerEntity.TryGetComponent(out IMobStateComponent? state))
{
continue;
}
if (!state.IsAlive())
{
continue;
}
if (winner != null)
{
// Found a second person alive, nothing decided yet!
return;
}
winner = playerSession;
}
_chatManager.DispatchServerAnnouncement(winner == null
? Loc.GetString("Everybody is dead, it's a stalemate!")
: Loc.GetString("{0} wins the death match!", winner));
var restartDelay = 10;
_chatManager.DispatchServerAnnouncement(Loc.GetString("Restarting in {0} seconds.", restartDelay));
Timer.Spawn(TimeSpan.FromSeconds(restartDelay), () => _gameTicker.RestartRound());
}
private void PlayerManagerOnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
if (e.NewStatus == SessionStatus.Disconnected)
{
_runDelayedCheck();
}
}
private void _runDelayedCheck()
{
_checkTimerCancel?.Cancel();
_checkTimerCancel = new CancellationTokenSource();
Timer.Spawn(DeadCheckDelay, _checkForWinner, _checkTimerCancel.Token);
}
}
}

View File

@@ -0,0 +1,93 @@
#nullable enable
using System;
using System.Threading;
using Content.Server.Chat.Managers;
using Robust.Server.Player;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.GameTicking.Rules
{
public class RuleInactivityTimeRestart : GameRule
{
[Dependency] private readonly IGameTicker _gameTicker = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
private CancellationTokenSource _timerCancel = new();
public TimeSpan InactivityMaxTime { get; set; } = TimeSpan.FromMinutes(10);
public TimeSpan RoundEndDelay { get; set; } = TimeSpan.FromSeconds(10);
public override void Added()
{
base.Added();
_gameTicker.OnRunLevelChanged += RunLevelChanged;
_playerManager.PlayerStatusChanged += PlayerStatusChanged;
}
public override void Removed()
{
base.Removed();
_gameTicker.OnRunLevelChanged -= RunLevelChanged;
_playerManager.PlayerStatusChanged -= PlayerStatusChanged;
StopTimer();
}
public void RestartTimer()
{
_timerCancel.Cancel();
_timerCancel = new CancellationTokenSource();
Timer.Spawn(InactivityMaxTime, TimerFired, _timerCancel.Token);
}
public void StopTimer()
{
_timerCancel.Cancel();
}
private void TimerFired()
{
_gameTicker.EndRound(Loc.GetString("Time has run out!"));
_chatManager.DispatchServerAnnouncement(Loc.GetString("Restarting in {0} seconds.", (int) RoundEndDelay.TotalSeconds));
Timer.Spawn(RoundEndDelay, () => _gameTicker.RestartRound());
}
private void RunLevelChanged(GameRunLevelChangedEventArgs args)
{
switch (args.NewRunLevel)
{
case GameRunLevel.InRound:
RestartTimer();
break;
case GameRunLevel.PreRoundLobby:
case GameRunLevel.PostRound:
StopTimer();
break;
}
}
private void PlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
if (_gameTicker.RunLevel != GameRunLevel.InRound)
{
return;
}
if (_playerManager.PlayerCount == 0)
{
RestartTimer();
}
else
{
StopTimer();
}
}
}
}

View File

@@ -0,0 +1,70 @@
using System;
using System.Threading;
using Content.Server.Chat.Managers;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.GameTicking.Rules
{
public sealed class RuleMaxTimeRestart : GameRule
{
[Dependency] private readonly IGameTicker _gameTicker = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
private CancellationTokenSource _timerCancel = new();
public TimeSpan RoundMaxTime { get; set; } = TimeSpan.FromMinutes(5);
public TimeSpan RoundEndDelay { get; set; } = TimeSpan.FromSeconds(10);
public override void Added()
{
base.Added();
_gameTicker.OnRunLevelChanged += RunLevelChanged;
}
public override void Removed()
{
base.Removed();
_gameTicker.OnRunLevelChanged -= RunLevelChanged;
StopTimer();
}
public void RestartTimer()
{
_timerCancel.Cancel();
_timerCancel = new CancellationTokenSource();
Timer.Spawn(RoundMaxTime, TimerFired, _timerCancel.Token);
}
public void StopTimer()
{
_timerCancel.Cancel();
}
private void TimerFired()
{
_gameTicker.EndRound(Loc.GetString("Time has run out!"));
_chatManager.DispatchServerAnnouncement(Loc.GetString("Restarting in {0} seconds.", (int) RoundEndDelay.TotalSeconds));
Timer.Spawn(RoundEndDelay, () => _gameTicker.RestartRound());
}
private void RunLevelChanged(GameRunLevelChangedEventArgs args)
{
switch (args.NewRunLevel)
{
case GameRunLevel.InRound:
RestartTimer();
break;
case GameRunLevel.PreRoundLobby:
case GameRunLevel.PostRound:
StopTimer();
break;
}
}
}
}

View File

@@ -0,0 +1,162 @@
using System;
using System.Threading;
using Content.Server.Chat.Managers;
using Content.Server.Doors;
using Content.Server.Players;
using Content.Server.Suspicion;
using Content.Server.Suspicion.EntitySystems;
using Content.Server.Suspicion.Roles;
using Content.Shared;
using Content.Shared.MobState;
using Robust.Server.Player;
using Robust.Shared.Audio;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Timing;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.GameTicking.Rules
{
/// <summary>
/// Simple GameRule that will do a TTT-like gamemode with traitors.
/// </summary>
public sealed class RuleSuspicion : GameRule, IEntityEventSubscriber
{
private static readonly TimeSpan DeadCheckDelay = TimeSpan.FromSeconds(1);
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly IGameTicker _gameTicker = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IGameTiming _timing = default!;
private readonly CancellationTokenSource _checkTimerCancel = new();
private TimeSpan _endTime;
public TimeSpan RoundMaxTime { get; set; } = TimeSpan.FromSeconds(CCVars.SuspicionMaxTimeSeconds.DefaultValue);
public TimeSpan RoundEndDelay { get; set; } = TimeSpan.FromSeconds(10);
public override void Added()
{
RoundMaxTime = TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.SuspicionMaxTimeSeconds));
_endTime = _timing.CurTime + RoundMaxTime;
_chatManager.DispatchServerAnnouncement(Loc.GetString("There are traitors on the station! Find them, and kill them!"));
var filter = Filter.Empty()
.AddWhere(session => ((IPlayerSession)session).ContentData()?.Mind?.HasRole<SuspicionTraitorRole>() ?? false);
SoundSystem.Play(filter, "/Audio/Misc/tatoralert.ogg", AudioParams.Default);
EntitySystem.Get<SuspicionEndTimerSystem>().EndTime = _endTime;
EntitySystem.Get<DoorSystem>().AccessType = DoorSystem.AccessTypes.AllowAllNoExternal;
Timer.SpawnRepeating(DeadCheckDelay, CheckWinConditions, _checkTimerCancel.Token);
}
public override void Removed()
{
base.Removed();
EntitySystem.Get<DoorSystem>().AccessType = DoorSystem.AccessTypes.Id;
EntitySystem.Get<SuspicionEndTimerSystem>().EndTime = null;
_checkTimerCancel.Cancel();
}
private void Timeout()
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("Time has run out for the traitors!"));
EndRound(Victory.Innocents);
}
private void CheckWinConditions()
{
if (!_cfg.GetCVar(CCVars.GameLobbyEnableWin))
return;
var traitorsAlive = 0;
var innocentsAlive = 0;
foreach (var playerSession in _playerManager.GetAllPlayers())
{
if (playerSession.AttachedEntity == null
|| !playerSession.AttachedEntity.TryGetComponent(out IMobStateComponent? mobState)
|| !playerSession.AttachedEntity.HasComponent<SuspicionRoleComponent>())
{
continue;
}
if (!mobState.IsAlive())
{
continue;
}
var mind = playerSession.ContentData()?.Mind;
if (mind != null && mind.HasRole<SuspicionTraitorRole>())
traitorsAlive++;
else
innocentsAlive++;
}
if (innocentsAlive + traitorsAlive == 0)
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("Everybody is dead, it's a stalemate!"));
EndRound(Victory.Stalemate);
}
else if (traitorsAlive == 0)
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("The traitors are dead! The innocents win."));
EndRound(Victory.Innocents);
}
else if (innocentsAlive == 0)
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("The innocents are dead! The traitors win."));
EndRound(Victory.Traitors);
}
else if (_timing.CurTime > _endTime)
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("Time has run out for the traitors!"));
EndRound(Victory.Innocents);
}
}
private enum Victory
{
Stalemate,
Innocents,
Traitors
}
private void EndRound(Victory victory)
{
string text;
switch (victory)
{
case Victory.Innocents:
text = Loc.GetString("The innocents have won!");
break;
case Victory.Traitors:
text = Loc.GetString("The traitors have won!");
break;
default:
text = Loc.GetString("Nobody wins!");
break;
}
_gameTicker.EndRound(text);
_chatManager.DispatchServerAnnouncement(Loc.GetString("Restarting in {0} seconds.", (int) RoundEndDelay.TotalSeconds));
_checkTimerCancel.Cancel();
Timer.Spawn(RoundEndDelay, () => _gameTicker.RestartRound());
}
}
}

View File

@@ -0,0 +1,25 @@
using Content.Server.Chat.Managers;
using Content.Server.Players;
using Content.Server.Traitor;
using Robust.Server.Player;
using Robust.Shared.Audio;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
namespace Content.Server.GameTicking.Rules
{
public class RuleTraitor : GameRule
{
[Dependency] private readonly IChatManager _chatManager = default!;
public override void Added()
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("Hello crew! Have a good shift!"));
var filter = Filter.Empty()
.AddWhere(session => ((IPlayerSession)session).ContentData()?.Mind?.HasRole<TraitorRole>() ?? false);
SoundSystem.Play(filter, "/Audio/Misc/tatoralert.ogg", AudioParams.Default);
}
}
}

View File

@@ -0,0 +1,7 @@
namespace Content.Server.GameTicking.Rules
{
public class RuleTraitorDeathMatch : GameRule
{
// This class only exists so that the game rule is available for the conditional spawner.
}
}