Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Spatison committed Sep 16, 2024
1 parent 1f84c4b commit 67c6eb6
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 122 deletions.
8 changes: 0 additions & 8 deletions Content.Server/GameTicking/GameTicker.GamePreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,6 @@ public bool OnGhostAttempt(EntityUid mindId, bool canReturnGlobal, bool viaComma
else
_mind.TransferTo(mindId, ghost, mind: mind);

// WD EDIT START
if (mind.Session == null)
return true;

var userId = mind.Session!.UserId;
if (!_ghostReturnToRound.DeathTime.TryGetValue(userId, out _))
_ghostReturnToRound.DeathTime[userId] = _gameTiming.CurTime;
// WD EDIT END
return true;
}

Expand Down
101 changes: 49 additions & 52 deletions Content.Server/GameTicking/GameTicker.Spawning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,61 +158,17 @@ private void SpawnPlayer(ICommonSession player, HumanoidCharacterProfile charact

//WD EDIT START
//Ghost system return to round, check for whether the character isn't the same.
if (lateJoin && !_adminManager.IsAdmin(player))
if (lateJoin && !_adminManager.IsAdmin(player) && !CheckGhostReturnToRound(player, character, out var checkAvoid))
{
var sameChar = false;
var checkAvoid = false;
var message = checkAvoid
? Loc.GetString("ghost-respawn-same-character-slightly-changed-name")
: Loc.GetString("ghost-respawn-same-character");
var wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));

var allPlayerMinds = EntityQuery<MindComponent>()
.Where(mind => mind.OriginalOwnerUserId == player.UserId);
_chatManager.ChatMessageToOne(ChatChannel.Server, message, wrappedMessage,
default, false, player.Channel, Color.Red);

foreach (var mind in allPlayerMinds)
{
if (mind.CharacterName == character.Name)
{
sameChar = true;
break;
}

if (mind.CharacterName != null)
{
var similarity = CalculateStringSimilarity(mind.CharacterName, character.Name);

switch (similarity)
{
case >= 85f:
{
_chatManager.SendAdminAlert(Loc.GetString("ghost-respawn-log-character-almost-same",
("player", player.Name), ("try", false), ("oldName", mind.CharacterName), ("newName", character.Name)));

checkAvoid = true;
sameChar = true;

break;
}
case >= 50f:
{
_chatManager.SendAdminAlert(Loc.GetString("ghost-respawn-log-character-almost-same",
("player", player.Name), ("try", true), ("oldName", mind.CharacterName), ("newName", character.Name)));

break;
}
}
}
}

if (sameChar)
{
var message = checkAvoid
? Loc.GetString("ghost-respawn-same-character-slightly-changed-name")
: Loc.GetString("ghost-respawn-same-character");
var wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));

_chatManager.ChatMessageToOne(ChatChannel.Server, message, wrappedMessage,
default, false, player.Channel, Color.Red);

return;
}
return;
}
//WD EDIT END

Expand Down Expand Up @@ -409,6 +365,47 @@ public void SpawnObserver(ICommonSession player)
}

//WD EDIT START
private bool CheckGhostReturnToRound(ICommonSession player, HumanoidCharacterProfile character, out bool checkAvoid)
{
checkAvoid = false;

var allPlayerMinds = EntityQuery<MindComponent>()
.Where(mind => mind.OriginalOwnerUserId == player.UserId);

foreach (var mind in allPlayerMinds)
{
if (mind.CharacterName == character.Name)
return false;

if (mind.CharacterName == null)
continue;

var similarity = CalculateStringSimilarity(mind.CharacterName, character.Name);
switch (similarity)
{
case >= 85f:
{
_chatManager.SendAdminAlert(Loc.GetString("ghost-respawn-log-character-almost-same",
("player", player.Name), ("try", false), ("oldName", mind.CharacterName),
("newName", character.Name)));
checkAvoid = true;

return false;
}
case >= 50f:
{
_chatManager.SendAdminAlert(Loc.GetString("ghost-respawn-log-character-almost-same",
("player", player.Name), ("try", true), ("oldName", mind.CharacterName),
("newName", character.Name)));

break;
}
}
}

return true;
}

private float CalculateStringSimilarity(string str1, string str2)
{
var minLength = Math.Min(str1.Length, str2.Length);
Expand Down
89 changes: 31 additions & 58 deletions Content.Server/_White/Ghost/GhostReturnToRoundSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,91 +18,64 @@ public sealed class GhostReturnToRoundSystem : EntitySystem
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;

public readonly Dictionary<NetUserId, TimeSpan> DeathTime = new();
[Dependency] private readonly IConfigurationManager _cfg = default!;

public override void Initialize()
{
SubscribeNetworkEvent<GhostReturnToRoundRequest>(OnGhostReturnToRoundRequest);
SubscribeLocalEvent<RoundRestartCleanupEvent>(ResetDeathTimes);
}

private void OnGhostReturnToRoundRequest(GhostReturnToRoundRequest msg, EntitySessionEventArgs args)
{
var cfg = IoCManager.Resolve<IConfigurationManager>();
var maxPlayers = cfg.GetCVar(WhiteCVars.GhostRespawnMaxPlayers);
var connectedClient = args.SenderSession.ConnectedClient;
if (_playerManager.PlayerCount >= maxPlayers)
{
var message = Loc.GetString("ghost-respawn-max-players", ("players", maxPlayers));
var wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));
_chatManager.ChatMessageToOne(Shared.Chat.ChatChannel.Server,
message,
wrappedMessage,
default,
false,
connectedClient,
Color.Red);
var uid = args.SenderSession.AttachedEntity;

if (uid == null)
return;
}

