From 5ff88f399702235af516dd23984ec5bcd070c32b Mon Sep 17 00:00:00 2001 From: Levi Date: Fri, 18 Nov 2022 20:27:12 -0600 Subject: [PATCH 1/6] increase legibility and efficiency changed the chained if-elseif to table lookup due to integers being used and not strings for the index/conditional check im still new to lua so feel free to roast me, im tryna learn, coming from java --- .../entities/entities/ttt_game_text.lua | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/gamemodes/terrortown/entities/entities/ttt_game_text.lua b/gamemodes/terrortown/entities/entities/ttt_game_text.lua index b691f524a..5fa9093a3 100644 --- a/gamemodes/terrortown/entities/entities/ttt_game_text.lua +++ b/gamemodes/terrortown/entities/entities/ttt_game_text.lua @@ -54,28 +54,26 @@ end function ENT:AcceptInput(name, activator) if name == "Display" then local recv = activator + + if IsValid(activator) and activator:IsPlayer() then + local receiver_tbl = { -- i think this is faster than chaining if/elseif + RECEIVE_ALL = nil, + RECEIVE_DETECTIVE = GetRoleChatFilter(ROLE_DETECTIVE), + RECEIVE_TRAITOR = GetRoleChatFilter(TEAM_TRAITOR), + RECEIVE_INNOCENT = GetRoleChatFilter(TEAM_INNOCENT) + } + + recv = (self.teamReceiver) ? GetTeamChatFilter(self.teamReceiver) : receiver_tbl[self.Receiver] - local r = self.Receiver - if r == RECEIVE_ALL then - recv = nil - elseif r == RECEIVE_DETECTIVE then - recv = GetRoleChatFilter(ROLE_DETECTIVE) - elseif r == RECEIVE_TRAITOR then - recv = GetTeamChatFilter(TEAM_TRAITOR) - elseif r == RECEIVE_INNOCENT then - recv = GetTeamChatFilter(TEAM_INNOCENT) - elseif r == RECEIVE_ACTIVATOR then - if not IsValid(activator) or not activator:IsPlayer() then - ErrorNoHalt("ttt_game_text tried to show message to invalid !activator\n") + CustomMsg(recv, self.Message, self.Color) - return true - end - elseif r == RECEIVE_CUSTOMROLE and self.teamReceiver then - recv = GetTeamChatFilter(self.teamReceiver) + return true -- success + + else + ErrorNoHalt("ttt_game_text tried to show message to invalid !activator\n") + return false -- failed, either invalid activator or activator was not a player end - - CustomMsg(recv, self.Message, self.Color) - - return true end + + return false -- failed, name wasn't "Display" end From 642add1888b65a1f3fc695f9c5404b29fcb95166 Mon Sep 17 00:00:00 2001 From: Levi Date: Fri, 18 Nov 2022 20:30:43 -0600 Subject: [PATCH 2/6] cleaned up formatting --- gamemodes/terrortown/entities/entities/ttt_game_text.lua | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/gamemodes/terrortown/entities/entities/ttt_game_text.lua b/gamemodes/terrortown/entities/entities/ttt_game_text.lua index 5fa9093a3..6c98a31c2 100644 --- a/gamemodes/terrortown/entities/entities/ttt_game_text.lua +++ b/gamemodes/terrortown/entities/entities/ttt_game_text.lua @@ -64,14 +64,11 @@ function ENT:AcceptInput(name, activator) } recv = (self.teamReceiver) ? GetTeamChatFilter(self.teamReceiver) : receiver_tbl[self.Receiver] - CustomMsg(recv, self.Message, self.Color) - return true -- success - - else - ErrorNoHalt("ttt_game_text tried to show message to invalid !activator\n") - return false -- failed, either invalid activator or activator was not a player + else + ErrorNoHalt("ttt_game_text tried to show message to invalid !activator\n") + return false -- failed, either invalid activator or activator was not a player end end From 4e635cdc82c476ded4225f114b2b29a88887d5d8 Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 13 Dec 2022 22:30:09 -0600 Subject: [PATCH 3/6] garbage cleanup i dont think RECEIVE_ALL is gonna have a fun time. It probably is going to fail to get called... but according to something i read off google: Nil isnt actually nothing, it's just an absolutely unique property value so this should be fine. Coinflippin it --- .../entities/entities/ttt_game_text.lua | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/gamemodes/terrortown/entities/entities/ttt_game_text.lua b/gamemodes/terrortown/entities/entities/ttt_game_text.lua index 6c98a31c2..3bf3be186 100644 --- a/gamemodes/terrortown/entities/entities/ttt_game_text.lua +++ b/gamemodes/terrortown/entities/entities/ttt_game_text.lua @@ -52,25 +52,28 @@ end -- @return[default=true] boolean -- @realm shared function ENT:AcceptInput(name, activator) - if name == "Display" then - local recv = activator + if name ~= "Display" then + return false + end - if IsValid(activator) and activator:IsPlayer() then - local receiver_tbl = { -- i think this is faster than chaining if/elseif - RECEIVE_ALL = nil, - RECEIVE_DETECTIVE = GetRoleChatFilter(ROLE_DETECTIVE), - RECEIVE_TRAITOR = GetRoleChatFilter(TEAM_TRAITOR), - RECEIVE_INNOCENT = GetRoleChatFilter(TEAM_INNOCENT) - } + if IsValid(activator) and activator:IsPlayer() then + local recv = activator + local receiver_tbl = { + RECEIVE_ALL = nil, + RECEIVE_DETECTIVE = GetRoleChatFilter(ROLE_DETECTIVE), + RECEIVE_TRAITOR = GetRoleChatFilter(TEAM_TRAITOR), + RECEIVE_INNOCENT = GetRoleChatFilter(TEAM_INNOCENT) + } - recv = (self.teamReceiver) ? GetTeamChatFilter(self.teamReceiver) : receiver_tbl[self.Receiver] - CustomMsg(recv, self.Message, self.Color) - return true -- success - else - ErrorNoHalt("ttt_game_text tried to show message to invalid !activator\n") - return false -- failed, either invalid activator or activator was not a player - end + recv = (self.teamReceiver) ? GetTeamChatFilter(self.teamReceiver) : receiver_tbl[self.Receiver] + CustomMsg(recv, self.Message, self.Color) + + recv = nil + receiver_tbl = nil + + return true end - return false -- failed, name wasn't "Display" + ErrorNoHalt("ttt_game_text tried to show message to invalid !activator\n") + return false -- either invalid activator or activator was not a player end From aeb51a02d8080c11299bc1a97516e261d0e8c9ba Mon Sep 17 00:00:00 2001 From: Histalek <16392835+Histalek@users.noreply.github.com> Date: Tue, 17 Oct 2023 02:48:29 +0200 Subject: [PATCH 4/6] Update gamemodes/terrortown/entities/entities/ttt_game_text.lua --- gamemodes/terrortown/entities/entities/ttt_game_text.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gamemodes/terrortown/entities/entities/ttt_game_text.lua b/gamemodes/terrortown/entities/entities/ttt_game_text.lua index 3bf3be186..69585979b 100644 --- a/gamemodes/terrortown/entities/entities/ttt_game_text.lua +++ b/gamemodes/terrortown/entities/entities/ttt_game_text.lua @@ -65,7 +65,7 @@ function ENT:AcceptInput(name, activator) RECEIVE_INNOCENT = GetRoleChatFilter(TEAM_INNOCENT) } - recv = (self.teamReceiver) ? GetTeamChatFilter(self.teamReceiver) : receiver_tbl[self.Receiver] + recv = (self.teamReceiver) and GetTeamChatFilter(self.teamReceiver) or receiver_tbl[self.Receiver] CustomMsg(recv, self.Message, self.Color) recv = nil From 500692dcc0ab7dadfe16948044d1bb51608a23f1 Mon Sep 17 00:00:00 2001 From: Histalek <16392835+Histalek@users.noreply.github.com> Date: Tue, 17 Oct 2023 02:56:10 +0200 Subject: [PATCH 5/6] remove whitespaces --- .../terrortown/entities/entities/ttt_game_text.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gamemodes/terrortown/entities/entities/ttt_game_text.lua b/gamemodes/terrortown/entities/entities/ttt_game_text.lua index 69585979b..9f195d792 100644 --- a/gamemodes/terrortown/entities/entities/ttt_game_text.lua +++ b/gamemodes/terrortown/entities/entities/ttt_game_text.lua @@ -55,7 +55,7 @@ function ENT:AcceptInput(name, activator) if name ~= "Display" then return false end - + if IsValid(activator) and activator:IsPlayer() then local recv = activator local receiver_tbl = { @@ -64,16 +64,16 @@ function ENT:AcceptInput(name, activator) RECEIVE_TRAITOR = GetRoleChatFilter(TEAM_TRAITOR), RECEIVE_INNOCENT = GetRoleChatFilter(TEAM_INNOCENT) } - + recv = (self.teamReceiver) and GetTeamChatFilter(self.teamReceiver) or receiver_tbl[self.Receiver] CustomMsg(recv, self.Message, self.Color) - + recv = nil receiver_tbl = nil - + return true end - + ErrorNoHalt("ttt_game_text tried to show message to invalid !activator\n") return false -- either invalid activator or activator was not a player end From ad6a4c9499c54d83dd6a7767bafa88e616fc328f Mon Sep 17 00:00:00 2001 From: Histalek <16392835+Histalek@users.noreply.github.com> Date: Tue, 17 Oct 2023 03:04:43 +0200 Subject: [PATCH 6/6] remove unused vars and unneeded parenthesis --- gamemodes/terrortown/entities/entities/ttt_game_text.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/gamemodes/terrortown/entities/entities/ttt_game_text.lua b/gamemodes/terrortown/entities/entities/ttt_game_text.lua index 9f195d792..47ea17356 100644 --- a/gamemodes/terrortown/entities/entities/ttt_game_text.lua +++ b/gamemodes/terrortown/entities/entities/ttt_game_text.lua @@ -9,10 +9,6 @@ ENT.Message = "" ENT.Color = COLOR_WHITE local RECEIVE_ACTIVATOR = 0 -local RECEIVE_ALL = 1 -local RECEIVE_DETECTIVE = 2 -local RECEIVE_TRAITOR = 3 -local RECEIVE_INNOCENT = 4 local RECEIVE_CUSTOMROLE = 5 ENT.Receiver = RECEIVE_ACTIVATOR @@ -65,7 +61,7 @@ function ENT:AcceptInput(name, activator) RECEIVE_INNOCENT = GetRoleChatFilter(TEAM_INNOCENT) } - recv = (self.teamReceiver) and GetTeamChatFilter(self.teamReceiver) or receiver_tbl[self.Receiver] + recv = self.teamReceiver and GetTeamChatFilter(self.teamReceiver) or receiver_tbl[self.Receiver] CustomMsg(recv, self.Message, self.Color) recv = nil