Advertisement
Guest User

Untitled

a guest
Oct 1st, 2010
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.08 KB | None | 0 0
  1. --[[
  2.  
  3.     Broadcast System
  4.     By Grandelf.
  5.  
  6. ]]--   
  7.  
  8. local broadcast_add = "#broadcast add"
  9. local broadcast_remove = "#broadcast remove"
  10. local broadcast_list = "#broadcast list"
  11. local broadcast_modify = "#broadcast modify"
  12.  
  13. function broadcast_system(_, plr, msg)
  14.     if (plr:CanUseCommand("a")) then
  15.         if (msg:find(broadcast_add.." ") == 1) then
  16.             local str = msg:gsub(broadcast_add.." ", "")
  17.             local tbl = string_split(str, "str")
  18.             if (tbl[1]) then
  19.                 local interval = number_check(tbl[1]) and tonumber(tbl[1]) or nil
  20.                 local color
  21.                 if (tbl[#tbl]:upper():sub(1, 2) == "CF") then
  22.                     color = "|"..tbl[#tbl]
  23.                     tbl[#tbl] = nil;
  24.                 end
  25.                 local broadcast_msg = ""
  26.                 for k, v in ipairs(tbl) do 
  27.                     if (k ~= 1) then
  28.                         broadcast_msg = broadcast_msg.." "..v
  29.                     end
  30.                 end
  31.                 local file = io.open("scripts/broadcast_file.txt", "a")
  32.                 if (not interval) then
  33.                     plr:SendBroadcastMessage("Interval number expected as first argument.")
  34.                 elseif (not broadcast_msg) then
  35.                     plr:SendBroadcastMessage("Broadcast message expected after inverval.")
  36.                 elseif (not color) then
  37.                     file:write(interval..","..broadcast_msg.."\n")
  38.                     plr:SendBroadcastMessage("You succesfully added a message.")
  39.                 elseif (color) then
  40.                     file:write(interval..","..broadcast_msg..", "..color.."\n")
  41.                     plr:SendBroadcastMessage("You succesfully added a message.")
  42.                 end
  43.                 file:close()
  44.                 restart_script()
  45.             end
  46.             return 0;
  47.         end
  48.         if (msg:find(broadcast_remove.." ") == 1) then
  49.             local str = msg:gsub(broadcast_remove.." ", "")
  50.             local id = number_check(str) and tonumber(str) or nil
  51.             if (id) then
  52.                 local tbl = {}
  53.                 local file = io.open("scripts/broadcast_file.txt", "a")
  54.                 local line = return_line_by_id(id)
  55.                 for l in io.lines("scripts/broadcast_file.txt") do
  56.                     if (l ~= "") and (l ~= line) then
  57.                         table.insert(tbl, l)
  58.                     end
  59.                     local f = io.open("scripts/broadcast_file.txt", "w+")
  60.                     for _, v in ipairs(tbl) do
  61.                         f:write(v.."\n")
  62.                     end
  63.                     f:close()
  64.                 end
  65.                 plr:SendBroadcastMessage("The message has been deleted.")
  66.                 file:close()
  67.                 restart_script()
  68.             else   
  69.                 plr:SendBroadcastMessage("Number expected as argument.")
  70.             end
  71.             return 0;  
  72.         end
  73.         if (msg:find(broadcast_modify.." ") == 1) then
  74.             local str = msg:gsub(broadcast_modify.." ", "")
  75.             local file = io.open("scripts/broadcast_file.txt", "a")
  76.             local tbl = {}
  77.             if (str:upper():sub(1, 1) == "|") then
  78.                 local str_tbl = string_split(str, "str")
  79.                 local color, id = unpack(str_tbl)
  80.                 if (color) and (id) then
  81.                     local msg_tbl = string_split(return_line_by_id(id), "file")
  82.                     rewrite_without_line(return_line_by_id(id), file)
  83.                     file:write(msg_tbl[1]..", "..msg_tbl[2]..", |"..color.."\n")
  84.                     file:close()
  85.                     restart_script()
  86.                 elseif (not id) then
  87.                     plr:SendBroadcastMessage("Id expected as second argument.")
  88.                 end
  89.             elseif (str ~= "") then
  90.                 local str_tbl = string_split(str, "str")
  91.                 if (number_check(str_tbl[1])) then
  92.                     local interval, id = unpack(str_tbl)
  93.                     local broadcast_msg
  94.                     if (interval) and (id) then
  95.                         local msg_tbl = string_split(return_line_by_id(id), "file")
  96.                         if (not msg_tbl[3]) then
  97.                             broadcast_msg = interval..", "..msg_tbl[2]
  98.                         elseif (msg_tbl[3]) then
  99.                             broadcast_msg = interval..", "..msg_tbl[2]..", "..msg_tbl[3]
  100.                         end
  101.                         rewrite_without_line(return_line_by_id(id), file)
  102.                         file:write(broadcast_msg.."\n")
  103.                         file:close()
  104.                         restart_script()
  105.                     elseif (not id) then
  106.                         plr:SendBroadcastMessage("Id expected as second argument.")
  107.                     end
  108.                 elseif (str ~= "") then
  109.                     local message, id = ""
  110.                     for k, v in ipairs(str_tbl) do if (k ~= #str_tbl) then message = message.." "..v else id = v end end       
  111.                     local broadcast_msg
  112.                     if (message) and (id) then
  113.                         local msg_tbl = string_split(return_line_by_id(id), "file")
  114.                         if (not msg_tbl[3]) then
  115.                             broadcast_msg = msg_tbl[1]..","..message
  116.                         elseif (msg_tbl[3]) then
  117.                             broadcast_msg = msg_tbl[1]..","..message..", "..msg_tbl[3]
  118.                         end
  119.                         rewrite_without_line(return_line_by_id(id), file)
  120.                         file:write(broadcast_msg.."\n")
  121.                         file:close()
  122.                         restart_script()
  123.                     elseif (not id) then
  124.                         plr:SendBroadcastMessage("Id expected as second argument.")
  125.                     end
  126.                 end
  127.             end
  128.             return 0;
  129.         end    
  130.         if (msg == broadcast_list) then
  131.             local count = 1
  132.             for l in io.lines("scripts/broadcast_file.txt") do
  133.                 if (count <= 11) and (l ~= "") then
  134.                     local tbl = string_split(l, "file")
  135.                     plr:SendBroadcastMessage(count.." - "..tbl[2])
  136.                     count = count + 1
  137.                 else
  138.                     plr:SendBroadcastMessage("|cffff0000More than 10 results returned, aborting.")
  139.                 end
  140.             end
  141.             if (count == 1) then
  142.                 plr:SendBroadcastMessage("No results found.")
  143.             end
  144.             return 0;
  145.         end
  146.     end    
  147. end
  148.        
  149. function string_split(str, type)
  150.     local words = {}
  151.     if (type == "file") then
  152.         local sp = str:find(",")
  153.         while (sp) do
  154.             table.insert(words, str:sub(1, sp-1))
  155.             str = str:gsub(str:sub(1, sp+1), "")
  156.             sp = str:find(",")
  157.         end
  158.         table.insert(words, str)
  159.     elseif (type == "str") then
  160.         str:gsub("(%w+)", function(arg) table.insert(words, arg) end)
  161.     end
  162.     return words;
  163. end
  164.  
  165. function return_line_by_id(id)
  166.     local count = 1
  167.     local line
  168.     for l in io.lines("scripts/broadcast_file.txt") do
  169.         if (count == tonumber(id)) then
  170.             line = l
  171.             break;
  172.         elseif (l ~= "") then
  173.             count = count + 1
  174.         end
  175.     end
  176.     return line;
  177. end
  178.  
  179. function rewrite_without_line(line, file)
  180.     local tbl = {}
  181.     for l in io.lines("scripts/broadcast_file.txt") do
  182.         if (l ~= "") and (l ~= line) then
  183.             table.insert(tbl, l)
  184.         end
  185.         local f = io.open("scripts/broadcast_file.txt", "w+")
  186.         for _, v in ipairs(tbl) do
  187.             f:write(v.."\n")
  188.         end
  189.         f:close()
  190.     end
  191. end
  192.  
  193. function number_check(str)
  194.     local c = 1
  195.     while (str:len()+1 ~= c) do
  196.         if (str:sub(c, c):find("%d")) then c = c + 1 else return false; end
  197.     end
  198.     return true;
  199. end
  200.  
  201. function load_strings()
  202.     local file = io.open("scripts/broadcast_file.txt", "a")
  203.     if (file) then
  204.         local loaded = 0
  205.         for l in io.lines("scripts/broadcast_file.txt") do
  206.             if (l ~= "") then
  207.                 local tbl = string_split(l, "file")
  208.                 local interval, broadcast_msg, color
  209.                 if (#tbl == 2) then
  210.                     interval, broadcast_msg = unpack(tbl)
  211.                 elseif (#tbl == 3) then
  212.                     interval, broadcast_msg, color = unpack(tbl)
  213.                 end
  214.                 if (color) then
  215.                     broadcast_msg = color..broadcast_msg
  216.                 end
  217.                 loaded = loaded + 1
  218.                 RegisterTimedEvent("broadcast_msg", tonumber(interval), 0, broadcast_msg)
  219.             end
  220.         end
  221.         print("[Broadcast System]: Loaded "..loaded.." messages.")
  222.         file:close()
  223.     else
  224.         print("Could not open file.")
  225.     end
  226. end
  227. load_strings()
  228.  
  229. function broadcast_msg(str)
  230.     for _, v in pairs(GetPlayersInWorld()) do
  231.         v:SendBroadcastMessage("|cffff0000[System Message]|cFFFFFF00: "..str)
  232.     end
  233. end
  234.  
  235. function restart_script()
  236.     RemoveTimedEventsWithName("broadcast_msg")
  237.     load_strings()
  238.     --local file = loadfile("scripts/broadcast_script.lua")
  239.     --return file();
  240. end
  241.  
  242. RegisterServerHook(16, "broadcast_system")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement