Advertisement
Guest User

Untitled

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