Don't like ads? PRO users don't see any ads ;-)
Guest

console.lua, player muting

By: a guest on Apr 13th, 2012  |  syntax: None  |  size: 14.24 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. -- player muting cobbled in by knorke 2012, its a friday 13th of course
  2. -- /mute playername
  3. -- /mute playerA playerB
  4. -- /unmute playername
  5. -- /unmute playerA playerB
  6. -- /unmute              (unmute everybody)
  7.  
  8. -- TODO if team0 dies, all specs are "dead" (hmm, team0 was a spec too) [hmm, or not]
  9. local console = window:New(
  10.   "Console",
  11.   empty:New(),
  12.   {634/1024, 8*18+2}, {260/1024, 25/768}
  13. )
  14. local function NewLine()
  15.   local newLine = frames:New("cols", {
  16.     margin:New(
  17.       teamLogo:New(),
  18.       {1, 1, 1, -17}
  19.     ),
  20.     text:New({})},
  21.     {19,-19}
  22.   )
  23.   newLine.SetLine = function(self, newTeamID, newText, newTimer)
  24.     self[1][1]:SetVisibility(newTeamID ~= nil)
  25.     if newTeamID then
  26.       self[1][1]:SetTeamID(newTeamID)
  27.     end
  28.     self.timer = newTimer
  29.     self[2]:SetText(newText)
  30.   end
  31.   newLine.GetLine = function(self)
  32.     teamID = self[1][1].visible and self[1][1].teamID or nil
  33.     return teamID, self[2].text, self.timer
  34.   end
  35.   return newLine
  36. end
  37. local function GetLines()
  38.   local returnTable = {}
  39.   for i=1,console.options.lines.value do
  40.     table.insert(returnTable, NewLine())
  41.   end
  42.   return returnTable
  43. end
  44. local function SetConsoleContent()
  45.   console:SetContent(frames:New("rows", GetLines()))
  46.   console:ExitHistoryMode()
  47. end
  48. console.options = {
  49.   lines = {
  50.     name  = 'Lines',
  51.     desc  = 'How many lines should the console display?',
  52.     type  = 'number', min = 2, max = 40, step = 1,
  53.     value = 8,
  54.     OnChange = function(self)
  55.       SetConsoleContent()
  56.       console.options.systemLines.max = self.value
  57.     end
  58.   },
  59.   systemLines = {
  60.     name  = 'System lines',
  61.     desc  = 'Up to how many lines should the system messages take up?',
  62.     type  = 'number', min = 1, max = 8, step = 1,
  63.     value = 3,
  64.     OnChange = SetConsoleContent
  65.   },
  66.   decayChat = {
  67.     name  = "Decay chat",
  68.     desc  = "If set, chat messages will disappear after a while.",
  69.     type  = "bool",
  70.     value = false,
  71.     OnChange = function()
  72.       console:ExitHistoryMode()
  73.     end
  74.   },
  75.   hideUnitMsgs = {
  76.     name  = "Hide unit messages",
  77.     desc  = "Hide \"Can't reach destination!\" messages.",
  78.     type  = "bool",
  79.     value = true
  80.   },
  81.   hideStartPoints = {
  82.     name  = "Hide start points",
  83.     desc  = "Hide \"Start X\" point messages.",
  84.     type  = "bool",
  85.     value = true
  86.   },
  87.   hidePoints = {
  88.     name  = "Hide all points",
  89.     desc  = "Hides all messages about added points. Useful if you have another widget visualizing points.",
  90.     type  = "bool",
  91.     value = false
  92.   },
  93.   hideBuildposMsgs = {
  94.     name  = "Hide \"Build pos blocked\" messages",
  95.     desc  = "Hide messages about blocked build positions.",
  96.     type  = "bool",
  97.     value = false
  98.   },
  99.   specToSystem = {
  100.     name  = "Spectator chat to system console",
  101.     desc  = "Display spectator chat messages in the upper area with the system messages.",
  102.     type  = "bool",
  103.     value = false
  104.   },
  105.   hidePlayernames = {
  106.     name  = "Hide playernames",
  107.     desc  = "Hide the names of players in front chat messages.",
  108.     type  = "bool",
  109.     value = false
  110.   },
  111.   historyWithCtrl = {
  112.     name  = "History with CTRL",
  113.     desc  = "Only show the chat history if CTRL is pressed.",
  114.     type  = "bool",
  115.     value = false
  116.   },
  117.   seperateMessages = {
  118.     name  = "Seperate messages",
  119.     desc  = "Seperate system and chat messages.",
  120.     type  = "bool",
  121.     value = true
  122.   },
  123.   addSystemToHistory = {
  124.     name  = "Add system messages to history",
  125.     desc  = "Also add system messages to the history.",
  126.     type  = "bool",
  127.     value = false
  128.   },
  129.   hideIfEmpty = {
  130.     name  = "Hide if empty",
  131.     desc  = "Hide the console if it's empty.",
  132.     type  = "bool",
  133.     value = false
  134.   }
  135. }
  136.  
  137. console.chatBuffer = {}
  138. console.historyMode = false
  139. console.historyModeLast = 0
  140.  
  141. console.currentLine = 1
  142. console.currentSystemLine = 1
  143. console.updateEvery = 75
  144. console.help = "This is the console. Textual feedback from the game and other\n"..
  145.                "players will popup here. (press <ENTER> to chat)\n"..
  146.                "- Allied chat is displayed in green.. (begin with \"a:\" for ally chat)\n"..
  147.                "- Yellow font marks points. (press <F3> to go to it)"
  148.  
  149. function console:GetTeam(line)
  150.   line = string.gsub(line, "  ", " ")
  151.   for i,v in ipairs(Spring.GetPlayerList()) do
  152.     local name, active, spec, teamID = Spring.GetPlayerInfo(v)
  153.     if name and name ~= "" and not self.options.specToSystem.value or not spec then
  154.       local playerFound = string.find(line, "<"..name..">", 1, true)
  155.       local specFound   = string.find(line, "["..name.."]", 1, true)
  156.       if playerFound == 1 or playerFound == 3 or specFound == 1 or specFound == 3 then
  157.         if specFound == 3 then
  158.           -- Springie doesn't insert a space after nick
  159.           specFound = 2
  160.         end
  161.         line = string.sub(line, string.len(name)+3+(playerFound or specFound), -1)
  162.         if not Spring.AreTeamsAllied(teamID, Spring.GetMyTeamID()) then
  163.           line = "\255\255\196\196" .. line
  164.         else
  165.           line = string.gsub(line, "Allies: ", GreenStr)
  166.         end
  167.         if line == "" or line == "\255\255\196\196" or line == GreenStr then line = line .. "<empty message>" end
  168.         local _, _, isDead = Spring.GetTeamInfo(teamID)
  169.         if spec and not isDead then teamID = -1 end
  170.         if not self.options.hidePlayernames.value or spec then
  171.           line = "\255\196\196\196"..name..": \255\255\255\255" .. line
  172.         end
  173.         return teamID, line
  174.       elseif line == "Player "..name.." left" or line == "Spectator "..name.." left" or
  175.              line == "Lost connection to "..name or string.find(line, "Sync error for "..name, 1, true) then
  176.         if line:find("Sync error", 1, true) then
  177.           line = RedStr .. line:sub(0, name:len()+15) .. ". He plays a different game now. This can't be undone. Ask him to give his units to someone else."
  178.         elseif not spec then
  179.           line = RedStr .. line
  180.         else
  181.           line = GreenStr .. line
  182.         end
  183.         local _, _, isDead = Spring.GetTeamInfo(teamID)
  184.         if spec and not isDead then teamID = -1 end
  185.         return teamID, line
  186.       elseif string.find(line, name.." added point: ", 1, true) then
  187.         line = string.sub(line, string.len(name)+15, -1)
  188.         if line == "" then line = "<empty point>" end
  189.         local _, _, isDead = Spring.GetTeamInfo(teamID)
  190.         if spec and not isDead then teamID = -1 end
  191.         line = YellowStr .. line
  192.         if not self.options.hidePlayernames.value or spec then
  193.           line = "\255\196\196\196"..name..": " .. line
  194.         end
  195.         return teamID, line
  196.       end
  197.     end
  198.   end
  199.   return nil, line
  200. end
  201. function console:AddConsoleLine(line)
  202.   --mute--
  203.   local name = getPlayerNameFromLine (line)
  204.   if (mutedPlayers[name]) then
  205.         return
  206.   end
  207.  
  208.   if self.options.hideUnitMsgs.value and string.find(line, ": Can't reach destination!") then
  209.     return
  210.   end
  211.   if self.options.hidePoints.value and string.find(line, " added point:") then
  212.     return
  213.   end
  214.   if self.options.hideStartPoints.value and string.find(line, " added point: Start ") then
  215.     return
  216.   end
  217.   if self.options.hideBuildposMsgs.value and string.find(line, ": Build pos blocked") then
  218.     return
  219.   end
  220.   if line == 'Reloaded ctrlpanel with: IceUI_ctrlpanel.txt' then
  221.     return
  222.   end
  223.  
  224.   local teamID, line = self:GetTeam(line) -- FIXME "Player tettenman left" created an icon
  225.   local start, stop, current = self.currentSystemLine, console.options.lines.value, "currentLine"
  226.  
  227.   -- write cache lines to the buffer for scrolling
  228.   if teamID ~= nil or self.options.addSystemToHistory.value then
  229.     table.insert(self.chatBuffer, { teamID, line })
  230.   end
  231.  
  232.   if self.historyMode then
  233.     return
  234.   end
  235.  
  236.   if teamID == nil and self.options.seperateMessages.value then
  237.     start, stop, current = 1, console.options.systemLines.value, "currentSystemLine"
  238.     if self.currentSystemLine <= console.options.systemLines.value and self.currentLine <= console.options.lines.value then
  239.       for i=self.currentSystemLine+1,self.currentLine do
  240.         local j=self.currentLine + self.currentSystemLine+1 - i
  241.         self[1][j]:SetLine(self[1][j-1]:GetLine())
  242.       end
  243.       self.currentLine = self.currentLine + 1
  244.     end
  245.   end
  246.   if self[current] == stop + 1 then
  247.     for i=start,stop-1 do
  248.       self[1][i]:SetLine(self[1][i+1]:GetLine())
  249.     end
  250.   end
  251.   self[current] = math.min(self[current], stop)
  252.   local newTimer = math.floor(string.len(line)/5 + 2)
  253.   if teamID and not self.options.decayChat.value then
  254.     newTimer = -1
  255.   end
  256.   self[1][self[current]]:SetLine(teamID, line, newTimer)
  257.   self[current] = self[current] + 1
  258.   if self.options.hideIfEmpty.value then
  259.     self:SetVisibility(true)
  260.   end
  261. end
  262. function console:Update()
  263.   local i = 1
  264.   while i < self.currentLine do
  265.     if self[1][i].timer then
  266.       self[1][i].timer = self[1][i].timer - 1
  267.     end
  268.     if self[1][i].timer == 0 then
  269.       for j=i,self.currentLine-2 do
  270.         self[1][j]:SetLine(self[1][j+1]:GetLine())
  271.       end
  272.       self.currentLine = self.currentLine - 1
  273.       self[1][self.currentLine]:SetLine(nil, "", -1)
  274.       if i < self.currentSystemLine then
  275.         self.currentSystemLine = self.currentSystemLine - 1
  276.       end
  277.     end
  278.     i = i + 1
  279.   end
  280.   if self.currentLine == 1 and self.options.hideIfEmpty.value then
  281.     self:SetVisibility(false)
  282.   end
  283. end
  284. function console:MouseWheel(up, value)
  285.   local _, ctrl = Spring.GetModKeyState()
  286.   if self.options.historyWithCtrl.value and not ctrl then
  287.     return
  288.   end
  289.   local x, y = Spring.GetMouseState()
  290.   if self:IsAbove(x, y) then
  291.     if self.historyMode then
  292.       self:HistoryScroll(up)
  293.     else
  294.       if up then
  295.         self:EnterHistoryMode()
  296.       end
  297.     end
  298.     return true
  299.   end
  300. end
  301.  
  302. function console:HistoryScroll(up)
  303.   if up == true and self.historyModeLast > (console.options.lines.value - 1) then
  304.     self.historyModeLast = self.historyModeLast - (console.options.lines.value - 1)
  305.   end
  306.   if up == false then
  307.     self.historyModeLast = self.historyModeLast + (console.options.lines.value - 1)
  308.   end
  309.  
  310.   if self.historyModeLast > #self.chatBuffer then
  311.     self:ExitHistoryMode()
  312.     return
  313.   end
  314.  
  315.   for i=1,console.options.lines.value-1 do
  316.     j=self.historyModeLast-console.options.lines.value+i+1
  317.     if j > 0 then
  318.       self[1][i]:SetLine(self.chatBuffer[j][1], self.chatBuffer[j][2], -1)
  319.     else
  320.       self[1][i]:SetLine(nil, "", -1)
  321.     end
  322.   end
  323. end
  324. function console:EnterHistoryMode()
  325.   self.historyMode = true
  326.   self.historyModeLast = #self.chatBuffer
  327.   self[1][console.options.lines.value]:SetLine(nil, "...", -1)
  328.   self:HistoryScroll()
  329. end
  330. function console:ExitHistoryMode()
  331.   self.historyMode = false
  332.   self.historyModeLast = 0
  333.  
  334.   local chatLines = #self.chatBuffer
  335.   if self.options.addSystemToHistory.value and self.options.seperateMessages.value then
  336.     for _,v in ipairs(self.chatBuffer) do
  337.       if v[1] == nil then
  338.         chatLines = chatLines - 1
  339.       end
  340.     end
  341.   end
  342.  
  343.   local skipped=0
  344.   for i=console.options.lines.value,1,-1 do
  345.     j=#self.chatBuffer+i-math.min(console.options.lines.value, chatLines)-skipped
  346.  
  347.     if self.options.addSystemToHistory.value and self.options.seperateMessages.value then
  348.       if i <= chatLines then
  349.         while j > #self.chatBuffer or self.chatBuffer[j][1] == nil do
  350.           skipped = skipped + 1
  351.           j = j - 1
  352.         end
  353.       else
  354.         skipped = skipped - 1
  355.       end
  356.     end
  357.  
  358.     if j > 0 and j <= #self.chatBuffer and i <= chatLines and not self.options.decayChat.value then
  359.       self[1][i]:SetLine(self.chatBuffer[j][1], self.chatBuffer[j][2], -1)
  360.     else
  361.       self[1][i]:SetLine(nil, "", -1)
  362.     end
  363.   end
  364.   self.currentSystemLine = 1
  365.   if chatLines < console.options.lines.value then
  366.     self.currentLine = chatLines + 1
  367.   else
  368.     self.currentLine = console.options.lines.value + 1
  369.   end
  370.   if self.options.decayChat.value then
  371.     self.currentLine = 1
  372.     if self.options.hideIfEmpty.value then
  373.       self:SetVisibility(false)
  374.     end
  375.   end
  376. end
  377.  
  378. SetConsoleContent()
  379. console:SetClickthrough(true)
  380. console:PersistentCallin("AddConsoleLine")
  381.  
  382.  
  383. --mute--
  384. function widget:TextCommand(s)    
  385.      local token = {}
  386.          local n = 0
  387.          --for w in string.gmatch(s, "%a+") do
  388.          for w in string.gmatch(s, "%S+") do
  389.                 n = n +1
  390.                 token[n] = w           
  391.      end
  392.          
  393.         --for i = 1,n do Spring.Echo (token[i]) end
  394.          
  395.          if (token[1] == "mute") then
  396.                 --Spring.Echo ("geht ums muten")
  397.                  for i = 2,n do
  398.                         mutePlayer (token[i])
  399.                         Spring.Echo ("muted " .. token[i])
  400.                 end
  401.         end
  402.        
  403.         if (token[1] == "unmute") then
  404.                 --Spring.Echo ("geht ums UNmuten")
  405.                  for i = 2,n do
  406.                         unmutePlayer (token[i])
  407.                         Spring.Echo ("unmuted " .. token[i])
  408.                 end
  409.                 if (n==1) then unmuteAll() Spring.Echo ("unmuted everybody") end
  410.         end
  411.        
  412. end
  413.  
  414. --mute
  415. mutedPlayers = {}
  416. function mutePlayer (playername)
  417.         mutedPlayers[playername] = true
  418. end
  419.  
  420. function unmutePlayer (playername)
  421.         mutedPlayers[playername] = nil
  422. end
  423.  
  424. function unmuteAll ()
  425.         mutedPlayers = {}
  426. end
  427.  
  428. local sfind = string.find
  429. local slen = string.len
  430. local ssub = string.sub
  431. local sGetPlayerRoster = Spring.GetPlayerRoster
  432. function getPlayerNameFromLine (line)
  433. --taken from gui_red_console.lua by Regret
  434.         local roster = sGetPlayerRoster()
  435.         local names = {}
  436.         for i=1,#roster do
  437.                 names[roster[i][1]] = {roster[i][4],roster[i][5],roster[i][3]}
  438.         end
  439.  
  440.         local name = ""
  441.         local text = ""
  442.         local linetype = 0 --other
  443.  
  444.         if (names[ssub(line,2,(sfind(line,"> ") or 1)-1)] ~= nil) then
  445.                 linetype = 1 --playermessage
  446.                 name = ssub(line,2,sfind(line,"> ")-1)
  447.                 text = ssub(line,slen(name)+4)
  448.         elseif (names[ssub(line,2,(sfind(line,"] ") or 1)-1)] ~= nil) then
  449.                 linetype = 2 --spectatormessage
  450.                 name = ssub(line,2,sfind(line,"] ")-1)
  451.                 text = ssub(line,slen(name)+4)
  452.         elseif (names[ssub(line,2,(sfind(line,"(replay)") or 3)-3)] ~= nil) then
  453.                 linetype = 2 --spectatormessage
  454.                 name = ssub(line,2,sfind(line,"(replay)")-3)
  455.                 text = ssub(line,slen(name)+13)
  456.         elseif (names[ssub(line,1,(sfind(line," added point: ") or 1)-1)] ~= nil) then
  457.                 linetype = 3 --playerpoint
  458.                 name = ssub(line,1,sfind(line," added point: ")-1)
  459.                 text = ssub(line,slen(name.." added point: ")+1)
  460.         elseif (ssub(line,1,1) == ">") then
  461.                 linetype = 4 --gamemessage
  462.                 text = ssub(line,3)
  463.         end
  464.         return name
  465. end