Advertisement
Marum

Mermegold V4 Store Clerk

Mar 25th, 2025 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.72 KB | None | 0 0
  1. -- Store clerk
  2.  
  3. -------------------- Start
  4. print("Finding ender modem...")
  5. peripheral.find("modem", rednet.open)
  6. print("Modem found!")
  7. local diskdrive = peripheral.find("drive")
  8. os.loadAPI("bankapi.lua")
  9. local serverData = bankapi.getServerData()
  10. local lang = serverData.lang
  11.  
  12. local text_error_noconnection = "Can't connect to server"
  13. --------------------------
  14.  
  15. local owner = nil
  16. local ownerView = false
  17. local catalog = {}
  18. local caralogueDirty = false
  19. local backgroundColor = colors.gray
  20.  
  21. local currentAccount = 0
  22.  
  23. local localization = {
  24. en={
  25. claimed={"You are now the owner", "of this machine"},
  26. invalid_card="Invalid card",
  27. unregistered_disk="The inserted disk is not registered",
  28. insert_card_to_configure="Insert a card to configure",
  29. registering_payment={"Registering payment. Please wait.","Do not remove the items","from my inventory yet..."},
  30. purchase_canceled="Purchase cancelled",
  31. foul_play={"","","Dont remove or add items","while I'm counting.","Please try again."},
  32. thanks_for_shopping_at="Thanks for buying at ",
  33. items_ready={"You can now take your items","from the chest"},
  34. please_return_items={"Please return the items","to their places before leaving."},
  35. come_again="Come again!",
  36. add_item_steps={
  37. "Products to register",
  38. "Product name",
  39. "Price",
  40. "Amount"
  41. },
  42. add_item_instructions={"Put one or more items in", "my inventory, and then press", "accept to continue"},
  43. detected_items=" Detected items ",
  44. various="<various>",
  45. register_confirm="Register these products?",
  46. register_complete="Product registered",
  47. button_editname="Edit name",
  48. button_editprice="Edit price",
  49. button_editcount="Edit amount",
  50. button_remove="Remove",
  51. button_back="Back",
  52. choose_item_name="Choose item's name",
  53. rename_successful="Item successfully renamed",
  54. choose_item_price="Choose item's price",
  55. reprice_successful="Price changed successfully",
  56. choose_item_count="Choose item amount",
  57. recount_successful="Amount changed successfully",
  58. remove_confirm={"Are you sure you want","to remove this item?"},
  59. remove_successful="Item removed successfully",
  60. add_items="Add item(s)",
  61. edit_shop_name="Edit shop name",
  62. edit_catalog="Edit catalog",
  63. resign="Resign as owner",
  64. quit="Exit",
  65. choose_shop_name="Choose shop name",
  66. shop_renamed_to="Shop renamed to ",
  67. resign_confirm={"Do you want to resign", "as the owner of this machine?"},
  68. resign_successful="Successfully resigned as owner.",
  69. see_you_later="See you later!",
  70. see_catalog="View catalog",
  71. buy="Buy",
  72. cant_buy_missing_card={
  73. "To buy, insert a Mermegold",
  74. "card into the disk drive."
  75. },
  76. buy_instructions={"Put the items you want to buy", "into my inventory"},
  77. your_balance="Your balance",
  78. total_price="Total price",
  79. final_balance="Final balance",
  80. buy_and_continue="Buy and continue",
  81. buy_and_quit="Buy and exit",
  82. cancel="Cancel",
  83. thanks_come_again="Thank you! Come again!",
  84. default_shop_name="Shop"
  85. },
  86. es={
  87. claimed="Ahora eres el propietario de esta maquina",
  88. invalid_card="Tarjeta invalida",
  89. unregistered_disk="El disco ingresado no esta registrado",
  90. insert_card_to_configure="Inserte tarjeta para configurar",
  91. registering_payment={"Registrando pago. Espere por favor.","No tome los articulos","de mi inventario todavia..."},
  92. purchase_canceled="Compra cancelada",
  93. foul_play={"","","No quite ni agregue articulos","a mi inventario mientras cuento.","Por favor intente nuevamente."},
  94. thanks_for_shopping_at="Gracias por comprar en",
  95. items_ready={"Ya puede llevarse su","compra del cofre"},
  96. please_return_items={"Por favor devuelva los articulos","a su lugar antes de irse."},
  97. come_again="Vuelva pronto!",
  98. add_item_steps={
  99. "Productos a registrar",
  100. "Nombre de estos productos",
  101. "Precio",
  102. "Cantidad"
  103. },
  104. add_item_instructions={"Ingresa uno o mas productos en", "mi inventario, y luego presiona", "aceptar para continuar"},
  105. detected_items=" Articulos detectados ",
  106. various="<varios>",
  107. register_confirm="Quieres registrar estos productos?",
  108. register_complete="Producto registrado",
  109. button_editname="Editar nombre",
  110. button_editprice="Editar precio",
  111. button_editcount="Editar cantidad",
  112. button_remove="Remover",
  113. button_back="Volver",
  114. choose_item_name="Elija el nombre del articulo",
  115. rename_successful="Articulo renombrado con exito",
  116. choose_item_price="Elija el precio del articulo",
  117. reprice_successful="Precio cambiado con exito",
  118. choose_item_count="Elija la cantidad del articulo",
  119. recount_successful="Cantidad cambiada con exito",
  120. remove_confirm={"Esta seguro que quiere","remover este articulo?"},
  121. remove_successful="Articulo removido con exito",
  122. add_items="Agregar articulo(s)",
  123. edit_shop_name="Editar nombre de la tienda",
  124. edit_catalog="Editar catalogo",
  125. resign="Renunciar como propietario",
  126. quit="Salir",
  127. choose_shop_name="Elija el nombre del local",
  128. shop_renamed_to="Local renombrado a ",
  129. resign_confirm={"Deseas desasignarte como", "propietario de esta maquina?"},
  130. resign_successful="Te desasignaste como propietario.",
  131. see_you_later="Hasta luego!",
  132. see_catalog="Ver catalogo",
  133. buy="Comprar",
  134. cant_buy_missing_card={
  135. "Para comprar, ingrese su tarjeta",
  136. "Mermegold en la disquetera."
  137. },
  138. buy_instructions={"Ingrese los articulos que quiera", "comprar dentro de mi inventario"},
  139. your_balance="Tu Balance",
  140. total_price="Precio Total",
  141. final_balance="Balance Final",
  142. buy_and_continue="Comprar y seguir",
  143. buy_and_quit="Comprar y salir",
  144. cancel="Cancelar",
  145. thanks_come_again="Gracias! Vuelva pronto!",
  146. default_shop_name="Tienda"
  147. },
  148. de = {
  149. claimed = {"Du bist jetzt der Besitzer", "dieser Maschine"},
  150. invalid_card = "Ungueltige Karte",
  151. unregistered_disk = "Die eingelegte Karte ist nicht registriert",
  152. insert_card_to_configure = "Karte einlegen, um zu konfigurieren",
  153. registering_payment = {"Zahlung wird registriert. Bitte warten.", "Noch keine Gegenstaende", "aus meinem Inventar entfernen..."},
  154. purchase_canceled = "Kauf abgebrochen",
  155. foul_play = {"", "", "Bitte keine Gegenstaende entfernen oder hinzufuegen,", "waehrend ich zaehle.", "Versuche es noch einmal."},
  156. thanks_for_shopping_at = "Danke fuer deinen Einkauf bei ",
  157. items_ready = {"Du kannst deine Gegenstaende", "jetzt aus der Truhe nehmen"},
  158. please_return_items = {"Bitte lege die Gegenstaende", "vor dem Verlassen wieder zurueck."},
  159. come_again = "Komm bald wieder!",
  160. add_item_steps = {
  161. "Zu registrierende Produkte",
  162. "Produktname",
  163. "Preis",
  164. "Anzahl"
  165. },
  166. add_item_instructions = {"Lege einen oder mehrere Gegenstaende", "in mein Inventar und druecke dann", "auf 'Bestaetigen', um fortzufahren"},
  167. detected_items = " Erkannte Gegenstaende ",
  168. various = "<verschiedenes>",
  169. register_confirm = "Diese Produkte registrieren?",
  170. register_complete = "Produkt registriert",
  171. button_editname = "Name bearbeiten",
  172. button_editprice = "Preis bearbeiten",
  173. button_editcount = "Anzahl bearbeiten",
  174. button_remove = "Entfernen",
  175. button_back = "Zurueck",
  176. choose_item_name = "Waehle einen Namen fuer den Gegenstand",
  177. rename_successful = "Name erfolgreich geaendert",
  178. choose_item_price = "Waehle einen Preis fuer den Gegenstand",
  179. reprice_successful = "Preis erfolgreich geaendert",
  180. choose_item_count = "Waehle eine Anzahl fuer den Gegenstand",
  181. recount_successful = "Anzahl erfolgreich geaendert",
  182. remove_confirm = {"Moechtest du diesen Gegenstand", "wirklich entfernen?"},
  183. remove_successful = "Gegenstand erfolgreich entfernt",
  184. add_items = "Gegenstand/Gegenstaende hinzufuegen",
  185. edit_shop_name = "Shopnamen bearbeiten",
  186. edit_catalog = "Katalog bearbeiten",
  187. resign = "Als Besitzer zuruecktreten",
  188. quit = "Beenden",
  189. choose_shop_name = "Waehle einen Shopnamen",
  190. shop_renamed_to = "Shop umbenannt in ",
  191. resign_confirm = {"Moechtest du wirklich", "als Besitzer dieser Maschine zuruecktreten?"},
  192. resign_successful = "Erfolgreich als Besitzer zurueckgetreten.",
  193. see_you_later = "Bis bald!",
  194. see_catalog = "Katalog ansehen",
  195. buy = "Kaufen",
  196. cant_buy_missing_card = {
  197. "Zum Kauf bitte eine Mermegold-Karte",
  198. "in das Laufwerk einlegen."
  199. },
  200. buy_instructions = {"Lege die Gegenstaende, die du kaufen moechtest,", "in mein Inventar"},
  201. your_balance = "Dein Kontostand",
  202. total_price = "Gesamtpreis",
  203. final_balance = "Restguthaben",
  204. buy_and_continue = "Kaufen und weitermachen",
  205. buy_and_quit = "Kaufen und beenden",
  206. cancel = "Abbrechen",
  207. thanks_come_again = "Danke! Komm bald wieder!",
  208. default_shop_name = "Shop"
  209. }
  210. }
  211.  
  212. local shopName = localization[lang].default_shop_name
  213.  
  214. if (not fs.exists("owner.txt")) then
  215. if (diskdrive.isDiskPresent()) then
  216. local tempClientData = bankapi.getClientData()
  217. local f = fs.open("disk/mermegold.txt", "r")
  218. if (f ~= nil) then
  219. local key = f.readLine()
  220. if (tempClientData[key] ~= nil) then
  221. local cf = fs.open("owner.txt", "w")
  222. cf.writeLine(key)
  223. cf.close()
  224. bankapi.successScreen(localization[lang].claimed)
  225. else
  226. bankapi.errorScreen(localization[lang].invalid_card)
  227. end
  228. else
  229. bankapi.errorScreen(localization[lang].unregistered_disk)
  230. end
  231. f.close()
  232. os.reboot()
  233. else
  234. bankapi.errorScreen(localization[lang].insert_card_to_configure)
  235. os.shutdown()
  236. end
  237. else
  238. local f = fs.open("owner.txt", "r")
  239. owner = f.readLine()
  240. f.close()
  241. end
  242.  
  243. local currentAccount = 0
  244.  
  245. local function readDisk()
  246. if (fs.exists("disk")) then
  247. local f = fs.open("disk/mermegold.txt", "r")
  248. if (f ~= nil) then
  249. local value = f.readLine()
  250. if (value ~= nil) then
  251. local tempClientData = bankapi.getClientData()
  252. if (tempClientData[value] ~= nil) then
  253. currentAccount = value
  254. else
  255. currentAccount = 0
  256. end
  257. end
  258. f.close()
  259. end
  260. end
  261. end
  262.  
  263. readDisk()
  264.  
  265. local function loadShopName()
  266. local f = fs.open("shopname.txt", "r")
  267. if (f ~= nil) then
  268. local value = f.readLine()
  269. if (value ~= nil) then
  270. shopName = value
  271. end
  272. f.close()
  273. else
  274. local f = fs.open("shopname.txt", "w")
  275. f.writeLine("Tienda")
  276. f.close()
  277. end
  278. end
  279.  
  280. local function loadCatalog()
  281. local f = fs.open("catalog.txt", "r")
  282. if (f ~= nil) then
  283. catalog = {}
  284. local line = f.readLine()
  285. while line ~= nil do
  286. local lline = line
  287. local item = ""
  288. local damage = 0
  289. local count = 0
  290. local price = 0
  291. local name = ""
  292.  
  293. while lline ~= "" and lline ~= nil do
  294. if (lline == "item") then
  295. item = f.readLine()
  296. elseif (lline == "damage") then
  297. damage = tonumber(f.readLine())
  298. elseif (lline == "count") then
  299. count = tonumber(f.readLine())
  300. elseif (lline == "price") then
  301. price = tonumber(f.readLine())
  302. elseif (lline == "name") then
  303. name = f.readLine()
  304. end
  305. lline = f.readLine()
  306. end
  307.  
  308. if (catalog[item] == nil) then
  309. catalog[item] = {}
  310. end
  311. catalog[item][damage] = {
  312. count=count,
  313. price=price,
  314. name=name
  315. }
  316.  
  317. line = f.readLine()
  318. end
  319.  
  320. f.close()
  321. else
  322. local f = fs.open("catalog.txt", "w")
  323. f.close()
  324. end
  325. end
  326.  
  327. local function updateCatalog()
  328. caralogueDirty = true
  329. local f = fs.open("catalog.txt", "w")
  330. for name, damagetypes in pairs(catalog) do
  331. print(name, damagetypes)
  332. for damage, detail in pairs(damagetypes) do
  333. print(damage, detail)
  334. for k, v in pairs(detail) do
  335. print(k, v)
  336. end
  337. f.writeLine("item")
  338. f.writeLine(name)
  339. f.writeLine("damage")
  340. f.writeLine(tostring(damage))
  341. f.writeLine("count")
  342. f.writeLine(tostring(detail.count))
  343. f.writeLine("name")
  344. f.writeLine(detail.name)
  345. f.writeLine("price")
  346. f.writeLine(tostring(detail.price))
  347. f.writeLine("")
  348. end
  349. end
  350. f.close()
  351. end
  352.  
  353. local function printWithPadding(x, leftText, rightText, newLine, color, dotColor)
  354. local scrW, scrH = term.getSize()
  355. leftText = string.rep(" ", x-1)..leftText
  356. if (newLine == nil) then newLine = true end
  357. local space = string.len(leftText)+string.len(rightText)
  358. local padding = scrW-space
  359. local paddingText = ""
  360. if (padding > 0) then
  361. paddingText = string.rep(".", padding)
  362. else
  363. leftText = string.sub(leftText, 1, string.len(leftText)+padding-1)
  364. paddingText = "."
  365. end
  366. if (color == nil) then
  367. term.setTextColor(colors.white)
  368. else
  369. term.setTextColor(color)
  370. end
  371. if (dotColor == nil) then dotColor = colors.gray end
  372. term.write(leftText)
  373. term.setTextColor(dotColor)
  374. term.write(paddingText)
  375. if (color == nil) then
  376. term.setTextColor(colors.white)
  377. else
  378. term.setTextColor(color)
  379. end
  380. term.write(rightText)
  381. if (newLine) then
  382. print("")
  383. end
  384. end
  385.  
  386. local function buy(previousEstimatedPrice)
  387. local scrW, scrH = term.getSize()
  388. bankapi.waitScreen(localization[lang].registering_payment)
  389.  
  390. local buyingItems = {}
  391. for i=1, 16 do
  392. turtle.select(i)
  393. local detail = turtle.getItemDetail(i)
  394. turtle.drop()
  395. if (detail ~= nil) then
  396. local item = detail.name
  397. if (catalog[item] ~= nil) then
  398. local damage = detail.damage
  399. if detail.damage == nil then
  400. damage = 1
  401. end
  402. if (catalog[item][damage] ~= nil) then
  403. local product = catalog[item][damage]
  404.  
  405. if (buyingItems[item] == nil) then
  406. buyingItems[item] = {}
  407. end
  408. if (buyingItems[item][damage] == nil) then
  409. buyingItems[item][damage] = detail.count
  410. else
  411. buyingItems[item][damage] = buyingItems[item][damage]+detail.count
  412. end
  413. end
  414. end
  415. end
  416. end
  417.  
  418. local totalPrice = 0
  419.  
  420. local totalReceitDescription = shopName..": "
  421.  
  422. for item, damagetypes in pairs(buyingItems) do
  423. for damage, count in pairs(damagetypes) do
  424. local product = catalog[item][damage]
  425. local estimatedPrice = math.ceil(count*product.price/product.count)
  426. totalPrice = totalPrice+estimatedPrice
  427. totalReceitDescription = totalReceitDescription.." "..product.name.." x"..count.." ($"..estimatedPrice..") |"
  428. end
  429. end
  430.  
  431. if (previousEstimatedPrice ~= totalPrice) then
  432. bankapi.waitScreen(localization[lang].purchase_canceled)
  433. while (turtle.suck()) do
  434. end
  435. bankapi.textScreen(localization[lang].foul_play)
  436. else
  437. bankapi.transaction(currentAccount, owner, totalPrice, totalReceitDescription)
  438. bankapi.successScreen({localization[lang].thanks_for_shopping_at, shopName.."!"})
  439. bankapi.successScreen(localization[lang].items_ready)
  440. end
  441. end
  442.  
  443. local function logout()
  444. for i=1, 16 do
  445. local detail = turtle.getItemDetail(i)
  446. if (detail ~= nil) then
  447. bankapi.waitScreen(localization[lang].please_return_items)
  448. diskdrive.ejectDisk()
  449. sleep(2.5)
  450. break
  451. end
  452. end
  453. diskdrive.ejectDisk()
  454. bankapi.successScreen(localization[lang].come_again)
  455. os.shutdown()
  456. end
  457.  
  458. local function addItemToCatalog()
  459. turtle.select(1)
  460. local steps = localization[lang].add_item_steps
  461. local count
  462. local returning = false
  463. local totalItems
  464. repeat
  465. count = {}
  466. local accept = bankapi.confirmScreen(localization[lang].add_item_instructions, {}, steps, 1)
  467. if (not accept) then
  468. returning = true
  469. break
  470. end
  471. totalItems = 0
  472. for i=1, 16 do
  473. local itemCount = turtle.getItemCount(i)
  474. if (itemCount > 0) then
  475. count[i] = itemCount
  476. totalItems = totalItems+itemCount
  477. end
  478. end
  479. until totalItems > 0
  480. if (returning) then return end
  481.  
  482. local detail = {}
  483. local itemList = {}
  484. itemList[1] = string.char(31)..localization[lang].detected_items..string.char(31)
  485.  
  486. for i=1, 16 do
  487. local itemDetail = turtle.getItemDetail(i)
  488. if (itemDetail ~= nil) then
  489. detail[i] = itemDetail
  490. if (i < 9) then
  491. itemList[i+1] = itemDetail.name
  492. elseif (i == 9) then
  493. itemList[i+1] = "..."
  494. end
  495. end
  496. end
  497.  
  498. bankapi.waitScreen(itemList)
  499. local continueButton = bankapi.drawContinueButton()
  500. os.pullEvent("mouse_click")
  501.  
  502. local name = bankapi.inputTextScreen(steps, 2)
  503. if (name == nil) then return end
  504.  
  505. local price = bankapi.inputNumberScreen(steps, 3)
  506. if (price == nil) then return end
  507.  
  508. local count = bankapi.inputNumberScreen(steps, 4)
  509. if (count == nil) then return end
  510.  
  511. local displayName = nil
  512. local oneName = true
  513. local set = false
  514. for k, v in pairs(detail) do
  515. if (not set) then
  516. displayName = v.name
  517. set = true
  518. else
  519. if (displayName ~= v.name) then
  520. oneName = false
  521. displayName = localization[lang].various
  522. end
  523. end
  524. end
  525.  
  526. local confirmData = {
  527. name = name,
  528. product = displayName,
  529. quantity = "x"..count,
  530. price = "$"..price
  531. }
  532. local accept = bankapi.confirmScreen({localization[lang].register_confirm}, confirmData)
  533. if (not accept) then return end
  534.  
  535. for k, v in pairs(detail) do
  536. if (catalog[v.name] == nil) then
  537. catalog[v.name] = {}
  538. end
  539. local damage = v.damage
  540. if v.damage == nil then
  541. damage = 1
  542. end
  543. catalog[v.name][damage] = {
  544. ["name"] = name,
  545. ["count"] = tonumber(count),
  546. ["price"] = tonumber(price)
  547. }
  548. end
  549.  
  550. updateCatalog()
  551.  
  552. bankapi.successScreen(localization[lang].register_complete)
  553. end
  554.  
  555. local function editItem(itemID)
  556. local item = catalog[itemID.name][itemID.damage]
  557. while (true) do
  558. local command = bankapi.optionMenu(item.name, {
  559. [1] = {
  560. ["option"] = "editname",
  561. ["text"] = localization[lang].button_editname},
  562. [2] = {
  563. ["option"] = "editprice",
  564. ["text"] = localization[lang].button_editprice},
  565. [3] = {
  566. ["option"] = "editcount",
  567. ["text"] = localization[lang].button_editcount},
  568. [4] = {
  569. ["option"] = "remove",
  570. ["text"] = localization[lang].button_remove},
  571. [5] = {
  572. ["option"] = "back",
  573. ["text"] = localization[lang].button_back},
  574. })
  575.  
  576. if (command == "editname") then
  577. local newname = bankapi.inputTextScreen({localization[lang].choose_item_name}, 1, 30)
  578. if (newname ~= nil) then
  579. catalog[itemID.name][itemID.damage].name = newname
  580. updateCatalog()
  581. bankapi.successScreen(localization[lang].rename_successful)
  582. end
  583. elseif(command == "editprice") then
  584. local newprice = bankapi.inputNumberScreen({localization[lang].choose_item_price}, 1)
  585. if (newprice ~= nil) then
  586. catalog[itemID.name][itemID.damage].price = tonumber(newprice)
  587. updateCatalog()
  588. bankapi.successScreen(localization[lang].reprice_successful)
  589. end
  590. elseif(command == "editcount") then
  591. local newcount = bankapi.inputNumberScreen({localization[lang].choose_item_count}, 1)
  592. if (newcount ~= nil) then
  593. catalog[itemID.name][itemID.damage].count = tonumber(newcount)
  594. updateCatalog()
  595. bankapi.successScreen(localization[lang].recount_successful)
  596. end
  597. elseif(command == "remove") then
  598. local confirm = bankapi.confirmScreen(localization[lang].remove_confirm, {item_name=item.name})
  599. if (confirm) then
  600. term.setBackgroundColor(colors.black)
  601. catalog[itemID.name][itemID.damage] = nil
  602. updateCatalog()
  603. bankapi.successScreen(localization[lang].remove_successful)
  604. return
  605. end
  606. elseif(command == "back") then
  607. return
  608. end
  609. end
  610. end
  611.  
  612. local function displayCatalogItem(item, x, y, w)
  613. term.setCursorPos(x,y)
  614. term.setBackgroundColor(colors.white)
  615. term.setTextColor(colors.gray)
  616. term.write(string.rep(string.char(131), w))
  617. term.setCursorPos(x,y+1)
  618. term.setTextColor(colors.lightGray)
  619. term.write(" ")
  620. term.write(string.rep(".", w-2))
  621. term.write(" ")
  622. term.setCursorPos(x+1,y+1)
  623. term.setTextColor(colors.black)
  624. term.write(item.name)
  625. local text = "$"..item.price
  626. if (item.count > 1) then
  627. text = item.count.." x $"..item.price
  628. end
  629. term.setCursorPos(x+w-string.len(text)-1,y+1)
  630. term.write(text)
  631. end
  632.  
  633. local function displayCatalog(edit)
  634. local scrW, scrH = term.getSize()
  635. local first = 0
  636.  
  637. if (caralogueDirty) then
  638. loadCatalog()
  639. end
  640.  
  641. while (true) do
  642.  
  643. local encounteredItems = {}
  644.  
  645. term.setBackgroundColor(backgroundColor)
  646. term.clear()
  647.  
  648. local row = 0
  649. local max = 5
  650. local buttons = {}
  651. local total = 0
  652. local width = scrW-2
  653. if (edit) then
  654. max = 4
  655. width = scrW-7
  656. end
  657. for name, damagetypes in pairs(catalog) do
  658. for damage, detail in pairs(damagetypes) do
  659. if (total >= first and total < first+max) then
  660. if (encounteredItems[detail.name]) == nil then
  661. term.setBackgroundColor(backgroundColor)
  662. displayCatalogItem(detail, 2, row*2+1, width)
  663. if (edit) then
  664. term.setBackgroundColor(colors.cyan)
  665. term.setTextColor(backgroundColor)
  666. term.setCursorPos(scrW-5, row*2+1)
  667. term.write(string.rep(string.char(143), 5))
  668. term.setBackgroundColor(colors.cyan)
  669. term.setTextColor(colors.white)
  670. term.setCursorPos(scrW-5, row*2+2)
  671. term.write(" Edit")
  672. buttons[row*2+1] = {name=name, damage=damage}
  673. buttons[row*2+2] = {name=name, damage=damage}
  674. end
  675. row = row+1
  676. end
  677. end
  678. total = total+1
  679. encounteredItems[detail.name] = true
  680. end
  681. end
  682.  
  683. local backButton = bankapi.drawBackButton()
  684.  
  685. local prevPage = bankapi.drawButton(colors.yellow, colors.orange, colors.black, 16, scrH-1, 7, string.char(27))
  686. if (first <= 0) then
  687. bankapi.drawButton(colors.lightGray, colors.gray, colors.gray, 16, scrH-1, 7, string.char(27))
  688. end
  689.  
  690. local nextPage = bankapi.drawButton(colors.yellow, colors.orange, colors.black, 32, scrH-1, 7, string.char(26))
  691. if (first+max >= total) then
  692. bankapi.drawButton(colors.lightGray, colors.gray, colors.gray, 32, scrH-1, 7, string.char(26))
  693. end
  694.  
  695. local addButton
  696. if (edit) then
  697. addButton = bankapi.drawButton(colors.lime, colors.green, colors.black, 2, scrH-3, scrW-2, localization[lang].add_items)
  698. end
  699.  
  700. -- page indicator
  701. local pages = math.max(1,math.ceil(total/max))
  702. local pageText = tostring((first/max)+1).."/"..pages
  703. term.setCursorPos(28-string.len(pageText)/2, scrH-1)
  704. term.setBackgroundColor(backgroundColor)
  705. term.setTextColor(colors.orange)
  706. term.write(pageText)
  707.  
  708. local eventData = {os.pullEvent()}
  709. local event = eventData[1]
  710.  
  711. if event == "mouse_click" then
  712. local cx = eventData[3]
  713. local cy = eventData[4]
  714. if (edit and bankapi.mouseInButton(addButton, cx, cy)) then
  715. addItemToCatalog()
  716. end
  717. if (bankapi.mouseInButton(prevPage, cx, cy)) then
  718. if (first > 0) then
  719. first = first-max
  720. end
  721. end
  722. if (bankapi.mouseInButton(nextPage, cx, cy)) then
  723. if (first+max < total) then
  724. first = first+max
  725. end
  726. end
  727. if (bankapi.mouseInButton(backButton, cx, cy)) then
  728. return nil
  729. end
  730. if buttons[cy] ~= nil then
  731. local itemID = buttons[cy]
  732. editItem(itemID)
  733. end
  734. elseif event == "mouse_scroll" then
  735. local scroll = eventData[2]
  736. if (scroll < 0) then
  737. if (first > 0) then
  738. first = first-max
  739. end
  740. else
  741. if (first+max < total) then
  742. first = first+max
  743. end
  744. end
  745. end
  746. end
  747. end
  748.  
  749. local function noCardScreen()
  750. displayCatalog()
  751. os.shutdown()
  752. end
  753.  
  754. loadShopName()
  755. loadCatalog()
  756.  
  757. while true do -- Second while to allow the use of breaks as continues
  758. ownerView = owner == currentAccount
  759. breaking = false
  760. while true do
  761.  
  762. if (ownerView) then
  763. while true do -- Sub menu
  764. local command = bankapi.optionMenu(shopName, {
  765. [1] = {
  766. ["option"] = "name",
  767. ["text"] = localization[lang].edit_shop_name},
  768. [2] = {
  769. ["option"] = "catalog",
  770. ["text"] = localization[lang].edit_catalog},
  771. [3] = {
  772. ["option"] = "resign",
  773. ["text"] = localization[lang].resign},
  774. [4] = {
  775. ["option"] = "quit",
  776. ["text"] = localization[lang].quit},
  777. })
  778.  
  779. if (command == "name") then
  780. local newname = bankapi.inputTextScreen({localization[lang].choose_shop_name}, 1, 30)
  781. if (newname ~= nil) then
  782. shopName = newname
  783. os.setComputerLabel(newname)
  784.  
  785. local f = fs.open("shopname.txt", "w")
  786. f.writeLine(shopName)
  787. f.close()
  788.  
  789. bankapi.successScreen(localization[lang].shop_renamed_to..shopName)
  790. end
  791.  
  792. elseif (command == "catalog") then
  793. displayCatalog(true)
  794.  
  795. elseif (command == "resign") then
  796. local accept = bankapi.confirmScreen(localization[lang].resign_confirm)
  797. if (accept) then
  798. fs.delete("owner.txt")
  799. diskdrive.ejectDisk()
  800. bankapi.successScreen(localization[lang].resign_successful)
  801. os.shutdown()
  802. end
  803.  
  804. elseif (command == "quit") then
  805. diskdrive.ejectDisk()
  806. bankapi.successScreen(localization[lang].see_you_later)
  807. os.shutdown()
  808. break
  809. end
  810.  
  811. end
  812. else -- Valid account
  813. while true do -- Sub menu
  814. local command = bankapi.optionMenu(shopName, {
  815. [1] = {
  816. ["option"] = "catalog",
  817. ["text"] = localization[lang].see_catalog},
  818. [2] = {
  819. ["option"] = "buy",
  820. ["text"] = localization[lang].buy},
  821. [3] = {
  822. ["option"] = "quit",
  823. ["text"] = localization[lang].quit},
  824. })
  825.  
  826. if (command == "catalog") then
  827. displayCatalog()
  828. elseif (command == "buy") then
  829. while (true) do
  830. readDisk()
  831. if (currentAccount == 0) then -- Invalid client
  832. bankapi.waitScreen(localization[lang].cant_buy_missing_card)
  833. local backButton = bankapi.drawBackButton()
  834. while (true) do
  835. local eventData = {os.pullEvent()}
  836. local event = eventData[1]
  837. if (event == "mouse_click") then
  838. local cx = eventData[3]
  839. local cy = eventData[4]
  840. if (bankapi.mouseInButton(backButton, cx, cy)) then
  841. breaking = true
  842. break
  843. end
  844. elseif (event == "disk") then
  845. break
  846. end
  847. end
  848. readDisk()
  849. else -- Valid client
  850. local scrW, scrH = term.getSize()
  851. local tempClientData = bankapi.getClientData()
  852. local first = 0
  853. while true do
  854. term.setBackgroundColor(backgroundColor)
  855. term.clear()
  856. local buyingItems = {}
  857. for i=1, 16 do
  858. local detail = turtle.getItemDetail(i)
  859. if (detail ~= nil) then
  860. local item = detail.name
  861. if (catalog[item] ~= nil) then
  862. local damage = detail.damage
  863. if detail.damage == nil then
  864. damage = 1
  865. end
  866. if (catalog[item][damage] ~= nil) then
  867. local product = catalog[item][damage]
  868.  
  869. if (buyingItems[item] == nil) then
  870. buyingItems[item] = {}
  871. end
  872. if (buyingItems[item][damage] == nil) then
  873. buyingItems[item][damage] = detail.count
  874. else
  875. buyingItems[item][damage] = buyingItems[item][damage]+detail.count
  876. end
  877. end
  878. end
  879. end
  880. end
  881.  
  882. local totalPrice = 0
  883.  
  884. local encounteredItems = {}
  885.  
  886. local row = 0
  887. local max = 4
  888.  
  889. local total = 0
  890. for name, damagetypes in pairs(buyingItems) do
  891. for damage, detail in pairs(damagetypes) do
  892. local product = catalog[name][damage]
  893. local count = buyingItems[name][damage]
  894. local estimatedPrice = math.ceil(count*product.price/product.count)
  895. if (total >= first and total < first+max) then
  896. --if (encounteredItems[name]) == nil then
  897. displayCatalogItem({name=product.name, price=estimatedPrice, count=count}, 2, row*2+1, scrW-2)
  898. row = row+1
  899. --end
  900. end
  901. totalPrice = totalPrice+estimatedPrice
  902. total = total+1
  903. encounteredItems[name] = true
  904. end
  905. end
  906.  
  907. if (total == 0) then
  908. bankapi.waitScreen(localization[lang].buy_instructions)
  909. end
  910.  
  911. local backButton = bankapi.drawBackButton()
  912.  
  913. local balance = tempClientData[currentAccount].balance
  914. local prevPage, nextPage = nil
  915.  
  916. if (total > 0) then
  917. prevPage = bankapi.drawButton(colors.yellow, colors.orange, colors.black, 20, scrH-4, 7, string.char(27))
  918. if (first <= 0) then
  919. bankapi.drawButton(colors.lightGray, colors.gray, colors.gray, 20, scrH-4, 7, string.char(27))
  920. end
  921.  
  922. nextPage = bankapi.drawButton(colors.yellow, colors.orange, colors.black, 32, scrH-4, 7, string.char(26))
  923. if (first+max >= total) then
  924. bankapi.drawButton(colors.lightGray, colors.gray, colors.gray, 32, scrH-4, 7, string.char(26))
  925. end
  926.  
  927.  
  928. -- page indicator
  929. local pages = math.max(1,math.ceil(total/max))
  930. local pageText = tostring((first/max)+1).."/"..pages
  931. term.setCursorPos(30-string.len(pageText)/2, scrH-4)
  932. term.setBackgroundColor(backgroundColor)
  933. term.setTextColor(colors.orange)
  934. term.write(pageText)
  935.  
  936.  
  937. print("")
  938. print("")
  939.  
  940. term.setCursorPos(1, scrH-2)
  941. term.setTextColor(colors.lightGray)
  942. term.setBackgroundColor(backgroundColor)
  943.  
  944. printWithPadding(20, localization[lang].your_balance, "$"..balance, true, colors.white, colors.lightGray)
  945. printWithPadding(20, localization[lang].total_price, "$"..totalPrice, nil, colors.white, colors.lightGray)
  946. local finalBalance = balance-totalPrice
  947. if (finalBalance < 0) then
  948. printWithPadding(20, localization[lang].final_balance, "$"..finalBalance, false, colors.red, colors.lightGray)
  949. else
  950. printWithPadding(20, localization[lang].final_balance, "$"..finalBalance, false, colors.white, colors.lightGray)
  951. end
  952. end
  953.  
  954. local buyButton, buyAndExitButton = nil
  955. if (total > 0 and totalPrice <= balance) then
  956. buyButton = bankapi.drawButton(colors.lime, colors.green, colors.black, 1, scrH-3, 18, localization[lang].buy_and_continue)
  957. buyAndExitButton = bankapi.drawButton(colors.yellow, colors.orange, colors.black, 1, scrH-2, 18, localization[lang].buy_and_quit)
  958. end
  959. local cancelButton = bankapi.drawButton(colors.red, colors.brown, colors.black, 1, scrH-1, 18, localization[lang].cancel)
  960.  
  961.  
  962. while true do
  963. local eventData = {os.pullEvent()}
  964. local event = eventData[1]
  965. if event == "mouse_click" then
  966. local cx = eventData[3]
  967. local cy = eventData[4]
  968. if (total > 0) then
  969. if (bankapi.mouseInButton(prevPage, cx, cy)) then
  970. if (first > 0) then
  971. first = first-max
  972. break
  973. end
  974. end
  975. if (bankapi.mouseInButton(nextPage, cx, cy)) then
  976. if (first+max < total) then
  977. first = first+max
  978. break
  979. end
  980. end
  981. end
  982. if (bankapi.mouseInButton(cancelButton, cx, cy)) then
  983. breaking = true
  984. break
  985. end
  986. if (totalPrice > 0 and totalPrice < balance and total >= 0) then
  987. if (bankapi.mouseInButton(buyButton, cx, cy)) then
  988. buy(totalPrice)
  989. break
  990. end
  991. if (bankapi.mouseInButton(buyAndExitButton, cx, cy)) then
  992. buy(totalPrice)
  993. logout()
  994. break
  995. end
  996. end
  997. if (bankapi.mouseInButton(cancelButton, cx, cy)) then
  998. logout()
  999. end
  1000. elseif event == "mouse_scroll" then
  1001. if (total > 0) then
  1002. local scroll = eventData[2]
  1003. if (scroll < 0) then
  1004. if (first > 0) then
  1005. first = first-max
  1006. break
  1007. end
  1008. else
  1009. if (first+max < total) then
  1010. first = first+max
  1011. break
  1012. end
  1013. end
  1014. end
  1015. elseif (event == "turtle_inventory") then
  1016. if (first > 1 and first >= total-1) then
  1017. first = first-max
  1018. end
  1019. break
  1020. end
  1021. end
  1022. if (breaking) then break end
  1023. end
  1024. end
  1025. if (breaking) then break end
  1026. end
  1027. elseif (command == "quit") then
  1028. diskdrive.ejectDisk()
  1029. bankapi.successScreen(localization[lang].thanks_come_again)
  1030. os.shutdown()
  1031. break
  1032. end
  1033. end
  1034. end
  1035. end
  1036. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement