Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local backgroundColor = colors.gray
- local buttonColor = colors.yellow
- local buttonTextColor = colors.black
- local grayedOutColor = colors.lightGray
- local specialTextColor = colors.yellow
- local bankServerID = 0
- local readingPosX = 0
- local readingPosY = 0
- local readingString = ""
- local reading = false
- local readingMax = 10
- local function contrastColor(color)
- if (color == colors.white) then return colors.black end
- if (color == colors.orange) then return colors.black end
- if (color == colors.magenta) then return colors.black end
- if (color == colors.lightBlue) then return colors.black end
- if (color == colors.yellow) then return colors.black end
- if (color == colors.lime) then return colors.black end
- if (color == colors.pink) then return colors.black end
- if (color == colors.gray) then return colors.white end
- if (color == colors.lightGray) then return colors.black end
- if (color == colors.cyan) then return colors.white end
- if (color == colors.purple) then return colors.white end
- if (color == colors.blue) then return colors.white end
- if (color == colors.brown) then return colors.white end
- if (color == colors.green) then return colors.black end
- if (color == colors.red) then return colors.white end
- if (color == colors.black) then return colors.white end
- return colors.black
- end
- function setBankServerID(id)
- bankServerID = id
- end
- function getClientData()
- local message = {
- ["action"] = "getClientData"
- }
- rednet.send(bankServerID, message, "mermegold")
- local sender, message = rednet.receive("mermegold")
- if (sender == nil) then
- print(text_error_noconnection)
- end
- return message.response
- end
- local function drawBackButton()
- local scrW, scrH = term.getSize()
- local text = " Volver "
- local x = 1
- local y = scrH
- local width = string.len(text)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.black)
- term.setCursorPos(x, y)
- term.write(text)
- term.setBackgroundColor(colors.black)
- return x, y, width
- end
- function transaction(from, to, amount, description)
- --local success, response = transaction(from, to, amount, description)
- local message = {
- ["action"] = "transaction",
- ["from"] = from,
- ["to"] = to,
- ["amount"] = amount,
- ["description"] = description
- }
- rednet.send(bankServerID, message, "mermegold")
- local sender, message = rednet.receive("mermegold")
- return message.success, message
- end
- function deposit(key, amount)
- local message = {
- ["action"] = "deposit",
- ["key"] = key,
- ["amount"] = amount
- }
- rednet.send(bankServerID, message, "mermegold")
- local sender, message = rednet.receive("mermegold")
- return message.success, message.response
- end
- function withdraw(key, amount)
- local message = {
- ["action"] = "withdraw",
- ["key"] = key,
- ["amount"] = amount
- }
- rednet.send(bankServerID, message, "mermegold")
- local sender, message = rednet.receive("mermegold")
- return message.success, message.response
- end
- function newAccount(name, balance, color)
- local message = {
- ["action"] = "new",
- ["name"] = name,
- ["balance"] = balance,
- ["color"] = color
- }
- rednet.send(bankServerID, message, "mermegold")
- local sender, message = rednet.receive("mermegold")
- return message.success, message.response
- end
- function deleteAccount(key)
- local message = {
- ["action"] = "delete",
- ["key"] = key
- }
- rednet.send(bankServerID, message, "mermegold")
- local sender, message = rednet.receive("mermegold")
- return message.success, message.response
- end
- function getTransactionLog(key)
- local message = {
- ["action"] = "getTransactionLog",
- ["key"] = key
- }
- rednet.send(bankServerID, message, "mermegold")
- local sender, message = rednet.receive("mermegold")
- if (sender == nil) then
- print(text_error_noconnection)
- end
- return message.response
- end
- function transactionInfoScreen(log)
- local tempClientData = getClientData()
- term.setBackgroundColor(backgroundColor)
- term.clear()
- term.setCursorPos(1,2)
- term.setTextColor(colors.white)
- print("Involucrado: "..tempClientData[log.other].name)
- term.write("Monto: ")
- local amountText
- if (tonumber(log.amount) > 0) then
- amountText = "+$"..log.amount
- term.setTextColor(colors.green)
- end
- if (tonumber(log.amount) < 0) then
- amountText = "-$"..math.abs(log.amount)
- term.setTextColor(colors.red)
- end
- print(amountText)
- term.setTextColor(colors.white)
- print("Balance resultante: $"..log.balance)
- print("Fecha y hora: "..log.time)
- if (string.len(log.description) > 0) then
- print("Descripción:")
- term.setTextColor(specialTextColor)
- print(log.description)
- else
- term.setTextColor(grayedOutColor)
- print("Sin descripción")
- end
- drawBackButton()
- os.pullEvent("mouse_click")
- end
- function transactionLogScreen(key)
- local tempClientData = getClientData()
- local backwardsLogs = getTransactionLog(key)
- local logs = {}
- local logCount = #backwardsLogs
- for i=0, logCount do
- logs[logCount-i] = backwardsLogs[i+1] -- Newest first
- end
- local scrW, scrH = term.getSize()
- local y = 3
- local x = 1
- local w = scrW
- local first = 0
- local logHeight = 3
- local max = math.floor((scrH-y)/logHeight)
- local upButtonY = y-2
- local downButtonY = y+max*logHeight
- while true do
- term.setBackgroundColor(backgroundColor)
- term.clear()
- local backx, backy, backw = drawBackButton()
- term.setCursorPos(backw+5, scrH)
- term.setBackgroundColor(backgroundColor)
- term.setTextColor(grayedOutColor)
- term.write("Haz click para ver detalle")
- if (first > 0) then
- term.setCursorPos(scrW/2-3,upButtonY)
- term.setTextColor(buttonTextColor)
- term.setBackgroundColor(buttonColor)
- term.write(" "..string.char(30).." ")
- end
- local totalLogs = 0
- local i = 0
- local buttons = {}
- for k, v in ipairs(logs) do
- if (totalLogs >= first and totalLogs < first+max) then
- local order = #logs-k+1
- local amountText
- if (tonumber(v.amount) > 0) then
- amountText = "+$"..v.amount
- end
- if (tonumber(v.amount) < 0) then
- amountText = "-$"..math.abs(v.amount)
- end
- term.setBackgroundColor(backgroundColor)
- local text = "#"..order.." "..v.description
- term.setCursorPos(1,y+i)
- term.setTextColor(colors.lightGray)
- term.write("#"..order.." ")
- term.setTextColor(specialTextColor)
- local desc = v.description
- if (string.len(desc) > scrW-8) then
- desc = string.sub(v.description, 1, scrW-8).."..."
- end
- print(desc)
- buttons[y+i] = k
- buttons[y+i+1] = k
- local totalText = tempClientData[v.other].name.." "..amountText.." ($"..v.balance..") "..v.time
- term.setTextColor(colors.white)
- term.write(tempClientData[v.other].name.." ")
- if (tonumber(v.amount) >= 0) then
- term.setTextColor(colors.green)
- else
- term.setTextColor(colors.red)
- end
- term.write(amountText)
- term.setTextColor(colors.white)
- term.write(" ($"..v.balance..") ")
- term.setTextColor(colors.lightGray)
- term.write(v.time)
- i = i+logHeight
- end
- totalLogs = totalLogs+1
- end
- if (first+max < totalLogs) then
- term.setCursorPos(scrW/2-3,downButtonY)
- term.setTextColor(buttonTextColor)
- term.setBackgroundColor(buttonColor)
- term.write(" "..string.char(31).." ")
- end
- local event, button, cx, cy = os.pullEvent("mouse_click")
- if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == upButtonY) then
- if (first > 0) then
- first = first-max
- end
- end
- if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == downButtonY) then
- if (first+max < totalLogs) then
- first = first+max
- end
- end
- if (cx >= x and cx <= x+w and cy >= y) then
- if buttons[cy] ~= nil then
- transactionInfoScreen(logs[buttons[cy]])
- end
- end
- if (cx >= backx and cx <= backx+backw and cy == backy) then
- return nil
- end
- end
- end
- function optionMenu(title, options, spacing)
- if (spacing == nil) then spacing = 2 end
- term.setBackgroundColor(backgroundColor)
- term.clear()
- local buttons = {}
- local scrW, scrH = term.getSize()
- local w = 30
- local x = scrW/2-w/2+1
- local y = math.floor(scrH/2-(#options+1)*spacing/2)+2
- local i = 0
- term.setCursorPos(scrW/2-string.len(title)/2+1, y+i)
- term.setTextColor(specialTextColor)
- term.write(title)
- i = i+spacing
- term.setBackgroundColor(buttonColor)
- term.setTextColor(buttonTextColor)
- for k, v in ipairs(options) do
- term.setCursorPos(x, y+i)
- term.write(string.rep(" ", w))
- term.setCursorPos(scrW/2-string.len(v.text)/2+1, y+i)
- term.write(v.text)
- buttons[y+i] = v.option
- i = i+spacing
- end
- while true do
- local event, button, cx, cy = os.pullEvent("mouse_click")
- if (cx >= x and cx <= x+w and cy >= y and cy < y+(#options+1)*spacing) then
- if buttons[cy] ~= nil then
- return buttons[cy]
- end
- end
- end
- end
- local function drawSteps(steps, currentStep)
- term.setCursorPos(1, 2)
- term.setBackgroundColor(backgroundColor)
- term.clear()
- for k, v in ipairs(steps) do
- term.setTextColor(grayedOutColor)
- if (k == currentStep) then
- term.setTextColor(specialTextColor)
- term.write(" > "..k..". ")
- else
- term.write(" "..k..". ")
- end
- print(v)
- end
- end
- local function startRead(maxLength)
- local scrW, scrH = term.getSize()
- readingPosX, readingPosY = term.getCursorPos()
- reading = true
- readingString = ""
- readingMax = maxLength
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(readingPosX, readingPosY)
- term.write(string.rep(" ", scrW-2))
- term.setCursorBlink(true)
- end
- local function processChar(char)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(readingPosX, readingPosY)
- readingString = readingString..char
- term.write(readingString)
- end
- local function processKey(key)
- if (key == keys.backspace) then
- readingString = string.sub(readingString, 1, string.len(readingString)-1)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(readingPosX+string.len(readingString), readingPosY)
- term.write(" ")
- term.setCursorPos(readingPosX+string.len(readingString), readingPosY)
- elseif (key == keys.enter) then
- term.setCursorBlink(false)
- return readingString
- end
- return nil
- end
- local function cancelableRead(maxLength)
- startRead(maxLength)
- local backx, backy, backw = drawBackButton()
- term.setCursorPos(readingPosX, readingPosY)
- local input = nil
- while input == nil do
- local event, a, b, c = os.pullEvent()
- if (event == "char") then
- processChar(a)
- elseif (event == "key") then
- input = processKey(a)
- elseif (event == "mouse_click") then
- local cx = b
- local cy = c
- if (cx >= backx and cx <= backx+backw and cy == backy) then
- term.setCursorBlink(false)
- return nil
- end
- end
- end
- term.setCursorBlink(false)
- return input
- end
- function showBalance(key)
- local tempClientData = getClientData()
- term.setBackgroundColor(backgroundColor)
- term.clear()
- local scrW, scrH = term.getSize()
- local text = "Tu balance:"
- term.setTextColor(specialTextColor)
- term.setCursorPos(scrW/2-string.len(text)/2,scrH/2-1)
- term.write(text)
- text = "$"..tempClientData[key].balance
- term.setCursorPos(scrW/2-string.len(text)/2,scrH/2+1)
- term.write(text)
- drawBackButton()
- while true do
- local event = os.pullEvent()
- if (event == "mouse_click" or event == "key") then
- break
- end
- end
- end
- function selectAccountScreen(steps, currentStep, disabledAccount)
- local tempClientData = getClientData()
- local scrW, scrH = term.getSize()
- local x, y, w, h = scrW/4, #steps+4, scrW/2, scrH-5
- local backx, backy, backw = drawBackButton()
- local first = 0
- local max = scrH-y-2
- local upButtonY = y-2
- local downButtonY = y+1+max
- while true do
- drawSteps(steps, currentStep)
- drawBackButton()
- local i = 0
- local buttons = {}
- if (first > 0) then
- term.setCursorPos(scrW/2-3,upButtonY)
- term.setTextColor(buttonTextColor)
- term.setBackgroundColor(buttonColor)
- term.write(" "..string.char(30).." ")
- end
- local totalAccounts = 0
- local showAccounts = 0
- for k, v in pairs(tempClientData) do
- if (disabledAccount ~= nil and disabledAccount ~= k) then
- if (showAccounts >= first and showAccounts < first+max) then
- buttons[y+i] = k
- term.setCursorPos(x,y+i)
- term.setTextColor(contrastColor(tonumber(v.color)))
- term.setBackgroundColor(tonumber(v.color))
- term.write(string.rep(" ", w))
- term.setCursorPos(x+w/2-string.len(v.name)/2-1,y+i)
- term.write(v.name)
- i = i+1
- end
- showAccounts = showAccounts+1
- end
- totalAccounts = totalAccounts+1
- end
- if (first+max < showAccounts) then
- term.setCursorPos(scrW/2-3,downButtonY)
- term.setTextColor(buttonTextColor)
- term.setBackgroundColor(buttonColor)
- term.write(" "..string.char(31).." ")
- end
- local event, button, cx, cy = os.pullEvent("mouse_click")
- if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == upButtonY) then
- if (first > 0) then
- first = first-max
- end
- end
- if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == downButtonY) then
- if (first+max < showAccounts) then
- first = first+max
- end
- end
- if (cx >= x and cx <= x+w and cy >= y and cy < y+i) then
- return buttons[cy]
- end
- if (cx >= backx and cx <= backx+backw and cy == backy) then
- return nil
- end
- end
- end
- function inputNumberScreen(steps, currentStep, max)
- drawSteps(steps, currentStep)
- local y = #steps+3
- term.setCursorPos(1, y)
- term.setBackgroundColor(backgroundColor)
- term.setTextColor(specialTextColor)
- term.write("Ingresa un numero ")
- if (max ~= nil and max ~= 0) then
- term.write("(máximo "..max..")")
- end
- term.write(":")
- local scrW, scrH = term.getSize()
- local number = nil
- repeat
- term.setCursorPos(2, y+2)
- number = cancelableRead()
- if (number == nil) then return nil end
- until number ~= nil and tonumber(number) <= max
- return number
- end
- function inputTextScreen(steps, currentStep, maxLength)
- drawSteps(steps, currentStep)
- local y = #steps+3
- term.setCursorPos(1, y)
- term.setBackgroundColor(backgroundColor)
- term.setTextColor(specialTextColor)
- term.write("Ingresa un texto ")
- if (max ~= nil and max ~= 0) then
- term.write("(largo máximo "..max..")")
- end
- term.write(":")
- local scrW, scrH = term.getSize()
- term.setCursorPos(2, y+2)
- return cancelableRead()
- end
- function selectColorScreen(steps, currentStep)
- drawSteps(steps, currentStep)
- local y = #steps+3
- local color = 1
- local buttons = {}
- local scrW, scrH = term.getSize()
- local x = scrW/2-10
- local w = 5 --button width
- for _x=0, 3 do
- buttons[_x] = {}
- for _y= 0, 3 do
- term.setCursorPos(x+_x*w+1, y+_y)
- term.setBackgroundColor(color)
- term.setTextColor(contrastColor(color))
- term.write("[")
- term.write(string.rep(" ", w-2))
- term.write("]")
- buttons[_x][_y] = color
- color = color*2
- end
- end
- local backx, backy, backw = drawBackButton()
- while true do
- local event, button, cx, cy = os.pullEvent("mouse_click")
- if (cx >= x and cx <= x+w*4 and cy >= y and cy < y+4) then
- local _x = math.floor((cx-x)/w)
- local _y = math.floor((cy-y))
- return buttons[_x][_y]
- end
- if (cx >= backx and cx <= backx+backw and cy == backy) then
- return nil
- end
- end
- end
- function responseScreen(success, response)
- if (success) then
- successScreen(response)
- else
- errorScreen(response)
- end
- end
- function errorScreen(message)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- term.clear()
- local scrW, scrH = term.getSize()
- term.setCursorPos(scrW/2-string.len(message)/2, scrH/2)
- term.write(message)
- sleep(1.5)
- end
- function successScreen(message)
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.white)
- term.clear()
- local scrW, scrH = term.getSize()
- term.setCursorPos(scrW/2-string.len(message)/2, scrH/2)
- term.write(message)
- sleep(1.5)
- end
- function confirmScreen(message, data)
- local y = 2
- local scrW, scrH = term.getSize()
- term.setBackgroundColor(backgroundColor)
- term.clear()
- term.setTextColor(specialTextColor)
- for k, v in pairs(message) do
- term.setCursorPos((scrW-string.len(v))/2, y)
- term.write(v)
- y = y+1
- end
- y = y+1
- if (data ~= nil) then
- for k, v in pairs(data) do
- local text = k..": "..v
- term.setCursorPos((scrW-string.len(text))/2, y)
- term.write(text)
- y = y+1
- end
- y = y+1
- end
- local buttonY = math.min(scrH-3, y)
- local buttonW = 20
- local buttonX = scrW/2-buttonW/2
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.black)
- term.setCursorPos(buttonX, buttonY)
- term.write(string.rep(" ", buttonW))
- local text = "Aceptar"
- term.setCursorPos(buttonX+buttonW/2-string.len(text)/2, buttonY)
- term.write(text)
- term.setBackgroundColor(colors.red)
- term.setCursorPos(buttonX, buttonY+2)
- term.write(string.rep(" ", buttonW))
- local text = "Cancelar"
- term.setCursorPos(buttonX+buttonW/2-string.len(text)/2, buttonY+2)
- term.write(text)
- while true do
- local event, button, cx, cy = os.pullEvent("mouse_click")
- if (cx >= buttonX and cx <= buttonX+buttonW and cy == buttonY) then
- return true
- end
- if (cx >= buttonX and cx <= buttonX+buttonW and cy == buttonY+2) then
- return false
- end
- end
- end
- function textScreen(message)
- local y = 1
- local scrW, scrH = term.getSize()
- term.setBackgroundColor(backgroundColor)
- term.clear()
- term.setTextColor(colors.white)
- for k, v in pairs(message) do
- term.setCursorPos((scrW-string.len(v))/2+1, y)
- term.write(v)
- y = y+1
- end
- drawBackButton()
- os.pullEvent("mouse_click")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement