Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.49 KB | None | 0 0
  1. local backgroundColor = colors.gray
  2. local buttonColor = colors.yellow
  3. local buttonTextColor = colors.black
  4. local grayedOutColor = colors.lightGray
  5. local specialTextColor = colors.yellow
  6.  
  7. local bankServerID = 0
  8. local readingPosX = 0
  9. local readingPosY = 0
  10. local readingString = ""
  11. local reading = false
  12. local readingMax = 10
  13.  
  14. local function contrastColor(color)
  15. if (color == colors.white) then return colors.black end
  16. if (color == colors.orange) then return colors.black end
  17. if (color == colors.magenta) then return colors.black end
  18. if (color == colors.lightBlue) then return colors.black end
  19. if (color == colors.yellow) then return colors.black end
  20. if (color == colors.lime) then return colors.black end
  21. if (color == colors.pink) then return colors.black end
  22. if (color == colors.gray) then return colors.white end
  23. if (color == colors.lightGray) then return colors.black end
  24. if (color == colors.cyan) then return colors.white end
  25. if (color == colors.purple) then return colors.white end
  26. if (color == colors.blue) then return colors.white end
  27. if (color == colors.brown) then return colors.white end
  28. if (color == colors.green) then return colors.black end
  29. if (color == colors.red) then return colors.white end
  30. if (color == colors.black) then return colors.white end
  31. return colors.black
  32. end
  33.  
  34. function setBankServerID(id)
  35. bankServerID = id
  36. end
  37.  
  38. function getClientData()
  39. local message = {
  40. ["action"] = "getClientData"
  41. }
  42. rednet.send(bankServerID, message, "mermegold")
  43. local sender, message = rednet.receive("mermegold")
  44. if (sender == nil) then
  45. print(text_error_noconnection)
  46. end
  47. return message.response
  48. end
  49.  
  50. local function drawBackButton()
  51. local scrW, scrH = term.getSize()
  52. local text = " Volver "
  53. local x = 1
  54. local y = scrH
  55.  
  56. local width = string.len(text)
  57. term.setBackgroundColor(colors.red)
  58. term.setTextColor(colors.black)
  59.  
  60. term.setCursorPos(x, y)
  61. term.write(text)
  62. term.setBackgroundColor(colors.black)
  63. return x, y, width
  64. end
  65.  
  66. function transaction(from, to, amount, description)
  67. --local success, response = transaction(from, to, amount, description)
  68. local message = {
  69. ["action"] = "transaction",
  70. ["from"] = from,
  71. ["to"] = to,
  72. ["amount"] = amount,
  73. ["description"] = description
  74. }
  75. rednet.send(bankServerID, message, "mermegold")
  76. local sender, message = rednet.receive("mermegold")
  77. return message.success, message
  78. end
  79.  
  80. function deposit(key, amount)
  81. local message = {
  82. ["action"] = "deposit",
  83. ["key"] = key,
  84. ["amount"] = amount
  85. }
  86. rednet.send(bankServerID, message, "mermegold")
  87. local sender, message = rednet.receive("mermegold")
  88. return message.success, message.response
  89. end
  90.  
  91. function withdraw(key, amount)
  92. local message = {
  93. ["action"] = "withdraw",
  94. ["key"] = key,
  95. ["amount"] = amount
  96. }
  97. rednet.send(bankServerID, message, "mermegold")
  98. local sender, message = rednet.receive("mermegold")
  99. return message.success, message.response
  100. end
  101.  
  102. function newAccount(name, balance, color)
  103. local message = {
  104. ["action"] = "new",
  105. ["name"] = name,
  106. ["balance"] = balance,
  107. ["color"] = color
  108. }
  109. rednet.send(bankServerID, message, "mermegold")
  110. local sender, message = rednet.receive("mermegold")
  111. return message.success, message.response
  112. end
  113.  
  114. function deleteAccount(key)
  115. local message = {
  116. ["action"] = "delete",
  117. ["key"] = key
  118. }
  119. rednet.send(bankServerID, message, "mermegold")
  120. local sender, message = rednet.receive("mermegold")
  121. return message.success, message.response
  122. end
  123.  
  124. function getTransactionLog(key)
  125. local message = {
  126. ["action"] = "getTransactionLog",
  127. ["key"] = key
  128. }
  129. rednet.send(bankServerID, message, "mermegold")
  130. local sender, message = rednet.receive("mermegold")
  131. if (sender == nil) then
  132. print(text_error_noconnection)
  133. end
  134. return message.response
  135. end
  136.  
  137. function transactionInfoScreen(log)
  138. local tempClientData = getClientData()
  139.  
  140. term.setBackgroundColor(backgroundColor)
  141. term.clear()
  142. term.setCursorPos(1,2)
  143. term.setTextColor(colors.white)
  144. print("Involucrado: "..tempClientData[log.other].name)
  145. term.write("Monto: ")
  146. local amountText
  147. if (tonumber(log.amount) > 0) then
  148. amountText = "+$"..log.amount
  149. term.setTextColor(colors.green)
  150. end
  151. if (tonumber(log.amount) < 0) then
  152. amountText = "-$"..math.abs(log.amount)
  153. term.setTextColor(colors.red)
  154. end
  155. print(amountText)
  156. term.setTextColor(colors.white)
  157. print("Balance resultante: $"..log.balance)
  158. print("Fecha y hora: "..log.time)
  159. if (string.len(log.description) > 0) then
  160. print("Descripción:")
  161. term.setTextColor(specialTextColor)
  162. print(log.description)
  163. else
  164. term.setTextColor(grayedOutColor)
  165. print("Sin descripción")
  166. end
  167.  
  168. drawBackButton()
  169. os.pullEvent("mouse_click")
  170. end
  171.  
  172. function transactionLogScreen(key)
  173. local tempClientData = getClientData()
  174. local backwardsLogs = getTransactionLog(key)
  175. local logs = {}
  176.  
  177. local logCount = #backwardsLogs
  178. for i=0, logCount do
  179. logs[logCount-i] = backwardsLogs[i+1] -- Newest first
  180. end
  181.  
  182. local scrW, scrH = term.getSize()
  183. local y = 3
  184. local x = 1
  185. local w = scrW
  186.  
  187. local first = 0
  188. local logHeight = 3
  189. local max = math.floor((scrH-y)/logHeight)
  190.  
  191. local upButtonY = y-2
  192. local downButtonY = y+max*logHeight
  193.  
  194. while true do
  195. term.setBackgroundColor(backgroundColor)
  196. term.clear()
  197.  
  198. local backx, backy, backw = drawBackButton()
  199.  
  200. term.setCursorPos(backw+5, scrH)
  201. term.setBackgroundColor(backgroundColor)
  202. term.setTextColor(grayedOutColor)
  203. term.write("Haz click para ver detalle")
  204.  
  205. if (first > 0) then
  206. term.setCursorPos(scrW/2-3,upButtonY)
  207. term.setTextColor(buttonTextColor)
  208. term.setBackgroundColor(buttonColor)
  209. term.write(" "..string.char(30).." ")
  210. end
  211.  
  212. local totalLogs = 0
  213. local i = 0
  214. local buttons = {}
  215. for k, v in ipairs(logs) do
  216. if (totalLogs >= first and totalLogs < first+max) then
  217. local order = #logs-k+1
  218. local amountText
  219. if (tonumber(v.amount) > 0) then
  220. amountText = "+$"..v.amount
  221. end
  222. if (tonumber(v.amount) < 0) then
  223. amountText = "-$"..math.abs(v.amount)
  224. end
  225. term.setBackgroundColor(backgroundColor)
  226.  
  227. local text = "#"..order.." "..v.description
  228. term.setCursorPos(1,y+i)
  229. term.setTextColor(colors.lightGray)
  230. term.write("#"..order.." ")
  231. term.setTextColor(specialTextColor)
  232. local desc = v.description
  233. if (string.len(desc) > scrW-8) then
  234. desc = string.sub(v.description, 1, scrW-8).."..."
  235. end
  236. print(desc)
  237. buttons[y+i] = k
  238. buttons[y+i+1] = k
  239.  
  240. local totalText = tempClientData[v.other].name.." "..amountText.." ($"..v.balance..") "..v.time
  241.  
  242. term.setTextColor(colors.white)
  243. term.write(tempClientData[v.other].name.." ")
  244. if (tonumber(v.amount) >= 0) then
  245. term.setTextColor(colors.green)
  246. else
  247. term.setTextColor(colors.red)
  248. end
  249. term.write(amountText)
  250. term.setTextColor(colors.white)
  251. term.write(" ($"..v.balance..") ")
  252. term.setTextColor(colors.lightGray)
  253. term.write(v.time)
  254.  
  255. i = i+logHeight
  256. end
  257. totalLogs = totalLogs+1
  258. end
  259.  
  260. if (first+max < totalLogs) then
  261. term.setCursorPos(scrW/2-3,downButtonY)
  262. term.setTextColor(buttonTextColor)
  263. term.setBackgroundColor(buttonColor)
  264. term.write(" "..string.char(31).." ")
  265. end
  266.  
  267. local event, button, cx, cy = os.pullEvent("mouse_click")
  268. if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == upButtonY) then
  269. if (first > 0) then
  270. first = first-max
  271. end
  272. end
  273. if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == downButtonY) then
  274. if (first+max < totalLogs) then
  275. first = first+max
  276. end
  277. end
  278. if (cx >= x and cx <= x+w and cy >= y) then
  279. if buttons[cy] ~= nil then
  280. transactionInfoScreen(logs[buttons[cy]])
  281. end
  282. end
  283. if (cx >= backx and cx <= backx+backw and cy == backy) then
  284. return nil
  285. end
  286. end
  287. end
  288.  
  289. function optionMenu(title, options, spacing)
  290. if (spacing == nil) then spacing = 2 end
  291. term.setBackgroundColor(backgroundColor)
  292. term.clear()
  293. local buttons = {}
  294. local scrW, scrH = term.getSize()
  295. local w = 30
  296. local x = scrW/2-w/2+1
  297. local y = math.floor(scrH/2-(#options+1)*spacing/2)+2
  298. local i = 0
  299. term.setCursorPos(scrW/2-string.len(title)/2+1, y+i)
  300. term.setTextColor(specialTextColor)
  301. term.write(title)
  302. i = i+spacing
  303.  
  304. term.setBackgroundColor(buttonColor)
  305. term.setTextColor(buttonTextColor)
  306. for k, v in ipairs(options) do
  307. term.setCursorPos(x, y+i)
  308. term.write(string.rep(" ", w))
  309. term.setCursorPos(scrW/2-string.len(v.text)/2+1, y+i)
  310. term.write(v.text)
  311. buttons[y+i] = v.option
  312. i = i+spacing
  313. end
  314.  
  315. while true do
  316. local event, button, cx, cy = os.pullEvent("mouse_click")
  317. if (cx >= x and cx <= x+w and cy >= y and cy < y+(#options+1)*spacing) then
  318. if buttons[cy] ~= nil then
  319. return buttons[cy]
  320. end
  321. end
  322. end
  323. end
  324.  
  325. local function drawSteps(steps, currentStep)
  326. term.setCursorPos(1, 2)
  327. term.setBackgroundColor(backgroundColor)
  328. term.clear()
  329. for k, v in ipairs(steps) do
  330. term.setTextColor(grayedOutColor)
  331. if (k == currentStep) then
  332. term.setTextColor(specialTextColor)
  333. term.write(" > "..k..". ")
  334. else
  335. term.write(" "..k..". ")
  336. end
  337. print(v)
  338. end
  339. end
  340.  
  341. local function startRead(maxLength)
  342. local scrW, scrH = term.getSize()
  343. readingPosX, readingPosY = term.getCursorPos()
  344. reading = true
  345. readingString = ""
  346. readingMax = maxLength
  347. term.setBackgroundColor(colors.white)
  348. term.setTextColor(colors.black)
  349. term.setCursorPos(readingPosX, readingPosY)
  350. term.write(string.rep(" ", scrW-2))
  351. term.setCursorBlink(true)
  352. end
  353.  
  354. local function processChar(char)
  355. term.setBackgroundColor(colors.white)
  356. term.setTextColor(colors.black)
  357. term.setCursorPos(readingPosX, readingPosY)
  358. readingString = readingString..char
  359. term.write(readingString)
  360. end
  361.  
  362. local function processKey(key)
  363. if (key == keys.backspace) then
  364. readingString = string.sub(readingString, 1, string.len(readingString)-1)
  365. term.setBackgroundColor(colors.white)
  366. term.setCursorPos(readingPosX+string.len(readingString), readingPosY)
  367. term.write(" ")
  368. term.setCursorPos(readingPosX+string.len(readingString), readingPosY)
  369. elseif (key == keys.enter) then
  370. term.setCursorBlink(false)
  371. return readingString
  372. end
  373. return nil
  374. end
  375.  
  376. local function cancelableRead(maxLength)
  377. startRead(maxLength)
  378. local backx, backy, backw = drawBackButton()
  379. term.setCursorPos(readingPosX, readingPosY)
  380. local input = nil
  381. while input == nil do
  382. local event, a, b, c = os.pullEvent()
  383. if (event == "char") then
  384. processChar(a)
  385. elseif (event == "key") then
  386. input = processKey(a)
  387. elseif (event == "mouse_click") then
  388. local cx = b
  389. local cy = c
  390. if (cx >= backx and cx <= backx+backw and cy == backy) then
  391. term.setCursorBlink(false)
  392. return nil
  393. end
  394. end
  395. end
  396. term.setCursorBlink(false)
  397. return input
  398. end
  399.  
  400. function showBalance(key)
  401. local tempClientData = getClientData()
  402. term.setBackgroundColor(backgroundColor)
  403. term.clear()
  404. local scrW, scrH = term.getSize()
  405. local text = "Tu balance:"
  406. term.setTextColor(specialTextColor)
  407. term.setCursorPos(scrW/2-string.len(text)/2,scrH/2-1)
  408. term.write(text)
  409. text = "$"..tempClientData[key].balance
  410. term.setCursorPos(scrW/2-string.len(text)/2,scrH/2+1)
  411. term.write(text)
  412. drawBackButton()
  413.  
  414. while true do
  415. local event = os.pullEvent()
  416. if (event == "mouse_click" or event == "key") then
  417. break
  418. end
  419. end
  420.  
  421. end
  422.  
  423. function selectAccountScreen(steps, currentStep, disabledAccount)
  424. local tempClientData = getClientData()
  425.  
  426. local scrW, scrH = term.getSize()
  427. local x, y, w, h = scrW/4, #steps+4, scrW/2, scrH-5
  428.  
  429. local backx, backy, backw = drawBackButton()
  430.  
  431. local first = 0
  432. local max = scrH-y-2
  433.  
  434. local upButtonY = y-2
  435. local downButtonY = y+1+max
  436.  
  437. while true do
  438. drawSteps(steps, currentStep)
  439. drawBackButton()
  440. local i = 0
  441. local buttons = {}
  442.  
  443. if (first > 0) then
  444. term.setCursorPos(scrW/2-3,upButtonY)
  445. term.setTextColor(buttonTextColor)
  446. term.setBackgroundColor(buttonColor)
  447. term.write(" "..string.char(30).." ")
  448. end
  449.  
  450. local totalAccounts = 0
  451. local showAccounts = 0
  452. for k, v in pairs(tempClientData) do
  453. if (disabledAccount ~= nil and disabledAccount ~= k) then
  454. if (showAccounts >= first and showAccounts < first+max) then
  455. buttons[y+i] = k
  456. term.setCursorPos(x,y+i)
  457. term.setTextColor(contrastColor(tonumber(v.color)))
  458. term.setBackgroundColor(tonumber(v.color))
  459. term.write(string.rep(" ", w))
  460. term.setCursorPos(x+w/2-string.len(v.name)/2-1,y+i)
  461. term.write(v.name)
  462. i = i+1
  463. end
  464. showAccounts = showAccounts+1
  465. end
  466. totalAccounts = totalAccounts+1
  467. end
  468.  
  469. if (first+max < showAccounts) then
  470. term.setCursorPos(scrW/2-3,downButtonY)
  471. term.setTextColor(buttonTextColor)
  472. term.setBackgroundColor(buttonColor)
  473. term.write(" "..string.char(31).." ")
  474. end
  475.  
  476. local event, button, cx, cy = os.pullEvent("mouse_click")
  477. if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == upButtonY) then
  478. if (first > 0) then
  479. first = first-max
  480. end
  481. end
  482. if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == downButtonY) then
  483. if (first+max < showAccounts) then
  484. first = first+max
  485. end
  486. end
  487. if (cx >= x and cx <= x+w and cy >= y and cy < y+i) then
  488. return buttons[cy]
  489. end
  490. if (cx >= backx and cx <= backx+backw and cy == backy) then
  491. return nil
  492. end
  493. end
  494. end
  495.  
  496. function inputNumberScreen(steps, currentStep, max)
  497. drawSteps(steps, currentStep)
  498. local y = #steps+3
  499. term.setCursorPos(1, y)
  500. term.setBackgroundColor(backgroundColor)
  501. term.setTextColor(specialTextColor)
  502. term.write("Ingresa un numero ")
  503. if (max ~= nil and max ~= 0) then
  504. term.write("(máximo "..max..")")
  505. end
  506. term.write(":")
  507. local scrW, scrH = term.getSize()
  508. local number = nil
  509. repeat
  510. term.setCursorPos(2, y+2)
  511. number = cancelableRead()
  512. if (number == nil) then return nil end
  513. until number ~= nil and tonumber(number) <= max
  514. return number
  515. end
  516.  
  517. function inputTextScreen(steps, currentStep, maxLength)
  518. drawSteps(steps, currentStep)
  519. local y = #steps+3
  520. term.setCursorPos(1, y)
  521. term.setBackgroundColor(backgroundColor)
  522. term.setTextColor(specialTextColor)
  523. term.write("Ingresa un texto ")
  524. if (max ~= nil and max ~= 0) then
  525. term.write("(largo máximo "..max..")")
  526. end
  527. term.write(":")
  528. local scrW, scrH = term.getSize()
  529. term.setCursorPos(2, y+2)
  530. return cancelableRead()
  531. end
  532.  
  533. function selectColorScreen(steps, currentStep)
  534. drawSteps(steps, currentStep)
  535. local y = #steps+3
  536. local color = 1
  537. local buttons = {}
  538. local scrW, scrH = term.getSize()
  539. local x = scrW/2-10
  540. local w = 5 --button width
  541. for _x=0, 3 do
  542. buttons[_x] = {}
  543. for _y= 0, 3 do
  544. term.setCursorPos(x+_x*w+1, y+_y)
  545. term.setBackgroundColor(color)
  546. term.setTextColor(contrastColor(color))
  547. term.write("[")
  548. term.write(string.rep(" ", w-2))
  549. term.write("]")
  550. buttons[_x][_y] = color
  551. color = color*2
  552. end
  553. end
  554.  
  555. local backx, backy, backw = drawBackButton()
  556.  
  557. while true do
  558. local event, button, cx, cy = os.pullEvent("mouse_click")
  559. if (cx >= x and cx <= x+w*4 and cy >= y and cy < y+4) then
  560. local _x = math.floor((cx-x)/w)
  561. local _y = math.floor((cy-y))
  562. return buttons[_x][_y]
  563. end
  564. if (cx >= backx and cx <= backx+backw and cy == backy) then
  565. return nil
  566. end
  567. end
  568. end
  569.  
  570. function responseScreen(success, response)
  571. if (success) then
  572. successScreen(response)
  573. else
  574. errorScreen(response)
  575. end
  576. end
  577.  
  578. function errorScreen(message)
  579. term.setBackgroundColor(colors.red)
  580. term.setTextColor(colors.white)
  581. term.clear()
  582. local scrW, scrH = term.getSize()
  583. term.setCursorPos(scrW/2-string.len(message)/2, scrH/2)
  584. term.write(message)
  585. sleep(1.5)
  586. end
  587.  
  588. function successScreen(message)
  589. term.setBackgroundColor(colors.green)
  590. term.setTextColor(colors.white)
  591. term.clear()
  592. local scrW, scrH = term.getSize()
  593. term.setCursorPos(scrW/2-string.len(message)/2, scrH/2)
  594. term.write(message)
  595. sleep(1.5)
  596. end
  597.  
  598. function confirmScreen(message, data)
  599. local y = 2
  600. local scrW, scrH = term.getSize()
  601. term.setBackgroundColor(backgroundColor)
  602. term.clear()
  603. term.setTextColor(specialTextColor)
  604. for k, v in pairs(message) do
  605. term.setCursorPos((scrW-string.len(v))/2, y)
  606. term.write(v)
  607. y = y+1
  608. end
  609. y = y+1
  610. if (data ~= nil) then
  611. for k, v in pairs(data) do
  612. local text = k..": "..v
  613. term.setCursorPos((scrW-string.len(text))/2, y)
  614. term.write(text)
  615. y = y+1
  616. end
  617. y = y+1
  618. end
  619.  
  620. local buttonY = math.min(scrH-3, y)
  621. local buttonW = 20
  622. local buttonX = scrW/2-buttonW/2
  623.  
  624. term.setBackgroundColor(colors.green)
  625. term.setTextColor(colors.black)
  626. term.setCursorPos(buttonX, buttonY)
  627. term.write(string.rep(" ", buttonW))
  628. local text = "Aceptar"
  629. term.setCursorPos(buttonX+buttonW/2-string.len(text)/2, buttonY)
  630. term.write(text)
  631.  
  632. term.setBackgroundColor(colors.red)
  633. term.setCursorPos(buttonX, buttonY+2)
  634. term.write(string.rep(" ", buttonW))
  635. local text = "Cancelar"
  636. term.setCursorPos(buttonX+buttonW/2-string.len(text)/2, buttonY+2)
  637. term.write(text)
  638.  
  639. while true do
  640. local event, button, cx, cy = os.pullEvent("mouse_click")
  641. if (cx >= buttonX and cx <= buttonX+buttonW and cy == buttonY) then
  642. return true
  643. end
  644. if (cx >= buttonX and cx <= buttonX+buttonW and cy == buttonY+2) then
  645. return false
  646. end
  647. end
  648. end
  649.  
  650. function textScreen(message)
  651. local y = 1
  652. local scrW, scrH = term.getSize()
  653. term.setBackgroundColor(backgroundColor)
  654. term.clear()
  655. term.setTextColor(colors.white)
  656. for k, v in pairs(message) do
  657. term.setCursorPos((scrW-string.len(v))/2+1, y)
  658. term.write(v)
  659. y = y+1
  660. end
  661.  
  662. drawBackButton()
  663.  
  664. os.pullEvent("mouse_click")
  665. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement