Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co --
- local scriptName, version, pastebinCode = "MESSAGEBOARD", "v0.2", "hRMHBK7z"
- --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
- -- INSTRUCTIONS ON USE --
- -- Instruction video on http://youtube.com/savagemessiah5
- -- Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
- -- CHANGELOG --
- -- V 0.1 - Working
- -- YET TO BE IMPLEMENTED --
- -- UPDATER --
- local tArgs = {...}
- local update_request = tostring(tArgs[1]) or nil
- do
- update = function(script_name, pastebincode)
- print("UPDATING") shell.run("pastebin get "..pastebincode.." update")
- if fs.exists("update") then
- fs.delete(script_name) fs.copy("update", script_name) fs.delete("update") print("UPDATE SUCCESS")
- else
- print("UPDATE FAILED")
- end
- sleep(2) os.reboot()
- end
- end
- if tostring(update_request) == "update" then update(shell.getRunningProgram(), pastebinCode) end
- -- label_check(script) --
- label_check = function(param_scriptName)
- local param_labelA, param_text, param_labelB = os.getComputerLabel() or false, "Computer label, blank for random label:", nil
- while tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" do
- term.clear() term.setCursorPos(1,1) print(param_text)
- param_labelB = tostring(read())
- if param_labelB == "nil" then param_labelB = tostring(param_scriptName.."-"..math.random(1000, 9999)) end
- os.setComputerLabel(param_labelB) param_labelA = os.getComputerLabel() or false
- if tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" then param_text = "Computer label too long, try again:" end
- end
- end
- label_check(scriptName)
- function boolean_check(params)
- local answer = nil
- if tostring(params) == "true" then
- answer = true
- else
- answer = false
- end
- return answer
- end
- -- DATASTORE --
- local settings_file = "config"
- local settings_template = [[-- adjust these configuration options as necessary.
- -- IMPORTANT!, API won't load if there are spaces in the values
- COMPUTER_LABEL = "${label}"
- COMPUTER_ID = ${id}
- MON_TEXT_SIZE = ${mon_text_size}
- MON_BOUNDRY_Y = ${mon_boundry_y}
- TERM_TEXT_SIZE = ${term_text_size}
- TIMEZONE = "${timezone}"
- TIMEZONE_OFFSET = ${timezone_offset}
- UPDATE_TIME = ${update_time}
- ]]
- local function interpolate(s, params)
- return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
- end
- do
- save_settings = function()
- local filehandler = fs.open(tostring(settings_file), "w")
- if tostring(CONFIG) == "nil" then
- settings = {
- label = os.getComputerLabel(),
- id = os.getComputerID(),
- mon_text_size = 0.5,
- mon_boundry_y = 3,
- term_text_size = 1,
- timezone = "utc",
- timezone_offset = 11,
- update_time = 5
- }
- else
- settings = {
- label = CONFIG.COMPUTER_LABEL or os.getComputerLabel(),
- id = os.getComputerID(),
- mon_text_size = MON_TEXT_SIZE or 0.5,
- mon_boundry_y = MON_BOUNDRY_Y or 3,
- term_text_size = TERM_TEXT_SIZE or 1,
- timezone = CONFIG.TIMEZONE or "utc",
- timezone_offset = CONFIG.TIMEZONE_OFFSET or 11,
- update_time = CONFIG.UPDATE_TIME or 5
- }
- end
- filehandler.write(interpolate(settings_template, settings))
- filehandler.close()
- end
- end
- if fs.exists(settings_file) == false then
- save_settings()
- return
- else
- os.unloadAPI(settings_file)
- if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
- CONFIG = nil
- for k, v in pairs(_G) do if k == settings_file then CONFIG = v break end end
- if CONFIG == nil then print("CONFIG came up nil") end
- end
- do
- load_one = function(command)
- local answer = nil
- if fs.exists(tostring(command)) then
- local fileHandler = fs.open(command, 'r')
- answer = textutils.unserialize(fileHandler.readAll())
- fileHandler.close()
- end
- return answer
- end
- save_one = function(command1, command2)
- local fileHandler = fs.open(tostring(command1), 'w')
- fileHandler.write(textutils.serialize(command2))
- fileHandler.close()
- end
- load_all = function()
- msg = load_one("msg")
- -- user = load_one("user")
- end
- save_all = function()
- save_one("msg", msg)
- -- save_one("user", user)
- end
- if not boolean_check(fs.exists("startup")) then
- outputFile = io.open("startup", "w")
- outputFile:write("print(\"Starting\")\n sleep(2) shell.run(\"")
- outputFile:write(shell.getRunningProgram())
- outputFile:write("\")\n")
- outputFile:close()
- end
- end
- -- PERIPHERAL --
- do
- peripheral_find = function(param)
- local method, answer, sides = param or nil, nil, {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
- for a,b in pairs(sides) do
- if peripheral.isPresent(b) then
- local test_methods = peripheral.getMethods(b)
- for c,d in pairs(test_methods) do if d == tostring(method) then answer = peripheral.wrap(b) end end
- end
- end
- return answer
- end
- end
- local mon = peripheral_find("setTextScale")
- -- MESSAGEBOARD --
- do
- -- getTime("date/time", "utc", 11) .. returns string
- getTime = function(param_timeFormat, param_timeZone, param_timeOffset)
- local param_timeOffset, param_timeZone, param_format = tonumber(param_timeOffset) or "now", param_timeZone or "utc", param_format or "date/time"
- local timeOffset, timeFormat, result = nil, true, nil
- if param_timeOffset == "now" or param_timeOffset == 0 then timeOffset = "now"
- elseif param_timeOffset <= -1 then timeOffset = tostring(math.floor(param_timeOffset)).."+hours+before"
- elseif param_timeOffset >= 1 then timeOffset = tostring("in+"..math.floor(param_timeOffset).."+hours")
- end
- -- if param_timeFormat == "time" then timeFormat = "\H:\M:\S"
- -- elseif param_timeFormat == "date" then timeFormat = "\y-\m-\d"
- -- elseif param_timeFormat == "date/time" then timeFormat = "\y-\m-\d~\H:\M:\S"
- -- end
- -- if tostring(timeFormat) ~= "nil" then result = http.get("http://www.timeapi.org/"..param_timeZone.."/"..timeOffset.."?format="..timeFormat).readAll()
- if tostring(timeFormat) ~= "nil" then result = http.get("http://www.timeapi.org/"..param_timeZone.."/"..timeOffset).readAll()
- else result = os.day() .. ": " .. textutils.formatTime(os.time(), true) end
- return result
- end
- end
- function msg_write(command, text, loc_x, loc_y, color_back, color_text)
- command.setCursorPos(loc_x, loc_y)
- command.setTextColor(color_text or colors.white)
- command.setBackgroundColor(color_back or colors.black)
- command.write(text)
- command.setTextColor(colors.white)
- command.setBackgroundColor(colors.black)
- end
- function add_msg(text)
- num = table.maxn(msg) + 1
- msg[num] = text
- bottom_msg = table.maxn(msg)
- end
- function mon_display(num)
- mon.clear() term.clear()
- mon.setTextScale(tonumber(CONFIG.MON_TEXT_SIZE))
- local oa_x, oa_y = mon.getSize()
- local color_1, color_2, color_3, color_4 = colors.red, colors.green, colors.white, colors.green
- if not boolean_check(mon.isColor()) then color_1, color_2, color_3 = colors.white, colors.white, colors.black end
- msg_write(mon, "UP", 1, 1, color_1, colors.black)
- msg_write(mon, "DN", 1, oa_y, color_1, colors.black)
- msg_write(mon, "Messageboard, Time: " .. Time, 3, 1, color_2, color_3)
- local number, cursor_y = num, oa_y - tonumber(CONFIG.MON_BOUNDRY_Y)
- while cursor_y ~= tonumber(CONFIG.MON_BOUNDRY_Y) do
- if tostring(msg[number]) ~= "nil" then msg_write(mon, msg[number], 3, cursor_y) end
- cursor_y, number = cursor_y - 1, number - 1
- end
- if not boolean_check(term.isColor()) then color_4 = colors.white end
- msg_write(term, " SPACE TO BEGIN ", 1, 1, color_4, colors.black)
- term.setCursorPos(1, 3)
- end
- do
- string_split = function()
- num_1, limit, word_split = 1, oa_x - 2, {}
- for word in string.gmatch(text, "%S+") do table.insert(word_split, word) end
- for num=1,table.maxn(word_split) do
- paragraph_length = string.len(msg_split[num_1] or "a") + 1
- if (paragraph_length + string.len(word_split[num])) <= limit then num_1 = num_1 else num_1 = num_1 + 1 end
- if tostring(msg_split[num_1]) ~= "nil" then
- msg_split[num_1] = msg_split[num_1]..word_split[num].." "
- else
- msg_split[num_1] = word_split[num].." "
- end
- end
- end
- -- event = eventFilter("timer", "monitor_touch", "mouse_click", "key", "etc") .. returns a table only when the event matches the arg --
- eventFilter = function(...)
- local param, whitelist, event = {...}, {}, {}
- for a,b in pairs(param) do whitelist[b] = true end
- while tostring(whitelist[event[1]]) == "nil" do
- event = {os.pullEvent()}
- end
- return event
- end
- end
- Event, Time = {}, getTime("date/time", tostring(CONFIG.TIMEZONE), tonumber(CONFIG.TIMEZONE_OFFSET))
- msg = {[1] = "Savage_Me55iah: Dawn of Creation: Welcome to the Savage in game MessageBoard"}
- if fs.exists("msg") then load_all() else save_all() end
- bottom_msg = table.maxn(msg)
- mon_display(bottom_msg)
- os.startTimer(tonumber(CONFIG.UPDATE_TIME))
- while true do
- if tostring(Event[1]) == "timer" then os.startTimer(tonumber(CONFIG.UPDATE_TIME)) Time = getTime("date/time", tostring(CONFIG.TIMEZONE), tonumber(CONFIG.TIMEZONE_OFFSET)) end
- Event = eventFilter("timer", "monitor_touch", "key")
- oa_x, oa_y = mon.getSize()
- if Event[1] == "monitor_touch" then
- if Event[4] < (math.floor(oa_y) / 2) then
- if bottom_msg ~= 1 then
- bottom_msg = bottom_msg - 1
- end
- else
- if bottom_msg ~= table.maxn(msg) then
- bottom_msg = bottom_msg + 1
- end
- end
- elseif Event[1] == "key" and Event[2] == keys.space then
- print("Enter user: ")
- term.setCursorPos(1, 4)
- local user = read():upper()
- if user == "S" then user = "Savage" end
- print("Enter message: ")
- term.setCursorPos(1, 6)
- local message = read()
- text = (user .. ": " .. Time .. ": " .. message)
- msg_split = {}
- string_split()
- for num=1,table.maxn(msg_split) do add_msg(msg_split[num]) end
- os.startTimer(tonumber(CONFIG.UPDATE_TIME))
- end
- save_all()
- mon_display(bottom_msg)
- end
Advertisement
Add Comment
Please, Sign In to add comment