Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --@version=1.5
- local version = 1.5
- local mon = peripheral.find("monitor")
- local cbx = peripheral.find("chat_box")
- local function fwrite(file, data)
- local f = fs.open(file, "w")
- f.write(data)
- f.close()
- end
- local function fread(file)
- local f = fs.open(file, "r")
- local r = f.readAll()
- f.close()
- return r
- end
- function cwrite(sText, yy)
- local w, h = mon.getSize()
- local x, y = mon.getCursorPos()
- x = math.max(math.floor((w / 2) - (#sText / 2)), y)
- mon.setCursorPos(x, yy)
- mon.write(sText)
- end
- local ok, err =
- pcall(
- function()
- local dshtp = http.get("https://pastebin.com/raw/akkinMTL")
- local dsl = dshtp.readLine()
- local dsr = dshtp.readAll()
- dshtp.close()
- if (tonumber(dsl:sub(12)) > version) then
- print("Update detected. Version " .. tostring(version) .. " > " .. tostring(dsl:sub(12)))
- local f = fs.open(shell.getRunningProgram(), "w")
- f.writeLine(dsl)
- f.write(dsr)
- f.close()
- sleep(2)
- os.reboot()
- end
- local pkey = fs.exists("/.pkey") and fread("/.pkey") or nil
- if not fs.exists("/dconfig") then
- fwrite(
- "/dconfig",
- [[
- local cfg = {}
- --Config starts here
- cfg["kristAddress"] = nil
- cfg["shopName"] = "Default Shop"
- cfg["redstoneHeartbeat"] = nil
- --Config ends here
- return cfg
- ]]
- )
- fwrite(
- "/shopdata",
- textutils.serialize(
- {
- {
- display = "End Crystal",
- item = "minecraft:end_crystal",
- meta = "endc",
- value = 1
- },
- {
- item = "computercraft:peripheral",
- display = "Advanced Monitor",
- meta = "amon",
- damage = 4,
- value = 2
- }
- }
- )
- )
- error("No config detected, loading default config and shopdata.", 0)
- end
- local cfg = loadfile("/dconfig")()
- if not cfg.kristAddress then
- error("No krist address in config!", 0)
- end
- if not fs.exists("/json") then
- local ht = http.get("http://pastebin.com/raw/4nRg9CHU")
- fwrite("/json", ht.readAll())
- ht.close()
- end
- os.loadAPI("/json")
- if not fs.exists("/jua") then
- local ht = http.get("https://raw.githubusercontent.com/justync7/Jua/master/jua.lua")
- fwrite("/jua", ht.readAll())
- ht.close()
- end
- os.loadAPI("/jua")
- local shopdata = {}
- local adr_prefix = "ERROR"
- local function load_shopdata()
- local f = fs.open("/shopdata", "r")
- shopdata = textutils.unserialize(f.readAll()) or {}
- f.close()
- if _G.whandle then
- _G.whandle.send(
- json.encode(
- {
- type = "DELTA-SHOPDATA",
- data = shopdata
- }
- )
- )
- end
- return shopdata
- end
- local itemcache = {}
- local namecache = {}
- local function getChests()
- local names = peripheral.getNames()
- local chests = {}
- for _, name in ipairs(names) do
- if not name:match("^turtle") then
- local wrapped = peripheral.wrap(name)
- if wrapped.getTransferLocations then
- chests[name] = wrapped
- end
- end
- end
- return chests
- end
- local function load_itemcache()
- local nc = {}
- for name, chest in pairs(getChests()) do
- for slot, meta in pairs(chest.list()) do
- local thingy = tostring(meta.name) .. "/" .. tostring(meta.damage or 0)
- if not nc[thingy] then
- nc[thingy] = meta.count
- else
- nc[thingy] = nc[thingy] + meta.count
- end
- end
- end
- itemcache = nc
- return nc
- end
- local function in_table(tab, dt)
- for k, v in pairs(tab) do
- if v == dt then
- return true
- end
- end
- return false
- end
- local function getOurTurtle(chest)
- for i, v in ipairs(chest.getTransferLocations()) do
- if v:match("^turtle") then
- return v
- end
- end
- end
- --Drawing code from TwijnMall. Thanks, Twijn!
- local function draw()
- local displayNameWidth = 0
- local stockWidth = 0
- local priceWidth = 0
- local metaWidth = 0
- local w, h = mon.getSize()
- load_itemcache()
- mon.setBackgroundColor(colors.black)
- mon.clear()
- mon.setTextScale(0.5)
- mon.setBackgroundColor(colors.white)
- mon.setTextColor(colors.black)
- mon.setCursorPos(1, 1)
- mon.clearLine()
- mon.setCursorPos(1, 2)
- mon.clearLine()
- cwrite(cfg.shopName, 1)
- mon.setTextColor(colors.lightGray)
- cwrite("deltashop, by Chervilpaw", 2)
- --
- --[[for k, v in pairs(shopdata) do
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- writething(v, itemcache[tostring(v.item) .. "/" .. tostring(v.damage or 0)], k + 3)
- end]]
- for i, v in pairs(shopdata) do
- if displayNameWidth < #v.display then
- displayNameWidth = #v.display
- end
- local stk = "0x"
- local prc = tonumber(v.value)
- if math.floor(prc) == prc then
- prc = prc .. ".00"
- end
- if type(prc) == "number" and math.floor(prc * 10) == prc * 10 then
- prc = prc .. "0"
- end
- if type(prc) == "number" then
- prc = "" .. prc
- end
- prc = prc .. " kst"
- if itemcache[tostring(v.item) .. "/" .. tostring(v.damage or 0)] ~= nil then
- stk = itemcache[tostring(v.item) .. "/" .. tostring(v.damage or 0)] .. "x"
- end
- if stockWidth < #stk then
- stockWidth = #stk
- end
- if priceWidth < #prc then
- priceWidth = #prc
- end
- if metaWidth < #(adr_prefix .. "-" .. tostring(v.meta) .. "@4g.kst") then
- metaWidth = #(adr_prefix .. "-" .. tostring(v.meta) .. "@4g.kst")
- end
- end
- if displayNameWidth == 0 and stockWidth == 0 and priceWidth == 0 and metaWidth == 0 then
- local stamt = math.floor((w - 3) / 4)
- displayNameWidth, stockWidth, priceWidth, metaWidth = stamt, stamt, stamt, stamt
- end
- if displayNameWidth == 0 and stockWidth == 0 and priceWidth == 0 and metaWidth == 0 then
- local stamt = math.floor((w - 3) / 4)
- displayNameWidth, stockWidth, priceWidth, metaWidth = stamt, stamt, stamt, stamt
- end
- local multiplier = (w - 3) / (displayNameWidth + stockWidth + priceWidth + metaWidth + 6)
- displayNameWidth = math.floor(displayNameWidth * multiplier)
- stockWidth = math.floor(stockWidth * multiplier)
- priceWidth = math.floor(priceWidth * multiplier)
- metaWidth = math.floor(metaWidth * multiplier)
- -- The following defines the points where the "lines" are put in between each section (meaning +1 would be the next section, -1 would be the previous; 0 is blank space.)
- local displayNameStart = 2
- local stockStart = displayNameWidth + 3
- local priceStart = stockWidth + displayNameWidth + 4
- local metaStart = priceWidth + stockWidth + displayNameWidth + 5
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.lightGray)
- mon.setCursorPos(displayNameStart + 2, 3)
- mon.write("Name")
- mon.setCursorPos(priceStart - 6, 3)
- mon.write("Stock")
- mon.setCursorPos(metaStart - 6, 3)
- mon.write("Price")
- mon.setCursorPos(w - 9, 3)
- mon.write("Address")
- local line = 4
- for i, item in pairs(shopdata) do
- if line >= 3 then
- local stk = "0x"
- local prc = tonumber(item.value)
- if math.floor(prc) == prc then
- prc = prc .. ".00"
- end
- if type(prc) == "number" and math.floor(prc * 10) == prc * 10 then
- prc = prc .. "0"
- end
- if type(prc) == "number" then
- prc = "" .. prc
- end
- prc = prc .. " kst"
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- if itemcache[tostring(item.item) .. "/" .. tostring(item.damage or 0)] then
- stk = itemcache[tostring(item.item) .. "/" .. tostring(item.damage or 0)] .. "x"
- end
- mon.setCursorPos(displayNameStart + 2, line)
- mon.clearLine()
- mon.write(item.display)
- local otc = mon.getTextColor()
- if stk == "0x" then
- mon.setTextColor(colors.red)
- else
- mon.setTextColor(otc)
- end
- mon.setCursorPos(priceStart - 1 - #stk, line)
- mon.write(stk)
- mon.setTextColor(otc)
- mon.setCursorPos(metaStart - 1 - #prc, line)
- mon.write(prc)
- mon.setCursorPos(w - 2 - #(adr_prefix .. "-" .. tostring(item.meta) .. "@4g.kst"), line)
- mon.write(adr_prefix .. "-" .. tostring(item.meta) .. "@4g.kst")
- end
- line = line + 1
- if line >= h - 1 then
- break
- end
- end
- end
- local function drawItems(name, amt, dmg)
- local required = amt
- for nam, chest in pairs(getChests()) do
- local target
- for i, v in pairs(chest.getTransferLocations()) do
- if v:find("turtle") then
- target = v
- break
- end
- end
- if not target then
- error("Could not find turtle on chest '" .. tostring(nam) .. "'!")
- end
- for slot, meta in pairs(chest.list()) do
- if meta.name == name and (not dmg or meta.damage == dmg) then
- required = required - chest.pushItems(target, slot, required)
- turtle.drop()
- if required <= 0 then
- break
- end
- end
- end
- end
- end
- http.websocketAsync("ws://ws.spaceball.cf:8081")
- jua.on(
- "websocket_success",
- function(e, url, handle)
- _G.whandle = handle
- end
- )
- jua.on(
- "terminate",
- function()
- error("deltashop terminated.", 0)
- end
- )
- jua.on(
- "websocket_closed",
- function(ev, url)
- _G.whandle = nil
- print("[warning] websocket closed, reopening in 3 seconds!")
- sleep(3)
- http.websocketAsync(url)
- end
- )
- jua.on(
- "websocket_message",
- function(e, url, data)
- if data then
- local j = json.decode(data)
- if type(j) == "table" then
- if j.type == "DELTA-REJECTNAME" then
- error("Shop name '" .. tostring(cfg.shopName) .. "' already in use!", 0)
- elseif j.type == "DELTA-SENDCODE" then
- whandle.send(
- json.encode(
- {
- type = "DELTA-CODE",
- code = pkey,
- kristadr = cfg.kristAddress,
- shopName = cfg.shopName
- }
- )
- )
- load_shopdata()
- elseif j.type == "DELTA-CODERECV" then
- pkey = j.code
- fwrite("/.pkey", j.code)
- adr_prefix = j.prefix
- load_shopdata()
- load_itemcache()
- draw()
- print("[log] connection with websocket succeeded! ")
- elseif j.type == "DELTA-BUY" then
- drawItems(j.id, j.amount, j.damage)
- print(tostring(j.amount) .. " of " .. j.name .. " bought!")
- load_itemcache()
- load_shopdata()
- draw()
- elseif j.type == "DELTA-MESSAGE" and type(cbx) == "table" then
- cbx.tell(j.target, j.message, "[deltaShop]")
- end
- end
- end
- end
- )
- jua.setInterval(
- function()
- load_shopdata()
- draw()
- end,
- 10
- )
- if type(cfg.redstoneHeartbeat) == "string" then
- jua.setInterval(
- function()
- rs.setOutput(cfg.redstoneHeartbeat, not rs.getOutput(cfg.redstoneHeartbeat))
- end,
- 5
- )
- end
- jua.run()
- end
- )
- if not ok then
- local wa, ha = mon.getSize()
- local hh = math.floor(ha / 2)
- printError("deltashop errored!: " .. tostring(err))
- mon.setBackgroundColor(colors.red)
- mon.setTextColor(colors.white)
- mon.clear()
- cwrite("deltashop errored!", hh - 1)
- cwrite(tostring(err), hh)
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment