Advertisement
Guest User

test1

a guest
Sep 21st, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.11 KB | None | 0 0
  1. -- by EnnekPL
  2. ------API's--------
  3. os.loadAPI("Apis/button")
  4. os.loadAPI("Apis/MisAPI")
  5. os.loadAPI("Apis/nbtAPI")
  6. -----Peripherals------
  7. mon = peripheral.wrap("back")
  8. p = peripheral.wrap("left")
  9. open = peripheral.wrap("right")
  10. --rednet.open("bottom")
  11. --test = open.listMethods()
  12. --print(test)
  13. --fs.makeDir("dane/")
  14.  
  15. ------Settings--------
  16. world = p.getWorld(p.getPeripheralWorldID())
  17. poz = world.getClosestPlayer( 37,62, -60, 20)
  18. --username = poz.getUsername()
  19. info = {}
  20. infoEmc = {}
  21. daneZapis = {"4:0","5:0"}
  22. daneOdczyt = {}
  23. tabEmcVal = {}
  24. tempTab = {}
  25.  
  26. local slotySkrzyni = 10
  27. local punkty = 0
  28. poj = false
  29. slotS = 0
  30. a, c, b = 37,62, -59 --poz skrzyni startowej
  31. d, e, f = 36, 62, -65 -- pozycja skrzynie przech
  32. local emc = 0
  33. local str = 1
  34. recordNaStr =1
  35. maxStron = 1
  36. iloscWybr = 1
  37. sumka = 0
  38. -------Functions-------
  39. function save(table, name)
  40.     local file = fs.open(name, "w")
  41.     file.write(textutils.serialize(table))
  42.     file.close()
  43.  sleep(0.1)
  44. end
  45. function load(name)
  46.     local file = fs.open(name, "r")
  47.     local data = file.readAll()
  48.     file.close()
  49. -- sleep(0.1)
  50.     return textutils.unserialize(data)
  51. end
  52. function cleanCheast(d, e, f)
  53.     local skrzynia = world.getTileEntity(d, e, f)
  54.     local nbt1 = nbtAPI.readTileNBT(skrzynia)
  55.     for i=0,10,1 do
  56.         table.insert(nbt1.value.Items.value, {type="compound", value={id={type="short", value=0}, Count={type="byte", value=0}, Damage={type="short", value=0}, Slot={type="byte", value=i}}})
  57.     end
  58.     nbtAPI.writeTileNBT(skrzynia, nbt1)
  59. end
  60.  
  61. function pay(a, c, b, itemIds, metData, ilosc)
  62. --  world.setBlock(a, c, b, 433, 5)
  63.     local te = world.getTileEntity(a, c, b)
  64.     local nbt = nbtAPI.readTileNBT(te)
  65.  for zz=1,ilosc,1 do
  66.     table.insert(nbt.value.Items.value, {type="compound", value={id={type="short", value=itemIds}, Count={type="byte", value=1}, Damage={type="short", value=metData}, Slot={type="byte", value=zz}}})
  67.     nbtAPI.writeTileNBT(te, nbt)
  68.  end
  69.     sleep(0.2)
  70. --  world.setBlock(a, c, b, 0, 0)
  71. -- sleep(0.2)
  72. end
  73.  
  74. function findInTable(myTable, searchValue)
  75.     for keys,vals in pairs(myTable) do
  76.         if vals == searchValue then
  77.             return true
  78.         end
  79.     end
  80.     return false
  81. end
  82. function findInTable2(myTable1, searchValue1)
  83.     for keys,vals in pairs(myTable1) do
  84.         for keys1,vals1 in pairs(vals) do
  85.             if vals1 == searchValue1 then
  86.                 return true
  87.             end
  88.         end
  89.     end
  90.     return false
  91. end
  92.  
  93. function setInfo2(name, nazwaP, idP, pktMin, lp, xmin, xmax, ymin, ymax, strona)
  94.     infoEmc[name] = {}
  95.     infoEmc[name]["nazwa"] = nazwaP -- nazwa bloku/itemu
  96.     infoEmc[name]["idP"] = idP -- id bloku/itemu
  97.     infoEmc[name]["pktMin"] = pktMin -- punkty do odjecia emc
  98.     infoEmc[name]["lp"]  = lp -- numer
  99.     infoEmc[name]["xmin"] = xmin
  100.     infoEmc[name]["ymin"] = ymin
  101.     infoEmc[name]["xmax"] = xmax
  102.     infoEmc[name]["ymax"] = ymax
  103.     infoEmc[name]["strona"] = strona
  104. end
  105. function clearTableEMC()
  106.    infoEmc = {}
  107. end
  108. function sprawdzUsera()
  109.     world = p.getWorld(p.getPeripheralWorldID())
  110.     poz = world.getClosestPlayer( 37,62, -60, 20)
  111.     username = poz.getUsername()
  112.     if fs.exists("dane/dane_"..username) and fs.exists("dane/emc_"..username) then
  113.         daneOdczyt = load("dane/dane_"..username)
  114.         loadEmc = load("dane/emc_"..username)
  115.         emc = tonumber(loadEmc)
  116.     else
  117.         emc = 0
  118.         save(daneZapis, "dane/dane_"..username)
  119.         save(emc, "dane/emc_"..username)
  120.     end
  121. end
  122. function clearNauka(y)
  123.     button.write("                                                   ",colors.lime, 10, y)
  124. end
  125.  
  126. function odbierzDane(slotS)
  127.     if fs.exists("dane/dane_player") and fs.exists("dane/emc_player") then
  128.         daneOdczyt = load("dane/dane_player")
  129.         loadEmc = load("dane/emc_player")
  130.         emc = tonumber(loadEmc)
  131.     else
  132.         emc = 0
  133.         save(daneZapis, "dane/dane_player")
  134.         save(emc, "dane/emc_player")
  135.     end
  136.         morDet = open.getStackInSlot(slotS)
  137.     --  print(morDet.name.."| ID: "..morDet.id..":"..morDet.dmg.." ilosc: "..morDet.qty)
  138.     --  morDet = open.getStackInSlot(0)
  139.         if morDet == nil then
  140.             poj = false
  141.             idMet = "0:0"
  142.             iloscMat = 0
  143.         else
  144.             --poj = true
  145.             idMet = morDet.id..":"..morDet.dmg
  146.             iloscMat = morDet.qty
  147.             if findInTable(daneOdczyt, idMet) == false and findInTable2(tabEmcVal, idMet) == true then
  148.                 table.insert(daneOdczyt, idMet)
  149.                 clearNauka(40)
  150.                 button.writeCenter("Nauczono monitor: "..morDet.name,colors.lightBlue,10,62, 40)
  151.                 save(daneOdczyt, "dane/dane_player")
  152.             else
  153.                 clearNauka(40)
  154.             end
  155.             if findInTable2(tabEmcVal, idMet) == false then
  156.                 pay(a, c, b, morDet.id, morDet.dmg, morDet.qty)
  157.                 button.writeCenter("Nie Mozna Nauczyc: "..morDet.name,colors.red,10,62, 40)
  158.             end
  159.         end
  160.     --  print(idMet.." - "..iloscMat)
  161.     return poj, idMet, iloscMat
  162. end
  163. function setInfo(name, Nazwa, emc)
  164.     info[name] = {}
  165.     info[name]["Nazwa"] = Nazwa -- nazwa
  166.     info[name]["emc"] = emc --kolor cieczy
  167. end
  168.  
  169. function fillTable2()
  170.     setInfo("EMC", emc)
  171. end
  172.  
  173.  
  174. function odswierzDane(nazwa)
  175.     sumka= 0
  176.     for name, data in pairs(infoEmc) do
  177.         if str == data["strona"] and nazwa == data["nazwa"] then
  178.             button.InfoButton(tostring(iloscWybr), colors.blue, 33,40,32,34)
  179.             button.InfoButton(data["nazwa"].." EMC = "..MisAPI.dot(data["pktMin"]), colors.blue, 12,60,28,30)
  180.             sumka = data["pktMin"] * iloscWybr
  181.             if sumka <= emc then
  182.                 button.InfoButton("Koszt: "..MisAPI.dot(sumka),colors.green, 12,35,36,38)
  183.                 button.setTable(" Wydaj ", butt6, 37,60,36,38)
  184.                 clearNauka(39)
  185.             else
  186.                 button.InfoButton("Koszt: "..MisAPI.dot(sumka),colors.red, 12,35,36,38)
  187.                 clearNauka(39)
  188.                 button.writeCenter("ZA MALO EMC!!!",colors.lime,10,62, 39)
  189.             end
  190.             return nazwa66
  191.         else
  192.             button.InfoButton("BRAK WYBRANEGO PRZEDMIOTU", colors.red, 12,60,28,30)
  193.         end
  194.     end
  195. end
  196. function WyswietlMozliwe(tab1, tab2, idMet, iloscMat)
  197.     tempTab = {}
  198.     t={}
  199.     t= tab2
  200.     preEmc = 0
  201.     punkty = emc
  202.     tempX = 1  
  203.    
  204.     for k,v in pairs(t) do
  205.         for k1,v1 in pairs(v) do
  206.             for k2,v2 in pairs(tab1) do
  207.                 if v1 == v2 and iloscMat > 0 and idMet ==v1 then
  208.                 --  print(k1.." : "..v1.." :"..k.." : "..iloscMat)
  209.                     preEmc = tonumber(k) * tonumber(iloscMat)
  210.                 --  print(k1.." ID: "..v1.." PreEMC: "..preEmc.." = "..iloscMat.." * "..k)
  211.                 end
  212.                 if v1 == v2 and k <= (punkty + preEmc) then
  213.                     tempTab1 = {k1,v1,k}
  214.                     table.insert(tempTab,tempTab1)
  215.                
  216.                 end
  217.                 tempX = tempX +1
  218.                  
  219.             end
  220.             emc1 = preEmc
  221.                 --print(k.." - "..k1.." : "..v1)
  222.         end
  223.     end
  224.     --save(tempTab, "dane/test")
  225.     return emc1, tempTab
  226. end
  227.  
  228. function wys(tempArray)
  229.     button.pole(colors.black, 11,61,9,25)
  230.     xb1= 12
  231.     xb2= 26
  232.     yb1= 10
  233.     yb2= 12
  234.     wysMax = 5
  235.     maxStron1 =1
  236.     for ix=1, #tempArray do
  237.         maxStron1 = (ix / 12)
  238.         maxStron = math.ceil(maxStron1)
  239.         if ix >= recordNaStr and ix <= recordNaStr + 11 and emc >= tempArray[ix][3] then
  240.            
  241.             setInfo2(tempArray[ix][1],tempArray[ix][1], tempArray[ix][2], tempArray[ix][3], ix,xb1,xb2,yb1,yb2, str)
  242.             button.InfoButton(tempArray[ix][1], colors.red, xb1,xb2, yb1,yb2)
  243.             yb1= yb1 + 4
  244.             yb2= yb2 + 4
  245.             if yb2 > 26 and xb1 == 12 then
  246.                 --print("dupa")
  247.                 xb1 = xb1 + 16
  248.                 xb2 = xb1 + 16
  249.                 yb1 = 10
  250.                 yb2 = 12   
  251.             end
  252.             if yb2 > 26 and xb1 == 28 then
  253.                 --print("dupa1")
  254.                 xb1 = xb1 + 18
  255.                 xb2 = xb2 + 16
  256.                 yb1 = 10
  257.                 yb2 = 12
  258.             end
  259.             --clearTableEMC()
  260.         end
  261.     end
  262.    
  263.     button.screen()
  264.     fillTable2()
  265. end
  266. function butt4()
  267.     iloscWybr = iloscWybr + 1
  268.     if iloscWybr > 64 then iloscWybr = 64 end
  269.     button.flash(" + ")
  270.     odswierzDane(nazwa66)
  271.     fillTable2()
  272. end
  273. function butt7()
  274.     iloscWybr = iloscWybr + 10
  275.     if iloscWybr > 64 then iloscWybr = 64 end
  276.     button.flash("+10")
  277.     odswierzDane(nazwa66)
  278.     fillTable2()
  279. end
  280. function butt5()
  281.     iloscWybr = iloscWybr - 1
  282.     if iloscWybr < 1 then iloscWybr = 1 end
  283.     odswierzDane(nazwa66)
  284.     button.flash(" - ")
  285.     fillTable2()
  286. end
  287. function butt8()
  288.     iloscWybr = iloscWybr - 10
  289.     if iloscWybr < 1 then iloscWybr = 1 end
  290.     odswierzDane(nazwa66)
  291.     button.flash("-10")
  292.     fillTable2()
  293. end
  294. function butt10()
  295.     iloscWybr = 64
  296.     odswierzDane(nazwa66)
  297.     button.flash("MAX")
  298.     fillTable2()
  299. end
  300. function butt11()
  301.     iloscWybr = 1
  302.     odswierzDane(nazwa66)
  303.     button.flash("MIN")
  304.     fillTable2()
  305. end
  306. function butt6()
  307.     sumka= 0
  308.     for name, data in pairs(infoEmc) do
  309.         if str == data["strona"] and nazwa66 == data["nazwa"] then
  310.             sumka = data["pktMin"] * iloscWybr
  311.             if sumka < emc then
  312.                 emc = emc - sumka
  313.                 idSub = data["idP"]
  314.                 pom = 0
  315.                 p = idSub:find(":") - pom
  316.                 pom = pom + p
  317.                 itemIds = idSub:sub(1, p -1)
  318.                 metData = idSub:sub(p + 1)  
  319.                 itemIds1 = tonumber(itemIds)
  320.                 metData1 = tonumber(metData)
  321.                 iloscWybr1 = tonumber(iloscWybr)
  322.             --  print(itemIds.."-"..metData)   
  323.                 pay(a, c, b, itemIds1, metData1, iloscWybr1)
  324.             --  print("Kupiono: "..iloscWybr.." szt. "..data["nazwa"].." za ".. sumka.." emc. ")
  325.             else
  326.             --  print("za malo emc ".. emc)
  327.             end
  328.             if emc <= 0 then emc = 0 end
  329.             save(emc, "dane/emc_player")
  330.          end
  331.     end
  332.     str = 1  -- ??????????
  333.     recordNaStr = 1
  334.     odswierzDane(nazwa66)
  335.     odbierzDane(0) 
  336.    
  337.     button.flash(" Wydaj ")
  338.     fillTable2()
  339.     return str, recordNaStr
  340. end
  341.  
  342. function uzupelnij(x, y, iloscWybr)
  343.     sumka= 0
  344.     for name, data in pairs(infoEmc) do
  345.         if str == data["strona"] then
  346.             if y>=data["ymin"] and  y <=data["ymax"] then
  347.                 if x>=data["xmin"] and x<= data["xmax"] then
  348.                     button.setTable(" + ", butt4, 42,46, 32,34)
  349.                     button.setTable("+10", butt7, 48,52, 32,34)
  350.                     button.setTable("MAX", butt10, 54,58, 32,34)
  351.                     button.setTable(" - ", butt5, 26,30, 32,34)
  352.                     button.setTable("-10", butt8, 20,24, 32,34)
  353.                     button.setTable("MIN", butt11, 14,18, 32,34)
  354.                     button.InfoButton(tostring(iloscWybr), colors.blue, 32,40,32,34)
  355.                     button.InfoButton(data["nazwa"].." EMC = "..MisAPI.dot(data["pktMin"]), colors.blue, 12,60,28,30)
  356.                     sumka = data["pktMin"] * iloscWybr
  357.                     nazwa66 = data["nazwa"]
  358.                     if sumka <= emc then
  359.                         button.InfoButton("Koszt: "..MisAPI.dot(sumka),colors.green, 12,35,36,38)
  360.                         button.setTable(" Wydaj ", butt6, 37,60,36,38)
  361.                         clearNauka(39)
  362.                     else
  363.                         button.InfoButton("Koszt: "..MisAPI.dot(sumka),colors.red, 12,35,36,38)
  364.                         clearNauka(39)
  365.                         button.writeCenter("ZA MALO EMC!!!",colors.lime,10,62, 39)
  366.                     end
  367.                     return sumka
  368.                 else
  369.                     button.InfoButton("BRAK WYBRANEGO PRZEDMIOTU", colors.red, 12,60,28,30)    
  370.                 end
  371.             end
  372.            
  373.         end
  374.     end
  375. end
  376. function butt1()
  377.     str = str -1
  378.     recordNaStr = recordNaStr - 12
  379.     if recordNaStr < 1 then recordNaStr = 1 end
  380.     if str < 1 then
  381.         str = 1
  382.     else
  383.    
  384.     end
  385.     button.flash("BACK")
  386.     fillTable2()
  387.    
  388.     --return pokaz()
  389. end
  390.  
  391. function butt2()
  392.     str = str +1
  393.     --recordNaStr = recordNaStr + 12
  394.     if str > maxStron then
  395.         str = maxStron
  396.     else
  397.         recordNaStr = recordNaStr + 12
  398.     end
  399.     button.flash("NEXT")
  400.     fillTable2()
  401.     --return pokaz()
  402. end
  403.  
  404. mon.clear()
  405. shell.run("clear")
  406. tabEmcVal = load("dane/emc_value")
  407.  
  408. button.setTable("BACK", butt1, 2,8, 8,39)
  409. button.setTable("NEXT", butt2, 64,70, 8,39)
  410.  
  411. button.border(colors.blue,10,62,8,26,1)
  412. button.border(colors.yellow,2,70,2,6,1)
  413. odbierzDane(0) 
  414. WyswietlMozliwe(daneOdczyt, tabEmcVal, idMet, iloscMat)
  415. button.write(" MASZ EMC: "..MisAPI.dot(emc),colors.lime, 20, 4)
  416.  
  417.  
  418. while true do
  419.     button.write("                         ",colors.lime, 20, 4)
  420.     button.write(" MASZ EMC: "..MisAPI.dot(emc),colors.lime, 20, 4)
  421.     button.pole(colors.black, 11,61,9,25)
  422.     clearTableEMC()
  423.     WyswietlMozliwe(daneOdczyt, tabEmcVal, idMet, iloscMat)
  424.     wys(tempTab)
  425.     button.write(" STRONA: "..str.."/"..maxStron, colors.yellow, 53, 4)
  426.     button.screen()
  427.     event,side,x,y,z = os.pullEvent()
  428.     if event == "redstone" or event == "monitor_touch" then
  429.         if event == "redstone" then
  430.             if redstone.getInput("top") == true then
  431.                 zasysa = 1
  432.                 iloscMat1 = 0
  433.                 while zasysa > 0 do
  434.                     --print("poj "..poj)
  435.                     if  poj == false then
  436.                         zasysa=0
  437.                     end
  438.                     sleep(0.8)
  439.                     for sloty=0,slotySkrzyni,1 do
  440.                         odbierzDane(sloty)
  441.                         WyswietlMozliwe(daneOdczyt, tabEmcVal, idMet, iloscMat)
  442.                         emc = emc + emc1
  443.                         save(emc, "dane/emc_player")
  444.                         odswierzDane(nazwa66)
  445.                     --  print("EMC ="..emc)
  446.                     end
  447.                     cleanCheast(d, e, f)
  448.                     sleep(0.3)
  449.                 end
  450.             end
  451.         elseif event == "monitor_touch" then
  452.             uzupelnij(x, y,iloscWybr)
  453.             button.checkxy(x, y)
  454.         end
  455.     end
  456.     fillTable2()
  457. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement