jRandomSkills/jRandomSkills - SRC Files/src/player/PlayerOnTick.cs
ByDexter 25cd8eb989 v1.2.2.b6
Performance & stability pass (issue #34 + audit)

General:
- DisableAll resets only skills active in the previous round instead of all loaded skills (~89ms -> ~5ms)
- Removed per-tick LINQ/allocations, O(1) HUD skill lookups, translation substitutions moved to load time
- ForceFullUpdate creates the network service once per broadcast; view-angle guard only skips a genuine (0,0,0) placeholder
- Debug hooks/logging only active when DebugMode is enabled (single reused writer)
- Player skill state unified to a single collection (fixes a rare disconnect race)
- Gamedata signatures resolved lazily/isolated; hardened tick/transmit paths against invalid handles
- Description HUD honours always-show / -1 duration consistently across all assignment paths

Skills:
- Jester: fixed the no-damage effect leaking after a mid-round skill change (Thief/Deactivator)
- Jackal: fixed trail entities accumulating over a round
- Falcon Eye / Third Eye / Spectator: camera state cleared on disable

Also: added missing translation keys as untranslated markers across language files.

Fixes #34
2026-07-05 21:48:29 +03:00

176 lines
7.2 KiB
C#

using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Utils;
using src.utils;
using static CounterStrikeSharp.API.Core.Listeners;
using static src.jRandomSkills;
namespace src.player
{
public static class PlayerOnTick
{
public static void Load()
{
Instance.RegisterListener<OnTick>(() =>
{
UpdateGameRules();
if (Server.TickCount % 2 != 0) return;
foreach (var player in Utilities.GetPlayers())
{
if (player != null && player.IsValid)
UpdatePlayerHud(player);
}
});
Instance.RegisterListener<OnMapStart>(OnMapStart);
Instance.RegisterListener<OnMapEnd>(OnMapEnd);
}
private static void OnMapStart(string mapName)
{
Instance.GameRules = null;
Event.OnMapChange();
BotManager.Initialize();
}
private static void OnMapEnd()
{
BotManager.Stop();
}
private static void InitializeGameRules()
{
if (Instance.GameRules != null) return;
var gameRulesProxy = Utilities.FindAllEntitiesByDesignerName<CCSGameRulesProxy>("cs_gamerules").FirstOrDefault();
if (gameRulesProxy != null)
Instance.GameRules = gameRulesProxy.GameRules;
}
private static void UpdateGameRules()
{
if (Instance?.GameRules == null || Instance.GameRules.Handle == IntPtr.Zero)
InitializeGameRules();
else if (Instance != null && Config.LoadedConfig.EnableFlashingHtmlHudFix && !Instance.GameRules.WarmupPeriod)
Instance.GameRules.GameRestart = Instance.GameRules.RestartRoundTime < Server.CurrentTime;
}
private static void UpdatePlayerHud(CCSPlayerController player)
{
if (player == null || !player.IsValid || player.IsBot) return;
var skillPlayer = PlayerManager.GetPlayerByIndex(PlayerManager.GetPlayerEvent(player)?.Index ?? player.Index);
if (skillPlayer == null || !skillPlayer.DisplayHUD) return;
var now = DateTime.Now;
if (player.PawnIsAlive && skillPlayer.SkillHudExpired < now) return;
string infoLine = string.Empty;
string skillLine = string.Empty;
string remainingLine = string.Empty;
bool showDescriptionHUD = skillPlayer.SkillDescriptionHudExpired >= now || Config.LoadedConfig.DisplayAlwaysDescription;
bool isDescription = true;
var skills = SkillData.Skills;
if (skills == null || skills.IsEmpty)
{
infoLine = player.GetTranslation("your_skill");
skillLine = player.GetTranslation("none");
}
else if (skillPlayer.IsDrawing && player.PawnIsAlive)
{
int skillCount = skills.Count;
if (skillCount > 0)
{
var skillsArray = skills.ToArray();
var randomSkill = skillsArray[Instance.Random.Next(skillCount)];
infoLine = player.GetTranslation("drawing_skill");
skillLine = $"<font color='{randomSkill.Color}'>{player.GetSkillName(randomSkill.Skill)}</font>";
}
}
else
{
if (player.PawnIsAlive)
{
var skillInfo = SkillData.GetInfo(skillPlayer.Skill);
if (skillInfo != null)
{
infoLine = player.GetTranslation("your_skill");
skillLine = $"<font color='{skillInfo.Color}'>{player.GetSkillName(skillInfo.Skill, skillPlayer.SkillChance)}</font>";
if (skillInfo.Skill != Skills.None)
{
remainingLine = string.IsNullOrEmpty(skillPlayer.PrintHTML)
? (showDescriptionHUD ? player.GetSkillDescription(skillInfo.Skill, skillPlayer.SkillChance) : "")
: skillPlayer.PrintHTML;
isDescription = string.IsNullOrEmpty(skillPlayer.PrintHTML);
}
}
}
else
{
if ((player.Team is CsTeam.Spectator or CsTeam.None && Config.LoadedConfig.DisableSpectateHUD)
|| AdminManager.PlayerHasPermissions(player, Config.LoadedConfig.DisableHUDOnDeathPermission))
return;
var pawn = player.Pawn.Value;
if (pawn?.ObserverServices == null) return;
var observerTarget = pawn.ObserverServices.ObserverTarget?.Value;
if (observerTarget == null || !observerTarget.IsValid) return;
var observedPlayer = Utilities.GetPlayers().FirstOrDefault(p =>
p != null && p.IsValid && p.Pawn?.Value?.Handle == observerTarget.Handle);
if (observedPlayer == null) return;
var observedEvent = PlayerManager.GetPlayerEvent(observedPlayer);
if (observedEvent == null || !observedEvent.IsValid) return;
var observedSkill = PlayerManager.GetPlayerByIndex(observedEvent.Index);
if (observedSkill == null) return;
var observedSkillInfo = SkillData.GetInfo(observedSkill.Skill);
var observedSpecialInfo = observedSkill.SpecialSkill != Skills.None
? SkillData.GetInfo(observedSkill.SpecialSkill)
: null;
string primaryName = player.GetSkillName(observedSkill.Skill, observedSkill.SkillChance);
string primaryColor = observedSkillInfo?.Color ?? SkillsInfo.GetValue<string>(Skills.None, "color");
string pName = System.Net.WebUtility.HtmlEncode(observedSkill.PlayerName);
if (pName.Length > 18)
pName = $"{pName[..17]}...";
var observerSkill = player.GetTranslation("observer_skill");
infoLine = string.IsNullOrEmpty(observerSkill) ? pName : $"{observerSkill} {pName}";
if (observedSkill.SpecialSkill == Skills.None || observedSpecialInfo == null)
skillLine = $"<font color='{primaryColor}'>{primaryName}</font>";
else
{
string specialName = player.GetSkillName(observedSpecialInfo.Skill);
skillLine = $"<font color='{observedSpecialInfo.Color}'>{specialName}({primaryName})</font>";
}
if (showDescriptionHUD)
remainingLine = player.GetSkillDescription(observedSkill.Skill, observedSkill.SkillChance);
}
}
if (string.IsNullOrEmpty(skillLine)) return;
if (SkillUtils.HasMenu(player)) return;
Event.UpdateSkillHUD(player, infoLine, skillLine, remainingLine, isDescription);
}
}
}