-- This version adds slight changes in the background of the code, as well as code optimization. Item list has also been changed slightly.
local itemIds = REDACTED DUE TO TABLE SIZE
-- This is the time that results and text other than clocks are displayed before clearing
resultTime = 5
-- DO NOT CHANGE ANYTHING BELOW THIS LINE
-- You no longer need to define these. The program will help you define them instead
defaultTimeZone = "notSet"
defaultTimeZoneCode = "notSet"
bridgeName = "notSet"
currentVersion = 5
updateCheck = false
devMode = false
bridge = peripheral.wrap("bottom") -- Location of Terminal Glasses Bridge
bridge.clear()
net = peripheral.wrap("back") -- Location of Wired Modem for ME Bridge
IGT = bridge.addText(2, 7, "", 0xffffff)
RLT = bridge.addText(4, 16, "", 0xffffff)
Text = bridge.addText(6, 25, "", 0xffffff)
if fs.exists("settings") then
settingValues = nil
filePath = "settings"
fileHandle = fs.open (filePath, 'r')
settingValues = textutils.unserialize (fileHandle.readAll())
fileHandle.close()
defaultTimeZone = settingValues[1]
defaultTimeZoneCode = settingValues[2]
bridgeName = settingValues[3]
else
Text.setText("What time zone should I use? EX: $$America/Chicago")
e, msg = os.pullEvent("chat_command")
defaultTimeZone = msg
Text.setText("What time zone code should I use? EX: $$CDT")
e, msg = os.pullEvent("chat_command")
defaultTimeZoneCode = msg
Text.setText("What is the name assigned to your ME Bridge? EX: $$meBridge_0")
e, msg = os.pullEvent("chat_command")
bridgeName = tostring(msg)
settingValues = {defaultTimeZone, defaultTimeZoneCode, bridgeName}
filePath = "settings"
fileHandle = fs.open (filePath, 'w')
fileHandle.write (textutils.serialize (settingValues))
fileHandle.close()
Text.setText("")
end
variables = {
["searchString"] = "",
["controller"] = bridgeName,
["chatString"] = ""
}
displayStrings = {}
timeZone = defaultTimeZone
timeZoneCode = defaultTimeZoneCode
function printSearch()
i = 1
pxFromTop = 25
tableInfo = net.callRemote(variables["controller"], "listItems")
displayStr = bridge.addText(6, pxFromTop, "", 0xffffff)
displayValue = string.format("ME search for '%s': ", variables["searchString"])
displayStr.setText(displayValue)
displayStrings[i] = displayStr
-- key = itemId, value = amount
for key, value in pairs(tableInfo) do
if itemIds[key] ~= nil and string.find(string.lower(itemIds[key]), variables["searchString"]) then
i = i + 1
pxFromTop = pxFromTop + 10
displayStr = bridge.addText(16, pxFromTop, "", 0xffffff)
displayValue = string.format("- %s = %i", itemIds[key], value)
displayStr.setText(displayValue)
displayStrings[i] = displayStr
end
end
term.clear()
term.setCursorPos(1,1)
term.write("ME Search System")
term.setCursorPos(1,3)
term.write("Users Connected at last search:")
term.setCursorPos(1,2)
term.write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
term.setCursorPos(1,5)
tableInfo = bridge.getUsers()
for key, value in pairs(tableInfo) do
print(key .. " = " .. tostring(value))
end
sleep(resultTime)
for key, displayStr in ipairs(displayStrings) do
displayStr.delete()
displayStr = nil
end
end
local function update()
http.request("http://pastebin.com/raw.php?i=ek9NSCpd")
requesting = true
while requesting do
event, url, sourceText = os.pullEvent()
if event == "http_success" then
result = sourceText.readAll()
onlineVersion = tonumber(result)
requesting = false
if currentVersion < onlineVersion then
Text.setText("An update is available, would you like to update? EX: $$yes or $$no")
e, msg = os.pullEvent("chat_command")
msg = string.lower(msg)
if msg == "yes" then
bridge.clear()
shell.run("update")
error()
elseif msg == "no" then
Text.setText("Update aborted.")
sleep(resultTime)
Text.setText("")
return
else
Text.setText("Unknown reply, update aborted.")
sleep(resultTime)
Text.setText("")
return
end
else
Text.setText("Your AetherVision is up-to-date.")
sleep(resultTime)
Text.setText("")
return
end
else
Text.setText("Server didn't respond, check aborted.")
sleep(resultTime)
Text.setText("")
requesting = false
return
end
end
end
local function autoUpdateCheck()
while true do
if updateCheck then
update()
updateCheck = false
end
sleep(0.5)
end
end
local function chat()
message = ""
chatMessage = bridge.addText(6, 25, "", 0xffffff)
for i=1,#words do
message = message.." "..words[i]
end
chatMessage.setText(message)
sleep(resultTime)
chatMessage.delete()
chatMessage = nil
end
local function resetScreen()
timeZone = defaultTimeZone
timeZoneCode = defaultTimeZoneCode
bridge.clear()
IGT.delete()
RLT.delete()
IGT = nil
RLT = nil
IGT = bridge.addText(2, 7, "", 0xffffff)
RLT = bridge.addText(4, 16, "", 0xffffff)
end
local function badCommand()
failure = bridge.addText(16, 24, "", 0xffffff)
failure.setText("Unknown Command")
sleep(resultTime)
failure.setText("")
end
local function changeZone()
displayStr = bridge.addText(16, 24, "", 0xffffff)
displayStr.setText("What is the new zone code? EX: $$CDT")
while true do
local e, msg = os.pullEvent("chat_command")
timeZoneCode = msg
displayStr.setText("Clock updated!")
sleep(resultTime)
displayStr.setText("")
return
end
end
function getTime()
while true do
if not http then error("no http") end
httpResponseHandle = http.get("http://artemix.hu/cctime.php?timezone="..timeZone)
if not httpResponseHandle then error("error fetching time") end
timeTable = textutils.unserialize(httpResponseHandle.readAll())
httpResponseHandle.close()
hour = timeTable.h
minute = timeTable.m
second = timeTable.s
empty = ""
if not (second < 60) then
second = second - 60
minute = minute + 1
end
if not (minute < 60) then
minute = minute - 60
hour = hour + 1
end
if not (hour < 24) then
hour = hour - 24
end
if (hour > 12) then
hour = hour - 12
AP = "PM"
else
AP = "AM"
end
realTime = string.format("%02d:%02d %s %s", hour, minute, AP, timeZoneCode)
time = os.time()
time = textutils.formatTime(time, false)
if devMode then
time = time.." ~Dev Mode~"
end
IGT.setText("")
RLT.setText("")
IGT.setText(time)
RLT.setText(realTime)
if (hour==3) or (hour==6) or (hour==9) or (hour==12) then
if (minute==00) and (second==00) then
updateCheck = true
end
end
sleep(0.5)
end
end
local function settings()
Text.setText("What time zone should I use? Current: "..defaultTimeZone)
e, msg = os.pullEvent("chat_command")
defaultTimeZone = msg
Text.setText("What time zone code should I use? Current: "..defaultTimeZoneCode)
e, msg = os.pullEvent("chat_command")
defaultTimeZoneCode = msg
Text.setText("What is the name assigned to your ME Bridge? EX: $$meBridge_0")
e, msg = os.pullEvent("chat_command")
bridgeName = tostring(msg)
settingValues = {defaultTimeZone, defaultTimeZoneCode, bridgeName}
filePath = "settings"
fs.delete("filePath")
fileHandle = fs.open (filePath, 'w')
fileHandle.write (textutils.serialize (settingValues))
fileHandle.close()
Text.setText("")
end
local function devToggle()
if devMode then
Text.setText("Currently in Developer Mode. Turn off Developer Mode? $$Y/N")
e, msg = os.pullEvent("chat_command")
if msg == "Y" then
Text.setText("Developer Mode disabled")
devMode = false
sleep(resultTime)
Text.setText("")
return
elseif msg == "N" then
Text.setText("Staying in Developer Mode")
sleep(resultTime)
Text.setText("")
return
else
Text.setText("Unknown reply. Staying in Developer Mode")
sleep(resultTime)
Text.setText("")
return
end
else
Text.setText("Currently not in Developer Mode. Turn on Developer Mode? $$Y/N")
e, msg = os.pullEvent("chat_command")
if msg == "Y" then
Text.setText("Input developer password")
e, pass = os.pullEvent("chat_command")
if pass == "AV2248" then
Text.setText("Developer Mode enabled")
devMode = true
sleep(resultTime)
Text.setText("")
return
else
Text.setText("Incorrect password")
sleep(resultTime)
Text.setText("")
return
end
elseif msg == "N" then
Text.setText("Staying out of Developer Mode")
sleep(resultTime)
Text.setText("")
return
else
Text.setText("Unknown reply. Command aborted")
sleep(resultTime)
Text.setText("")
return
end
end
end
local function getVar()
if devMode then
var = tostring(words[1])
Text.setText("The variable ~"..var.."~ is currently: "..tostring(_G[var]))
sleep(resultTime)
Text.setText("")
return
else
Text.setText("This command only available in Developer Mode")
sleep(resultTime)
Text.setText("")
return
end
end
local function command()
while true do
evt, command = os.pullEvent("chat_command")
words = {}
i = 0
for word in string.gmatch(command, "%S+") do
words[i] = word
i = i + 1
end
if string.lower(words[0]) == "?" then
variables["searchString"] = string.lower(tostring(words[1]))
printSearch()
elseif words[0] == "reset" then
resetScreen()
elseif words[0] == "newZone" then
timeZone = tostring(words[1])
changeZone()
elseif string.lower(words[0]) == "!" then
chat()
elseif words[0] == "update" then
update()
elseif words[0] == "newSettings" then
settings()
elseif words[0] == "devToggle" then
devToggle()
elseif words[0] == "getVar" then
getVar()
else
badCommand()
end
end
end
parallel.waitForAny(getTime, command, autoUpdateCheck)