Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Refactor entire project #1

Closed
wants to merge 15 commits into from
28 changes: 28 additions & 0 deletions TTT.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TTT.Round", "mod\TTT.Round\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TTT.Shop", "mod\TTT.Shop\TTT.Shop.csproj", "{DFBB6B95-ED8D-4E02-BAFA-C705ACBC6DB2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TTT.Formatting", "public\TTT.Formatting\TTT.Formatting.csproj", "{C6EE882B-E597-45DB-85FF-A521DC7A0AAE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TTT.Logs", "mod\TTT.Logs\TTT.Logs.csproj", "{E76CD078-0F1B-4EE8-9A19-EE93BAE3FA6B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TTT.Player", "mod\TTT.Player\TTT.Player.csproj", "{D9327D60-67EE-4AF3-BD99-8A95634600B5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TTT.Manager", "mod\TTT.Manager\TTT.Manager.csproj", "{C3C26EF9-0E5E-4AAF-AE00-EFF4BB129FDB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -60,6 +68,22 @@ Global
{DFBB6B95-ED8D-4E02-BAFA-C705ACBC6DB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DFBB6B95-ED8D-4E02-BAFA-C705ACBC6DB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DFBB6B95-ED8D-4E02-BAFA-C705ACBC6DB2}.Release|Any CPU.Build.0 = Release|Any CPU
{C6EE882B-E597-45DB-85FF-A521DC7A0AAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C6EE882B-E597-45DB-85FF-A521DC7A0AAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6EE882B-E597-45DB-85FF-A521DC7A0AAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6EE882B-E597-45DB-85FF-A521DC7A0AAE}.Release|Any CPU.Build.0 = Release|Any CPU
{E76CD078-0F1B-4EE8-9A19-EE93BAE3FA6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E76CD078-0F1B-4EE8-9A19-EE93BAE3FA6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E76CD078-0F1B-4EE8-9A19-EE93BAE3FA6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E76CD078-0F1B-4EE8-9A19-EE93BAE3FA6B}.Release|Any CPU.Build.0 = Release|Any CPU
{D9327D60-67EE-4AF3-BD99-8A95634600B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9327D60-67EE-4AF3-BD99-8A95634600B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9327D60-67EE-4AF3-BD99-8A95634600B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9327D60-67EE-4AF3-BD99-8A95634600B5}.Release|Any CPU.Build.0 = Release|Any CPU
{C3C26EF9-0E5E-4AAF-AE00-EFF4BB129FDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3C26EF9-0E5E-4AAF-AE00-EFF4BB129FDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3C26EF9-0E5E-4AAF-AE00-EFF4BB129FDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3C26EF9-0E5E-4AAF-AE00-EFF4BB129FDB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{A917B0F5-762F-4C4A-9F50-02BA4A0C6B63} = {C7CCF187-9F99-4091-B092-6397C35BE9F1}
Expand All @@ -69,5 +93,9 @@ Global
{0E7A451E-D56F-4D96-9B5C-5F1B0A6B0656} = {14FF3D75-F9CF-4A32-9F59-83765EA68874}
{511E712D-31D5-4EFC-9F0F-D9D4A83ED202} = {14FF3D75-F9CF-4A32-9F59-83765EA68874}
{DFBB6B95-ED8D-4E02-BAFA-C705ACBC6DB2} = {14FF3D75-F9CF-4A32-9F59-83765EA68874}
{C6EE882B-E597-45DB-85FF-A521DC7A0AAE} = {C84CEB1F-0132-43EF-9A5F-12C7AA08B8E7}
{E76CD078-0F1B-4EE8-9A19-EE93BAE3FA6B} = {14FF3D75-F9CF-4A32-9F59-83765EA68874}
{D9327D60-67EE-4AF3-BD99-8A95634600B5} = {14FF3D75-F9CF-4A32-9F59-83765EA68874}
{C3C26EF9-0E5E-4AAF-AE00-EFF4BB129FDB} = {14FF3D75-F9CF-4A32-9F59-83765EA68874}
EndGlobalSection
EndGlobal
1 change: 0 additions & 1 deletion a

This file was deleted.

7 changes: 7 additions & 0 deletions mod/TTT.Detective/DetectiveConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TTT.Detective;

public class DetectiveConfig
{
public bool DNAScannerEnabled { get; } = true;

}
92 changes: 75 additions & 17 deletions mod/TTT.Detective/DetectiveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
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.Behaviors;
using TTT.Public.Extensions;
using TTT.Public.Formatting;
using TTT.Public.Mod.Detective;
using TTT.Public.Mod.Role;
using TTT.Public.Player;

namespace TTT.Detective;

public class DetectiveManager : IDetectiveService, IPluginBehavior
{
private const int TaserAmmoType = 18;
private readonly IRoleService _roleService;
private readonly IPlayerService _roleService;

public DetectiveManager(IRoleService roleService)
public DetectiveManager(IPlayerService roleService)
{
_roleService = roleService;
}
Expand All @@ -30,24 +32,45 @@ public void Start(BasePlugin parent)
});


/**
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(hook =>
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnZeus, HookMode.Pre);

}

public HookResult OnZeus(DynamicHook hook)
{
var ent = hook.GetParam<CBaseEntity>(0);

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 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;
var playerWhoAttacked = info.Attacker.Value.As<CCSPlayerPawn>();

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

}

info.Damage = 1f;
if (info.BitsDamageType is not 256) return HookResult.Continue;
if (attacker == null) return HookResult.Continue;

if (!attacker.IsReal()) return HookResult.Continue;
info.Damage = 0;

var ammoType = info.AmmoType;
var targetRole = _roleService.GetPlayer(playerWhoWasDamaged);

return HookResult.Changed;
}, HookMode.Pre);
*/
Server.NextFrame(() =>
{
attacker.PrintToChat(
StringUtils.FormatTTT(
$"You tased player {playerWhoWasDamaged.PlayerName} they are a {targetRole.PlayerRole().FormatRoleFull()}"));
});

return HookResult.Stop;
}


Expand All @@ -60,7 +83,7 @@ private void IdentifyBody(CCSPlayerController caller)
{
//add states

if (_roleService.GetRole(caller) != Role.Detective) return;
if (_roleService.GetPlayer(caller).PlayerRole() != Role.Detective) return;

var entity = caller.GetClientRagdollAimTarget();

Expand All @@ -85,11 +108,46 @@ private void IdentifyBody(CCSPlayerController caller)
else
message = StringUtils.FormatTTT(
player.PlayerRole().FormatStringFullAfter($"{plr.PlayerName} was killed by ") +
_roleService.GetRole(killerEntity).FormatStringFullAfter(killerEntity.PlayerName));
_roleService.GetPlayer(killerEntity).PlayerRole().FormatStringFullAfter(killerEntity.PlayerName));


player.SetFound(true);

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

//to be moved to a utility class
public 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;
}
}
78 changes: 78 additions & 0 deletions mod/TTT.Logs/LogBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Collections.Generic;
using System.Linq;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using TTT.Public.Action;
using TTT.Public.Behaviors;
using TTT.Public.Extensions;
using TTT.Public.Mod.Logs;
using Action = TTT.Public.Action.Action;

namespace TTT.Logs;

public class LogBehavior : ILogService, IPluginBehavior
{
private int _round = 1;

public void Start(BasePlugin plugin)
{
}

[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart _, GameEventInfo __)
{
CreateRound(_round++);
return HookResult.Continue;
}

[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd _, GameEventInfo __)
{
PrintLogs(_round);
return HookResult.Continue;
}

private readonly Dictionary<int, IRoundLogs> _logs = new();

public void AddLog(Action action)
{
_logs[_round].AddLog(action);
}

public bool PrintLogs(int round)
{
if (_logs.ContainsKey(round)) return false;
foreach (var player in Utilities.GetPlayers().Where(plr => plr.IsReal()))
{
PrintToPlayer(player, round);
}

PrintToConsole(round);
return true;
}

public bool PrintToPlayer(CCSPlayerController player, int round)
{
if (!_logs.ContainsKey(round)) return false;
player.PrintToConsole(GetLogs(round).FormattedLogs(round));
return true;
}

public bool PrintToConsole(int round)
{
if (!_logs.ContainsKey(round)) return false;
Server.PrintToConsole(GetLogs(round).FormattedLogs(round));
return true;
}

public IRoundLogs GetLogs(int round)
{
return _logs[round];
}

public void CreateRound(int round)
{
_logs.Add(round, new RoundLogs());
}
}
15 changes: 15 additions & 0 deletions mod/TTT.Logs/LogServiceExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.Extensions.DependencyInjection;
using TTT.Public.Extensions;
using TTT.Public.Mod.Logs;
using TTT.Public.Mod.Role;

namespace TTT.Logs;

public static class LogServiceExtension
{
public static void AddLogsService(this IServiceCollection collection)
{
collection.AddPluginBehavior<ILogService, LogBehavior>();
collection.AddPluginBehavior<LogsCommand>();
}
}
47 changes: 47 additions & 0 deletions mod/TTT.Logs/LogsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using TTT.Public.Behaviors;
using TTT.Public.Mod.Logs;

namespace TTT.Logs;

public class LogsCommand(ILogService service) : IPluginBehavior
{
private readonly ILogService _service = service;

Check warning on line 12 in mod/TTT.Logs/LogsCommand.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ILogService service' is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event.

public void Start(BasePlugin plugin)
{

}

[ConsoleCommand("css_logs", "Prints logs to console")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_ONLY)]
public void Command_Logs(CCSPlayerController? executor, CommandInfo info)
{
var roundIdString = info.GetArg(1);

var roundId = 0;

if (!string.IsNullOrEmpty(roundIdString) || !int.TryParse(roundIdString, out roundId))
{
info.ReplyToCommand("Invalid round id");
}

if (executor == null)
{
if (!service.PrintToConsole(roundId)) info.ReplyToCommand("No logs found for round " + roundId);
return;
}

if (!AdminManager.PlayerHasPermissions(executor, "@css/kick"))
{
info.ReplyToCommand("You do not have permission to execute this command");
return;
}

if (!service.PrintToPlayer(executor, roundId)) info.ReplyToCommand("No logs found for round " + roundId);

}
}
Loading
Loading