Advertisement
GravityCube

Crates 2.0

Dec 19th, 2018
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.04 KB | None | 0 0
  1. --------------------------------------------------------
  2. --> Setup <--
  3. --------------------------------------------------------
  4. function setConfigs()
  5. cfg = {}
  6. cfg.barColor = colors.cyan
  7. cfg.titleProgram = "Crates program"
  8. cfg.freeUses = 1
  9.  
  10. cfg.commandsToMoveKeys = {"movekeys","movekey","addkey","addkeys"}
  11. cfg.commandsToOpenCrate = {"open","opencrate","crate", "play"}
  12. cfg.commandsToMoveLegendary = {"addlegendary", "addlegendarykeys", "addleg", "addlry", "addl"}
  13. return cfg
  14. end
  15.  
  16. --------------------------------------------------------
  17. --> Variables <--
  18. --------------------------------------------------------
  19. os.loadAPI("gcapi")
  20.  
  21. local config = {}
  22.  
  23. me = peripheral.find("appeng_me_tilecable") or peripheral.find("appeng_me_tilecolorlesscable")
  24. pim = peripheral.find("pim")
  25. chest = peripheral.find("crystal")
  26.  
  27. spinning = false
  28.  
  29.  
  30. function hasPermissions(player)
  31. local hasPerms = gcapi.hasPermissions(player)
  32. if not hasPerms then
  33. printLog(player .. " tried to use an admin command!")
  34. end
  35.  
  36. return hasPerms
  37. end
  38.  
  39. --------------------------------------------------------
  40. --> Setup File <--
  41. --------------------------------------------------------
  42. function readConfigFile(cfg)
  43. local list = gcapi.getListFromFile("/config.cfg")
  44. for key,value in pairs(list) do
  45. cfg[key] = value
  46. end
  47. return cfg
  48. end
  49. function saveConfigFile(cfg)
  50. gcapi.saveListToFile("/config.cfg", cfg)
  51. end
  52. --------------------------------------------------------
  53. --> Key Names <--
  54. --------------------------------------------------------
  55. key_name = nil
  56. l_key_name = nil
  57. if fs.exists("/keyNames") then
  58. local list = gcapi.getListFromFile("/keyNames")
  59. key_name = list["key_name"]
  60. l_key_name = list["l_key_name"]
  61. else
  62. error("You need to create the keyNames file with the names of the keys ({key_name and l_key_name})")
  63. end
  64.  
  65. --------------------------------------------------------
  66. --> Manual Setup <--
  67. --------------------------------------------------------
  68. config = readConfigFile(config)
  69. if config.chestDirection == nil then
  70. write("Where is the chest (Center: pim): ")
  71. config.chestDirection = read():gsub(' ','')
  72. print()
  73. saveConfigFile(config)
  74. end
  75.  
  76. if config.players_database == nil then
  77. write("Are you allowed to make databases? ")
  78. local input = read()
  79. config.players_database = ( (input:gsub(' ',''):lower() == "yes") or (input:gsub(' ',''):lower() == "y") )
  80. print()
  81. saveConfigFile(config)
  82. end
  83.  
  84. config = setConfigs()
  85. config = readConfigFile(config)
  86. --------------------------------------------------------
  87. --> ME STUFF <--
  88. --------------------------------------------------------
  89. function extractItem(item)
  90. emptyChest()
  91. if item ~= nil then
  92. me.extractItem(item,"up")
  93. end
  94. end
  95.  
  96. function emptyChest()
  97. me.insertItem(1,64,"up")
  98. end
  99. --------------------------------------------------------
  100. --> PIM STUFF <--
  101. --------------------------------------------------------
  102. function checkPlayer(player)
  103. if getCurrentPlayer() == player then
  104. return true
  105. end
  106. return false
  107. end
  108.  
  109. function getCurrentPlayer()
  110. return pim.getInventoryName()
  111. end
  112.  
  113. function movePrize()
  114. if pim.getInventoryName() ~= "pim" then
  115. itemsToBeMoved = chest.getStackInSlot(1)["qty"]
  116. local itemsMoved = 0
  117. for i=1,36,1 do
  118. stackInSlot = pim.getStackInSlot(i)
  119. if stackInSlot == nil then
  120. itemsMoved = itemsMoved + pim.pullItemIntoSlot(config.chestDirection,1,64,i)
  121. if itemsMoved >= itemsToBeMoved then
  122. return true
  123. end
  124. end
  125. end
  126. else
  127. error('The player left the PIM before being able to deliver the prize.')
  128. end
  129. error('The items couldnt be moved for some unknown reason.')
  130. end
  131.  
  132. function getKeysInInventory()
  133. --CONFIG
  134. local id = 131
  135. local name = key_name
  136.  
  137. qty = 0
  138.  
  139. for i,item in pairs(pim.getAllStacks()) do
  140. if item["id"] == id then
  141. if item["name"] == name then
  142. if item["ench"] ~= nil then
  143. if item["ench"][1] ~= nil and item["ench"][1] == "Unbreaking I" then
  144. qty = qty + item["qty"]
  145. end
  146. end
  147. end
  148. end
  149. end
  150.  
  151. return qty
  152. end
  153.  
  154. function moveKeysToSystem(amount)
  155.  
  156. --CONFIG
  157. local id = 131
  158. local name = key_name
  159.  
  160. if amount > getKeysInInventory() then
  161. return false, "Not enough keys"
  162. end
  163.  
  164. local qty = 0
  165.  
  166. for i,item in pairs(pim.getAllStacks()) do
  167. if item["id"] == id then
  168. if item["name"] == name then
  169. if item["ench"] ~= nil then
  170. if item["ench"][1] ~= nil and item["ench"][1] == "Unbreaking I" then
  171. local keysMoved = pim.pushItemIntoSlot(config.chestDirection, i, amount, 1)
  172. qty = qty + keysMoved
  173. amount = amount - keysMoved
  174. end
  175. end
  176. end
  177. end
  178. end
  179.  
  180. return qty
  181. end
  182.  
  183. function getLegendaryKeysInInventory()
  184. --CONFIG
  185. local id = 131
  186. local name = l_key_name
  187.  
  188. qty = 0
  189.  
  190. for i,item in pairs(pim.getAllStacks()) do
  191. if item["id"] == id then
  192. if item["name"] == name then
  193. if item["ench"] ~= nil then
  194. if item["ench"][1] ~= nil and item["ench"][1] == "Unbreaking I" then
  195. qty = qty + item["qty"]
  196. end
  197. end
  198. end
  199. end
  200. end
  201.  
  202. return qty
  203. end
  204.  
  205. function moveLegendaryKeysToSystem(amount)
  206.  
  207. --CONFIG
  208. local id = 131
  209. local name = l_key_name
  210.  
  211. if amount > getLegendaryKeysInInventory() then
  212. return false, "Not enough keys"
  213. end
  214.  
  215. local qty = 0
  216.  
  217. for i,item in pairs(pim.getAllStacks()) do
  218. if item["id"] == id then
  219. if item["name"] == name then
  220. if item["ench"] ~= nil then
  221. if item["ench"][1] ~= nil and item["ench"][1] == "Unbreaking I" then
  222. local keysMoved = pim.pushItemIntoSlot(config.chestDirection, i, amount, 1)
  223. qty = qty + keysMoved
  224. amount = amount - keysMoved
  225. end
  226. end
  227. end
  228. end
  229. end
  230.  
  231. return qty
  232. end
  233.  
  234.  
  235. --------------------------------------------
  236. --> Economy <--
  237. --> <--
  238. --> This is saved every use <--
  239. --> for safety purposes. <--
  240. --------------------------------------------
  241. playerUses = {}
  242.  
  243. function loadUses()
  244. if not config.players_database then
  245. return false
  246. end
  247. playerUses = gcapi.getListFromFile("/disk/playerUses")
  248. return true
  249. end
  250.  
  251. function saveUses()
  252. if not config.players_database then
  253. return false
  254. end
  255. gcapi.saveListToFile("/disk/playerUses", playerUses)
  256. return true
  257. end
  258.  
  259. function getPlayerUses(player)
  260. if not config.players_database then
  261. return 0
  262. end
  263.  
  264. loadUses()
  265.  
  266. if playerUses[player] then
  267. return tonumber(playerUses[player])
  268. end
  269.  
  270. return config.freeUses
  271. end
  272.  
  273. function addUses(player, newuses)
  274. if not config.players_database then
  275. return false
  276. end
  277.  
  278. prevQty = getPlayerUses(player)
  279.  
  280. playerUses[player] = prevQty + newuses
  281.  
  282. saveUses()
  283.  
  284. return true
  285. end
  286.  
  287. function withdraw(player, amount)
  288. if not config.players_database then
  289. return false
  290. end
  291. if getPlayerUses(player) < amount then
  292. return false
  293. end
  294.  
  295. prevQty = getPlayerUses(player)
  296.  
  297. playerUses[player] = (prevQty - amount)
  298. saveUses()
  299. return true
  300. end
  301. --End Codes
  302. --------------------------------------------------------
  303. --> Codes <--
  304. --------------------------------------------------------
  305. --Codes
  306. codes = {}
  307.  
  308. function loadCodes()
  309. codes = gcapi.getListFromFile("/disk/codes")
  310. end
  311.  
  312. function saveCodes()
  313. gcapi.saveListToFile("/disk/codes", codes)
  314. end
  315.  
  316. function getUsesOfCode(code)
  317. loadCodes()
  318. return codes[code]
  319. end
  320.  
  321. function addCode(code)
  322. codes[code]=1
  323. saveCodes()
  324. end
  325.  
  326. function disableCode(code)
  327. codes[code]=0
  328. saveCodes()
  329. end
  330.  
  331. function generateCode()
  332. local code = string.upper(gcapi.randomString(2)) .. tostring(gcapi.getRandom(1111,9999))
  333. if getUsesOfCode(code) == nil then
  334. addCode(code)
  335. if pcall(printPage, code) then
  336. displayMessage("Ticket Job Done!" )
  337. else
  338. displayError("Printer error")
  339. end
  340. else
  341. displayError("Try again")
  342. end
  343. end
  344.  
  345. function printPage(code)
  346. local oPrinter = peripheral.find( "printer" )
  347. oPrinter.newPage()
  348. oPrinter.setPageTitle( "Crate Ticket! ")
  349.  
  350. oPrinter.setCursorPos( 1, 2 )
  351. oPrinter.write( "The Key Code is " .. code )
  352. oPrinter.endPage()
  353. end
  354. --End Codes
  355. --------------------------------------------------------
  356. --> Log <--
  357. --------------------------------------------------------
  358. function printLog(line)
  359. print(line)
  360.  
  361. if config.players_database then
  362. local file = fs.open("/disk/log","a")
  363. file.writeLine(line)
  364. file.close()
  365. end
  366. end
  367.  
  368. --------------------------------------------------------
  369. --> Prizes <--
  370. --------------------------------------------------------
  371. prizes = {}
  372.  
  373. function loadPrizes()
  374. prizes = gcapi.getListFromFile("/disk/prizes")
  375. end
  376.  
  377. function savePrizes()
  378. gcapi.saveListToFile("/disk/prizes", prizes)
  379. end
  380.  
  381. function addPrize(id, dmg, qty, name, color)
  382. loadPrizes()
  383.  
  384. if not getIDPrize(id, dmg) then
  385. if not color then
  386. color = (2^math.random(2,14))
  387. end
  388.  
  389. prize = {["id"] = id, ["dmg"] = dmg, ["qty"] = qty, ["name"] = name, ["color"] = color}
  390. table.insert(prizes, prize)
  391.  
  392. savePrizes()
  393. return true
  394. else
  395. return false, "Prize already exists"
  396. end
  397.  
  398. end
  399.  
  400. function getIDPrize(id, dmg)
  401. loadPrizes()
  402.  
  403. for i, prize in pairs(prizes) do
  404. if id == prize["id"] then
  405. if dmg == prize["dmg"] then
  406. return i
  407. end
  408. end
  409. end
  410. return nil
  411. end
  412.  
  413. function removePrizeByID(id, dmg)
  414. loadPrizes()
  415. local i = getIDPrize(id, dmg)
  416.  
  417. if i then
  418. prizes[i] = nil
  419. savePrizes()
  420. return true
  421. end
  422. return false, "Prize doesnt exist"
  423. end
  424.  
  425. function removePrize(i)
  426. loadPrizes()
  427. i = tonumber(i)
  428. if i then
  429. prizes[i] = nil
  430. savePrizes()
  431. return true
  432. end
  433. return false, "Prize doesnt exist"
  434. end
  435.  
  436.  
  437. --------------------------------------------------------
  438. --> Spin <--
  439. --------------------------------------------------------
  440. function getRandomItem(outOfStockBefore)
  441.  
  442. local outOfStock = outOfStockBefore
  443.  
  444. if outOfStock == nil then
  445. outOfStock = {}
  446. end
  447.  
  448. local randomNumber = math.random(1, #prizes)
  449. while table.contains(outOfStock, randomNumber) do
  450. randomNumber = math.random(1, #prizes)
  451. end
  452.  
  453. local item = prizes[randomNumber]
  454. local id = item["id"]
  455. local dmg = item["dmg"] or 0
  456. local qty = item["qty"]
  457.  
  458. if me.containsItemType(id, dmg) and me.countOfItemType(id,dmg) >= qty then
  459. return item, outOfStock
  460. end
  461.  
  462. table.insert(outOfStock, randomNumber)
  463. return getRandomItem(outOfStock)
  464. end
  465.  
  466. table.contains = function (eTable, varE)
  467. for k,v in pairs(eTable) do
  468. if varE == v then
  469. return true
  470. end
  471. end
  472. return false
  473. end
  474.  
  475.  
  476. actualPrize = nil
  477. function spin(player)
  478.  
  479. printArrows()
  480.  
  481. local items = {}
  482. local outOfStock = nil
  483. for n = 1, 20, 1 do
  484. randomItem, outOfStock = getRandomItem(outOfStock)
  485. table.insert(items, randomItem)
  486. end
  487.  
  488. actualPrize = nil
  489. for offsetY = 1, 63, 1 do
  490. for i, item in pairs(items) do
  491. lastPrize = actualPrize
  492. local displayName = item["name"] .. " (" .. item["qty"] .. ")"
  493. if printBox(i, displayName, offsetY, item["color"]) then
  494. break
  495. end
  496.  
  497. if lastPrize ~= actualPrize then
  498. if actualPrize ~= nil then
  499. local local_prize = getLocalPrize(actualPrize, items)
  500. local_prize["color"] = nil
  501. local_prize["name"] = nil
  502. extractItem(local_prize)
  503. end
  504. end
  505. end
  506. music("note.harp",3,1)
  507. sleep(0.05)
  508. end
  509. music("random.successful_hit",1,1)
  510. local ok, err = pcall(movePrize)
  511. if ok then
  512. if not err then
  513. printLog(player .. " ERROR: " .. tostring(err))
  514. end
  515. else
  516. printLog(player .. " ERROR: " .. tostring(err))
  517. end
  518. emptyChest()
  519. end
  520.  
  521. function getLocalPrize(actualPrize, items)
  522. return textutils.unserialize(textutils.serialize(items[actualPrize]))
  523. end
  524.  
  525. --------------------------------------------------------
  526. --> Commands <--
  527. --------------------------------------------------------
  528. function executeCommand(player, co)
  529.  
  530. local co = (co .. " ")
  531.  
  532. local command = gcapi.split(co .. " ", " ")
  533.  
  534.  
  535. if not command[1] then
  536. return nil
  537. end
  538.  
  539. if command[1] == "generatecode" and hasPermissions(player) then
  540. generateCode()
  541. return
  542. end
  543.  
  544. if command[1] == "addprize" and hasPermissions(player) then
  545. item = pim.getAllStacks()[1]
  546. if item then
  547. local id, dmg, qty, name = item["id"], item["dmg"], item["qty"], item["name"]
  548. if command[2] and command[2] ~= '' then
  549. name = command[2]
  550. end
  551. addPrize(id, dmg, qty, name)
  552. displayMessage("Item added! -> " .. name .. " (" .. qty .. ")")
  553. else
  554. displayError("Item not found", player, true)
  555. end
  556. return
  557. end
  558.  
  559. if config.players_database and table.contains(config.commandsToMoveKeys, command[1]) then
  560. local amount = 1
  561. if command[2] and tonumber(command[2]) then
  562. amount = tonumber(command[2])
  563. end
  564.  
  565. keysMoved, err = moveKeysToSystem(amount)
  566.  
  567. if keysMoved then
  568. addUses(player, keysMoved)
  569. displayMessage(keysMoved .. " keys moved!")
  570. emptyChest()
  571. else
  572. displayError(err, player)
  573. end
  574.  
  575. return
  576. end
  577.  
  578. if command[1] == "test" and hasPermissions(player) then
  579. spin(player)
  580. return
  581. end
  582. if command[1] == "prizes" then
  583. displayPrizes()
  584. return
  585. end
  586. if command[1] == "removeprize" and hasPermissions(player) then
  587. if tonumber(command[2]) then
  588. if removePrize(i) then
  589. displayMessage("Prize removed.")
  590. end
  591. else
  592. displayError("Usage: !removeprize <i>", player)
  593. end
  594. return
  595. end
  596.  
  597. if command[1] == "code" then
  598. if command[2] then
  599. local code = string.upper(command[2])
  600. if getUsesOfCode(code) then
  601. local u = tonumber(getUsesOfCode(code))
  602. if u > 0 then
  603. disableCode(code)
  604. spin(player)
  605. sleep(3)
  606. else
  607. displayError("Code already used!")
  608. end
  609. else
  610. displayError("Code not found!")
  611. end
  612. else
  613. displayError("Usage: !code <code>")
  614. end
  615. return
  616. end
  617.  
  618. if table.contains(config.commandsToOpenCrate, command[1]) then
  619. if withdraw(player, 1) then
  620. clearWhiteWindow()
  621. displayPlayerInformation()
  622. spin(player)
  623. sleep(3)
  624. clearWhiteWindow()
  625. displayPlayerInformation()
  626. else
  627. if moveKeysToSystem(1) then
  628. clearWhiteWindow()
  629. displayPlayerInformation()
  630. spin(player)
  631. sleep(3)
  632. clearWhiteWindow()
  633. displayPlayerInformation()
  634. else
  635. displayError("You do not have enough keys!")
  636. end
  637. end
  638.  
  639. return
  640. end
  641.  
  642. if config.players_database and table.contains(config.commandsToMoveLegendary, command[1]) then
  643. local amount = 1
  644. if command[2] and tonumber(command[2]) then
  645. amount = tonumber(command[2])
  646. end
  647.  
  648. keysMoved, err = moveLegendaryKeysToSystem(amount)
  649.  
  650. if keysMoved then
  651. addUses(player, keysMoved*10)
  652. displayMessage(keysMoved .. " legendary keys moved!")
  653. emptyChest()
  654. else
  655. displayError(err, player)
  656. end
  657.  
  658. return
  659. end
  660.  
  661. displayError("Command not found!", nil, true)
  662. end
  663. --------------------------------------------------------
  664. --> Monitor <--
  665. --------------------------------------------------------
  666. weidhtBox = 34
  667. heightBox = 6
  668.  
  669. mon = nil
  670. max_x = 0
  671. max_y = 0
  672. nextReset = 0
  673.  
  674. function getCenter(f, varP)
  675. if not varP then
  676. varP = ""
  677. end
  678. if (f - string.len(varP) % 2) == 0 then
  679. return math.floor((f - string.len(varP))/2)
  680. end
  681.  
  682. return math.floor((f - string.len(varP))/2)+1
  683.  
  684. end
  685.  
  686. function setupScreen()
  687. if mon == nil then
  688. mon = peripheral.find("monitor")
  689. end
  690.  
  691. mon.setBackgroundColor(colors.white)
  692. mon.clear()
  693.  
  694. max_x, max_y = mon.getSize()
  695.  
  696. --Print bar for title
  697. mon.setBackgroundColor(config.barColor)
  698. for x=1, max_x, 1 do
  699. mon.setCursorPos(x,1)
  700. mon.write(" ")
  701. end
  702.  
  703. --Print title
  704. local x = getCenter(max_x, config.titleProgram)
  705. mon.setCursorPos(x,1)
  706. mon.setTextColor(colors.black)
  707. mon.write(config.titleProgram)
  708.  
  709. --Bottom bar for title
  710. mon.setBackgroundColor(config.barColor)
  711. for x=1, max_x, 1 do
  712. mon.setCursorPos(x,max_y)
  713. mon.write(" ")
  714. end
  715.  
  716. end
  717.  
  718. function displayError(errorMessage, player, notLog)
  719. displayMessage(errorMessage, colors.red)
  720. if player then
  721. errorMessage = player .. " - " .. "errorMessage"
  722. end
  723. if not notLog then
  724. printLog(errorMessage)
  725. end
  726. end
  727.  
  728. function displayMessage(message, color, expireTime)
  729.  
  730. if not tonumber(expireTime) then
  731. expireTime = 4
  732. end
  733.  
  734. mon.setBackgroundColor(colors.white)
  735. if not color then
  736. mon.setTextColor(colors.black)
  737. else
  738. mon.setTextColor(color)
  739. end
  740.  
  741. local x = getCenter(max_x, message)
  742. local y = getCenter(max_y)
  743.  
  744. --CLEAR OLD MESSAGE
  745. for x=1, max_x, 1 do
  746. mon.setCursorPos(x,y)
  747. mon.write(" ")
  748. end
  749.  
  750. --PRINT NEW MESSAGE
  751. mon.setCursorPos(x,y)
  752. mon.write(message)
  753.  
  754. if expireTime == -1 then
  755. nextReset = -1
  756. else
  757. nextReset = clock + expireTime
  758. end
  759.  
  760. mon.setTextColor(colors.black)
  761. end
  762.  
  763. function expireOldMessages()
  764. if clock > nextReset and nextReset ~= -1 then
  765.  
  766. mon.setBackgroundColor(colors.white)
  767. for x=1, max_x, 1 do
  768. for y=2, max_y-1 do
  769. mon.setCursorPos(x,y)
  770. mon.write(" ")
  771. end
  772. end
  773. nextReset = -1
  774. displayPlayerInformation()
  775. end
  776. end
  777.  
  778. function displayWelcome()
  779. mon.setBackgroundColor(colors.white)
  780. mon.setTextColor(colors.orange)
  781. writeCentered("Welcome to WatchStore Crates", 3)
  782.  
  783. mon.setTextColor(colors.black)
  784. writeCentered("To use this machine you need vote keys.", 5)
  785.  
  786. writeCentered("You can see the prizes on the ME monitors on your left", 7)
  787.  
  788. writeCentered("You must remain on the PIM (plessure plate)", 9)
  789. writeCentered("while the machine is rolling your reward.", 10)
  790.  
  791. writeCentered("To start playing, you need to stand on the PIM.", 12)
  792.  
  793. writeCentered("Good luck!.", 14)
  794.  
  795. if config.players_database then
  796. writeCentered("You can trade 1 legendary key for 10 vote keys.", 16)
  797. writeCentered("(stored in the machine) doing !addlegendary <qty>", 17)
  798. end
  799.  
  800. writeCentered("By using this machine and writing !play" .. ( (config.players_database and "or !addkeys <qty>") or '' ) .. ", you", max_y-4)
  801. writeCentered("acknowledge that your keys will be deducted from your inventory.", max_y-3)
  802. end
  803.  
  804. function displayPrizes()
  805. loadPrizes()
  806.  
  807. clearWhiteWindow()
  808. mon.setBackgroundColor(colors.white)
  809. mon.setTextColor(colors.orange)
  810.  
  811. writeCentered("Prizes", 4)
  812.  
  813. mon.setTextColor(colors.black)
  814. for i, prize in pairs(prizes) do
  815. writeCentered(i .. ". " .. prize["name"] .. " (" .. prize["qty"] .. ")", 5 + i)
  816. end
  817.  
  818. nextReset = clock + 8
  819. end
  820.  
  821. function writeCentered(message, y)
  822. local x = getCenter(max_x, message)
  823. mon.setCursorPos(x,y)
  824. mon.write(message)
  825. end
  826.  
  827. function clearWhiteWindow()
  828.  
  829. mon.setBackgroundColor(colors.white)
  830. local whiteLine = ""
  831. for x=1, max_x, 1 do
  832. whiteLine = whiteLine .. " "
  833. end
  834. for y=2,max_y-1,1 do
  835. mon.setCursorPos(1,y)
  836. mon.write(whiteLine)
  837. end
  838.  
  839. end
  840. function displayPlayerInformation()
  841.  
  842. player = getCurrentPlayer()
  843.  
  844. if nextReset == -1 or player == "pim" then
  845. clearWhiteWindow()
  846. end
  847.  
  848. --Clear bottom bar
  849. mon.setBackgroundColor(config.barColor)
  850. mon.setCursorPos(1, max_y)
  851. for x=1, max_x, 1 do
  852. mon.write(" ")
  853. end
  854.  
  855. if player == "pim" then
  856. displayWelcome()
  857. return nil
  858. end
  859.  
  860. --Display help message
  861. if nextReset == -1 then
  862. local message = "To play type !play"
  863. displayMessage(message, colors.lime, -1)
  864. end
  865.  
  866. mon.setTextColor(colors.black)
  867. mon.setBackgroundColor(config.barColor)
  868.  
  869. --Display keys in the inventory
  870. mon.setCursorPos(2,max_y)
  871. mon.write("Keys in the inventory: " .. getKeysInInventory() .. " ")
  872.  
  873. --Display name of player
  874. mon.setCursorPos(getCenter(max_x, player),max_y)
  875. mon.write(player)
  876.  
  877. --Display remaining keys
  878. if config.players_database then
  879. local rText = "Keys stored: " .. getPlayerUses(player)
  880. mon.setCursorPos(max_x - string.len(rText) - 2,max_y)
  881. mon.write(rText)
  882. end
  883.  
  884. end
  885.  
  886.  
  887. --> MONITOR EXTEND -> Prizes display <--
  888.  
  889. function printArrows()
  890. local centerX = getCenter(max_x)
  891. local centerY = getCenter(max_y)
  892.  
  893. local x = centerX + (weidhtBox/2) + 2
  894.  
  895. mon.setBackgroundColor(colors.white)
  896. mon.setTextColor(colors.black)
  897.  
  898. mon.setCursorPos(x, centerY)
  899. mon.write("<----")
  900.  
  901.  
  902. local x = centerX - (weidhtBox/2) - 1 - string.len("---->")
  903.  
  904. mon.setCursorPos(x, centerY)
  905. mon.write("---->")
  906. end
  907.  
  908. function printBox(i, name, offsetY, backgroundColor)
  909.  
  910. local min_y_local = max_y + (i-1)*heightBox - offsetY
  911.  
  912. if min_y_local > - heightBox then
  913.  
  914. if not backgroundColor then
  915. backgroundColor = colors.orange
  916. end
  917.  
  918. local max_y_local = min_y_local+heightBox
  919.  
  920. local centerY = getCenter(max_y)
  921. local centerX = getCenter(max_x)
  922. local min_x_local = centerX-(weidhtBox/2)
  923. local max_x_local = centerX+(weidhtBox/2)
  924.  
  925. if min_y_local < centerY and centerY < max_y_local then
  926. actualPrize = i
  927. end
  928.  
  929. printBoxPaint(min_x_local, min_y_local, max_x_local, max_y_local, backgroundColor)
  930.  
  931. --PRINT NAME
  932. mon.setBackgroundColor(backgroundColor)
  933. local yText = getCenter(max_y_local-min_y_local) + min_y_local - 1
  934. local xText = getCenter(max_x, name)
  935. mon.setTextColor(colors.black)
  936. mon.setCursorPos(xText, yText)
  937. if yText > 2 and yText < (max_y - 1) then
  938. mon.write(name)
  939. end
  940. end
  941. if min_y_local > max_y + heightBox then
  942. return true
  943. end
  944. return false
  945. end
  946.  
  947. function isBorder(x,y, min_x_local, min_y_local, max_x_local, max_y_local)
  948. if x == min_x_local or y == min_y_local then
  949. return true
  950. end
  951. if x == max_x_local or y == max_y_local then
  952. return true
  953. end
  954. return false
  955. end
  956.  
  957. function printBoxPaint(min_x_local, min_y_local, max_x_local, max_y_local, backgroundColor)
  958. for x=min_x_local, max_x_local, 1 do
  959. for y=min_y_local, max_y_local, 1 do
  960.  
  961. mon.setCursorPos(x,y)
  962. --PAINT BOX
  963. if isBorder(x,y, min_x_local, min_y_local, max_x_local, max_y_local) then
  964. mon.setBackgroundColor(colors.black)
  965. else
  966. mon.setBackgroundColor(backgroundColor)
  967. end
  968. if y > 2 and y < (max_y - 1) then
  969. mon.write(" ")
  970. end
  971.  
  972. end
  973. end
  974. end
  975.  
  976. --------------------------------------------------------
  977. --> Music <--
  978. --------------------------------------------------------
  979. noteblock = nil
  980. function music(int, pitch, vol)
  981. if not noteblock then
  982. noteblock = peripheral.find("music")
  983. end
  984. noteblock.playSound(int, pitch, vol)
  985. end
  986. --------------------------------------------------------
  987. --> Main program <--
  988. --------------------------------------------------------
  989. clock = 0
  990. oldPlayer = nil
  991. function GUIUpdater()
  992. while true do
  993.  
  994. --Update clock
  995. clock = os.clock()
  996.  
  997. --Get current player
  998. player = getCurrentPlayer()
  999.  
  1000. --Monitor tasks
  1001. if not usingMonitor then
  1002. if player ~= "pim" then
  1003. expireOldMessages()
  1004. end
  1005. if oldPlayer ~= player then
  1006. displayPlayerInformation()
  1007. end
  1008. oldPlayer = player
  1009. end
  1010.  
  1011. sleep(0.25)
  1012. end
  1013. end
  1014.  
  1015. usingMonitor = false
  1016. function listener()
  1017. sleep(1)
  1018. while true do
  1019. tEvent = {os.pullEvent()}
  1020. if tEvent[1] == "chatEvent" then
  1021. player = tEvent[2]
  1022. co = tEvent[3]
  1023. if checkPlayer(player) then
  1024. usingMonitor = true
  1025. executeCommand(player, co)
  1026. oldPlayer = nil
  1027. usingMonitor = false
  1028. else
  1029. print(player .. " no esta en el PIM")
  1030. end
  1031. end
  1032.  
  1033. if tEvent[1] == "chat_command" then
  1034. co = tostring(tEvent[2])
  1035. player = tostring(tEvent[3])
  1036. if checkPlayer(player) then
  1037. usingMonitor = true
  1038. executeCommand(player, co)
  1039. oldPlayer = nil
  1040. usingMonitor = false
  1041. else
  1042. print(player .. " no esta en el PIM")
  1043. end
  1044. end
  1045. end
  1046. end
  1047.  
  1048. function startProgram()
  1049. --Setup screen
  1050. setupScreen()
  1051.  
  1052. --LOAD FILES
  1053. loadPrizes()
  1054. loadCodes()
  1055. loadUses()
  1056.  
  1057. parallel.waitForAny(listener,GUIUpdater)
  1058. error()
  1059. end
  1060.  
  1061. parallel.waitForAny(gcapi.startChatEventQueue, startProgram)
  1062. error()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement