Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ntm5 committed Dec 16, 2024
1 parent e2141e4 commit 30dbcb0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 376 deletions.
90 changes: 74 additions & 16 deletions mod/TTT.Detective/DetectiveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
using TTT.Player;
using TTT.Public.Action;
using TTT.Public.Behaviors;
using TTT.Public.Extensions;
using TTT.Public.Formatting;
Expand All @@ -29,27 +31,49 @@ public void Start(BasePlugin parent)
.Where(player => (player.Buttons & PlayerButtons.Use) != 0)) OnPlayerUse(player);
});


/**
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(hook =>
{
var info = hook.GetParam<CTakeDamageInfo>(1);
if (info.Attacker.Value == null || !info.Attacker.Value.IsValid) return HookResult.Continue;
var attacker = info.Attacker.Value.As<CCSPlayerController>();
if (attacker == hook.GetParam<CBaseEntity>(0)) return HookResult.Continue;
if (info.AmmoType is not TaserAmmoType) return HookResult.Continue;
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnZeus, HookMode.Pre);

info.Damage = 1f;
}

if (!attacker.IsReal()) return HookResult.Continue;

var ammoType = info.AmmoType;
private HookResult OnZeus(DynamicHook hook)
{
var ent = hook.GetParam<CBaseEntity>(0);

return HookResult.Changed;
}, HookMode.Pre);
*/
}
var playerWhoWasDamaged = player(ent);

if (playerWhoWasDamaged == null) return HookResult.Continue;

var info = hook.GetParam<CTakeDamageInfo>(1);

CCSPlayerController? attacker = null;

if (info.Attacker.Value != null)
{
var playerWhoAttacked = info.Attacker.Value.As<CCSPlayerPawn>();

attacker = playerWhoAttacked.Controller.Value.As<CCSPlayerController>();

}

if (info.BitsDamageType != DamageTypes_t.DMG_SHOCK) return HookResult.Continue;
if (attacker == null) return HookResult.Continue;

info.Damage = 0;

var targetRole = _roleService.GetPlayer(playerWhoWasDamaged);

Server.NextFrame(() =>
{
attacker.PrintToChat(
StringUtils.FormatTTT(
$"You tased player {playerWhoWasDamaged.PlayerName} they are a {targetRole.PlayerRole().FormatRoleFull()}"));
});

//_roundService.GetLogsService().AddLog(new MiscAction("tased player " + targetRole.PlayerRole().FormatStringFullAfter(playerWhoWasDamaged.PlayerName), attacker));

return HookResult.Stop;
}

private void OnPlayerUse(CCSPlayerController player)
{
Expand Down Expand Up @@ -92,4 +116,38 @@ private void IdentifyBody(CCSPlayerController caller)

Server.NextFrame(() => { Server.PrintToChatAll(message); });
}

private static CCSPlayerController? player(CEntityInstance? instance)
{
if (instance == null)
{
return null;
}

if (instance.DesignerName != "player")
{
return null;
}

// grab the pawn index
int player_index = (int)instance.Index;

// grab player controller from pawn
CCSPlayerPawn player_pawn = Utilities.GetEntityFromIndex<CCSPlayerPawn>(player_index);

// pawn valid
if (player_pawn == null || !player_pawn.IsValid)
{
return null;
}

// controller valid
if (player_pawn.OriginalController == null || !player_pawn.OriginalController.IsValid)
{
return null;
}

// any further validity is up to the caller
return player_pawn.OriginalController.Value;
}
}
84 changes: 0 additions & 84 deletions mod/TTT.Roles/Helper/InfoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public InfoManager(RoleManager roleService, IRoundService manager, BasePlugin pl
{
_roleService = roleService;
_manager = manager;
plugin.RegisterListener<Listeners.OnTick>(OnTick);
plugin.AddTimer(0.3f, OnTickAll, TimerFlags.REPEAT);
plugin.AddTimer(0.1f, OnTickScoreboard, TimerFlags.REPEAT);

plugin.RegisterEventHandler<EventSpecTargetUpdated>(OnPlayerSpectateChange);
Expand Down Expand Up @@ -60,88 +58,6 @@ public void OnTickScoreboard()
}


public void OnTick()
{
foreach (var gamePlayer in _roleService.Players())
{
if (_manager.GetRoundStatus() != RoundStatus.Started) return;

var player = gamePlayer.Player();

if (player == null) continue;
if (!player.IsReal()) continue;

var playerRole = gamePlayer.PlayerRole();

if (playerRole == Role.Unassigned) continue;
if (gamePlayer.ShopOpen()) continue;

if (!player.PawnIsAlive && AdminManager.PlayerHasPermissions(player, "@css/kick"))
{
if (player.ObserverPawn.Value == null || !player.ObserverPawn.Value.IsValid) continue;
if (player.ObserverPawn.Value.ObserverServices?.ObserverTarget.Value is null) continue;
var target = player.ObserverPawn.Value.ObserverServices.ObserverTarget.Value?.As<CCSPlayerController>();
if (target == null)
{
Server.NextFrame(() =>
player.PrintToCenterHtml(
$"<font class='fontsize=m' color='yellow'>Your Role: {playerRole.GetCenterRole()}"));
}
else
{
Server.NextFrame(() => player.PrintToCenterHtml(
$"<font class='fontsize=m' color='yellow'>Your Role: {playerRole.GetCenterRole()} <br>"
+ $"<font class='fontsize=m' color='yellow'>{target.PlayerName}'s Role: {_roleService.GetRole(target).GetCenterRole()}"));
}

continue;
}

if (!_playerLookAtRole.TryGetValue(player, out var value))
{
Server.NextFrame(() =>
player.PrintToCenterHtml(
$"<font class='fontsize=m' color='yellow'>Your Role: {playerRole.GetCenterRole()}"));
continue;
}

if (!value.Item1.IsReal()) continue;

if (value.Item2 == playerRole || playerRole == Role.Traitor || value.Item2 == Role.Detective)
{
Server.NextFrame(() => player.PrintToCenterHtml(
$"<font class='fontsize=m' color='yellow'>Your Role: {playerRole.GetCenterRole()} <br>"
+ $"<font class='fontsize=m' color='yellow'>{value.Item1.PlayerName}'s Role: {value.Item2.GetCenterRole()}"));
}


if (playerRole != Role.Innocent && (value.Item2 != Role.Traitor || playerRole != Role.Detective)) continue;

Server.NextFrame(() => player.PrintToCenterHtml(
$"<font class='fontsize=m' color='yellow'>Your Role: {playerRole.GetCenterRole()} <br>"
+ $"<font class='fontsize=m' color='yellow'>{value.Item1.PlayerName}'s Role: {Role.Unassigned.GetCenterRole()}"));
}
}

public void OnTickAll()
{
var players = _roleService.GetPlayers().Keys;

_playerLookAtRole.Clear();

foreach (var player in players)
{
if (!player.IsReal()) continue;

var target = player.GetClientPlayerAimTarget();

if (target == null) continue;
if (!target.IsReal()) continue;

RegisterLookAtRole(player, new Tuple<CCSPlayerController, Role>(target, _roleService.GetRole(target)));
}
}

[GameEventHandler]
private HookResult OnPlayerSpectateChange(EventSpecTargetUpdated @event, GameEventInfo info)
{
Expand Down
Loading

0 comments on commit 30dbcb0

Please sign in to comment.