var connectedClient = args.SenderSession.ConnectedClient;
var userId = args.SenderSession.UserId;
if (!DeathTime.TryGetValue(userId, out var deathTime))

TryGhostReturnToRound(uid.Value, connectedClient, userId, out var message, out var wrappedMessage);

_chatManager.ChatMessageToOne(Shared.Chat.ChatChannel.Server,
message,
wrappedMessage,
default,
false,
connectedClient,
Color.Red);
}

private void TryGhostReturnToRound(EntityUid uid, INetChannel connectedClient, NetUserId userId, out string message, out string wrappedMessage)
{
var maxPlayers = _cfg.GetCVar(WhiteCVars.GhostRespawnMaxPlayers);
if (_playerManager.PlayerCount >= maxPlayers)
{
var message = Loc.GetString("ghost-respawn-bug");
var wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));
_chatManager.ChatMessageToOne(Shared.Chat.ChatChannel.Server,
message,
wrappedMessage,
default,
false,
connectedClient,
Color.Red);
DeathTime[userId] = _gameTiming.CurTime;
message = Loc.GetString("ghost-respawn-max-players", ("players", maxPlayers));
wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));
return;
}

var timeUntilRespawn = (double) cfg.GetCVar(WhiteCVars.GhostRespawnTime);
var deathTime = EnsureComp<GhostComponent>(uid).TimeOfDeath;
var timeUntilRespawn = _cfg.GetCVar(WhiteCVars.GhostRespawnTime);
var timePast = (_gameTiming.CurTime - deathTime).TotalMinutes;
if (timePast >= timeUntilRespawn)
{
var ticker = Get<GameTicker>();
var playerMgr = IoCManager.Resolve<IPlayerManager>();
playerMgr.TryGetSessionById(userId, out var targetPlayer);
_playerManager.TryGetSessionById(userId, out var targetPlayer);

if (targetPlayer != null)
ticker.Respawn(targetPlayer);
DeathTime.Remove(userId);

_adminLogger.Add(LogType.Mind, LogImpact.Medium, $"{Loc.GetString("ghost-respawn-log-return-to-lobby", ("userName", connectedClient.UserName))}");

var message = Loc.GetString("ghost-respawn-window-rules-footer");
var wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));
_chatManager.ChatMessageToOne(Shared.Chat.ChatChannel.Server,
message,
wrappedMessage,
default,
false,
connectedClient,
Color.Red);
message = Loc.GetString("ghost-respawn-window-rules-footer");
wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));

return;
}
else
{
var message = Loc.GetString("ghost-respawn-time-left", ("time", (int) (timeUntilRespawn - timePast)));
var wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));
_chatManager.ChatMessageToOne(Shared.Chat.ChatChannel.Server,
message,
wrappedMessage,
default,
false,
connectedClient,
Color.Red);
}
}

private void ResetDeathTimes(RoundRestartCleanupEvent ev)
{
DeathTime.Clear();
message = Loc.GetString("ghost-respawn-time-left", ("time", (int) (timeUntilRespawn - timePast)));
wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));
}
}
4 changes: 2 additions & 2 deletions Content.Shared/_White/CVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static readonly CVarDef<string>
#endregion

#region GhostRespawn
public static readonly CVarDef<float> GhostRespawnTime =
CVarDef.Create("ghost.respawn_time", 15f, CVar.SERVERONLY);
public static readonly CVarDef<double> GhostRespawnTime =
CVarDef.Create("ghost.respawn_time", 15d, CVar.SERVERONLY);

public static readonly CVarDef<int> GhostRespawnMaxPlayers =
CVarDef.Create("ghost.respawn_max_players", 40, CVar.SERVERONLY);
Expand Down
1 change: 0 additions & 1 deletion Resources/Locale/en-US/_white/ghost/ghost-respawn.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ ghost-respawn-time-left = Before the opportunity to return to the round { $time
ghost-respawn-max-players = The function is not available, there should be fewer players on the server { $players }.
ghost-respawn-window-title = Rules for returning to the round
ghost-respawn-window-rules-footer = By using this feature, you [color=#ff7700]agree[/color] [color=#ff0000]not to transfer[/color] the knowledge of your past character to a new one. For violation of the clause specified here, [color=#ff0000]a ban in the amount of 3 days or more follows[/color].
ghost-respawn-bug = There is no time of death. The standard value is set.
ghost-respawn-same-character = You cannot enter the round for the same character. Change it in the character settings.
ghost-respawn-log-character-almost-same = Player { $player } { $try ->
Expand Down
1 change: 0 additions & 1 deletion Resources/Locale/ru-RU/_white/ghost/ghost-respawn.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ ghost-respawn-time-left = До возможности вернуться в ра
ghost-respawn-max-players = Функция недоступна, игроков на сервере должно быть меньше { $players }.
ghost-respawn-window-title = Правила возвращения в раунд
ghost-respawn-window-rules-footer = Пользуясь это функцией, вы [color=#ff7700]обязуетесь[/color] [color=#ff0000]не переносить[/color] знания своего прошлого персонажа в нового. За нарушение пункта, указанного здесь, следует [color=#ff0000]бан в размере от 3-ех дней[/color].
ghost-respawn-bug = Нет времени смерти. Установлено стандартное значение.
ghost-respawn-same-character = Нельзя заходить в раунд за того же персонажа. Поменяйте его в настройках персонажей.
ghost-respawn-log-character-almost-same = Игрок { $player } { $try ->
Expand Down

0 comments on commit 67c6eb6

Please sign in to comment.