Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Credits/Shop: Add hooks for credit related events. #1451

Merged
merged 16 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel

- Added hook ENTITY:ClientUse(), which is triggered clientside if an entity is used
- Return true to prevent also using this on the server for clientside only usecases
- Added hook `GM:TTT2OnGiveFoundCredits()`, which is triggered serverside when a player has been given credits for searching a corpse.
- Added hook `GM:TTT2ReceivedKillCredits()`, which is called when a player recieves credits for a kill.
- Added hook `GM:TTT2ReceivedTeamAwardCredits()`, which is called when a player recieves credits as a team award.
- Added hook `GM:TTT2TransferedCredits()`, which is called when a player has successfully transfered a credit to another player.

### Changed

- TryRerollShop calls `TTT2OrderedEquipment` hook.

### Fixed

## [v0.13.1b](https://github.com/TTT-2/TTT2/tree/v0.13.1b) (2024-02-27)
Expand Down
9 changes: 9 additions & 0 deletions gamemodes/terrortown/gamemode/server/sv_corpse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,12 @@ function GM:TTT2ModifyRagdollVelocity(deadply, rag, velocity) end
-- @hook
-- @realm server
function GM:TTTOnCorpseCreated(rag, deadply) end

---
-- Called after a player has been given credits for searching a corpse.
-- @param Player ply The player that searched the corpse
-- @param Entity rag The ragdoll that was searched
-- @param number credits The amount of credits that were given
-- @hook
-- @realm server
function GM:TTT2OnGiveFoundCredits(ply, rag, credits) end
TimGoll marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions gamemodes/terrortown/gamemode/server/sv_player_ext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1891,3 +1891,20 @@ function GM:TTT2SetDefaultCredits(ply) end
-- @hook
-- @realm server
function GM:TTT2ModifyDefaultTraitorCredits(ply, credits) end

---
-- Hook that is called when a player recieves credits for a kill.
-- @param Player ply The player who killed another player
-- @param Player victim The player who was killed
-- @param number credits The amount of credits the player received
-- @hook
-- @realm server
function GM:TTT2ReceivedKillCredits(ply, victim, credits) end

---
-- Hook that is called when a player recieves credits as a team award.
-- @param Player ply The player who was awarded the credits
-- @param number credits The amount of credits the player received
-- @hook
-- @realm server
function GM:TTT2ReceivedTeamAwardCredits(ply, credits) end
10 changes: 10 additions & 0 deletions gamemodes/terrortown/gamemode/server/sv_shop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ concommand.Add("ttt_cheat_credits", CheatCredits, nil, nil, FCVAR_CHEAT)
-- @realm server
function GM:TTT2CanTransferCredits(sender, recipient, credits_per_xfer) end

---
-- Called when a player has successfully transfered a credit to another player.
-- @param Player sender Player that has sent the credits.
-- @param Player recipient Player that has received the credits.
-- @param number credits Amount of credits that have been transferred.
-- @param boolean isRecipientDead If the recipient is dead or not.
-- @hook
-- @realm server
function GM:TTT2TransferedCredits(sender, recipient, credits, isRecipientDead) end

local function TransferCredits(ply, cmd, args)
if #args ~= 2 then
return
Expand Down
12 changes: 12 additions & 0 deletions gamemodes/terrortown/gamemode/shared/sh_shop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ function shop.TryRerollShop(ply)
else
ply:SubtractCredits(GetGlobalInt("ttt2_random_shop_reroll_cost"))
shop.ForceRerollShop(ply)
---
-- @realm server
-- stylua: ignore
hook.Run("TTT2OrderedEquipment", ply, "reroll_shop", false, GetGlobalInt("ttt2_random_shop_reroll_cost"), false)
end

return true
Expand Down Expand Up @@ -427,13 +431,21 @@ function shop.TransferCredits(ply, targetPlyId64, credits)

if target:IsTerror() and target:Alive() then
target:AddCredits(credits)
---
-- @realm server
-- stylua: ignore
hook.Run("TTT2TransferedCredits", ply, target, credits, false)
else
-- The would be recipient is dead, which the sender may not know.
-- Instead attempt to send the credits to the target's corpse, where they can be picked up.
local rag = target:FindCorpse()

if IsValid(rag) then
CORPSE.SetCredits(rag, CORPSE.GetCredits(rag, 0) + credits)
---
-- @realm server
-- stylua: ignore
hook.Run("TTT2TransferedCredits", ply, target, credits, false)
end
end

Expand Down
12 changes: 12 additions & 0 deletions lua/ttt2/libraries/credits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ function credits.HandleKillCreditsAward(victim, attacker)
local creditsAmount = GetConVar("ttt_credits_award_kill"):GetInt()

attacker:AddCredits(creditsAmount)

---
-- @realm server
-- stylua: ignore
hook.Run("TTT2ReceivedKillCredits", victim, attacker, creditsAmount)

LANG.Msg(
attacker,
"credit_kill",
Expand Down Expand Up @@ -143,6 +149,12 @@ function credits.HandleKillCreditsAward(victim, attacker)

-- now reward their player for their good game
plyToAward:AddCredits(creditsAmount)

---
-- @realm server
-- stylua: ignore
hook.Run("TTT2ReceivedTeamAwardCredits", plyToAward, creditsAmount)

LANG.Msg(plyToAward, "credit_all", { num = creditsAmount }, MSG_MSTACK_ROLE)
end
end
Expand Down