daily pastebin goal
41%
SHARE
TWEET

Alias System

Chalwk77 Jan 10th, 2019 (edited) 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. --=====================================================================================================--
  3. Script Name: Alias System, for SAPP (PC & CE)
  4. Description: Query a player's hash to check what aliases have been used with it.
  5.  
  6. Command syntax: /alias [id]
  7.  
  8. * Coming in a future update:
  9.     - ip search feature
  10.     - name search feature
  11.     - hash search feature
  12.  
  13. Copyright (c) 2016-2018, Jericho Crosby <jericho.crosby227@gmail.com>
  14. * Notice: You can use this document subject to the following conditions:
  15. https://github.com/Chalwk77/Halo-Scripts-Phasor-V2-/blob/master/LICENSE
  16.  
  17. * Written by Jericho Crosby (Chalwk)
  18. --=====================================================================================================--
  19. ]]--
  20.  
  21. api_version = "1.11.0.0"
  22.  
  23. -- configuration starts
  24. file_name = "alias.lua" -- File is saved to server root dir/sapp/alias.lua
  25. base_command = "alias"
  26.  
  27. -- How long should the alias results be displayed for? (in seconds) --
  28. duration = 10
  29.  
  30. -- Message Alignment:
  31. -- Left = l,    Right = r,    Center = c,    Tab: t
  32. Message_Alignment = "l"
  33.  
  34. -- minimum admin level required to use /alias command
  35. min_admin_level = 1
  36. -- configuration ends
  37.  
  38. -- do not touch
  39. trigger = { }
  40. new_timer = { }
  41. players = { }
  42. index = nil
  43. bool = {}
  44.  
  45. function OnScriptLoad()
  46.     register_callback(cb['EVENT_TICK'], "OnTick")
  47.     register_callback(cb['EVENT_PREJOIN'], "OnPlayerPrejoin")
  48.     register_callback(cb['EVENT_GAME_END'], "OnGameEnd")
  49.     register_callback(cb['EVENT_LEAVE'], "OnPlayerLeave")
  50.     register_callback(cb['EVENT_GAME_START'], "OnNewGame")
  51.     register_callback(cb['EVENT_COMMAND'], "OnServerCommand")
  52.     checkFile()
  53.     for i = 1, 16 do
  54.         if player_present(i) then
  55.             players[get_var(i, "$n")].new_timer = 0
  56.         end
  57.     end
  58. end
  59.  
  60. function OnScriptUnload()
  61.  
  62. end
  63.  
  64. function OnPlayerPrejoin(PlayerIndex)
  65.     local name = get_var(PlayerIndex, "$name")
  66.     local hash = get_var(PlayerIndex, "$hash")
  67.     addAlias(name, hash)
  68.     players[get_var(PlayerIndex, "$n")] = { }
  69.     players[get_var(PlayerIndex, "$n")].new_timer = 0
  70. end
  71.  
  72. function OnPlayerLeave(PlayerIndex)
  73.     bool[PlayerIndex] = false
  74.     trigger[PlayerIndex] = false
  75.     players[get_var(PlayerIndex, "$n")].new_timer = 0
  76. end
  77.  
  78. function OnNewGame()
  79.     for i = 1, 16 do
  80.         if player_present(i) then
  81.             if player_present(i) then
  82.                 bool[i] = false
  83.                 trigger[i] = false
  84.                 players[get_var(i, "$n")].new_timer = 0
  85.             end
  86.         end
  87.     end
  88. end
  89.  
  90. function OnGameEnd()
  91.     for i = 1, 16 do
  92.         if player_present(i) then
  93.             if player_present(i) then
  94.                 trigger[i] = false
  95.                 players[get_var(i, "$n")].new_timer = 0
  96.             end
  97.         end
  98.     end
  99. end
  100.  
  101. function OnTick()
  102.     for i = 1, 16 do
  103.         if player_present(i) then
  104.             if (trigger[i] == true) then
  105.                 players[get_var(i, "$n")].new_timer = players[get_var(i, "$n")].new_timer + 0.030
  106.                 cls(i)
  107.                 concatValues(i, 1, 6)
  108.                 concatValues(i, 7, 12)
  109.                 concatValues(i, 13, 18)
  110.                 concatValues(i, 19, 24)
  111.                 concatValues(i, 25, 30)
  112.                 concatValues(i, 31, 36)
  113.                 concatValues(i, 37, 42)
  114.                 concatValues(i, 43, 48)
  115.                 concatValues(i, 49, 55)
  116.                 concatValues(i, 56, 61)
  117.                 concatValues(i, 62, 67)
  118.                 concatValues(i, 68, 73)
  119.                 concatValues(i, 74, 79)
  120.                 concatValues(i, 80, 85)
  121.                 concatValues(i, 86, 91)
  122.                 concatValues(i, 92, 97)
  123.                 concatValues(i, 98, 100)
  124.                 if (bool[i] == true) then
  125.                     rprint(i, "|" .. Message_Alignment .. " " .. 'Showing aliases for: "' .. target_hash .. '"')
  126.                 end
  127.                 if players[get_var(i, "$n")].new_timer >= math.floor(duration) then
  128.                     trigger[i] = false
  129.                     bool[i] = false
  130.                     players[get_var(i, "$n")].new_timer = 0
  131.                 end
  132.             end
  133.         end
  134.     end
  135. end
  136.  
  137. function addAlias(name, hash)
  138.     local file = io.open("sapp\\" .. file_name, "r")
  139.     local data = file:read("*a")
  140.     file:close()
  141.     if string.match(data, hash) then
  142.         local lines = lines_from("sapp\\" .. file_name)
  143.         for k, v in pairs(lines) do
  144.             if string.match(v, hash) then
  145.                 if not v:match(name) then
  146.                     local alias = v .. ", " .. name
  147.                     local f = io.open("sapp\\" .. file_name, "r")
  148.                     local content = f:read("*all")
  149.                     f:close()
  150.                     content = string.gsub(content, v, alias)
  151.                     local f = io.open("sapp\\" .. file_name, "w")
  152.                     f:write(content)
  153.                     f:close()
  154.                 end
  155.             end
  156.         end
  157.     else
  158.         local file = assert(io.open("sapp\\" .. file_name, "a+"))
  159.         file:write("\n" .. hash .. ":" .. name, "\n")
  160.         file:close()
  161.     end
  162. end
  163.  
  164. function checkFile()
  165.     local file = io.open('sapp\\' .. file_name, "rb")
  166.     if file then
  167.         file:close()
  168.     else
  169.         local file = io.open('sapp\\' .. file_name, "a+")
  170.         if file then
  171.             file:close()
  172.         end
  173.     end
  174. end
  175.  
  176. function OnServerCommand(PlayerIndex, Command)
  177.     local t = tokenizestring(Command)
  178.     if isAdmin(PlayerIndex) and t[1] == string.lower(base_command) then
  179.         if t[2] ~= nil then
  180.             if t[2] == string.match(t[2], "^%d+$") and t[3] == nil then
  181.                 if player_present(tonumber(t[2])) then
  182.                     index = tonumber(t[2])
  183.                     target_hash = tostring(get_var(index, "$hash"))
  184.                     if trigger[PlayerIndex] == true then
  185.                         -- aliases already showing (clear console then show again)
  186.                         cls(PlayerIndex)
  187.                         players[get_var(PlayerIndex, "$n")].new_timer = 0
  188.                         trigger[PlayerIndex] = true
  189.                         bool[PlayerIndex] = true
  190.                     else
  191.                         -- show aliases (first time)
  192.                         trigger[PlayerIndex] = true
  193.                         bool[PlayerIndex] = true
  194.                     end
  195.                 else
  196.                     players[get_var(PlayerIndex, "$n")].new_timer = 0
  197.                     trigger[PlayerIndex] = false
  198.                     cls(PlayerIndex)
  199.                     rprint(PlayerIndex, "Player not present")
  200.                 end
  201.             else
  202.                 players[get_var(PlayerIndex, "$n")].new_timer = 0
  203.                 trigger[PlayerIndex] = false
  204.                 cls(PlayerIndex)
  205.                 rprint(PlayerIndex, "Invalid player id")
  206.             end
  207.             return false
  208.         else
  209.             rprint(PlayerIndex, "Invalid syntax. Use /" .. base_command .. " [id]")
  210.             return false
  211.         end
  212.     end
  213. end
  214.  
  215. function isAdmin(PlayerIndex)
  216.     if (tonumber(get_var(PlayerIndex, "$lvl"))) >= min_admin_level then
  217.         return true
  218.     else
  219.         return false
  220.     end
  221. end
  222.  
  223. function lines_from(file)
  224.     lines = {}
  225.     for line in io.lines(file) do
  226.         lines[#lines + 1] = line
  227.     end
  228.     return lines
  229. end
  230.  
  231. function tokenizestring(inputstr, sep)
  232.     if sep == nil then
  233.         sep = "%s"
  234.     end
  235.     local t = { };
  236.     i = 1
  237.     for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
  238.         t[i] = str
  239.         i = i + 1
  240.     end
  241.     return t
  242. end
  243.  
  244. function cls(PlayerIndex)
  245.     for _ = 1, 25 do
  246.         rprint(PlayerIndex, " ")
  247.     end
  248. end
  249.  
  250. function concatValues(PlayerIndex, start_index, end_index)
  251.     local lines = lines_from("sapp\\" .. file_name)
  252.     for k, v in pairs(lines) do
  253.         if v:match(target_hash) then
  254.             local aliases = string.match(v, (":(.+)"))
  255.             local words = tokenizestring(aliases, ", ")
  256.             local word_table = {}
  257.             local row
  258.             for i = tonumber(start_index), tonumber(end_index) do
  259.                 if words[i] ~= nil then
  260.                     table.insert(word_table, words[i])
  261.                     row = table.concat(word_table, ", ")
  262.                 end
  263.             end
  264.             if row ~= nil then rprint(PlayerIndex, "|" .. Message_Alignment .. " " .. row) end
  265.             for _ in pairs(word_table) do word_table[_] = nil end
  266.             break
  267.         end
  268.     end
  269. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top