Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function widget:GetInfo()
  2.     local version = "0.3 beta"
  3.     return {
  4.         name      = "Commshare: Auto Inviter v" .. version,
  5.         desc      = "Automatically invites players to squad.",
  6.         author    = "_Shaman",
  7.         date      = "7-15-2018",
  8.         license   = "OH LET IT BURN CUZ I NEED MOAR MONEY",
  9.         layer     = -99999999,  
  10.         enabled   = true,
  11.     }
  12. end
  13.  
  14.  
  15. --      Config
  16. local friends        = false                                                            -- do we invite our friends automatically?
  17. local clanmembers    = false                                                            -- do we invite our clanmates automatically?
  18. local invitebyelo    = false                                                            -- do we attempt to merge with strongest / weaker players? **May require redesign**
  19. local autoaccept     = false                                                            -- do we automatically accept lower player's requests?
  20. local automergecfg   = {ignore = {}, automerge = {}, autoaccept = {}, whitelist = {}}   -- config table. Contains ignore and automerge.
  21. local ignoreall      = false
  22.  
  23. --      variables
  24. local clan, friendlist, myelo, mylevel, ignorelist      -- These are used for determining merges.
  25. local mergelist      = ""                               -- list of players to merge with.
  26. local mergecount     = 0                                -- count of the number of players we're merging with.
  27. local allylist       = {}                               -- Processed Alliance members.
  28. local autoinvited    = false                            -- have we automatically invited people?
  29. local enabledplayers = {}                               -- table of players who have this widget.
  30.  
  31. --      Options
  32. options_path = 'Settings/Interface/Commshare'
  33.  
  34. options_order = {
  35.     'invitefriends',
  36.     'inviteclanmembers',
  37.     --'invitebyelo',
  38.     --'autoaccept',
  39.     'ignoreall',
  40. }
  41.  
  42. options = {
  43.     invitefriends = {
  44.         name  = "Automatically Invite Friends",
  45.         type  = "bool",
  46.         value = false,
  47.         OnChange = function(self)
  48.             friends = self.value
  49.         end,
  50.         noHotkey = true,
  51.         desc = "Automatically invites your friends to a commshare merger."
  52.     },
  53.     inviteclanmembers = {
  54.         name  = "Automatically Invite Clan Members",
  55.         type  = "bool",
  56.         value = false,
  57.         OnChange = function(self)
  58.             clanmembers = self.value
  59.         end,
  60.         noHotkey = true,
  61.         desc = "Automatically invites your fellow clan members to a commshare merger."
  62.     },
  63.     invitebyelo = {
  64.         name  = "Invite Strongest Player",
  65.         type  = "bool",
  66.         value = false,
  67.         OnChange = function(self)
  68.             invitebyelo = self.value
  69.         end,
  70.         noHotkey = true,
  71.         desc = "Automatically requests a merge with the strongest player."
  72.     },
  73.     autoaccept = {
  74.         name  = "Autoaccept automated requests",
  75.         type  = "bool",
  76.         value = false,
  77.         OnChange = function(self)
  78.             autoaccept = self.value
  79.         end,
  80.         noHotkey = true,
  81.         desc = "Automatically accept automerge requests?"
  82.     },
  83.     ignoreall = {
  84.         name  = "Decline mode:",
  85.         type  = "radioButton",
  86.         items = {
  87.             {name = 'All',key='all',description="Ignore all invites."},
  88.             {name = 'Non-clan members', key='nonclan',description="Ignore all nonclan members."},
  89.             {name = 'Non-friend', key='nonfriend', description="Ignore all nonfriends."},
  90.             {name = 'Non-clan/friends', key='nonfriendclan', description="Ignore everyone who isn't your friend or in your clan."},
  91.             {name = 'None', key='none', description="Don't ignore any invites."}
  92.         },
  93.         value = 'none',
  94.         OnChange = function(self)
  95.             ignoreall = self.value
  96.         end,
  97.         noHotkey = true,
  98.         desc = "Declines certain users based on this param."
  99.     },
  100. }
  101.  
  102. -- Loading tables used--
  103. function widget:GetConfigData()
  104.     return automergecfg
  105. end
  106.  
  107. function widget:SetConfigData(data)
  108.     automergecfg = data
  109. end
  110.  
  111. local function ProcessAllyTeam()
  112.     local teamlist = Spring.GetTeamList(Spring.GetMyAllyTeamID())
  113.     for i=1, #teamlist do
  114.         local playerlist = Spring.GetPlayerList(teamlist[i])
  115.         for j=1, #playerlist do
  116.             local playerID, _, spec, teamID, _, _, _, _, _, playerKeys = Spring.GetPlayerInfo(playerlist[j])
  117.             if not spec then
  118.                 allylist[playerID] = {
  119.                 team = playerlist[j],
  120.                 elo = playerKeys.elo,
  121.                 level = playerKeys.level,
  122.                 clan = playerKeys.clanfull,
  123.                 }
  124.             end
  125.         end
  126.     end
  127. end
  128.  
  129. local function ProccessCommand(str)
  130.     local strtbl = {}
  131.     for w in string.gmatch(str, "%S+") do
  132.         if #strtbl < 5 then
  133.             strtbl[#strtbl+1] = w
  134.         else
  135.             strtbl[5] = strtbl .. " " .. w
  136.         end
  137.     end
  138.     return strtbl
  139. end
  140.  
  141. local function GetHighestElo(tab)
  142.     local highestteamid = -1
  143.     local highestelo = 0
  144.     for name,data in pairs(tab) do
  145.         if data["elo"] > highestelo then
  146.             highestelo = data["elo"]
  147.             highestteamid = data["team"]
  148.         end
  149.     end
  150.     return highestteamid
  151. end
  152.  
  153. function widget:GamePreload()
  154.     Spring.SendLuaUIMsg("automerger", "a")
  155. end
  156.  
  157. function widget:RecvLuaMsg(msg, playerID)
  158.     if msg == "automerger" then
  159.         enabledplayers[select(1, Spring.GetPlayerInfo(playerID))] = playerID
  160.         Spring.Echo("player " .. select(1, Spring.GetPlayerInfo(playerID)) .. " is using automerge!")
  161.     end
  162. end
  163.  
  164. function widget:Initialize()
  165.     local _,_,spec,_ = Spring.GetPlayerInfo(Spring.GetMyPlayerID())
  166.     if spec then
  167.         widgetHandler:RemoveWidget()
  168.     end
  169.     local customkeys = select(10, Spring.GetPlayerInfo(Spring.GetMyPlayerID()))
  170.     clan = customkeys["clanfull"] -- the full name of your clan.
  171.     friendlist = customkeys["friends"] -- your friends list.
  172.     myelo = customkeys["elo"] -- your elo.
  173.     ignorelist = customkeys["ignored"] -- your ignore list. We automatically ignore requests from these users.
  174.     mylevel = customkeys["level"] -- your level.
  175.     ProcessAllyTeam()
  176.     Spring.Echo("Automerge: Determing merge list. Config:\nClan: " .. tostring(clanmembers) .. "\nfriends: " .. tostring(friends))
  177.     for name, data in pairs(allylist) do
  178.         if automergecfg.automerge[name] or (friends and friendlist[name]) or (clanmembers and data.clan == clan) then
  179.             mergelist = mergelist .. " " .. name -- clever way of working around table creation.
  180.             mergecount = mergecount + 1
  181.         end
  182.     end
  183. end
  184.  
  185. function widget:TextCommand(msg)
  186.     if msg:find("automerge") then
  187.         local command = ProccessCommand(msg)
  188.         if command[2] == "ignore" then
  189.             if command[3] == "add" and command[4] and command[4] ~= "" then
  190.                 Spring.Echo("game_message: ignoring invites from " .. command[4] .. ".")
  191.                 if command[5] == nil or command[5] == "" then
  192.                     local randomnum = math.random(1,5)
  193.                     if randomnum == 1 then
  194.                         command[5] = "Reasons"
  195.                     elseif randomnum == 2 then
  196.                         command[5] = "emotions"
  197.                     elseif randomnum == 3 then
  198.                         command[5] = "Don't know"
  199.                     elseif randomnum == 4 then
  200.                         command[5] = "Wubwub"
  201.                     else
  202.                         command[5] = "Lobster"
  203.                     end
  204.                 end
  205.                 automergecfg.ignore[command[4]] = command[5]
  206.                 automergecfg.automerge[command[4]] = nil
  207.             elseif command[3] == "remove" and command[4] and command[4] ~= "" then
  208.                 automergecfg.ignore[command[4]] = nil
  209.                 Spring.Echo("game_message: You are no longer ignoring " .. command[4] .. ".")
  210.             elseif command[3] == "list" then
  211.                 local ignorelist = ""
  212.                 local count = 0
  213.                 for name,reason in pairs(automergecfg.ignore) do
  214.                     ignorelist = ignorelist .. name .. ": " .. reason .. "\n"
  215.                     count = count+1
  216.                 end
  217.                 if count > 1 then
  218.                     Spring.Echo("game_message: You are ignoring " .. count .. " users' requests:\n" .. ignorelist)
  219.                 elseif count == 1 then
  220.                     Spring.Echo("game_message: You are ignoring " .. count .. " user's request:\n" .. ignorelist)
  221.                 else
  222.                     Spring.Echo("game_message: You aren't ignoring any user's request.")
  223.                 end
  224.             end
  225.         elseif command[2] == "add" then
  226.             if command[3] and command[3] ~= "" then
  227.                 if automergecfg.ignore[command[3]] or ignorelist:find(command[3]) then
  228.                     Spring.Echo("game_message: Remove " .. command[3] .. " from your ignore list first.")
  229.                 end
  230.                 automergecfg.automerge[command[3]] = true
  231.                 Spring.Echo("game_message:Added " .. command[3] .. " to automerge.")
  232.             else
  233.                 Spring.Echo("game_message: Invalid param for add.")
  234.             end
  235.         elseif command[2] == "remove" then
  236.             if command[3] and command[3] ~= "" then
  237.                 automergecfg.automerge[command[3]] = nil
  238.                 Spring.Echo("game_message:Removed " .. command[3] .. " from automerge.")
  239.             else
  240.                 Spring.Echo("game_message: Invalid param for remove.")
  241.             end
  242.         elseif command[2] == "list" then
  243.             local ignorelist = ""
  244.             local count = 0
  245.             for id,_ in pairs(automergecfg.automerge) do
  246.                 if count == 0 then
  247.                     ignorelist = ignorelist .. id
  248.                 else
  249.                     ignorelist = ignorelist .. ", "
  250.                 end
  251.                 count = count+1
  252.             end
  253.             if count > 1 then
  254.                 Spring.Echo("game_message: You are automerging with " .. count .. " users:\n" .. ignorelist)
  255.             elseif count == 1 then
  256.                 Spring.Echo("game_message: You are automerging with " .. count .. " user:\n" .. ignorelist)
  257.             else
  258.                 Spring.Echo("game_message: You aren't automerging with anyone.")
  259.             end
  260.         else
  261.             Spring.Echo("game_message: Invalid automerge command.")
  262.         end
  263.     end
  264. end
  265.  
  266. function widget:GameFrame(f)
  267.     if autoinvited == false and f> 10 then
  268.         autoinvited = true
  269.         local invited = "I invited " -- list of players we've invited.
  270.         for name,data in pairs(allylist) do
  271.             if mergelist:find(name) then
  272.                 Spring.SendLuaRulesMsg("sharemode invite " .. data["team"])
  273.                 if enabledplayers[name] == nil then
  274.                     invited = invited .. name .. ","
  275.                 end
  276.             end
  277.         end
  278.         if invited ~= "I invited " then
  279.             invited = invited:sub(1, -2)
  280.             Spring.SendCommands("say a:" .. invited .. " to join my squad.")
  281.         end
  282.         if invitebyelo then
  283.             local highestplayer = GetHighestElo(playerlistprocessed)
  284.             if highestplayer == Spring.GetMyTeamID() then
  285.                 for name,data in pairs(enabledplayers) do
  286.                     if automergecfg.ignore[name] == nil and not ignorelist:find(name) then
  287.                         Spring.SendLuaRulesMsg("sharemode invite " .. data["team"])
  288.                     end
  289.                 end
  290.             end
  291.         end
  292.     end
  293.     if f%15 == 0 then
  294.         local invitecount = Spring.GetPlayerRulesParam(Spring.GetMyPlayerID(), "commshare_invitecount") or 0
  295.         if invitecount > 0 then
  296.             for i=1, invitecount do
  297.                 local id = Spring.GetPlayerRulesParam(Spring.GetMyPlayerID(), "commshare_invite_" .. i .. "_id")
  298.                 local name,_ = Spring.GetPlayerInfo(id)
  299.                 if enabledplayers[name] and autoaccept then
  300.                     Spring.SendLuaRulesMsg("sharemode accept " .. id)
  301.                 elseif automergecfg.ignore[name] or string.find(ignorelist,name) or ((ignoreall == "nonfriend" or ignoreall == "nonfriendclan") and not friendlist:find(name)) or ((ignoreall == "nonclan" or ignoreall == "nonfriendclan") and clan ~= allylist[name].clan) then
  302.                     Spring.SendLuaRulesMsg("sharemode decline " .. id)
  303.                     local reason = ""
  304.                     if string.find(ignorelist,name) then
  305.                         reason = "You are ignored by this user."
  306.                     end
  307.                     if reason == "" and automergecfg.ignore[name] then
  308.                         reason = automergecfg.ignore[name]
  309.                     end
  310.                     if reason == "" and ignoreall ~= "none" and ignoreall ~= "all" then
  311.                         reason = "Not interested in random invites."
  312.                     else
  313.                         reason = "Not interested in commshare."
  314.                     end
  315.                     Spring.SendCommands("w " .. name .. " is not interested in merging with you. Reason: " .. reason)
  316.                 end
  317.             end
  318.         end
  319.     end
  320. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement