Advertisement
Marum

Mermegold V4 Bank API

Mar 25th, 2025 (edited)
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.41 KB | None | 0 0
  1. -- Bank API V4
  2.  
  3. local backgroundColor = colors.gray
  4. local buttonShadowColor = colors.brown
  5. local buttonTextColor = colors.black
  6. local grayedOutColor = colors.lightGray
  7. local specialTextColor = colors.yellow
  8.  
  9. local buttonColor = colors.yellow
  10. local secondaryButtonColor = colors.orange
  11.  
  12. local acceptButtonColor = colors.lime
  13. local acceptSecondaryColor = colors.green
  14.  
  15. local cancelButtonColor = colors.red
  16. local cancelSecondaryColor = colors.brown
  17.  
  18. local bankServerID = 0
  19. local readingPosX = 0
  20. local readingPosY = 0
  21. local readingString = ""
  22. local reading = false
  23. local readingMax = 10
  24.  
  25. local function contrastColor(color)
  26. if (color == colors.white) then return colors.black end
  27. if (color == colors.orange) then return colors.black end
  28. if (color == colors.magenta) then return colors.black end
  29. if (color == colors.lightBlue) then return colors.black end
  30. if (color == colors.yellow) then return colors.black end
  31. if (color == colors.lime) then return colors.black end
  32. if (color == colors.pink) then return colors.black end
  33. if (color == colors.gray) then return colors.black end
  34. if (color == colors.lightGray) then return colors.black end
  35. if (color == colors.cyan) then return colors.white end
  36. if (color == colors.purple) then return colors.white end
  37. if (color == colors.blue) then return colors.white end
  38. if (color == colors.brown) then return colors.white end
  39. if (color == colors.green) then return colors.black end
  40. if (color == colors.red) then return colors.black end
  41. if (color == colors.black) then return colors.white end
  42. return colors.black
  43. end
  44.  
  45. local serverData = nil
  46. local lang = "en"
  47. local localization = {
  48. en={
  49. back = "Back",
  50. continue = "Continue",
  51. sender = "Sender",
  52. recipient = "Recipient",
  53. amount = "Amount",
  54. resulting_balance = "Resulting balance",
  55. date_and_time = "Date and time",
  56. description = "Description",
  57. no_description = "No description",
  58. click_to_expand = "Click entry for more info",
  59. balance = "Balance",
  60. name = "Name",
  61. key = "Key",
  62. deleted = "<deleted>",
  63. accept = "Accept",
  64. cancel = "Cancel",
  65. input_text = "Input text",
  66. max_length = "Max length",
  67. input_number = "Input number",
  68. max = "Max",
  69. product = "Product",
  70. quantity = "Quantity",
  71. price = "Price",
  72. item_name = "Item",
  73. invalid_value = "Invalid value",
  74. no_accounts = "There are no accounts at the moment"
  75. },
  76. es={
  77. back = "Volver",
  78. continue = "Continuar",
  79. sender = "Remitente",
  80. recipient = "Destinatario",
  81. amount = "Monto",
  82. resulting_balance = "Balance resultante",
  83. date_and_time = "Fecha y hora",
  84. description = "Descripcion",
  85. no_description = "Sin descripcion",
  86. click_to_expand = "Toca una para mas info",
  87. balance = "Balance",
  88. name = "Nombre",
  89. key = "Clave",
  90. deleted = "<borrada>",
  91. accept = "Aceptar",
  92. cancel = "Cancelar",
  93. input_text = "Ingresa un texto",
  94. max_length = "largo maximo",
  95. input_number = "Ingresa un numero",
  96. max = "Maximo",
  97. product = "Producto",
  98. quantity = "Cantidad",
  99. price = "Precio",
  100. item_name = "Articulo",
  101. invalid_value = "Valor invalido",
  102. no_accounts = "No hay cuentas por el momento"
  103. },
  104. de={
  105. back = "Zurueck",
  106. continue = "Weiter",
  107. sender = "Sender",
  108. recipient = "Empfaenger",
  109. amount = "Betrag",
  110. resulting_balance = "Verbleibender Kontostandt",
  111. date_and_time = "Datum und Zeit",
  112. description = "Beschreibung",
  113. no_description = "Keine Beschreibung",
  114. click_to_expand = "Klicke führ mehr Infos",
  115. balance = "Kontostandt",
  116. name = "Name",
  117. key = "Schluessel",
  118. deleted = "<geloescht>",
  119. accept = "Aktzeptieren",
  120. cancel = "Abbrechen",
  121. input_text = "Eingabe Text",
  122. max_length = "Maximale Laenge",
  123. input_number = "Zahl eingeben",
  124. max = "Max",
  125. product = "Produkt",
  126. quantity = "Anzahl",
  127. price = "Preis",
  128. item_name = "Artikel",
  129. invalid_value = "Ungültiger Wert",
  130. no_accounts = "Es gibt im Moment keine Konten"
  131. }
  132. }
  133.  
  134. function getServerData()
  135. print("Connecting to server...")
  136. local message = {
  137. action = "getServerData"
  138. }
  139. while (true) do
  140. rednet.broadcast(message, "mermegold")
  141. local sender, message = rednet.receive("mermegold", 3)
  142. if (message ~= nil and os.computerID() ~= sender and message.response ~= nil) then
  143. bankServerID = sender
  144. print("Successfully connected to server [#"..sender.."]")
  145. lang = message.response.lang
  146. serverData = message.response
  147. return message.response
  148. end
  149. end
  150. end
  151.  
  152. function getClientData()
  153. local message = {
  154. action = "getClientData"
  155. }
  156. while(true) do
  157. rednet.send(bankServerID, message, "mermegold")
  158. local sender, message = rednet.receive("mermegold", 3)
  159. if (message ~= nil and os.computerID() ~= sender and message.response ~= nil) then
  160. return message.response
  161. end
  162. end
  163. end
  164.  
  165. local function drawBox(background, foreground, x, y, w, h)
  166. term.setBackgroundColor(background)
  167. term.setTextColor(foreground)
  168. -- Top
  169. term.setCursorPos(x, y)
  170. term.write(string.char(151))
  171. term.write(string.rep(string.char(131), w-2))
  172. term.setBackgroundColor(foreground)
  173. term.setTextColor(background)
  174. term.write(string.char(148))
  175. -- Left
  176. term.setBackgroundColor(background)
  177. term.setTextColor(foreground)
  178. for i=y+1, y+h-2 do
  179. term.setCursorPos(x, i)
  180. term.write(string.char(149))
  181. end
  182. -- Right
  183. term.setBackgroundColor(foreground)
  184. term.setTextColor(background)
  185. for i=y+1, y+h-2 do
  186. term.setCursorPos(x+w-1, i)
  187. term.write(string.char(149))
  188. end
  189. -- Bottom
  190. term.setCursorPos(x, y+h-1)
  191. term.write(string.char(138))
  192. term.write(string.rep(string.char(143), w-2))
  193. term.setBackgroundColor(foreground)
  194. term.setTextColor(background)
  195. term.write(string.char(133))
  196. end
  197.  
  198. local function drawBackground()
  199. local scrW, scrH = term.getSize()
  200. term.setBackgroundColor(backgroundColor)
  201. term.clear()
  202. end
  203.  
  204. function drawButton(primary, secondary, textColor, x, y, w, text)
  205. local ch = string.char(127)
  206. term.setCursorPos(x, y)
  207. term.setBackgroundColor(secondary)
  208. term.setTextColor(primary)
  209. term.write(" ")
  210. term.write(ch)
  211. term.setBackgroundColor(primary)
  212. term.setTextColor(secondary)
  213. term.write(ch)
  214. term.setBackgroundColor(primary)
  215. term.write(string.rep(" ", w-6))
  216. term.setBackgroundColor(primary)
  217. term.setTextColor(secondary)
  218. term.write(ch)
  219. term.setBackgroundColor(secondary)
  220. term.setTextColor(primary)
  221. term.write(ch)
  222. term.write(" ")
  223.  
  224. -- Shadow
  225. term.setBackgroundColor(backgroundColor)
  226. term.setTextColor(colors.black)
  227. term.write(string.char(148))
  228. local scrW, scrH = term.getSize()
  229. if (y < scrH) then
  230. term.setCursorPos(x, y+1)
  231. term.write(string.char(130))
  232. term.write(string.rep(string.char(131), w-1))
  233. term.write(string.char(129))
  234. end
  235.  
  236. term.setBackgroundColor(primary)
  237. term.setTextColor(textColor)
  238.  
  239. term.setCursorPos(x+w/2-string.len(text)/2, y)
  240. term.write(text)
  241.  
  242. return {x=x, y=y, w=w}
  243. end
  244.  
  245. function mouseInButton(button, mousex, mousey)
  246. return (mousex >= button.x and mousex <= button.x+button.w and button.y == mousey)
  247. end
  248.  
  249. function drawBackButton()
  250. local scrW, scrH = term.getSize()
  251. local text = localization[lang].back
  252. local x = 2
  253. local y = scrH-1
  254.  
  255. local ch = string.char(127)
  256.  
  257. local width = string.len(text)
  258.  
  259. return drawButton(cancelButtonColor, cancelSecondaryColor, buttonTextColor, x, y, width+6, text)
  260. end
  261.  
  262. function drawContinueButton()
  263. local scrW, scrH = term.getSize()
  264. local text = localization[lang].continue
  265. local x = 2
  266. local y = scrH-1
  267.  
  268. local ch = string.char(127)
  269.  
  270. local width = string.len(text)
  271.  
  272. return drawButton(acceptButtonColor, acceptSecondaryColor, buttonTextColor, scrW-width-7, y, width+6, text)
  273. end
  274.  
  275. function drawAcceptButton()
  276. local scrW, scrH = term.getSize()
  277. local text = localization[lang].accept
  278. local x = 2
  279. local y = scrH-1
  280.  
  281. local ch = string.char(127)
  282.  
  283. local width = string.len(text)
  284.  
  285. return drawButton(acceptButtonColor, acceptSecondaryColor, buttonTextColor, scrW-width-7, y, width+6, text)
  286. end
  287.  
  288. function transaction(from, to, amount, description)
  289. --local success, response = transaction(from, to, amount, description)
  290. local message = {
  291. action = "transaction",
  292. from = from,
  293. to = to,
  294. amount = amount,
  295. description = description
  296. }
  297. rednet.send(bankServerID, message, "mermegold")
  298. local sender, message = rednet.receive("mermegold")
  299. return message.success, message.response
  300. end
  301.  
  302. function deposit(key, amount)
  303. local message = {
  304. action = "deposit",
  305. key = key,
  306. amount = amount
  307. }
  308. rednet.send(bankServerID, message, "mermegold")
  309. local sender, message = rednet.receive("mermegold")
  310. return message.success, message.response
  311. end
  312.  
  313. function withdraw(key, amount)
  314. local message = {
  315. action = "withdraw",
  316. key = key,
  317. amount = amount
  318. }
  319. rednet.send(bankServerID, message, "mermegold")
  320. local sender, message = rednet.receive("mermegold")
  321. return message.success, message.response
  322. end
  323.  
  324. function newAccount(name, balance, color)
  325. local message = {
  326. action = "new",
  327. name = name,
  328. balance = balance,
  329. color = color
  330. }
  331. rednet.send(bankServerID, message, "mermegold")
  332. local sender, message = rednet.receive("mermegold")
  333. return message.success, message.response
  334. end
  335.  
  336. function deleteAccount(key)
  337. local message = {
  338. action = "delete",
  339. key = key
  340. }
  341. rednet.send(bankServerID, message, "mermegold")
  342. local sender, message = rednet.receive("mermegold")
  343. return message.success, message.response
  344. end
  345.  
  346. function getTransactionLog(key)
  347. local message = {
  348. action = "getTransactionLog",
  349. key = key
  350. }
  351. rednet.send(bankServerID, message, "mermegold")
  352. local sender, message = rednet.receive("mermegold")
  353. if (sender == nil) then
  354. print(text_error_noconnection)
  355. end
  356. return message.response
  357. end
  358.  
  359. function transactionInfoScreen(log)
  360. local tempClientData = getClientData()
  361.  
  362. drawBackground()
  363. term.setCursorPos(1,2)
  364. term.setTextColor(colors.white)
  365. local amountText
  366. if (tonumber(log.amount) > 0) then
  367. print(localization[lang].sender..": "..tempClientData[log.other].name)
  368. term.write(localization[lang].amount..": ")
  369. amountText = "+$"..log.amount
  370. term.setTextColor(colors.green)
  371. end
  372. if (tonumber(log.amount) < 0) then
  373. print(localization[lang].recipient..": "..tempClientData[log.other].name)
  374. term.write(localization[lang].amount..": ")
  375. amountText = "-$"..math.abs(log.amount)
  376. term.setTextColor(colors.red)
  377. end
  378. print(amountText)
  379. term.setTextColor(colors.white)
  380. print(localization[lang].resulting_balance..": $"..log.balance)
  381. print(localization[lang].date_and_time..": "..log.time)
  382. if (string.len(log.description) > 0) then
  383. print(localization[lang].description..":")
  384. term.setTextColor(specialTextColor)
  385. print(log.description)
  386. else
  387. term.setTextColor(grayedOutColor)
  388. print(localization[lang].no_description)
  389. end
  390.  
  391. drawBackButton()
  392. os.pullEvent("mouse_click")
  393. end
  394.  
  395. function transactionLogScreen(key)
  396. local tempClientData = getClientData()
  397. local backwardsLogs = getTransactionLog(key)
  398. local logs = {}
  399.  
  400. local logCount = #backwardsLogs
  401. for i=0, logCount do
  402. logs[logCount-i] = backwardsLogs[i+1] -- Newest first
  403. end
  404.  
  405. local scrW, scrH = term.getSize()
  406. local y = 2
  407. local floor = 1
  408. local x = 1
  409. local w = scrW
  410.  
  411. if (pocket) then
  412. floor = 2
  413. end
  414.  
  415. local first = 0
  416. local logHeight = 2
  417. local max = math.floor((scrH-floor-logHeight)/logHeight)
  418.  
  419. local scrollButtonY = scrH-1
  420. local scrollButtonCenterX = math.floor(scrW/2)+10
  421.  
  422. if (pocket) then
  423. scrollButtonY = scrH-3
  424. scrollButtonCenterX = math.floor(scrW/2)+1
  425. end
  426.  
  427. while true do
  428. drawBackground()
  429.  
  430. local prevPage = drawButton(buttonColor, secondaryButtonColor, buttonTextColor,scrollButtonCenterX-13, scrollButtonY, 7, string.char(27))
  431. if (first <= 0) then
  432. drawButton(colors.lightGray, colors.gray, colors.gray, scrollButtonCenterX-13, scrollButtonY, 7, string.char(27))
  433. end
  434.  
  435. local totalLogs = 0
  436. local i = 0
  437. local buttons = {}
  438. for k, v in ipairs(logs) do
  439. if (totalLogs >= first and totalLogs < first+max) then
  440. local order = #logs-k+1
  441. local amountText
  442. if (tonumber(v.amount) > 0) then
  443. amountText = "+$"..v.amount
  444. end
  445. if (tonumber(v.amount) < 0) then
  446. amountText = "-$"..math.abs(v.amount)
  447. end
  448.  
  449. local text = "#"..order.." "..v.description
  450.  
  451. -- box
  452. if (i > 0) then
  453. term.setCursorPos(1,y+i-1)
  454. term.setBackgroundColor(colors.white)
  455. term.setTextColor(colors.lightGray)
  456. print(string.rep(string.char(140), scrW))
  457. term.setBackgroundColor(colors.white)
  458. print(string.rep(" ", scrW))
  459. else
  460. term.setCursorPos(1,y+i)
  461. term.setBackgroundColor(colors.white)
  462. print(string.rep(" ", scrW))
  463. end
  464.  
  465. -- content
  466. term.setCursorPos(1,y+i)
  467. buttons[y+i] = k
  468.  
  469. local name
  470. if (tempClientData[v.other] ~= nil) then
  471. name = tempClientData[v.other].name
  472. else
  473. name = localization[lang].deleted
  474. end
  475.  
  476. term.setTextColor(colors.lightGray)
  477. term.write("#"..order.." ")
  478.  
  479. term.setTextColor(colors.black)
  480. term.write(name.." ")
  481.  
  482. term.setCursorPos(scrW*0.3, y+i)
  483. if (pocket) then
  484. term.setCursorPos(scrW*0.5, y+i)
  485. end
  486. if (tonumber(v.amount) >= 0) then
  487. term.setTextColor(colors.green)
  488. else
  489. term.setTextColor(colors.red)
  490. end
  491. term.write(amountText)
  492.  
  493. if (pocket == nil) then
  494. term.setCursorPos(scrW*0.5, y+i)
  495. term.setTextColor(colors.black)
  496. term.write(" ($"..v.balance..")")
  497. end
  498.  
  499. term.setTextColor(colors.lightGray)
  500. local time = v.time
  501. if (pocket) then
  502. time = string.sub(v.time, 1, 5)
  503. end
  504. term.setCursorPos(scrW-string.len(time),y+i)
  505. term.write(" "..time)
  506.  
  507. i = i+logHeight
  508. end
  509. totalLogs = totalLogs+1
  510. end
  511.  
  512. local nextPage = drawButton(buttonColor, secondaryButtonColor, buttonTextColor, scrollButtonCenterX+5, scrollButtonY, 7, string.char(26))
  513. if (first+max >= totalLogs) then
  514. drawButton(colors.lightGray, colors.gray, colors.gray, scrollButtonCenterX+5, scrollButtonY, 7, string.char(26))
  515. end
  516.  
  517. -- page indicator
  518. local pages = math.ceil(totalLogs/max)
  519. local pageText = tostring((first/max)+1).."/"..pages
  520. term.setCursorPos(scrollButtonCenterX-string.len(pageText)/2, scrollButtonY)
  521. term.setBackgroundColor(backgroundColor)
  522. term.setTextColor(colors.orange)
  523. term.write(pageText)
  524.  
  525. local backButton = drawBackButton()
  526.  
  527. term.setCursorPos(scrW/2-string.len(localization[lang].click_to_expand)/2+1, 1)
  528.  
  529. term.setBackgroundColor(backgroundColor)
  530. term.setTextColor(grayedOutColor)
  531. term.write(localization[lang].click_to_expand)
  532.  
  533. --local event, button, cx, cy = os.pullEvent("mouse_click")
  534.  
  535. local eventData = {os.pullEvent()}
  536. local event = eventData[1]
  537.  
  538. if event == "mouse_click" then
  539. local cx = eventData[3]
  540. local cy = eventData[4]
  541. if (mouseInButton(prevPage, cx, cy)) then
  542. if (first > 0) then
  543. first = first-max
  544. end
  545. end
  546. if (mouseInButton(nextPage, cx, cy)) then
  547. if (first+max < totalLogs) then
  548. first = first+max
  549. end
  550. end
  551. if (cx >= x and cx <= x+w and cy >= y) then
  552. if buttons[cy] ~= nil then
  553. transactionInfoScreen(logs[buttons[cy]])
  554. end
  555. end
  556. if (mouseInButton(backButton, cx, cy)) then
  557. return nil
  558. end
  559. elseif event == "mouse_scroll" then
  560. local scroll = eventData[2]
  561. if (scroll < 0) then
  562. if (first > 0) then
  563. first = first-max
  564. end
  565. else
  566. if (first+max < totalLogs) then
  567. first = first+max
  568. end
  569. end
  570. end
  571. end
  572. end
  573.  
  574. function optionMenu(title, options, spacing, width)
  575. if (spacing == nil) then spacing = 2 end
  576. if (width == nil) then width = 30 end
  577. drawBackground()
  578. local buttons = {}
  579. local scrW, scrH = term.getSize()
  580. local w = width
  581. local x = scrW/2-w/2+1
  582. local y = math.floor(scrH/2-(#options+1)*spacing/2)+2
  583. local i = 0
  584. term.setCursorPos(scrW/2-string.len(title)/2+1, y+i)
  585. term.setTextColor(specialTextColor)
  586. term.write(title)
  587. i = i+spacing
  588.  
  589. term.setBackgroundColor(buttonColor)
  590. term.setTextColor(buttonTextColor)
  591. for k, v in ipairs(options) do
  592. drawButton(buttonColor, secondaryButtonColor, buttonTextColor, x, y+i, w, v.text)
  593.  
  594. buttons[y+i] = v.option
  595. i = i+spacing
  596. end
  597.  
  598. while true do
  599. local event, button, cx, cy = os.pullEvent("mouse_click")
  600. if (cx >= x-1 and cx <= x+width-1 and cy >= y and cy < y+(#options+1)*spacing) then
  601. if buttons[cy] ~= nil then
  602. return buttons[cy]
  603. end
  604. end
  605. end
  606. end
  607.  
  608. local function drawSteps(steps, currentStep)
  609. term.setCursorPos(1, 2)
  610. drawBackground()
  611. local stepCount = #steps
  612. if (stepCount == 1) then
  613. term.setTextColor(specialTextColor)
  614. print(steps[1])
  615. else
  616. for k, v in ipairs(steps) do
  617. term.setTextColor(grayedOutColor)
  618. if (k == currentStep) then
  619. term.setTextColor(specialTextColor)
  620. term.write(string.char(16).." "..k..". ")
  621. else
  622. term.write(" "..k..". ")
  623. end
  624. print(v)
  625. end
  626. end
  627. end
  628.  
  629. local function startRead(maxLength)
  630. local scrW, scrH = term.getSize()
  631. readingPosX, readingPosY = term.getCursorPos()
  632. reading = true
  633. readingString = ""
  634. readingMax = maxLength
  635. term.setBackgroundColor(colors.white)
  636. term.setTextColor(colors.black)
  637. term.setCursorPos(readingPosX, readingPosY)
  638. term.write(string.rep(" ", scrW-2))
  639. term.setCursorBlink(true)
  640. end
  641.  
  642. local function processChar(char)
  643. term.setBackgroundColor(colors.white)
  644. term.setTextColor(colors.black)
  645. term.setCursorPos(readingPosX, readingPosY)
  646. readingString = readingString..char
  647. term.write(readingString)
  648. end
  649.  
  650. local function processKey(key)
  651. if (key == keys.backspace) then
  652. readingString = string.sub(readingString, 1, string.len(readingString)-1)
  653. term.setBackgroundColor(colors.white)
  654. term.setCursorPos(readingPosX+string.len(readingString), readingPosY)
  655. term.write(" ")
  656. term.setCursorPos(readingPosX+string.len(readingString), readingPosY)
  657. elseif (key == keys.enter) then
  658. term.setCursorBlink(false)
  659. return readingString
  660. end
  661. return nil
  662. end
  663.  
  664. local function cancelableRead(maxLength, secondaryButtonX)
  665. startRead(maxLength)
  666. local back = drawBackButton()
  667. local accept = drawAcceptButton()
  668. term.setCursorPos(readingPosX, readingPosY)
  669. local input = nil
  670. while input == nil do
  671. local event, a, b, c = os.pullEvent()
  672. if (event == "char") then
  673. processChar(a)
  674. elseif (event == "key") then
  675. input = processKey(a)
  676. elseif (event == "mouse_click") then
  677. local cx = b
  678. local cy = c
  679. if (mouseInButton(back, cx, cy)) then
  680. term.setCursorBlink(false)
  681. return nil
  682. elseif (mouseInButton(accept, cx, cy)) then
  683. term.setCursorBlink(false)
  684. input = readingString
  685. break
  686. elseif (secondaryButtonX ~= nil) then
  687. if (cy == back.y and cx >= secondaryButtonX) then
  688. return true
  689. end
  690. end
  691. end
  692. end
  693. term.setCursorBlink(false)
  694. if (readingMax > 0 and string.len(input) > readingMax) then
  695. input = string.sub(input, 1, readingMax)
  696. end
  697. return input
  698. end
  699.  
  700. function showBalance(key)
  701. local tempClientData = getClientData()
  702. drawBackground()
  703. local scrW, scrH = term.getSize()
  704.  
  705. local text = localization[lang].balance..":"
  706. term.setTextColor(specialTextColor)
  707. term.setCursorPos(scrW/2-string.len(text)/2+2,scrH/2-3)
  708. term.write(text)
  709. local itemMax = math.floor(tempClientData[key].balance/serverData.valueMultiplier)
  710.  
  711. text = "$"..tempClientData[key].balance
  712. drawBox(backgroundColor, colors.lightGray, 4, scrH/2-1, scrW-6, 5)
  713. term.setCursorPos(scrW/2-string.len(text)/2+1,scrH/2)
  714. term.setBackgroundColor(backgroundColor)
  715. term.setTextColor(specialTextColor)
  716. term.write(text)
  717.  
  718. text = itemMax.." "..serverData.currency[1].plural[lang]
  719. term.setCursorPos(scrW/2-string.len(text)/2+1,scrH/2+2)
  720. term.write(text)
  721.  
  722. drawBackButton()
  723.  
  724. while true do
  725. local event = os.pullEvent()
  726. if (event == "mouse_click" or event == "key") then
  727. break
  728. end
  729. end
  730. end
  731.  
  732. function selectAccountScreen(steps, currentStep, disabledAccount, overrideClientData)
  733. local tempClientData
  734.  
  735. if (overrideClientData == nil) then
  736. tempClientData = getClientData()
  737. else
  738. tempClientData = overrideClientData
  739. end
  740.  
  741. local clientCount = 0
  742. for k, v in pairs(tempClientData) do
  743. clientCount = clientCount+1
  744. end
  745.  
  746. if (clientCount == 0) then
  747. errorScreen(localization[lang].no_accounts)
  748. return nil
  749. end
  750.  
  751. local scrW, scrH = term.getSize()
  752. local x, y, w, h = scrW/4, #steps+4, scrW/2+2, scrH-5
  753.  
  754. local back = drawBackButton()
  755.  
  756. local first = 0
  757. local max = scrH-y-2
  758.  
  759. local upButtonY = y-2
  760. local downButtonY = y+1+max
  761.  
  762. while true do
  763. drawSteps(steps, currentStep)
  764. drawBackButton()
  765. local i = 0
  766. local buttons = {}
  767.  
  768. if (first > 0) then
  769. term.setCursorPos(scrW/2-3,upButtonY)
  770. term.setTextColor(buttonTextColor)
  771. term.setBackgroundColor(buttonColor)
  772. term.write(" "..string.char(24).." ")
  773. end
  774.  
  775. local totalAccounts = 0
  776. local showAccounts = 0
  777. for k, v in pairs(tempClientData) do
  778. if (disabledAccount ~= nil and disabledAccount ~= k) then
  779. if (totalAccounts >= first and showAccounts < first+max) then
  780. buttons[y+i] = k
  781. term.setCursorPos(x,y+i)
  782. term.setTextColor(contrastColor(tonumber(v.color)))
  783. term.setBackgroundColor(tonumber(v.color))
  784. term.write(string.rep(" ", w))
  785. term.setCursorPos(x+w/2-string.len(v.name)/2-1,y+i)
  786. term.write(v.name)
  787. i = i+1
  788. end
  789. showAccounts = showAccounts+1
  790. end
  791. totalAccounts = totalAccounts+1
  792. end
  793.  
  794. if (first+max < showAccounts) then
  795. term.setCursorPos(scrW/2-3,downButtonY)
  796. term.setTextColor(buttonTextColor)
  797. term.setBackgroundColor(buttonColor)
  798. term.write(" "..string.char(25).." ")
  799. end
  800.  
  801. local event, button, cx, cy = os.pullEvent("mouse_click")
  802. if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == upButtonY) then
  803. if (first > 0) then
  804. first = first-max
  805. end
  806. end
  807. if (cx >= scrW/2-3 and cx < scrW/2+3 and cy == downButtonY) then
  808. if (first+max < showAccounts) then
  809. first = first+max
  810. end
  811. end
  812. if (cx >= x and cx <= x+w and cy >= y and cy < y+i) then
  813. return buttons[cy]
  814. end
  815. if (mouseInButton(back, cx, cy)) then
  816. return nil
  817. end
  818. end
  819. end
  820.  
  821. function inputNumberScreen(steps, currentStep, max, maxString)
  822. if (max == nil) then max = 0 end
  823. local y = #steps+3
  824. while true do
  825. drawSteps(steps, currentStep)
  826.  
  827. term.setCursorPos(1, y)
  828. term.setBackgroundColor(backgroundColor)
  829. term.setTextColor(specialTextColor)
  830. local text = localization[lang].input_number
  831. if (maxString ~= nil and maxString ~= "") then
  832. text = text.." ("..localization[lang].max.." "..maxString..")"
  833. elseif (max ~= nil and max ~= 0) then
  834. text = text.." ("..localization[lang].max.." $"..max..")"
  835. end
  836. text = text..":"
  837. print(text)
  838. local scrW, scrH = term.getSize()
  839.  
  840. term.setCursorPos(2, y+2)
  841.  
  842. local result = cancelableRead(max)
  843.  
  844. if (result == nil) then
  845. return nil
  846. end
  847.  
  848. local numberResult = tonumber(result)
  849. if (numberResult == nil or numberResult <= 0 or numberResult%1 ~= 0) then
  850. errorScreen(localization[lang].invalid_value)
  851. else
  852. return result
  853. end
  854. end
  855. end
  856.  
  857. function inputTextScreen(steps, currentStep, maxLength)
  858. if (maxLength == nil) then maxLength = 0 end
  859. drawSteps(steps, currentStep)
  860. local y = #steps+3
  861. term.setCursorPos(1, y)
  862. term.setBackgroundColor(backgroundColor)
  863. term.setTextColor(specialTextColor)
  864. local text = localization[lang].input_text
  865. if (maxLength ~= nil and maxLength ~= 0) then
  866. text = text.." ("..localization[lang].max_length.." "..maxLength..")"
  867. end
  868. text = text..":"
  869. print(text)
  870. local scrW, scrH = term.getSize()
  871. term.setCursorPos(2, y+2)
  872. return cancelableRead(maxLength)
  873. end
  874.  
  875. function selectColorScreen(steps, currentStep)
  876. drawSteps(steps, currentStep)
  877. local y = #steps+3
  878. local color = 1
  879. local buttons = {}
  880. local scrW, scrH = term.getSize()
  881. local x = scrW/2-10
  882. local w = 5 --button width
  883. for _x=0, 3 do
  884. buttons[_x] = {}
  885. for _y= 0, 3 do
  886. term.setCursorPos(x+_x*w+1, y+_y)
  887. term.setBackgroundColor(color)
  888. term.setTextColor(contrastColor(color))
  889. term.write("[")
  890. term.write(string.rep(" ", w-2))
  891. term.write("]")
  892. buttons[_x][_y] = color
  893. color = color*2
  894. end
  895. end
  896.  
  897. local backx, backy, backw = drawBackButton()
  898.  
  899. while true do
  900. local event, button, cx, cy = os.pullEvent("mouse_click")
  901. if (cx >= x and cx <= x+w*4 and cy >= y and cy < y+4) then
  902. local _x = math.floor((cx-x)/w)
  903. local _y = math.floor((cy-y))
  904. return buttons[_x][_y]
  905. end
  906. if (cx >= backx and cx <= backx+backw and cy == backy) then
  907. return nil
  908. end
  909. end
  910. end
  911.  
  912. function responseScreen(success, response)
  913. if (success) then
  914. successScreen(response)
  915. else
  916. errorScreen(response)
  917. end
  918. end
  919.  
  920. function errorScreen(message)
  921. term.setBackgroundColor(colors.red)
  922. term.setTextColor(colors.white)
  923. term.clear()
  924. term.setCursorBlink(false)
  925. term.setBackgroundColor(colors.black)
  926. local scrW, scrH = term.getSize()
  927. term.setBackgroundColor(colors.red)
  928. if (type(message) == 'table') then
  929. for k, v in pairs(message) do
  930. term.setCursorPos(scrW/2-string.len(v)/2+1, scrH/2-math.ceil(#message/2)+k)
  931. term.write(v)
  932. end
  933. sleep(1.5 * #message)
  934. else
  935. term.setCursorPos(scrW/2-string.len(message)/2+1, scrH/2)
  936. term.write(message)
  937. sleep(1.5)
  938. end
  939. end
  940.  
  941. function successScreen(message)
  942. term.setBackgroundColor(colors.green)
  943. term.setTextColor(colors.white)
  944. term.clear()
  945. term.setCursorBlink(false)
  946. local scrW, scrH = term.getSize()
  947. if (type(message) == 'table') then
  948. for k, v in pairs(message) do
  949. term.setCursorPos(scrW/2-string.len(v)/2+1, scrH/2-math.ceil(#message/2)+k)
  950. term.write(v)
  951. end
  952. sleep(1.5 * #message)
  953. else
  954. term.setCursorPos(scrW/2-string.len(message)/2+1, scrH/2)
  955. term.write(message)
  956. sleep(1.5)
  957. end
  958. end
  959.  
  960. function waitScreen(message)
  961. drawBackground()
  962. term.setTextColor(colors.white)
  963. local scrW, scrH = term.getSize()
  964. if (type(message) == 'table') then
  965. for k, v in pairs(message) do
  966. term.setCursorPos(scrW/2-string.len(v)/2+1, scrH/2-math.ceil(#message/2)+k)
  967. term.write(v)
  968. end
  969. else
  970. term.setCursorPos(scrW/2-string.len(message)/2+1, scrH/2)
  971. term.write(message)
  972. end
  973. end
  974.  
  975. function confirmScreen(message, data, steps, currentStep)
  976. local y = 2
  977. drawBackground()
  978. if (steps ~= nil) then
  979. drawSteps(steps, currentStep)
  980. y = #steps+3
  981. end
  982. local scrW, scrH = term.getSize()
  983. term.setTextColor(specialTextColor)
  984. for k, v in pairs(message) do
  985. term.setCursorPos((scrW-string.len(v))/2, y)
  986. term.write(v)
  987. y = y+1
  988. end
  989. y = y+1
  990. if (data ~= nil) then
  991. for k, v in pairs(data) do
  992. local text = localization[lang][k]..": "..v
  993. term.setCursorPos((scrW-string.len(text))/2, y)
  994. term.write(text)
  995. y = y+1
  996. end
  997. y = y+1
  998. end
  999.  
  1000. local buttonY = math.min(scrH-3, y)
  1001. local buttonW = 20
  1002. local buttonX = scrW/2-buttonW/2
  1003.  
  1004. local acceptButton = drawButton(acceptButtonColor, acceptSecondaryColor, buttonTextColor, buttonX, buttonY, buttonW, localization[lang].accept)
  1005.  
  1006. local cancelButton = drawButton(cancelButtonColor, cancelSecondaryColor, buttonTextColor, buttonX, buttonY+2, buttonW, localization[lang].cancel)
  1007.  
  1008. while true do
  1009. local event, button, cx, cy = os.pullEvent("mouse_click")
  1010. if (mouseInButton(acceptButton, cx, cy)) then
  1011. return true
  1012. end
  1013. if (mouseInButton(cancelButton, cx, cy)) then
  1014. return false
  1015. end
  1016. end
  1017. end
  1018.  
  1019. function textScreen(message)
  1020. local y = 1
  1021. local scrW, scrH = term.getSize()
  1022. drawBackground()
  1023. term.setTextColor(colors.white)
  1024. for k, v in pairs(message) do
  1025. term.setCursorPos((scrW-string.len(v))/2+1, y)
  1026. term.write(v)
  1027. y = y+1
  1028. end
  1029.  
  1030. drawBackButton()
  1031.  
  1032. os.pullEvent("mouse_click")
  1033. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement