squelch mod - need a code review
So, I decided to create a mod that will enable you to squelch certain individuals in advance. This will be helpful to folks that get harassed a bit or just generally have bad experiences with certain folks. The user just has to update the lua code with the user name they want to block. During testing, I'll see if its possible to manually unsquelch a person in game - i assume it is. This mod's only purpose is so that folks can automatically block communication with certain players in advance.
Anyway, I just copied the squelch fix mod (no longer needed as the chat is now fixed) and added in a string to block specific individuals. My question is, what do I really need in this mod? I have everything including a fix that is no longer needed. Any guidance would be appreciated.
Here's the bit of the code I modified:
- function SetupIgnoreList()
- local armies = GetArmiesTable()
- local focus = armies.focusArmy
- for id, army in armies.armiesTable do
- -- Can't ignore yourself, as much as you may want to
- if id != focus and army.human then
- ignoredPlayers[string.lower(army.nickname)] = army.nickname=='poopy' or army.nickname=='Yomothua' --added by pacov
- end
- end
- end
And here's the full code of the mod in its entirety:
- #*****************************************************************************
- #* File: lua/modules/ui/game/chat.lua
- #* Summary: In game chat ui
- #*
- #* Copyright © 2008 Gas Powered Games, Inc. All rights reserved.
- #*****************************************************************************
- local UIMain = import('/lua/ui/uimain.lua')
- local UIUtil = import('/lua/ui/uiutil.lua')
- local LayoutHelpers = import('/lua/maui/layouthelpers.lua')
- local EffectHelpers = import('/lua/maui/effecthelpers.lua')
- local Group = import('/lua/maui/group.lua').Group
- local Button = import('/lua/maui/button.lua').Button
- local Checkbox = import('/lua/maui/checkbox.lua').Checkbox
- local Text = import('/lua/maui/text.lua').Text
- local Edit = import('/lua/maui/edit.lua').Edit
- local Bitmap = import('/lua/maui/bitmap.lua').Bitmap
- local ItemList = import('/lua/maui/itemlist.lua').ItemList
- local Window = import('/lua/ui/controls/window.lua').Window
- local BitmapCombo = import('/lua/ui/controls/combo.lua').BitmapCombo
- local IntegerSlider = import('/lua/maui/slider.lua').IntegerSlider
- local Prefs = import('/lua/ui/prefs.lua')
- local Dragger = import('/lua/maui/dragger.lua').Dragger
- local InGameUI = import('/lua/ui/game/InGameUI.lua')
- --[[ LOC Strings
- <LOC chat_win_0001>To %s:
- <LOC chat_win_0002>Chat (%d - %d of %d lines)
- --]]
- local CHAT_INACTIVITY_TIMEOUT = 5 -- in seconds
- local savedParent = false
- local commandhistory = {}
- local ignoredPlayers = {}
- local bg = false
- function HandleIgnore(param)
- if param != "" then
- playerName = string.lower(param)
- if ignoredPlayers[playerName] != nil then
- ignoredPlayers[playerName] = not ignoredPlayers[playerName]
- if ignoredPlayers[playerName] then
- bg.history:AddItem(LOCF("<LOC chatui_0005>Ignoring %s", playerName))
- else
- bg.history:AddItem(LOCF("<LOC chatui_0006>No longer ignoring %s", playerName))
- end
- else
- bg.history:AddItem(LOCF("<LOC chatui_0007>Player not found: %s", playerName))
- end
- if bg:IsHidden() then
- ShowHistory()
- end
- end
- end
- local specialCommands = {
- {
- key = "ignore",
- action = HandleIgnore,
- },
- {
- key = "squelch",
- action = HandleIgnore,
- },
- }
- local ChatOptions = { all_color = 1,
- allies_color = 2,
- priv_color = 3,
- link_color = 4,
- font_size = 14,
- fade_time = 15,
- win_alpha = 1}
- local chatTo = 'allies'
- function FindClients(id)
- local t = GetArmiesTable()
- local focus = t.focusArmy
- local result = {}
- if focus == -1 then
- for index,client in GetSessionClients() do
- if table.getn(client.authorizedCommandSources) == 0 then
- table.insert(result, index)
- end
- end
- else
- local srcs = {}
- for army,info in t.armiesTable do
- if id then
- if army == id then
- for k,cmdsrc in info.authorizedCommandSources do
- srcs[cmdsrc] = true
- end
- break
- end
- else
- if IsAlly(focus, army) then
- for k,cmdsrc in info.authorizedCommandSources do
- srcs[cmdsrc] = true
- end
- end
- end
- end
- for index,client in GetSessionClients() do
- for k,cmdsrc in client.authorizedCommandSources do
- if srcs[cmdsrc] then
- table.insert(result, index)
- break
- end
- end
- end
- end
- return result
- end
- -- Setup the ignore list with all of the player armies in the game.
- function SetupIgnoreList()
- local armies = GetArmiesTable()
- local focus = armies.focusArmy
- for id, army in armies.armiesTable do
- -- Can't ignore yourself, as much as you may want to
- if id != focus and army.human then
- ignoredPlayers[string.lower(army.nickname)] = army.nickname=='poopy' or army.nickname=='Yomothua' --added by pacov
- end
- end
- end
- function ChatPageUp(mod)
- end
- function ChatPageDown(mod)
- end
- function Create()
- savedParent = GetFrame(0)
- CreateChat()
- end
- function CreateChat()
- bg = Bitmap(savedParent)
- #bg:SetSolidColor('cc000000')
- bg.Height:Set(220)
- bg.Width:Set(400)
- #bg.Left:Set(savedParent.Left)
- #bg.Bottom:Set(function() return savedParent.Bottom() - 150 end)
- LayoutHelpers.AtLeftIn(bg, GetFrame(0), 10)
- LayoutHelpers.AtVerticalCenterIn(bg, GetFrame(0), -50)
- bg.Depth:Set(15000)
- bg.history = ItemList(bg)
- bg.history.Configs = {}
- bg.history:SetFont(UIUtil.bodyFont, 18)
- bg.history:SetColors("ffffffff", "00000000", 'black', UIUtil.highlightColor, "ffbcfffe", '4f4f5f')
- bg.history:ShowMouseoverItem(false)
- bg.history:SetDropShadow(true)
- LayoutHelpers.AtLeftIn(bg.history, bg, 5)
- LayoutHelpers.AtTopIn(bg.history, bg, 5)
- bg.history.Width:Set(360)
- bg.history.Height:Set(250)
- bg.history.Depth:Set(function() return bg.Depth() + 2 end)
- bg.historyBackLeft = Bitmap(bg, '/textures/ui/hud/hud_chat_bg_left.dds')
- local width, height = GetTextureDimensions('/textures/ui/hud/hud_chat_bg_left.dds')
- bg.historyBackLeft.Width:Set(width)
- bg.historyBackLeft.Height:Set(height)
- LayoutHelpers.AtLeftIn(bg.historyBackLeft, bg, -15)
- LayoutHelpers.AtTopIn(bg.historyBackLeft, bg, -20)
- bg.historyBackLeft:DisableHitTest(true)
- bg.historyBackLeft.Depth:Set(function() return bg.history.Depth() - 1 end)
- bg.historyBackMid = Bitmap(bg, '/textures/ui/hud/hud_chat_bg_middle.dds')
- width, height = GetTextureDimensions('/textures/ui/hud/hud_chat_bg_middle.dds')
- bg.historyBackMid.Width:Set(width)
- bg.historyBackMid.Height:Set(height)
- LayoutHelpers.AtTopIn(bg.historyBackMid, bg, -20)
- bg.historyBackMid.Left:Set( function() return bg.historyBackLeft.Right() end )
- bg.historyBackMid:DisableHitTest(true)
- bg.historyBackMid.Depth:Set(function() return bg.history.Depth() - 1 end)
- bg.historyBackRight = Bitmap(bg, '/textures/ui/hud/hud_chat_bg_right.dds')
- width, height = GetTextureDimensions('/textures/ui/hud/hud_chat_bg_right.dds')
- bg.historyBackRight.Width:Set(width)
- bg.historyBackRight.Height:Set(height)
- LayoutHelpers.AtTopIn(bg.historyBackRight, bg, -20)
- bg.historyBackRight:DisableHitTest(true)
- bg.historyBackRight.Left:Set( function() return bg.historyBackMid.Right() end )
- bg.historyBackRight.Depth:Set(function() return bg.history.Depth() - 1 end)
- #bg.historyScroll = UIUtil.CreateVertScrollbarFor(bg.history)
- #bg.historyScroll:SetParent(bg)
- --[[
- bg.edit = Edit(bg)
- bg.edit.Left:Set(function() return bg.toText.Right() + 5 end)
- bg.edit.Right:Set(function() return bg.Right() - 5 end)
- bg.edit.Bottom:Set(function() return bg.Bottom() - 5 end)
- bg.edit.Height:Set(function() return bg.edit:GetFontHeight() + 2 end)
- UIUtil.SetupEditStd(bg.edit, "ff00ff00", nil, "blue", UIUtil.highlightColor, UIUtil.bodyFont, ChatOptions.font_size, 200)
- bg.edit:SetDropShadow(true)
- bg.edit:ShowBackground(true)
- ]]--
- bg.edit = Edit(bg)
- LayoutHelpers.Above(bg.edit, InGameUI.HUDParent, 50)
- LayoutHelpers.AtHorizontalCenterIn(bg.edit, GetFrame(0), 40)
- bg.edit.Width:Set(350)
- bg.edit.Height:Set(function() return bg.edit:GetFontHeight() + 2 end)
- UIUtil.SetupEditStd(bg.edit, 'ffffffff', '00000000', 'blue', UIUtil.highlightColor, UIUtil.bodyFont, 20, 200)
- bg.edit:SetDropShadow(true)
- bg.edit:SetText('')
- bg.imgleft = Bitmap(savedParent, UIUtil.UIFile('/textboxes/edit_left.dds'))
- bg.imgleft.Width:Set(10)
- bg.imgleft.Height:Set(45)
- LayoutHelpers.AtLeftTopIn(bg.imgleft, bg.edit, -60, -9)
- bg.imgleft:Hide()
- bg.imgleft.Depth:Set(15000)
- bg.imgmid = Bitmap(savedParent, UIUtil.UIFile('/textboxes/edit_mid.dds'))
- bg.imgmid.Width:Set(410)
- bg.imgmid.Height:Set(45)
- LayoutHelpers.AnchorToRight(bg.imgmid, bg.imgleft, -1)
- LayoutHelpers.AtTopIn(bg.imgmid, bg.imgleft)
- bg.imgmid:Hide()
- bg.imgmid.Depth:Set(15000)
- bg.imgright = Bitmap(savedParent, UIUtil.UIFile('/textboxes/edit_right.dds'))
- bg.imgright.Width:Set(10)
- bg.imgright.Height:Set(45)
- LayoutHelpers.AnchorToRight(bg.imgright, bg.imgmid)
- LayoutHelpers.AtTopIn(bg.imgright, bg.imgmid)
- bg.imgright:Hide()
- bg.imgright.Depth:Set(15000)
- bg.toText = UIUtil.CreateText(bg.edit, '<LOC chatui_0000>Allies:', 20, UIUtil.bodyFont)
- bg.toText:SetColor(UIUtil.specialFontColor)
- LayoutHelpers.AnchorToLeft(bg.toText, bg.edit, 5)
- LayoutHelpers.AtVerticalCenterIn(bg.toText, bg.edit, -1)
- bg.toText:SetDropShadow(true)
- bg.edit.OnNonTextKeyPressed = function(self, charcode, event)
- local function RecallCommand(entryNumber)
- self:SetText(commandhistory[self.recallEntry].text)
- end
- if charcode == UIUtil.VK_UP then
- if table.getsize(commandhistory) > 0 then
- if self.recallEntry then
- self.recallEntry = math.max(self.recallEntry-1, 1)
- else
- self.recallEntry = table.getsize(commandhistory)
- end
- RecallCommand(self.recallEntry)
- end
- elseif charcode == UIUtil.VK_DOWN then
- if table.getsize(commandhistory) > 0 then
- if self.recallEntry then
- self.recallEntry = math.min(self.recallEntry+1, table.getsize(commandhistory))
- RecallCommand(self.recallEntry)
- if self.recallEntry == table.getsize(commandhistory) then
- self.recallEntry = nil
- end
- else
- self:SetText('')
- end
- end
- end
- end
- bg.edit.OnCharPressed = function(self, charcode)
- local charLim = self:GetMaxChars()
- if STR_Utf8Len(self:GetText()) >= charLim then
- # Play Sound when charlimit reached
- PlaySound('Forge/Lobby/snd_ui_lobby_lobby_unique_fail')
- end
- end
- bg.edit.OnEnterPressed = function(self, text)
- if text ~= "" then
- if string.sub(text, 1, 1) == '/' then
- local spaceStart = string.find(text, " ") or string.len(text)
- local comKey = string.sub(text, 2, spaceStart - 1)
- local param = string.sub(text, spaceStart + 1)
- local found = false
- for i, command in specialCommands do
- if command.key == string.lower(comKey) then
- command.action(param)
- found = true
- break
- end
- end
- if not found then
- bg.history:AddItem(LOCF("<LOC chatui_0008>Unknown command: %s", comKey))
- if bg:IsHidden() then
- ShowHistory()
- end
- end
- else
- msg = { to = chatTo, text = text }
- # Play Sound when chat text is submitted
- #PlaySound('Forge/UI/Lobby/snd_ui_lobby_chatbox_submit_text' )
- if chatTo == 'allies' then
- SessionSendChatMessage(FindClients(), msg)
- else
- SessionSendChatMessage(msg)
- end
- table.insert(commandhistory, msg)
- end
- end
- # always close the chat window
- ToggleChat()
- end
- bg.edit.OnEscapePressed = function(self, text)
- # if the chat text box is currently shown, ESC should close it
- if not bg:IsHidden() then
- ToggleChat()
- end
- end
- SetupIgnoreList()
- bg:Hide()
- end
- function ReceiveChat(sender, msg)
- sender = sender or "nil sender"
- if type(msg) == 'string' then
- msg = { text = msg }
- elseif type(msg) != 'table' then
- msg = { text = repr(msg) }
- end
- if(msg.to) then
- if(msg.to == 'allies') then
- msg.to = '<LOC chatui_0002>Allies'
- elseif(msg.to == 'all') then
- msg.to = '<LOC chatui_0001>All'
- end
- end
- #if ignoredPlayers[sender] then return end
- -----------------------------------------
- if ignoredPlayers[string.lower(sender)] then return end
- -----------------------------------------
- local textBoxWidth = bg.history.Right() - bg.history.Left()
- local wrappedText = import('/lua/maui/text.lua').WrapText(LOCF("<LOC chatui_0003>%s (to %s): %s", sender, msg.to, msg.text), textBoxWidth,
- function(text)
- return bg.history:GetStringAdvance(text)
- end)
- for i, textLine in wrappedText do
- bg.history:AddItem(textLine)
- end
- bg.history:ScrollToBottom()
- local numItems = bg.history:GetItemCount()
- local longest = 0
- for i = numItems - 10, numItems do
- if i < 0 or i >= numItems then
- continue
- end
- local textLine = bg.history:GetItem(i)
- local length = import('/lua/maui/text.lua').GetTextWidth(textLine, function(text) return bg.history:GetStringAdvance(text) end)
- if length > longest then
- longest = length
- end
- end
- bg.historyBackMid.Width:Set(longest - 76)
- if bg:IsHidden() then
- ShowHistory()
- #Play Sound when receiving chat message
- PlaySound( 'Forge/UI/HUD/snd_ui_hud_chatbox_receive_message' )
- end
- end
- function ActivateChat(mods)
- if mods.Shift then
- chatTo = 'all'
- bg.toText:SetText(LOC('<LOC chatui_0004>All:'))
- else
- chatTo = 'allies'
- bg.toText:SetText(LOC('<LOC chatui_0000>Allies:'))
- end
- ToggleChat()
- end
- function ShowHistory()
- bg.history:Show()
- bg.history:SetNeedsFrameUpdate(true)
- bg.history.time = 0
- bg.history:DisableHitTest()
- if bg.history:GetItemCount() > 0 then
- bg.historyBackLeft:Show()
- bg.historyBackMid:Show()
- bg.historyBackRight:Show()
- bg.historyBackLeft:DisableHitTest()
- bg.historyBackMid:DisableHitTest()
- bg.historyBackRight:DisableHitTest()
- end
- bg.history.OnFrame = function(self, delta)
- self.time = self.time + delta
- if self.time > CHAT_INACTIVITY_TIMEOUT then
- self:SetNeedsFrameUpdate(false)
- self:Hide()
- self:GetParent().historyBackLeft:Hide()
- self:GetParent().historyBackMid:Hide()
- self:GetParent().historyBackRight:Hide()
- end
- end
- end
- function ToggleChat()
- bg:SetHidden(not bg:IsHidden())
- if bg.history:GetItemCount() <= 0 then
- bg.historyBackLeft:Hide()
- bg.historyBackMid:Hide()
- bg.historyBackRight:Hide()
- end
- if bg:IsHidden() then
- bg.edit:AbandonFocus()
- bg.imgright:Hide()
- bg.imgleft:Hide()
- bg.imgmid:Hide()
- bg.history.Depth:Set(1)
- UIMain.RemoveEscapeHandler(ToggleChat)
- else
- bg.imgright:Show()
- bg.imgleft:Show()
- bg.imgmid:Show()
- bg.edit:AcquireFocus()
- bg.history:SetNeedsFrameUpdate(false)
- bg.history:EnableHitTest()
- bg.history.Depth:Set(15000)
- UIMain.AddEscapeHandler(ToggleChat, UIUtil.ESCPRI_Commands)
- end
- end