Savage_Me55iah

CC MESSAGEBOARD

Jun 10th, 2014
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.38 KB | None | 0 0
  1. -- Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co --
  2. local scriptName, version, pastebinCode = "MESSAGEBOARD", "v0.2", "hRMHBK7z"
  3.  
  4. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  5.  
  6. -- INSTRUCTIONS ON USE --
  7. -- Instruction video on http://youtube.com/savagemessiah5
  8.  
  9. -- Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  10.  
  11. -- CHANGELOG --
  12. -- V 0.1 - Working
  13.  
  14. -- YET TO BE IMPLEMENTED --
  15.  
  16.  
  17. -- UPDATER --
  18.  
  19. local tArgs = {...}
  20. local update_request = tostring(tArgs[1]) or nil
  21.  
  22. do
  23.     update = function(script_name, pastebincode)
  24.         print("UPDATING") shell.run("pastebin get "..pastebincode.." update")
  25.         if fs.exists("update") then
  26.             fs.delete(script_name) fs.copy("update", script_name) fs.delete("update") print("UPDATE SUCCESS")
  27.         else
  28.             print("UPDATE FAILED")
  29.         end
  30.         sleep(2) os.reboot()
  31.     end
  32. end
  33.  
  34. if tostring(update_request) == "update" then update(shell.getRunningProgram(), pastebinCode) end
  35.  
  36. --  label_check(script) --
  37. label_check = function(param_scriptName)
  38.     local param_labelA, param_text, param_labelB = os.getComputerLabel() or false, "Computer label, blank for random label:", nil
  39.     while tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" do
  40.         term.clear() term.setCursorPos(1,1) print(param_text)
  41.         param_labelB = tostring(read())
  42.         if param_labelB == "nil" then param_labelB = tostring(param_scriptName.."-"..math.random(1000, 9999)) end
  43.         os.setComputerLabel(param_labelB) param_labelA = os.getComputerLabel() or false
  44.         if tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" then param_text = "Computer label too long, try again:" end
  45.     end
  46. end
  47. label_check(scriptName)
  48.  
  49. function boolean_check(params)
  50.     local answer = nil
  51.     if tostring(params) == "true" then
  52.         answer = true
  53.     else
  54.         answer = false
  55.     end
  56.     return answer
  57. end
  58.  
  59. -- DATASTORE --
  60.  
  61. local settings_file = "config"
  62.  
  63. local settings_template = [[-- adjust these configuration options as necessary.
  64. -- IMPORTANT!, API won't load if there are spaces in the values
  65. COMPUTER_LABEL = "${label}"
  66. COMPUTER_ID = ${id}
  67.  
  68. MON_TEXT_SIZE = ${mon_text_size}
  69. MON_BOUNDRY_Y = ${mon_boundry_y}
  70. TERM_TEXT_SIZE = ${term_text_size}
  71. TIMEZONE = "${timezone}"
  72. TIMEZONE_OFFSET = ${timezone_offset}
  73. UPDATE_TIME = ${update_time}
  74. ]]
  75.  
  76. local function interpolate(s, params)
  77.     return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  78. end
  79.  
  80. do
  81.     save_settings = function()
  82.         local filehandler = fs.open(tostring(settings_file), "w")
  83.  
  84.         if tostring(CONFIG) == "nil" then
  85.             settings = {
  86.             label = os.getComputerLabel(),
  87.             id = os.getComputerID(),
  88.             mon_text_size = 0.5,
  89.             mon_boundry_y = 3,
  90.             term_text_size = 1,
  91.             timezone = "utc",
  92.             timezone_offset = 11,
  93.             update_time = 5
  94.             }
  95.         else
  96.             settings = {
  97.             label = CONFIG.COMPUTER_LABEL or os.getComputerLabel(),
  98.             id = os.getComputerID(),
  99.             mon_text_size = MON_TEXT_SIZE or 0.5,
  100.             mon_boundry_y = MON_BOUNDRY_Y or 3,
  101.             term_text_size = TERM_TEXT_SIZE or 1,
  102.             timezone = CONFIG.TIMEZONE or "utc",
  103.             timezone_offset = CONFIG.TIMEZONE_OFFSET or 11,
  104.             update_time = CONFIG.UPDATE_TIME or 5
  105.             }
  106.         end
  107.  
  108.         filehandler.write(interpolate(settings_template, settings))
  109.         filehandler.close()
  110.     end
  111. end
  112.  
  113. if fs.exists(settings_file) == false then
  114.     save_settings()
  115.     return
  116. else
  117.     os.unloadAPI(settings_file)
  118.     if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
  119.     CONFIG = nil
  120.     for k, v in pairs(_G) do if k == settings_file then CONFIG = v break end end
  121.     if CONFIG == nil then print("CONFIG came up nil") end
  122. end
  123.  
  124. do
  125.     load_one = function(command)
  126.         local answer = nil
  127.         if fs.exists(tostring(command)) then
  128.             local fileHandler = fs.open(command, 'r')
  129.             answer = textutils.unserialize(fileHandler.readAll())
  130.             fileHandler.close()
  131.         end
  132.         return answer
  133.     end
  134.  
  135.     save_one = function(command1, command2)
  136.         local fileHandler = fs.open(tostring(command1), 'w')
  137.         fileHandler.write(textutils.serialize(command2))
  138.         fileHandler.close()
  139.     end
  140.  
  141.     load_all = function()
  142.         msg = load_one("msg")
  143. --        user = load_one("user")
  144.     end
  145.  
  146.     save_all = function()
  147.         save_one("msg", msg)
  148. --        save_one("user", user)
  149.     end
  150.  
  151.     if not boolean_check(fs.exists("startup")) then
  152.         outputFile = io.open("startup", "w")
  153.         outputFile:write("print(\"Starting\")\n sleep(2) shell.run(\"")
  154.         outputFile:write(shell.getRunningProgram())
  155.         outputFile:write("\")\n")
  156.         outputFile:close()
  157.     end
  158. end
  159.            
  160. -- PERIPHERAL --
  161.  
  162. do
  163.     peripheral_find = function(param)
  164.         local method, answer, sides = param or nil, nil, {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
  165.         for a,b in pairs(sides) do
  166.             if peripheral.isPresent(b) then
  167.                 local test_methods = peripheral.getMethods(b)
  168.                 for c,d in pairs(test_methods) do if d == tostring(method) then answer = peripheral.wrap(b) end end
  169.             end
  170.         end
  171.         return answer
  172.     end
  173. end
  174.  
  175. local mon = peripheral_find("setTextScale")
  176.  
  177. -- MESSAGEBOARD --
  178.  
  179. do
  180. --  getTime("date/time", "utc", 11) .. returns string
  181.     getTime = function(param_timeFormat, param_timeZone, param_timeOffset)
  182.         local param_timeOffset, param_timeZone, param_format = tonumber(param_timeOffset) or "now", param_timeZone or "utc", param_format or "date/time"
  183.         local timeOffset, timeFormat, result = nil, true, nil
  184.  
  185.         if param_timeOffset == "now" or param_timeOffset == 0 then timeOffset = "now"
  186.         elseif param_timeOffset <= -1 then timeOffset = tostring(math.floor(param_timeOffset)).."+hours+before"
  187.         elseif param_timeOffset >= 1 then timeOffset = tostring("in+"..math.floor(param_timeOffset).."+hours")
  188.         end
  189.  
  190. --      if param_timeFormat == "time" then timeFormat = "\H:\M:\S"
  191. --      elseif param_timeFormat == "date" then timeFormat = "\y-\m-\d"
  192. --      elseif param_timeFormat == "date/time" then timeFormat = "\y-\m-\d~\H:\M:\S"
  193. --      end
  194. --      if tostring(timeFormat) ~= "nil" then result = http.get("http://www.timeapi.org/"..param_timeZone.."/"..timeOffset.."?format="..timeFormat).readAll()
  195.         if tostring(timeFormat) ~= "nil" then result = http.get("http://www.timeapi.org/"..param_timeZone.."/"..timeOffset).readAll()
  196.         else result = os.day() .. ": " .. textutils.formatTime(os.time(), true) end
  197.         return result
  198.     end
  199. end
  200.    
  201. function msg_write(command, text, loc_x, loc_y, color_back, color_text)
  202.   command.setCursorPos(loc_x, loc_y)
  203.   command.setTextColor(color_text or colors.white)
  204.   command.setBackgroundColor(color_back or colors.black)
  205.   command.write(text)
  206.   command.setTextColor(colors.white)
  207.   command.setBackgroundColor(colors.black)
  208. end
  209.  
  210. function add_msg(text)
  211.   num = table.maxn(msg) + 1
  212.   msg[num] = text
  213.   bottom_msg = table.maxn(msg)
  214. end
  215.  
  216. function mon_display(num)
  217.   mon.clear() term.clear()
  218.   mon.setTextScale(tonumber(CONFIG.MON_TEXT_SIZE))
  219.   local oa_x, oa_y = mon.getSize()
  220.   local color_1, color_2, color_3, color_4 = colors.red, colors.green, colors.white, colors.green
  221.   if not boolean_check(mon.isColor()) then color_1, color_2, color_3 = colors.white, colors.white, colors.black end
  222.   msg_write(mon, "UP", 1, 1, color_1, colors.black)
  223.   msg_write(mon, "DN", 1, oa_y, color_1, colors.black)
  224.   msg_write(mon, "Messageboard, Time: " .. Time, 3, 1, color_2, color_3)
  225.  
  226.   local number, cursor_y = num, oa_y - tonumber(CONFIG.MON_BOUNDRY_Y)
  227.   while cursor_y ~= tonumber(CONFIG.MON_BOUNDRY_Y) do
  228.     if tostring(msg[number]) ~= "nil" then msg_write(mon, msg[number], 3, cursor_y) end
  229.     cursor_y, number = cursor_y - 1, number - 1
  230.   end
  231.  
  232.   if not boolean_check(term.isColor()) then color_4 = colors.white end
  233.   msg_write(term, " SPACE TO BEGIN ", 1, 1, color_4, colors.black)
  234.   term.setCursorPos(1, 3)
  235. end
  236.  
  237. do  
  238.     string_split = function()
  239.         num_1, limit, word_split = 1, oa_x - 2, {}
  240.         for word in string.gmatch(text, "%S+") do table.insert(word_split, word) end
  241.         for num=1,table.maxn(word_split) do
  242.             paragraph_length = string.len(msg_split[num_1] or "a") + 1
  243.             if (paragraph_length + string.len(word_split[num])) <= limit then num_1 = num_1 else num_1 = num_1 + 1 end
  244.             if tostring(msg_split[num_1]) ~= "nil" then
  245.                 msg_split[num_1] = msg_split[num_1]..word_split[num].." "
  246.             else
  247.                 msg_split[num_1] = word_split[num].." "
  248.             end
  249.         end
  250.     end
  251.    
  252. --  event = eventFilter("timer", "monitor_touch", "mouse_click", "key", "etc") .. returns a table only when the event matches the arg --
  253.     eventFilter = function(...)
  254.         local param, whitelist, event = {...}, {}, {}
  255.         for a,b in pairs(param) do whitelist[b] = true end
  256.         while tostring(whitelist[event[1]]) == "nil" do
  257.             event = {os.pullEvent()}
  258.         end
  259.         return event
  260.     end
  261. end
  262.  
  263. Event, Time = {}, getTime("date/time", tostring(CONFIG.TIMEZONE), tonumber(CONFIG.TIMEZONE_OFFSET))
  264. msg = {[1] = "Savage_Me55iah: Dawn of Creation: Welcome to the Savage in game MessageBoard"}
  265. if fs.exists("msg") then load_all() else save_all() end
  266. bottom_msg = table.maxn(msg)
  267. mon_display(bottom_msg)
  268. os.startTimer(tonumber(CONFIG.UPDATE_TIME))
  269.  
  270.  
  271. while true do
  272. if tostring(Event[1]) == "timer" then os.startTimer(tonumber(CONFIG.UPDATE_TIME)) Time = getTime("date/time", tostring(CONFIG.TIMEZONE), tonumber(CONFIG.TIMEZONE_OFFSET)) end
  273. Event = eventFilter("timer", "monitor_touch", "key")
  274. oa_x, oa_y = mon.getSize()
  275. if Event[1] == "monitor_touch" then
  276.   if Event[4] < (math.floor(oa_y) / 2) then
  277.     if bottom_msg ~= 1 then
  278.         bottom_msg = bottom_msg - 1
  279.     end
  280.   else
  281.     if bottom_msg ~= table.maxn(msg) then
  282.         bottom_msg = bottom_msg + 1
  283.     end
  284.   end
  285. elseif Event[1] == "key" and Event[2] == keys.space then
  286.   print("Enter user: ")
  287.   term.setCursorPos(1, 4)
  288.   local user = read():upper()
  289.   if user == "S" then user = "Savage" end
  290.   print("Enter message: ")
  291.   term.setCursorPos(1, 6)
  292.   local message = read()
  293.   text = (user .. ": " .. Time .. ": " .. message)
  294.   msg_split = {}
  295.   string_split()
  296.   for num=1,table.maxn(msg_split) do add_msg(msg_split[num]) end
  297.   os.startTimer(tonumber(CONFIG.UPDATE_TIME))
  298. end
  299. save_all()
  300. mon_display(bottom_msg)
  301. end
Advertisement
Add Comment
Please, Sign In to add comment