Advertisement
Tastyep

mini Admin mod ET

Jul 12th, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.45 KB | None | 0 0
  1.  local  pass = 0
  2.  local  client_lev = {}
  3.  local  sys_level = {}
  4.  
  5.  --[[
  6.     It must contain a file lev_list.txt where levels are defined
  7.     ex: Admin = 777 !ban, !help, !setlevel, !lev, !ref, !kick, !mute, !unmute, !splat
  8.     There must be a ',' between each commands
  9.  --]]
  10.  
  11.  --[[ flag list
  12.         256: "!ref",
  13.         128: "!ban",
  14.         64: "!kick",
  15.         32: "!setlevel",
  16.         16: "!mute",
  17.         8: "!unmute",
  18.         4: "!splat",
  19.         2: "!lev",
  20.         1: "!help"
  21.     --]]
  22.  
  23.  function   skip_carac(str, spe, i)
  24.     while (string.sub(str, i, i) == sep) do
  25.         i = i + 1
  26.     end
  27.     return  i
  28.  end
  29.  
  30.  function   str_to_wordtab(str, sep)
  31.     local   tab = {}
  32.     local   len = string.len(str)
  33.     local   wend = 1
  34.     local   y = 1
  35.    
  36.     wend = skip_carac(str, sep, wend)
  37.     wstart = wend
  38.     while (wend <= len) do
  39.         local carac = string.sub(str, wend, wend)
  40.         if (carac == sep or wend == len) then
  41.             if (wend == len) then wend = wend + 1 end
  42.             tab[y] = string.sub(str, wstart, wend - 1)
  43.             y = y + 1
  44.             wend = skip_carac(str, sep, wend) + 1
  45.             wstart = wend
  46.         else
  47.             wend = wend + 1
  48.         end
  49.     end
  50.     return  tab
  51.  end
  52.  
  53.  function   fill_client_level(tab, maxclients)
  54.     local   i = 0
  55.    
  56.     while (i < maxclients) do
  57.         local   cl_guid = string.lower(et.Info_ValueForKey(et.trap_GetUserinfo(i), "cl_guid"))
  58.        
  59.         if (cl_guid == tab[2]) then
  60.             client_lev[i + 1] = tonumber(tab[3])
  61.         end
  62.         i = i + 1
  63.     end
  64.  end
  65.  
  66.  function   get_pl_levels() -- load client levels
  67.     local   maxclients = tonumber((et.trap_Cvar_Get("sv_maxClients")) - 1)
  68.     local   file = io.open("pl_levels.txt", "r")
  69.     local   line = nil
  70.     local   i = 0
  71.    
  72.     while (i < maxclients) do -- init to 0
  73.         client_lev[i + 1] = 0
  74.         i = i + 1
  75.     end
  76.     if (file ~= nil) then -- load levels
  77.         line = file:read("*line")
  78.         while (line ~= nil) do
  79.             local   tab = str_to_wordtab(line, ' ')
  80.             fill_client_level(tab, maxclients)
  81.             line = file:read("*line")
  82.         end
  83.         file:close()
  84.     end
  85.  end
  86.  
  87.  function   et_ClientBegin(clientNum)
  88.     local   game = 2
  89.     local   gamestate = tonumber(et.trap_Cvar_Get( "gamestate"))
  90.    
  91.     if (pass == 0) then
  92.         sys_level = get_levels()
  93.         if (sys_level == nil) then et.trap_SendServerCommand(-1, "chat \"FAIL\"") end
  94.         get_pl_levels()
  95.     end
  96.     pass = 1
  97.     if (gamestate == game) then
  98.         local   name = string.gsub(et.gentity_get(clientNum, "pers.netname"), "%^$", "^^ ")
  99.         local   rank = get_pl_rank_from_lev(client_lev[clientNum + 1])
  100.    
  101.         et.trap_SendServerCommand(-1, "chat \"^7Welcome to "..name..", ^2"..rank.."\"")
  102.     end
  103.  end
  104.  
  105.  function   epur_name(id)   -- remove spaces and color codes
  106.     local   name = et.gentity_get(id, "pers.netname")
  107.    
  108.     if (name == nil) then return nil end
  109.     name = string.gsub(name, "%^$", "^^ ")
  110.     name = string.gsub(name, " ", "")
  111.     name = string.gsub(name, "(^%w)", "")
  112.     return  name
  113.  end
  114.  
  115.  function   get_player_info(client)
  116.     local   maxclients = tonumber((et.trap_Cvar_Get("sv_maxClients")) - 1)
  117.     local   name = nil
  118.     local   i = 0
  119.    
  120.     if (string.find(client, "%a") == nil) then
  121.         name = epur_name(client)
  122.         return client, name
  123.     end
  124.     client = string.gsub(client, " ", "")
  125.     client = string.gsub(client, "(^%w)", "")
  126.     while (i < maxclients) do
  127.         local   pl_name = epur_name(i)
  128.  
  129.         if (pl_name ~= nil) then
  130.             if (pl_name ~= nil and string.find(pl_name, client) ~= nil) then
  131.                 return i, pl_name
  132.             end
  133.         end
  134.         i = i + 1
  135.     end
  136.     return -1, nil
  137.  end
  138.  
  139.  function   setlevel(adm, client, lev) -- set the new level of a client
  140.     local   ptr = io.open("pl_levels.txt", "r")
  141.     local   file = {}
  142.     local   i = 1
  143.     local   j = 1
  144.    
  145.     local   id = -1     -- players info
  146.     local   name = nil
  147.    
  148.     id, name = get_player_info(client)
  149.     if (id == -1 or name == nil) then
  150.         et.trap_SendServerCommand(adm, "chat \"Player "..client.." doesn't exist\"")
  151.         return -1
  152.     end
  153.     local   pl_guid = string.lower(et.Info_ValueForKey(et.trap_GetUserinfo(id), "cl_guid"))
  154.     if (pl_guid == "unknown") then
  155.         et.trap_SendServerCommand(adm, "chat \"Unknown guid\"")
  156.         return -1
  157.     end
  158.     if (ptr ~= nil) then
  159.         line = ptr:read("*l")
  160.         while (line ~= nil) do
  161.             file[i] = line
  162.             i = i + 1
  163.             line = ptr:read("*l")
  164.         end
  165.         ptr:close()
  166.     end
  167.     ptr = io.open("pl_levels.txt", "w")
  168.     while (j < i and string.find(file[j], pl_guid) == nil) do
  169.         j = j + 1
  170.     end
  171.     file[j] = name .. " " .. pl_guid .. " " .. lev
  172.     if (j == i) then
  173.         i = i + 1
  174.     end
  175.     j = 1
  176.     while (j < i) do
  177.         ptr:write(file[j])
  178.         ptr:write("\n")
  179.         j = j + 1
  180.     end
  181.     name = string.gsub(et.gentity_get(id, "pers.netname"), "%^$", "^^ ")
  182.     et.trap_SendServerCommand(-1, "chat \"^7Player ^7"..name.."^7 has been setted to level "..lev.."\"")
  183.     ptr:close()
  184.     return 0
  185.  end
  186.  
  187.  function   checkmuted(client)
  188.     return  et.gentity_get(client, "sess.muted", 1)
  189.  end
  190.  
  191.  function   mute_pl(client, id, mu_time)        -- time doesnt work yet
  192.     local   name = string.gsub(et.gentity_get(id, "pers.netname"), "%^$", "^^ ")
  193.     if (checkmuted(id) == 1) then               -- already muted
  194.         et.trap_SendServerCommand(client, "chat \"^2The player is already muted\"")
  195.         return 1
  196.     end
  197.     if (mu_time == nil) then
  198.         mu_time = 600   -- muted time set to 10 mins
  199.     end
  200.     et.gentity_set(id, "sess.muted", 1)
  201.     et.trap_SendServerCommand(-1, "cpm \""..name.."^7 is ^qmuted\"")
  202.     return 1
  203.  end
  204.  
  205.  function   unmute_pl(client, id)       -- time doesnt work yet
  206.     local   name = string.gsub(et.gentity_get(id, "pers.netname"), "%^$", "^^ ")
  207.     if (checkmuted(id) ~= 1) then               -- already muted
  208.         et.trap_SendServerCommand(client, "chat \"^2The player is not muted\"")
  209.         return 1
  210.     end
  211.     et.gentity_set(id, "sess.muted", 0)
  212.     et.trap_SendServerCommand(-1, "cpm \""..name.."^7 is ^qunmuted\"")
  213.     return 1
  214.  end
  215.  
  216.  function   check_levels(id1, id2)
  217.     if (client_lev[id1 + 1] >= client_lev[id2 + 1]) then
  218.         return 1
  219.     else
  220.         et.trap_SendServerCommand(id1, "chat \"You can't use this command on this player because of your level\"")
  221.         return -1
  222.     end
  223.  end
  224.  
  225.  function   ban_pl(client, id, ban_time)
  226.     if (ban_time == nil) then
  227.         ban_time = 6000000
  228.     end
  229.     if (type(ban_time) ~= 'number') then
  230.         et.trap_SendServerCommand(client, "chat \"ban time must be a number\"")
  231.         return 1
  232.     end
  233.     et.trap_DropClient(id, "Banned by admin", ban_time) -- ban for x2 mins
  234.  end
  235.  
  236.  function   help_cmd(client, flag)
  237.     local   bits = convert_to_binary(flag)
  238.     local   tflag = flag
  239.     local   i = 1
  240.     local   cmd_list = {
  241.                         "!help",    --  1
  242.                         "!lev",     --  2
  243.                         "!splat",   --  4
  244.                         "!unmute",  --  8
  245.                         "!mute",    --  16
  246.                         "!kick",    --  32
  247.                         "!setlevel",--  64
  248.                         "!ban",     --  128
  249.                         "!ref"      --  256
  250.                         }
  251.  
  252.     et.trap_SendServerCommand(client, "chat \"\nCommand list^q:\n\"")
  253.     while (cmd_list[i] ~= nil) do
  254.         if (bits[i] == 1) then et.trap_SendServerCommand(client, "chat \""..cmd_list[i].."\"") end
  255.         i = i + 1
  256.     end
  257.  end
  258.  
  259.  function   count_alive_players()
  260.     local   alive = 0
  261.     local   maxclients = tonumber((et.trap_Cvar_Get( "sv_maxClients" )) -1)
  262.    
  263.     for i = 0, maxclients, 1 do
  264.         local   clientteam = tonumber(et.gentity_get(i, "sess.sessionTeam"))
  265.         if (clientteal == 1 or clientteam == 2) then
  266.             if (tonumber(et.gentity_get(i, "health")) > 0) then alive = alive + 1 end
  267.         end
  268.     end
  269.     return alive
  270.  end
  271.  
  272.  function   splat_cmd(client, id)
  273.     local   namep = string.gsub(et.gentity_get(id, "pers.netname"), "%^$", "^^ ")
  274.     local   namec = string.gsub(et.gentity_get(client, "pers.netname"), "%^$", "^^ ")
  275.    
  276.     et.gentity_set(id, "health", -500)
  277.     et.G_globalSound("sound/player/gib.wav")
  278.     et.trap_SendServerCommand(-1, "chat \""..namep.." ^7got ^qSplated ^7by "..namec.."\"")
  279.     if (tonumber(et.trap_Cvar_Get( "gamestate")) == 0 and count_alive_players() == 0) then
  280.         et.trap_SendServerCommand(-1, "chat \"^t-^7Match ended because there was no players alive^t-\"")
  281.         et.trap_SendConsol3eCommand(et.EXEC_APPEND, "ref matchreset")
  282.     end
  283.  end
  284.  
  285.  function   get_level_name(line)
  286.     local   pos = string.find(line, '=') -- geting the level name
  287.     local   name = nil
  288.    
  289.     if (pos == nil) then -- no = in the line
  290.         return nil
  291.     elseif (pos == 1) then -- no name
  292.         return nil
  293.     end
  294.     pos = pos - 1
  295.     name = string.sub(line, 1, pos)
  296.     return name
  297.  end
  298.  
  299.  function   get_flag(line)
  300.     local   flag = 0
  301.     local   pos1, pos2 = 1
  302.     local   lev = 0
  303.     local   i = 1
  304.     local   commands = {}
  305.     local   cmd_list = {
  306.                         "!help",    --  1
  307.                         "!lev",     --  2
  308.                         "!splat",   --  4
  309.                         "!unmute",  --  8
  310.                         "!mute",    --  16
  311.                         "!kick",    --  32
  312.                         "!setlevel",--  64
  313.                         "!ban",     --  128
  314.                         "!ref"      --  256
  315.                         }
  316.    
  317.     pos1, pos2 = string.find(line, "%d+", 1)
  318.     if (pos1 == nil or pos2 == nil) then return nil, 0 end
  319.     lev = tonumber(string.sub(line, pos1, pos2))
  320.     if (lev == nil) then return nil, 0 end -- no level
  321.     pos1, pos2 = string.find(line, '!')
  322.     if (pos1 == nil) then return lev, flag end
  323.     line = string.sub(line, pos1)
  324.     if (line == nil) then return lev, flag end  -- no commands
  325.     commands = str_to_wordtab(line, ',')        -- Here, ',' is used as separator
  326.     local   j = 1
  327.     while (commands[i] ~= nil) do
  328.         j = 1
  329.         while (cmd_list[j] ~= nil and string.find(cmd_list[j], commands[i]) == nil) do
  330.             j = j + 1
  331.         end
  332.         if (cmd_list[j] ~= nil) then
  333.             flag = flag + math.pow(2, j - 1)
  334.         end
  335.         i = i + 1
  336.     end
  337.     return lev, flag
  338.  end
  339.  
  340.  function   get_levels()        -- get levels from file
  341.     local   levels = {}         -- contains Name - level - flag
  342.     local   file = io.open("lev_list.txt", "r")
  343.     local   line = nil
  344.     local   i = 1
  345.    
  346.     if (file ~= nil) then -- load levels
  347.         line = file:read("*line")
  348.         while (line ~= nil) do
  349.             levels[i] = {}  -- create the second dimention to stock the informations
  350.             local   name = get_level_name(line)
  351.            
  352.             if (name == nil) then return nil end
  353.             levels[i][1] = name
  354.             line = string.sub(line, string.find(line, '=') + 1)
  355.             if (line == nil) then return nil end
  356.             line = string.gsub(line, ' ', '') -- take off the spaces
  357.             levels[i][2], levels[i][3] = get_flag(line)
  358.             if (levels[i][2] == nil) then return nil end
  359.             line = file:read("*line")
  360.             i = i + 1
  361.         end
  362.         file:close()
  363.     end
  364.     return levels
  365.  end
  366.  
  367.  
  368.  function   get_pl_rank_from_lev(pl_level)
  369.     local   i = 1
  370.    
  371.     while (sys_level[i] ~= nil and sys_level[i][2] ~= pl_level) do
  372.         i = i + 1
  373.     end
  374.     if (sys_level[i] ~= nil) then return sys_level[i][1] end
  375.     return "not setted"
  376.  end
  377.  
  378.  function   get_pl_flag_from_lev(pl_level)
  379.     local   i = 1
  380.    
  381.     while (sys_level[i] ~= nil and sys_level[i][2] ~= pl_level) do
  382.         i = i + 1
  383.     end
  384.     if (sys_level[i] ~= nil) then return sys_level[i][3] end
  385.     return 0
  386.  end
  387.  
  388.  function   convert_to_binary(nb)
  389.     local   bin = {}
  390.     local   i = 1
  391.     local   cast = 0
  392.    
  393.     while (nb > 0) do
  394.         bin[i] = math.mod(nb, 2)
  395.         nb = nb / 2
  396.         cast = math.mod(nb, 1)
  397.         nb = nb - cast
  398.         i = i + 1
  399.     end
  400.     return bin
  401.  end
  402.  
  403.  function   handle_cmd(client, flag, rank, arg, id)
  404.     local   bits = convert_to_binary(flag)
  405.     local   tflag = flag
  406.    
  407.     if (bits[9] == 1 and arg[2] == "!ref") then
  408.         et.trap_SendConsoleCommand(et.EXEC_APPEND, "ref referee "..client.."")
  409.         return 1
  410.     elseif (bits[8] == 1 and arg[2] == "!ban") then
  411.             if (check_levels(client, id) == -1) then return -1 end
  412.             ban_pl(client, id, tonumber(arg[4]))
  413.             return 1
  414.     elseif (bits[7] == 1 and string.len(arg[2]) > 2 and string.find("!setlevel", arg[2]) == 1) then -- !setlevel
  415.             if (arg[3] ~= nil and arg[4] ~= nil) then
  416.                 if (check_levels(client, id) == -1) then return -1 end
  417.                 local   ret = 0
  418.                 if (tonumber(arg[4]) == nil) then
  419.                     et.trap_SendServerCommand(client, "chat \"Invalid level\"")
  420.                     return 1
  421.                 end
  422.                 if (setlevel(client, arg[3], tonumber(arg[4])) == 0) then -- all went good
  423.                     get_pl_levels() -- need to refresh the players level
  424.                 end
  425.             end
  426.             return 1
  427.     elseif (bits[6] == 1 and arg[2] == "!kick") then
  428.             if (check_levels(client, id) == -1) then return -1 end
  429.             et.trap_DropClient(id, "Kicked by admin", 600) -- kick for 10 mins
  430.             return 1
  431.     elseif (bits[5] == 1 and string.len(arg[2]) > 2 and string.find("!mute", arg[2]) == 1) then
  432.             if (check_levels(client, id) == -1) then return -1 end
  433.             mute_pl(client, id, arg[4])
  434.             return 1
  435.     elseif (bits[4] == 1 and string.len(arg[2]) > 2 and string.find("!unmute", arg[2]) == 1) then
  436.             unmute_pl(client, id)
  437.             return 1
  438.     elseif (bits[3] == 1 and string.len(arg[2]) > 3 and string.find("!splat", arg[2]) == 1) then
  439.             if (check_levels(client, id) == -1) then return -1 end
  440.             splat_cmd(client, id)
  441.             return 1
  442.     elseif (bits[2] == 1 and arg[2] == "!lev" or cmd == "!adm") then
  443.             local   cl_name = string.gsub(et.gentity_get(client, "pers.netname"), "%^$", "^^ ")
  444.             et.trap_SendServerCommand(-1, "chat \"^7Player ^7"..cl_name.."^7 is level ^q"..client_lev[client + 1].."^7, rank: ^2"..rank.."\"")
  445.             return 1
  446.     elseif (bits[1] == 1 and string.len(arg[2]) > 2 and string.find("!help", arg[2]) == 1) then
  447.             help_cmd(client, flag)
  448.             return 1
  449.     end
  450.     return 0
  451.  end
  452.  
  453.  function   et_ClientCommand(clientNum, command)
  454.     local   argc = et.trap_Argc()
  455.     local   i = 0
  456.     local   arg = {}
  457.    
  458.     while (i < argc) do
  459.         arg[i + 1] = et.trap_Argv(i)
  460.         i = i + 1
  461.     end
  462.     if (arg[1] == "say" and arg[2] ~= nil) then
  463.         local   id = -1
  464.         local   name = nil
  465.         local   flag = 0
  466.         local   rank = nil
  467.  
  468.         if (arg[1] == "say" and string.find(arg[2], '!') == 1 and arg[3] ~= nil) then -- it's an admin command
  469.             id, name = get_player_info(arg[3])
  470.             if (id == -1) then
  471.                 et.trap_SendServerCommand(clientNum, "chat \"^7Player "..arg[3].."^7 doesn't exist\"")
  472.                 return 1
  473.             end
  474.         end
  475.         flag = get_pl_flag_from_lev(client_lev[clientNum + 1])
  476.         rank = get_pl_rank_from_lev(client_lev[clientNum + 1])
  477.         if (handle_cmd(clientNum, flag, rank, arg, id) == 1) then return 1 end
  478.         return 0
  479.     end
  480.     return 0
  481.  end
  482.  
  483.  function   et_ClientConnect(clientNum, firstTime, isBot )
  484.     get_pl_levels() -- need to refresh the players level
  485.  end
  486.  
  487.  function   et_ClientDisconnect(clientNum)
  488.     get_pl_levels() -- need to refresh the players level
  489.  end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement