Skip to content

Commit

Permalink
UI: Added force role admin command (#1585)
Browse files Browse the repository at this point in the history
Added an additional admin command to force a role onto a player.
Tested and worked in v0.14.
See:

![image](https://github.com/user-attachments/assets/b243f8cd-a59d-45f9-a643-50a9846a320a)
  • Loading branch information
mexikoedi authored Aug 20, 2024
1 parent a81f4dd commit 8006a57
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel
- Added the `SWEP.DryFireSound` field to the weapon base to allow the dryfire sound to be easily changed (by @TW1STaL1CKY)
- Added role derandomization options for perceptually fairer role distribution
- Added targetID to buttons (by @TimGoll)
- Added force role admin command (by @mexikoedi)

### Changed

Expand Down
7 changes: 7 additions & 0 deletions lua/terrortown/lang/en.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2345,3 +2345,10 @@ L.button_default = "Press [{usekey}] to trigger"
L.button_rotating = "Press [{usekey}] to flip"

L.undefined_key = "???"

-- 2024-08-18
L.header_commands_player_force_role = "Force Player Role"

L.label_button_player_force_role = "force role"

L.label_player_role = "Select role"
37 changes: 37 additions & 0 deletions lua/terrortown/menus/gamemode/commands/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ CLGAMEMODESUBMENU.title = "submenu_commands_commands_title"

function CLGAMEMODESUBMENU:Populate(parent)
local plys = player.GetAll()
local roles = roles.GetSortedRoles()
local plyChoices = {}
local roleChoices = {}

for i = 1, #plys do
local ply = plys[i]
Expand All @@ -18,6 +20,15 @@ function CLGAMEMODESUBMENU:Populate(parent)
}
end

for i = 1, #roles do
local role = roles[i]

roleChoices[i] = {
title = role.name,
value = role.index,
}
end

-- RESTART ROUND --

local form = vgui.CreateTTT2Form(parent, "header_commands_round_restart")
Expand Down Expand Up @@ -188,4 +199,30 @@ function CLGAMEMODESUBMENU:Populate(parent)
end,
master = playerArmor,
})

-- FORCE PLAYER ROLE --

local form8 = vgui.CreateTTT2Form(parent, "header_commands_player_force_role")

local playerList = form8:MakeComboBox({
label = "label_player_select",
choices = plyChoices,
})

local playerRole = form8:MakeComboBox({
label = "label_player_role",
choices = roleChoices,
master = playerList,
})

form8:MakeButton({
label = "label_execute_command",
buttonLabel = "label_button_player_force_role",
OnClick = function(slf)
local ply, _ = playerList:GetSelected()

admin.PlayerForceRole(ply, playerRole:GetSelected())
end,
master = playerRole,
})
end
28 changes: 28 additions & 0 deletions lua/ttt2/libraries/admin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local ADMIN_COMMAND_RESPAWN = 5
local ADMIN_COMMAND_CREDITS = 6
local ADMIN_COMMAND_HEALTH = 7
local ADMIN_COMMAND_ARMOR = 8
local ADMIN_COMMAND_FORCE = 9

admin = {}

Expand Down Expand Up @@ -191,6 +192,31 @@ function admin.PlayerSetArmor(ply, amount)
end
end

---
-- Sets the role of the given player.
-- @param Player ply The player which should get another role
-- @param number roleIndex The role index to which the player should be forced
-- @note When called on the client the local player has to be a super admin.
-- @realm shared
function admin.PlayerForceRole(ply, roleIndex)
if CLIENT then
net.Start("TTT2AdminCommand")
net.WriteUInt(ADMIN_COMMAND_FORCE, 4)
net.WritePlayer(ply)
net.WriteUInt(roleIndex, ROLE_BITS)
net.SendToServer()
end

if SERVER then
if not ply:IsTerror() then
return
end

ply:SetRole(roleIndex)
SendFullStateUpdate()
end
end

-- checks if a player is in the admin user group by internally calling
-- @{GM:TTT2AdminCheck}.
-- @param Player ply The player to check
Expand Down Expand Up @@ -234,6 +260,8 @@ if SERVER then
admin.PlayerSetHealth(net.ReadPlayer(), net.ReadUInt(16))
elseif command == ADMIN_COMMAND_ARMOR then
admin.PlayerSetArmor(net.ReadPlayer(), net.ReadUInt(16))
elseif command == ADMIN_COMMAND_FORCE then
admin.PlayerForceRole(net.ReadPlayer(), net.ReadUInt(ROLE_BITS))
end
end)

Expand Down

0 comments on commit 8006a57

Please sign in to comment.