From c3d1cdb9d7ccc5cd7502a7bccc4207a17a27c525 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 7 Jan 2023 05:15:11 +0000 Subject: shop2 --- shop2/.luacheckrc | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ shop2/disp.lua | 43 ++++++++++++++----------- shop2/main.lua | 39 ++++++----------------- shop2/stock.lua | 24 +++++++++++++- 4 files changed, 153 insertions(+), 48 deletions(-) create mode 100644 shop2/.luacheckrc (limited to 'shop2') diff --git a/shop2/.luacheckrc b/shop2/.luacheckrc new file mode 100644 index 0000000..8d18d80 --- /dev/null +++ b/shop2/.luacheckrc @@ -0,0 +1,95 @@ +local function mk_fields(fields) + local out = {} + for i = 1, #fields do out[fields[i]] = {} end + return { fields = out } +end + +std = { + read_globals = { + fs = mk_fields { "makeDir", "complete", "copy", "isDir", "list", "getSize", "delete", "isReadOnly", "getFreeSpace", "find", "getDrive", "exists", "move", "combine", "getName", "getDir", "open" }, + colours = mk_fields { "cyan", "purple", "brown", "lightGrey", "orange", "red", "magenta", "blue", "white", "grey", "rgb8", "pink", "test", "subtract", "black", "green", "combine", "yellow", "lime", "lightBlue" }, + tostring = {}, + _CC_DEFAULT_SETTINGS = {}, + getfenv = {}, + debug = mk_fields { "getupvalue", "debug", "getfenv", "getmetatable", "gethook", "setmetatable", "setfenv", "traceback", "getinfo", "setlocal", "setupvalue", "sethook", "getregistry", "getlocal" }, + disk = mk_fields { "getID", "isPresent", "eject", "getMountPath", "stopAudio", "getLabel", "hasAudio", "playAudio", "getAudioTitle", "setLabel", "hasData" }, + assert = {}, + tonumber = {}, + io = mk_fields { "lines", "write", "close", "flush", "open", "output", "read", "input", "type" }, + redstone = mk_fields { "setAnalogOutput", "getBundledOutput", "getBundledInput", "setOutput", "setAnalogueOutput", "getInput", "getOutput", "setBundledOutput", "getAnalogueOutput", "testBundledInput", "getSides", "getAnalogInput", "getAnalogOutput", "getAnalogueInput" }, + load = {}, + http = mk_fields { "checkURLAsync", "websocketAsync", "checkURL", "request", "websocket", "post", "get" }, + _G = { + read_only = false, + other_fields = true, + }, + peripheral = mk_fields { "call", "isPresent", "getType", "getMethods", "wrap", "find", "getNames" }, + vector = mk_fields { "new" }, + term = mk_fields { "getCursorPos", "isColour", "getBackgroundColor", "getTextColour", "scroll", "redirect", "getPaletteColor", "setTextColor", "native", "setPaletteColour", "getTextColor", "clear", "write", "setPaletteColor", "setCursorPos", "getBackgroundColour", "isColor", "setTextColour", "blit", "current", "getSize", "getPaletteColour", "setCursorBlink", "setBackgroundColor", "setBackgroundColour", "clearLine" }, + coroutine = mk_fields { "running", "yield", "status", "wrap", "create", "resume" }, + textutils = mk_fields { "serialize", "unserialize", "empty_json_array", "serializeJSON", "urlEncode", "serialiseJSON", "unserialiseJSON","unserializeJSON", "complete", "serialise", "formatTime", "pagedTabulate", "slowWrite", "slowPrint", "pagedPrint", "tabulate", "unserialise" }, + loadstring = {}, + rednet = mk_fields { "CHANNEL_REPEAT", "isOpen", "unhost", "close", "send", "open", "host", "CHANNEL_BROADCAST", "broadcast", "run", "receive", "lookup" }, + _HOST = {}, + string = mk_fields { "sub", "find", "len", "gfind", "reverse", "rep", "match", "gmatch", "dump", "byte", "upper", "gsub", "format", "char", "lower" }, + xpcall = {}, + package = { + fields = { + preload = {}, + config = {}, + loaders = {}, + loaded = { + read_only = false, + other_fields = true, + }, + path = {}, + }, + }, + parallel = mk_fields { "waitForAll", "waitForAny" }, + print = {}, + unpack = {}, + __inext = {}, + printError = {}, + require = {}, + _ENV = { + read_only = false, + other_fields = true, + }, + write = {}, + next = {}, + ipairs = {}, + rawequal = {}, + getmetatable = {}, + sleep = {}, + loadfile = {}, + settings = mk_fields { "clear", "unset", "set", "getNames", "save", "load", "get" }, + rawset = {}, + dofile = {}, + bit32 = mk_fields { "band", "rshift", "bor", "bnot", "bxor", "arshift", "btest", "lrotate", "lshift", "replace", "rrotate", "extract" }, + pairs = {}, + rs = mk_fields { "setAnalogOutput", "getBundledOutput", "getBundledInput", "setOutput", "setAnalogueOutput", "getInput", "getOutput", "setBundledOutput", "getAnalogueOutput", "testBundledInput", "getSides", "getAnalogInput", "getAnalogOutput", "getAnalogueInput" }, + help = mk_fields { "path", "topics", "completeTopic", "setPath", "lookup" }, + window = mk_fields { "create" }, + math = mk_fields { "log", "ceil", "atan", "acos", "ldexp", "rad", "pow", "asin", "pi", "deg", "tan", "cos", "tanh", "random", "abs", "frexp", "log10", "floor", "sinh", "max", "sqrt", "modf", "huge", "min", "mod", "fmod", "randomseed", "atan2", "exp", "sin", "cosh" }, + _VERSION = {}, + pcall = {}, + keys = mk_fields { "a", "c", "b", "e", "pageUp", "g", "f", "i", "h", "k", "j", "space", "l", "o", "n", "q", "f1", "s", "insert", "u", "t", "eight", "numPadEnter", "six", "x", "numPad6", "z", "backslash", "rightBracket", "f9", "yen", "left", "numPadSubtract", "noconvert", "grave", "rightCtrl", "numPad2", "rightAlt", "delete", "f4", "home", "leftAlt", "numPad8", "getName", "numLock", "f8", "r", "pageDown", "y", "numPadEquals", "at", "pause", "w", "f11", "slash", "f5", "rightShift", "period", "multiply", "numPad0", "nine", "f13", "capsLock", "two", "leftBracket", "minus", "scollLock", "colon", "f14", "v", "equals", "three", "up", "d", "convert", "f7", "apostrophe", "f15", "f10", "stop", "f2", "numPad3", "comma", "numPad1", "numPadAdd", "tab", "f3", "numPad4", "kana", "four", "right", "numPadDecimal", "numPad7", "leftShift", "backspace", "numPad9", "end", "one", "kanji", "cimcumflex", "m", "semiColon", "underscore", "zero", "p", "return", "ax", "seven", "f6", "enter", "numPadDivide", "numPad5", "f12", "leftCtrl", "down", "five", "numPadComma" }, + gps = mk_fields { "CHANNEL_GPS", "locate" }, + bit = mk_fields { "band", "blogic_rshift", "bxor", "bor", "bnot", "blshift", "brshift" }, + select = {}, + paintutils = mk_fields { "drawPixel", "drawLine", "loadImage", "drawImage", "drawFilledBox", "parseImage", "drawBox" }, + os = mk_fields { "sleep", "unloadAPI", "getComputerID", "startTimer", "queueEvent", "cancelTimer", "time", "clock", "setAlarm", "computerID", "reboot", "getComputerLabel", "setComputerLabel", "run", "pullEventRaw", "cancelAlarm", "computerLabel", "version", "shutdown", "loadAPI", "pullEvent", "epoch", "day" }, + rawget = {}, + table = mk_fields { "pack", "insert", "getn", "foreachi", "maxn", "foreach", "sort", "unpack", "concat", "remove" }, + read = {}, + colors = mk_fields { "cyan", "purple", "brown", "orange", "red", "magenta", "lime", "white", "blue", "rgb8", "lightGray", "pink", "test", "subtract", "black", "green", "combine", "yellow", "lightBlue", "gray" }, + setfenv = {}, + setmetatable = {}, + type = {}, + error = {}, + shell = mk_fields { "exit", "path", "setDir", "clearAlias", "programs", "dir", "getCompletionInfo", "resolveProgram", "setPath", "getRunningProgram", "setCompletionFunction", "history", "run", "complete", "completeProgram", "resolve", "setAlias", "aliases" }, + -- todo somewhat + turtle={other_fields=true}, + chatbox={other_fields=true}, + } +} diff --git a/shop2/disp.lua b/shop2/disp.lua index 5260cdc..0283742 100644 --- a/shop2/disp.lua +++ b/shop2/disp.lua @@ -1,4 +1,5 @@ local ourname = "ac.kst" +local stock=require"shop2.stock" local function printseq(m) return function(q) for _,v in ipairs(q) do @@ -34,42 +35,48 @@ end local function disp_shopscreen(items) -- i don't like how hardcoded all the spacing is in this -- but i also don't feel like rewriting it yet. - local m = peripheral.wrap"left" - m.clear() - m.setTextScale(0.5) + local mon = peripheral.wrap"left" + mon.clear() + mon.setTextScale(0.5) + + local si = stock.take_stock() + -- Stock Price Adr. Name -- xxxx xxxkst dmn Diamond -- 123456789012345678901234567890123456789012345678901234567 -- 0 1 2 3 4 5 -- local function fmt_row(m,y, stock,price,adr,name, colour) - local function fmt_row(m,y, idesc, + local function fmt_row(y, idesc, colour) + local amt = stock.amt_of(si,idesc) colour = colour or colors.cyan - printseq(m) { - {1,y}, colors.white, pad(stock,5), - {8,y}, colors.yellow, pad(price,3), colors.lightGray, "kst", - {16,y}, colour, adr, - {22,y}, colour, name, + printseq(mon) { + {1,y}, colors.white, pad(amt,5), + {8,y}, colors.yellow, pad(idesc.price,3), colors.lightGray, "kst", + {16,y}, colour, idesc.adr, + {22,y}, colour, idesc.hname, } end - - printseq(m) { + printseq(mon) { {1,1}, colors.orange, "Apiaristics Consortium Store", {1,2}, colors.lightGray, "Stock Price Adr. Name", } - -- sort alphabetically + -- sort alphabetically by address -- todo: more intuitive sort, once more things are for sale - -- - for ix,i in ipairs() do - fmt_row(m,ix+2,i[6],i[2],i[1],i[4],i[5]) + local adrs = {} + for adr in pairs(items) do table.insert(adrs,adr) end + table.sort(adrs) + for ix,adr in ipairs(adrs) do + local idesc = items[adr] + fmt_row(ix+2, idesc) end - local w,h = m.getSize() + local w,h = mon.getSize() - printseq(m) { - {1,h-1}, colors.blue, centre("/pay @ac.kst ",w) + printseq(mon) { + {1,h-1}, colors.blue, centre("/pay @"..ourname.." ",w) } diff --git a/shop2/main.lua b/shop2/main.lua index c110f83..322d76c 100644 --- a/shop2/main.lua +++ b/shop2/main.lua @@ -1,5 +1,9 @@ +package.path = "/?;/?.lua;"..package.path + local key=assert(settings.get"krist.pkey","please set private key") -local pp = require"cc.pretty".pretty_print + +local stock=require"shop2.stock" + -- ac.kst local OURNAME = "ac" @@ -10,7 +14,6 @@ end i("dmn","minecraft:diamond","Diamond",5) i("blz","minecraft:blaze_rod","Blaze Rod",10) i("ely","minecraft:elytra","Elytra",200) -i=nil local sres =textutils.unserializeJSON( http.post("https://krist.dev/ws/start","privatekey="..key).readAll()) @@ -40,25 +43,6 @@ local function parsemeta(m) end return out end -local function give(stockinfo,idesc,amt) - amt = amt or 1 - -- assumes that we have at least amt in stock already - -- actually no, might as well double check this anyway - assert(stock_amt(stockinfo,idesc) >= amt) - assert(amt>0) - -- make sure current turtle inv slot is empty - assert(turtle.getItemCount() == 0) - - for slotidx,idtl in pairs(stockinfo) do - if item_desc_matches(idesc,idtl) then - local a = math.min(amt,idtl.count) - chest.pushItems(localname, slotidx, a, 1) - turtle.drop(a) - amt = amt - a - if amt <= 0 then break end - end - end -end local function proctrans(tr) if tr.to ~= our_addr then return end @@ -82,8 +66,8 @@ local function proctrans(tr) tell("i can only give you a whole number of items. make sure the amount you gave me is a multiple of "..idesc.price) refund() else - local si = take_stock() - local in_stock = stock_amt(si,idesc) + local si = stock.take_stock() + local in_stock = stock.amt_of(si,idesc) if in_stock == 0 then tell("we are out of stock! please yell at ubq323 about this") refund() @@ -93,7 +77,7 @@ local function proctrans(tr) tell("i don't have that many items in stock, sorry") refund() else - give(si,idesc,to_give) + stock.give(si,idesc,to_give) tell("thank you!!! :3") end end @@ -103,9 +87,9 @@ end local function run_shop() print("RUNSHOP") while true do - msgtxt = assert(ws.receive(),"ws error") + local msgtxt = assert(ws.receive(),"ws error") print(msgtxt) - msg = textutils.unserializeJSON(msgtxt) + local msg = textutils.unserializeJSON(msgtxt) if msg.type == "response" and msg.responding_to_type == "me" then our_addr = msg.address.address end @@ -116,9 +100,6 @@ local function run_shop() end end -local function run_sidedisp() -end - print(pcall(parallel.waitForAny,run_shop)) -- print("hi") -- peripheral.call("top","clear") diff --git a/shop2/stock.lua b/shop2/stock.lua index ce76bf0..71a6844 100644 --- a/shop2/stock.lua +++ b/shop2/stock.lua @@ -27,7 +27,7 @@ local function item_desc_matches(idesc,itemdetail) return idesc.itname == itemdetail.name end -local function stock_amt(stockinfo,idesc) +local function amt_of(stockinfo,idesc) local total = 0 for ix,itm in pairs(stockinfo) do if item_desc_matches(idesc,itm) then @@ -37,3 +37,25 @@ local function stock_amt(stockinfo,idesc) return total end +local function give(stockinfo,idesc,amt) + amt = amt or 1 + -- assumes that we have at least amt in stock already + -- actually no, might as well double check this anyway + assert(amt_of(stockinfo,idesc) >= amt) + assert(amt>0) + -- make sure current turtle inv slot is empty + assert(turtle.getItemCount() == 0) + + for slotidx,idtl in pairs(stockinfo) do + if item_desc_matches(idesc,idtl) then + local a = math.min(amt,idtl.count) + chest.pushItems(localname, slotidx, a, 1) + turtle.drop(a) + amt = amt - a + if amt <= 0 then break end + end + end +end + + +return {take_stock=take_stock,amt_of=amt_of,give=give} -- cgit v1.2.3