Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------Params
- local bankServerID = 15
- local modemSide = "left"
- local diskSide = "right"
- local chestSide = "bottom"
- local text_error_noconnection = "Can't connect to server"
- --------------------------
- rednet.open(modemSide)
- os.loadAPI("bankapi.lua")
- bankapi.setBankServerID(bankServerID)
- --------------------------
- local owner = nil
- local catalog = {}
- local shopName = "Tienda"
- if (not fs.exists("owner.txt")) then
- if (fs.exists("disk")) then
- local tempClientData = bankapi.getClientData()
- local f = fs.open("disk/mermegold.txt", "r")
- if (f ~= nil) then
- local key = f.readLine()
- if (tempClientData[key] ~= nil) then
- local cf = fs.open("owner.txt", "w")
- cf.writeLine(key)
- cf.close()
- bankapi.successScreen("Reclamaste esta maquina como dueño")
- else
- bankapi.errorScreen("Tarjeta invalida")
- end
- else
- bankapi.errorScreen("El Disquet ingresado no es tarjeta")
- end
- f.close()
- os.reboot()
- else
- bankapi.errorScreen("Inserte tarjeta para configurar")
- os.shutdown()
- end
- else
- local f = fs.open("owner.txt", "r")
- owner = f.readLine()
- f.close()
- end
- local currentAccount = 0
- if (fs.exists("disk")) then
- local f = fs.open("disk/mermegold.txt", "r")
- if (f ~= nil) then
- local value = f.readLine()
- if (value ~= nil) then
- local tempClientData = bankapi.getClientData()
- if (tempClientData[value] ~= nil) then
- currentAccount = value
- end
- end
- f.close()
- end
- else
- bankapi.errorScreen("Inserte una tarjeta")
- os.shutdown()
- end
- local function loadShopName()
- local f = fs.open("shopname.txt", "r")
- if (f ~= nil) then
- local value = f.readLine()
- if (value ~= nil) then
- shopName = value
- end
- f.close()
- else
- local f = fs.open("shopname.txt", "w")
- f.writeLine("Tienda")
- f.close()
- end
- end
- local function loadCatalog()
- local f = fs.open("catalog.txt", "r")
- if (f ~= nil) then
- catalog = {}
- local line = f.readLine()
- while line ~= nil do
- local lline = line
- local item = ""
- local damage = 0
- local count = 0
- local price = 0
- local name = ""
- while lline ~= "" and lline ~= nil do
- if (lline == "item") then
- item = f.readLine()
- elseif (lline == "damage") then
- damage = tonumber(f.readLine())
- elseif (lline == "count") then
- count = tonumber(f.readLine())
- elseif (lline == "price") then
- price = tonumber(f.readLine())
- elseif (lline == "name") then
- name = f.readLine()
- end
- lline = f.readLine()
- end
- if (catalog[item] == nil) then
- catalog[item] = {}
- end
- catalog[item][damage] = {
- ["count"] = count,
- ["price"] = price,
- ["name"] = name
- }
- line = f.readLine()
- end
- f.close()
- else
- local f = fs.open("catalog.txt", "w")
- f.close()
- end
- end
- local function updateCatalog()
- local f = fs.open("catalog.txt", "w")
- for name, damagetypes in pairs(catalog) do
- print(name, damagetypes)
- for damage, detail in pairs(damagetypes) do
- print(damage, detail)
- for k, v in pairs(detail) do
- print(k, v)
- end
- f.writeLine("item")
- f.writeLine(name)
- f.writeLine("damage")
- f.writeLine(tostring(damage))
- f.writeLine("count")
- f.writeLine(tostring(detail.count))
- f.writeLine("name")
- f.writeLine(detail.name)
- f.writeLine("price")
- f.writeLine(tostring(detail.price))
- f.writeLine("")
- end
- end
- f.close()
- end
- local function printWithPadding(x, leftText, rightText, newLine, color)
- local scrW, scrH = term.getSize()
- leftText = string.rep(" ", x-1)..leftText
- if (newLine == nil) then newLine = true end
- local space = string.len(leftText)+string.len(rightText)
- local padding = scrW-space
- local paddingText = ""
- if (padding > 0) then
- paddingText = string.rep(".", padding)
- else
- leftText = string.sub(leftText, 1, string.len(leftText)+padding-1)
- paddingText = "."
- end
- if (color == nil) then
- term.setTextColor(colors.white)
- else
- term.setTextColor(color)
- end
- term.write(leftText)
- term.setTextColor(colors.gray)
- term.write(paddingText)
- if (color == nil) then
- term.setTextColor(colors.white)
- else
- term.setTextColor(color)
- end
- term.write(rightText)
- if (newLine) then
- print("")
- end
- end
- local function buy(previousEstimatedPrice)
- local scrW, scrH = term.getSize()
- term.setBackgroundColor(colors.gray)
- term.clear()
- term.setTextColor(colors.white)
- local text = "Registrando pago. Espere por favor..."
- term.setCursorPos(scrW/2-string.len(text)/2+1, scrH/2)
- term.write(text)
- local text = "Por favor no quite los items"
- term.setCursorPos(scrW/2-string.len(text)/2+1, scrH/2+1)
- term.write(text)
- local buyingItems = {}
- for i=1, 16 do
- turtle.select(i)
- local detail = turtle.getItemDetail(i)
- turtle.dropDown()
- if (detail ~= nil) then
- local item = detail.name
- if (catalog[item] ~= nil) then
- local damage = detail.damage
- if detail.damage == nil then
- damage = 1
- end
- if (catalog[item][damage] ~= nil) then
- local product = catalog[item][damage]
- if (buyingItems[item] == nil) then
- buyingItems[item] = {}
- end
- if (buyingItems[item][damage] == nil) then
- buyingItems[item][damage] = detail.count
- else
- buyingItems[item][damage] = buyingItems[item][damage]+detail.count
- end
- end
- end
- end
- end
- local totalPrice = 0
- local totalReceitDescription = shopName..": "
- for item, damagetypes in pairs(buyingItems) do
- for damage, count in pairs(damagetypes) do
- local product = catalog[item][damage]
- local estimatedPrice = math.ceil(count*product.price/product.count)
- totalPrice = totalPrice+estimatedPrice
- totalReceitDescription = totalReceitDescription.." "..product.name.." x"..count.." ($"..estimatedPrice..") |"
- end
- end
- bankapi.transaction(currentAccount, owner, totalPrice, totalReceitDescription)
- if (previousEstimatedPrice ~= totalPrice) then
- bankapi.errorScreen("No quite los items mientras cuento")
- bankapi.errorScreen("No pagó por esos.")
- bankapi.errorScreen("Solo le corresponden los del cofre")
- bankapi.errorScreen("Por favor no lo haga de nuevo.")
- else
- bankapi.successScreen("¡Gracias por comprar en "..shopName.."!")
- bankapi.successScreen("Ya puede llevarse su compra del cofre")
- end
- end
- local function logout()
- for i=1, 16 do
- local detail = turtle.getItemDetail(i)
- if (detail ~= nil) then
- bankapi.errorScreen("Por favor devuelva los items...")
- bankapi.errorScreen("...a su lugar antes de irse")
- disk.eject(diskSide)
- os.shutdown()
- return
- end
- end
- disk.eject(diskSide)
- bankapi.successScreen("¡Vuelva pronto!")
- os.shutdown()
- end
- loadShopName()
- loadCatalog()
- while true do -- Second while to allow the use of breaks as continues
- while true do
- if (owner == currentAccount) then
- local command = bankapi.optionMenu(shopName, {
- [1] = {
- ["option"] = "help",
- ["text"] = "Ayuda"},
- [2] = {
- ["option"] = "name",
- ["text"] = "Configurar Nombre"},
- [3] = {
- ["option"] = "catalog",
- ["text"] = "Configurar catalogo"},
- [4] = {
- ["option"] = "forfeit",
- ["text"] = "Desasignar dueño"},
- [5] = {
- ["option"] = "logout",
- ["text"] = "Salir"},
- })
- if (command == "help") then
- bankapi.textScreen({
- "Debes registrar los productos que",
- "quieras vender en esta terminal. ",
- "Puedes registrar, editar y eliminar",
- "mas de uno a la vez poniendo varios",
- "en el inventario.",
- "La cantidad de items que ingreses",
- "indica cada cuanto se cobra ese precio,",
- "por ejemplo poner 64 troncos hará que",
- "el precio que ingreses sea cada 64",
- "items."
- })
- elseif (command == "name") then
- local newname = bankapi.inputTextScreen({"Ingresar el nombre del local"}, 1, 30)
- if (newname == nil) then break end
- shopName = newname
- local f = fs.open("shopname.txt", "w")
- f.writeLine(shopName)
- f.close()
- bankapi.successScreen("Local renombrado a "..shopName)
- elseif (command == "catalog") then
- while true do -- Sub menu
- local command = bankapi.optionMenu(shopName, {
- [1] = {
- ["option"] = "list",
- ["text"] = "Ver Catalogo"},
- [2] = {
- ["option"] = "add",
- ["text"] = "Registrar Producto/s"},
- [3] = {
- ["option"] = "edit",
- ["text"] = "Editar Producto/s"},
- [4] = {
- ["option"] = "remove",
- ["text"] = "Remover Producto/s"},
- [5] = {
- ["option"] = "back",
- ["text"] = "Volver"},
- })
- if (command == "list") then
- local firstLog = 0
- local currentLog = 0
- local scrW, scrH = term.getSize()
- local catalogLength = 0
- for k, v in pairs(catalog) do
- catalogLength = catalogLength+1
- end
- term.setBackgroundColor(colors.black)
- if (catalogLength > 0) then
- term.clear()
- term.setCursorPos(1,1)
- for name, damagetypes in pairs(catalog) do
- for damage, detail in pairs(damagetypes) do
- term.setTextColor(colors.white)
- print(detail.name.." x"..detail.count.." = $"..detail.price)
- term.setTextColor(colors.gray)
- print(name.." (damage: "..damage..")")
- local cursorx, cursory = term.getCursorPos()
- if (cursory > scrH-2) then
- term.setTextColor(colors.gray)
- term.setCursorPos(1, scrH-1)
- print("Apreta cualquier tecla para ver mas")
- os.pullEvent("key")
- term.clear()
- term.setCursorPos(1,1)
- end
- end
- end
- else
- bankapi.errorScreen("No hay productos registrados")
- end
- term.setTextColor(colors.white)
- term.setCursorPos(1, scrH-1)
- print("Apreta cualquier tecla para volver")
- os.pullEvent("key")
- elseif (command == "add") then
- turtle.select(1)
- local steps = {
- "Productos y cantidades a registrar",
- "Nombre de estos productos",
- "Precio por los productos"
- }
- local count
- local breaking = false
- local totalItems
- repeat
- count = {}
- local accept = bankapi.confirmScreen({"Ingresa uno o mas productos en la", "cantidad que quieras registrar en", "el inventario, y luego presiona", "aceptar para continuar"}, {}, steps, 1)
- if (not accept) then
- breaking = true
- break
- end
- totalItems = 0
- for i=1, 16 do
- local itemCount = turtle.getItemCount(i)
- if (itemCount > 0) then
- count[i] = itemCount
- totalItems = totalItems+itemCount
- end
- end
- until totalItems > 0
- if (breaking) then break end
- local detail = {}
- for i=1, 16 do
- local itemDetail = turtle.getItemDetail(i)
- if (itemDetail ~= nil) then
- detail[i] = itemDetail
- end
- end
- local name = bankapi.inputTextScreen(steps, 2)
- if (name == nil) then break end
- local price = bankapi.inputNumberScreen(steps, 3)
- if (price == nil) then break end
- local displayName = nil
- local displayCount = nil
- local oneName = true
- local oneCount = true
- local set = false
- for k, v in pairs(detail) do
- if (not set) then
- displayName = v.name
- displayCount = v.count
- set = true
- else
- if (displayName ~= v.name) then
- oneName = false
- displayName = "<varios>"
- end
- if (displayCount ~= v.count) then
- oneCount = false
- displayCount = "<varios>"
- end
- end
- end
- local confirmData = {
- ["Nombre"] = name,
- ["Producto"] = displayName,
- ["Cantidad"] = "x"..displayCount,
- ["Precio"] = "$"..price
- }
- local accept = bankapi.confirmScreen({"¿Quieres registrar estos productos?"}, confirmData)
- if (not accept) then break end
- for k, v in pairs(detail) do
- if (catalog[v.name] == nil) then
- catalog[v.name] = {}
- end
- local damage = v.damage
- if v.damage == nil then
- damage = 1
- end
- catalog[v.name][damage] = {
- ["name"] = name,
- ["count"] = v.count,
- ["price"] = price
- }
- end
- updateCatalog()
- bankapi.successScreen("Producto registrado")
- elseif (command == "edit") then
- turtle.select(1)
- local steps = {
- "Producto a editar",
- "Nuevo nombre (vacio = no cambiar)",
- "Nuevo precio (vacio = no cambiar)"
- }
- local count
- local breaking = false
- local totalItems
- repeat
- count = {}
- local accept = bankapi.confirmScreen({"Ingresa un producto que quieras", "editar en el primer slot", "y luego presiona aceptar"}, {}, steps, 1)
- if (not accept) then
- breaking = true
- break
- end
- totalItems = 0
- for i=1, 16 do
- local itemCount = turtle.getItemCount(i)
- if (itemCount > 0) then
- count[i] = itemCount
- totalItems = totalItems+itemCount
- end
- end
- until totalItems > 0
- if (breaking) then break end
- local detail = {}
- for i=1, 16 do
- local itemDetail = turtle.getItemDetail(i)
- if (itemDetail ~= nil) then
- detail[i] = itemDetail
- end
- end
- local breaking = false
- for k, v in pairs(detail) do
- local damage = v.damage
- if v.damage == nil then
- damage = 1
- end
- if (catalog[v.name] == nil) then
- bankapi.errorScreen("'"..v.name.."' no esta registrado")
- breaking = true
- elseif (catalog[v.name][damage] == nil) then
- bankapi.errorScreen("'"..v.name.."' no esta registrado")
- breaking = true
- end
- end
- if (breaking) then break end
- local displayName = nil
- local displayCount = nil
- local oneName = true
- local oneCount = true
- local set = false
- for k, v in pairs(detail) do
- if (not set) then
- displayName = v.name
- displayCount = v.count
- set = true
- else
- if (displayName ~= v.name) then
- oneName = false
- displayName = "<varios>"
- end
- if (displayCount ~= v.count) then
- oneCount = false
- displayCount = "<varios>"
- end
- end
- end
- local displayDetail = {
- ["Nombre"] = displayName,
- ["Cantidad"] = displayCount
- }
- local accept = bankapi.confirmScreen({"¿Quieres editar estos productos?"}, displayDetail)
- if (not accept) then break end
- local editName = true
- local name = bankapi.inputTextScreen(steps, 2)
- if (name == nil) then break end
- if (name == "") then editName = false end
- local editPrice = true
- local price = bankapi.inputNumberScreen(steps, 3)
- if (price == nil) then break end
- if (price == "") then editPrice = false end
- local accept = bankapi.confirmScreen({"¿Quieres confirmar este cambio?"})
- if (not accept) then break end
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.black)
- print(540)
- for k, v in pairs(detail) do
- if (catalog[v.name] == nil) then
- catalog[v.name] = {}
- end
- local damage = v.damage
- if v.damage == nil then
- damage = 1
- end
- if (catalog[v.name][damage] == nil) then
- catalog[v.name][damage] = {}
- end
- if (editName) then
- catalog[v.name][damage].name = name
- end
- if (editPrice) then
- catalog[v.name][damage].price = price
- end
- catalog[v.name][damage].count = count[k]
- end
- updateCatalog()
- bankapi.successScreen("Producto registrado")
- elseif (command == "remove") then
- turtle.select(1)
- local steps = {
- "Productos a eliminar"
- }
- local count = turtle.getItemCount()
- local breaking = false
- repeat
- local accept = bankapi.confirmScreen({"Ingresa los productos que quieras", "eliminar en el inventario", "y luego presiona aceptar"}, {}, steps, 1)
- if (not accept) then
- breaking = true
- break
- end
- count = turtle.getItemCount()
- until count > 0
- if (breaking) then break end
- local detail = {}
- for i=1, 16 do
- local itemDetail = turtle.getItemDetail(i)
- if (itemDetail ~= nil) then
- detail[i] = itemDetail
- end
- end
- local displayName = nil
- local oneName = true
- local set = false
- for k, v in pairs(detail) do
- if (not set) then
- displayName = v.name
- set = true
- else
- if (displayName ~= v.name) then
- oneName = false
- displayName = "<varios>"
- end
- end
- end
- local displayMessage = {
- ["Nombre"] = displayName
- }
- local accept = bankapi.confirmScreen({"¿Quieres eliminar estos productos?"}, displayMessage)
- if (not accept) then break end
- for k, v in pairs (detail) do
- local damage = v.damage
- if v.damage == nil then
- damage = 1
- end
- catalog[v.name][damage] = nil
- end
- updateCatalog()
- bankapi.successScreen("Producto eliminado")
- elseif (command == "back") then
- break
- end
- end
- elseif (command == "forfeit") then
- local accept = bankapi.confirmScreen({"¿Deseas desasignarte como dueño", "de esta maquina?"})
- if (accept) then
- fs.delete("owner.txt")
- disk.eject(diskSide)
- bankapi.successScreen("Te desasignaste como dueño.")
- os.shutdown()
- end
- elseif (command == "logout") then
- disk.eject(diskSide)
- os.shutdown()
- end
- elseif (currentAccount == 0) then -- No valid disk
- bankapi.errorScreen("Inserte una tarjeta")
- os.shutdown()
- else -- Valid account
- local scrW, scrH = term.getSize()
- local tempClientData = bankapi.getClientData()
- while true do
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lightGray)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.gray)
- local buyingItems = {}
- for i=1, 16 do
- local detail = turtle.getItemDetail(i)
- if (detail ~= nil) then
- local item = detail.name
- if (catalog[item] ~= nil) then
- local damage = detail.damage
- if detail.damage == nil then
- damage = 1
- end
- if (catalog[item][damage] ~= nil) then
- local product = catalog[item][damage]
- if (buyingItems[item] == nil) then
- buyingItems[item] = {}
- end
- if (buyingItems[item][damage] == nil) then
- buyingItems[item][damage] = detail.count
- else
- buyingItems[item][damage] = buyingItems[item][damage]+detail.count
- end
- end
- end
- end
- end
- local totalPrice = 0
- for item, damagetypes in pairs(buyingItems) do
- for damage, count in pairs(damagetypes) do
- local product = catalog[item][damage]
- local estimatedPrice = math.ceil(count*product.price/product.count)
- local leftText = product.name.." x"..count
- local rightText = "$"..estimatedPrice
- totalPrice = totalPrice+estimatedPrice
- printWithPadding(1, leftText, rightText)
- end
- end
- print("")
- print("")
- term.setCursorPos(1, scrH-2)
- term.setTextColor(colors.lightGray)
- local balance = tempClientData[currentAccount].balance
- printWithPadding(23, "Tu Balance", "$"..balance, true, colors.gray)
- printWithPadding(23, "Precio", "$"..totalPrice)
- local total = balance-totalPrice
- if (total < 0) then
- printWithPadding(23, "Total", "$"..total, false, colors.red)
- else
- printWithPadding(23, "Total", "$"..total, false, colors.gray)
- end
- term.setTextColor(colors.black)
- if (totalPrice > 0 and total >= 0) then
- term.setCursorPos(1, scrH-2)
- term.setBackgroundColor(colors.green)
- term.write(" Comprar y continuar ")
- term.setCursorPos(1, scrH-1)
- term.setBackgroundColor(colors.yellow)
- term.write(" Comprar y salir ")
- else
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.orange)
- term.write(" Instrucciones ")
- end
- term.setCursorPos(1, scrH)
- term.setBackgroundColor(colors.red)
- term.write(" Cancelar y salir ")
- while true do
- local event, button, cx, cy = os.pullEvent()
- if (event == "turtle_inventory") then
- break
- elseif (event == "mouse_click") then
- if (cx < 23) then
- if (totalPrice > 0 and total >= 0) then
- if (cy == scrH-2) then
- buy(totalPrice)
- break
- end
- if (cy == scrH-1) then
- buy(totalPrice)
- logout()
- break
- end
- else
- if (cy == 1) then
- bankapi.textScreen({
- "Trae los items que quieras comprar",
- "y ponlos en el inventario de este",
- "robot. Se contará el monto a pagar",
- "automáticamente, y podrás aceptar para",
- "finalizar el pago. Cuando confirmes,",
- "tus compras serán depositadas en el",
- "cofre debajo de este robot.",
- "Si cancelas y dejaste items adentro",
- "del robot, por favor, devuelvelos a",
- "su lugar. ¡No te olvides tu tarjeta!",
- "Siempre usa el boton de 'cancelar y",
- "salir' en vez de quitarla a mano."
- })
- break
- end
- end
- if (cy == scrH) then
- logout()
- end
- end
- end
- end
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment