Muzze77

Bank_Lycodon

Aug 6th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.11 KB | None | 0 0
  1. shell.run("clear")
  2. uu = 0
  3. pipe = peripheral.wrap("bottom")
  4. mon = peripheral.wrap("top")
  5. mon.setTextScale(0.5)
  6. gx, gy = mon.getSize()
  7.  
  8. mon.clear()
  9. bank = "bank"
  10.  
  11. users = {}
  12. users = {["Count"] = 0}
  13.  
  14.  
  15. firstRun = 0
  16. minUser = "NONE"
  17. minUserMoney = 0
  18. auszahlen = 0
  19. einzahlen = 0
  20. ueberweisen = 0
  21. state = "NONE"
  22. sx = 0
  23. sy = 0
  24. tag = {"EINZAHLEN","AUSZAHLEN","UEBERWEISEN"}
  25. calc = 0
  26. function save(tab)
  27.  
  28.     f = fs.open(bank,"w")
  29.     f.write(textutils.serialize(tab))
  30.     f.close()
  31. end
  32.  
  33.  
  34. function load()
  35.     f = fs.open(bank,"r")
  36.     tab = f.readAll()
  37.     f.close()
  38.  
  39.     return  textutils.unserialize(tab)
  40. end
  41.  
  42. if ( fs.exists("bank")) then
  43.    
  44.     users = load()
  45.    
  46. else
  47.    
  48.     save(users)
  49. end
  50.  
  51.  
  52.  
  53. function setUser(usr)
  54.     users = load()
  55.    
  56.      math.randomseed(os.time())
  57.     id = math.floor(math.random()*10) .. math.floor(math.random()*10) .. math.floor(math.random()*10) .. math.floor(math.random()*10) .. "-" .. math.floor(math.random()*10) .. math.floor(math.random()*10) .. math.floor(math.random()*10) .. math.floor(math.random()*10)
  58.     c = 0
  59.  
  60.     if (users.Count ~= 0) then
  61.             for v, k in pairs(users) do
  62.             if (v ~= "Count") then
  63.                 if (v == usr) then
  64.                     c= c + 1
  65.                 end
  66.             end
  67.         end
  68.     else
  69.    
  70.  
  71.    
  72.  
  73.     end
  74.    
  75.     if usr ~= "NONE" then
  76.         if (c == 0) then
  77.        
  78.    
  79.    
  80.             if disk.isPresent("front") then
  81.                 f = fs.open("disk/ID","w")
  82.                 f.write(id)
  83.                 f.close()
  84.             end
  85.            
  86.             users[usr] = {}
  87.             users[usr]["Money"] = 0
  88.             users[usr]["ID"] = id
  89.             users.Count = users.Count + 1
  90.             save(users)
  91.        
  92.             cbut(2,gy,"                   ")
  93.             cbut(2,gy,"User wurde erstellt")
  94.             sleep(2)
  95.        
  96.         else
  97.        
  98.  
  99.         end
  100.     end
  101.     save(users)
  102.     return users
  103. end
  104.  
  105.  
  106.  
  107.  
  108. function getUser()
  109. cbut(2,gy,"                           ")
  110. cbut(2,gy,"Warten auf User")
  111. if disk.isPresent("front") then
  112.  
  113.   disk.eject("front")
  114.  
  115. end
  116.  
  117. evt,side = os.pullEvent("disk")
  118. minUser = disk.getLabel("front")
  119. gusr = disk.getLabel("front")
  120.  if gusr == nil then
  121.     disk.eject("front")
  122.     shell.run("reboot")
  123. end
  124.  
  125.   users = setUser(gusr)
  126.   save(users)
  127.    
  128.    
  129.    
  130.    f = fs.open("disk/ID","r")
  131.    id = f.readAll()
  132.    f.close()
  133.    
  134.    
  135. if (id == nil or users[minUser].ID == nil) then
  136.     minUser = "NONE"
  137.  
  138.        
  139.        
  140. else
  141.     if (id ~= users[minUser].ID) then
  142.             cbut(2,gy,"                             ")
  143.             cbut(2,gy,"Falsche ID: " .. minUser)
  144.             minUser = "NONE"
  145.             sleep(2)
  146.    
  147.     cbut(2,gy,"                             ")
  148.     else
  149.         cbut(2,gy,"                   ")
  150.         cbut(2,gy,"User wurde geladen")
  151.         sleep(2)
  152.        minUser = gusr
  153.        minUserMoney = users[minUser].Money
  154.     end
  155. end
  156.    
  157.    
  158.    
  159.  
  160.    
  161. disk.eject("front")
  162.  
  163. if minUser == nil then
  164.     minUser = "NONE"
  165. end
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. return minUser
  173. end
  174.  
  175. uID = 0
  176. new = 0
  177. function getUU()
  178.   pipe.getAvailableItems()
  179.   evt , items = os.pullEvent("available_items_return")
  180.  
  181.   count = 0
  182.   for v, k in pairs(items) do
  183.     name = pipe.getItemName(k[1])
  184.     if name == "UU-Matter" then
  185.     uID =  k[1]
  186.       count = count + k[2]
  187.     end
  188.   end
  189.  
  190.   return count
  191. end
  192.  
  193. function cbut(cx,cy,text)
  194. mon.setCursorPos(cx,cy)
  195. mon.write(text)
  196. end
  197.  
  198. function uu_bank()
  199. mon.clear()
  200. cbut(2,2,"                             ")
  201. cbut(2,2,"UU-Matter in Bank: " .. getUU())
  202. end
  203.  
  204.  
  205.  
  206. function addMoney()
  207. money = {}
  208. money["Max"] = 0
  209. repeat
  210. old = getUU()
  211.  
  212.  
  213.  
  214. uu_bank()
  215.     cbut(2,5,"                                  ")
  216.     cbut(2,5,"USER: " .. minUser)
  217.     cbut(2,6,"                                  ")
  218.     cbut(2,6,"UU-Matter USER: " .. string.format("%d",minUserMoney))
  219.         cbut(2,8,"                                  ")
  220.         cbut(2,8,tag[1] .. " <<")
  221.         cbut(2,10,"                                         ")
  222.         cbut(2,10,"R [" )
  223.         cbut(8,10,string.format("%d",money["Max"]) )
  224.         cbut(19,10,"]  OK")
  225. evt,a,b,c = os.pullEvent("monitor_touch")
  226.  
  227.  
  228.  
  229. uu = getUU()
  230. calc = uu - old
  231. if  calc ~= 0 then
  232.      money["Max"] =  money["Max"] + calc
  233. end
  234.  
  235.     if b == 22 or b == 23 and c == 10 then
  236.    
  237.     users[minUser]["Money"] = users[minUser]["Money"]  + money["Max"]
  238.     term.setCursorPos(1,5)
  239.     minUserMoney = users[minUser]["Money"]
  240.     save(users)
  241.     calc = 0
  242.     einzahlen = 0
  243.     state = "NONE"
  244.    
  245.    
  246.     end
  247.     cbut(2,gy-1,"X:" .. string.format("%d", b))
  248.     cbut(8,gy-1,"Y:" .. string.format("%d", c))
  249. until (not(einzahlen > 0))
  250.  
  251.  
  252. end
  253.  
  254. function bank_activate()
  255.  
  256. if state == "NONE" then
  257.     cbut(2,8,"                                  ")
  258.     cbut(2,8,"EINZAHLEN")
  259.    
  260.     cbut(20,8,"                                  ")
  261.     cbut(20,8,"AUSZAHLEN")
  262.  
  263.     cbut(40,8,"                                  ")
  264.     cbut(40,8,"UEBERWEISEN")
  265.  
  266.    
  267.  
  268.  
  269.     elseif state == "+" then
  270.         cbut(2,8,"                                  ")
  271.         cbut(2,8,tag[1] .. " <<")
  272.         cbut(2,10,"                                         ")
  273.         cbut(2,10,"R [" )
  274.         cbut(8,10,string.format("%d",calc) )
  275.         cbut(19,10,"]  OK")
  276.        
  277.     elseif state == "-" then
  278.         cbut(20,8,"                                  ")
  279.         cbut(20,8,tag[2] .. " <<")
  280.         cbut(2,10,"                                         ")
  281.         cbut(2,10,"<51 [" )
  282.         cbut(8,10,string.format("%d",calc) )
  283.         cbut(19,10,"] 15>")
  284.     elseif state == ">>" then
  285.         cbut(40,8,"                                  ")
  286.         cbut(40,8,tag[3] .. " <<")
  287.         cbut(2,10,"                                         ")
  288.         cbut(2,10,"<51 [" )
  289.         cbut(8,10,string.format("%d",calc) )
  290.         cbut(19,10,"] 15>")
  291.         cbut(2,gy,"Noch gesperrt")
  292.     end
  293.  
  294.     cbut(gx-7,1,"LOGOUT")
  295. end
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306. while true do
  307.     term.setCursorPos(1,1)
  308.     print("###>INIT USERS<###")
  309.     print("###" .. textutils.serialize(users) .. "###")
  310.     print("###<INIT USERS>###")
  311.     term.setCursorPos(1,6)
  312.     uu_bank()
  313.     uu = getUU()
  314.     cbut(2,5,"                                  ")
  315.     cbut(2,5,"USER: " .. minUser)
  316.     cbut(2,6,"                                  ")
  317.     cbut(2,6,"UU-Matter USER: " .. string.format("%d",minUserMoney))
  318.    
  319.     cbut(2,gy-1,"X:" .. string.format("%d", sx))
  320.     cbut(8,gy-1,"Y:" .. string.format("%d", sy))
  321.    
  322.    
  323.     if minUser == "NONE" or uu < 1 then
  324.         cbut(2,gy-2,"                ")
  325.         cbut(2,gy-2,"Bank Deaktiviert")
  326.        
  327.     else
  328.         cbut(2,gy-2,"                ")
  329.         cbut(2,gy-2,"Bank Aktiviert  ")
  330.         bank_activate()
  331.     end
  332.  
  333.     if minUser == "NONE" then
  334.         minUser = getUser()
  335.    
  336.    
  337.     else
  338.    
  339.     cbut(2,gy,"                         ")
  340.     end
  341.     if (disk.isPresent("front")) then
  342.         disk.eject("front")
  343.     end
  344.     if (firstRun ~= 0) then
  345.        
  346.        
  347.         evt ,a,b,c = os.pullEvent()
  348.         if (evt == disk) then
  349.             disk.eject("front")
  350.         end
  351.         if evt == "monitor_touch" then
  352.             sx = b
  353.             sy = c
  354.    
  355.             if minUser ~= "NONE" then
  356.                 print(minUser)
  357.                 term.setCursorPos(1,7)
  358.                 if b >= gx-7 and b < gx and c == 1 then
  359.                    
  360.                     cbut(2,gy,"                                         ")
  361.                     cbut(2,gy,minUser .. " hat sich ausgelogt")
  362.                     minUser = "NONE"
  363.                     minUserMoney = 0
  364.                 elseif (b >= 2 and b < 2+ string.len(tag[1])  and c == 8 and state == "NONE")  then
  365.                    
  366.                     state = "+"
  367.                     einzahlen = 1
  368.                     addMoney()
  369.                    
  370.                 elseif (b >= 20 and b < 20+ string.len(tag[2]) and c == 8 and state == "NONE") then
  371.                     state = "-"
  372.                 elseif (b >= 40 and b < 40+ string.len(tag[3]) and c == 8 and state == "NONE") then
  373.                     state = ">>"
  374.                 elseif (b>= 2+ string.len(tag[1])+1 and b< 2+ string.len(tag[1])+3   and c == 8 and state == "+") then
  375.                     state = "NONE"
  376.                 elseif (b>= 20+ string.len(tag[2])+1 and b< 20+ string.len(tag[2])+3 and c == 8 and state == "-") then
  377.                     state = "NONE"
  378.                 elseif (b>= 40+ string.len(tag[3])+1 and b< 40+ string.len(tag[3])+3 and c == 8 and state == ">>") then
  379.                     state = "NONE"
  380.                 elseif (b == 2 and c == 10 and state ~= "NONE") then
  381.                 if (state == "-"  or state == ">>" ) then
  382.                    
  383.                     calc = 0
  384.                     print("MIN")
  385.            
  386.                 end
  387.                 elseif (b == 3 and c == 10 and state ~= "NONE") then
  388.                 if (state == "-"  or state == ">>") then
  389.                    
  390.                         calc = calc - 5
  391.                         if calc < 0 then
  392.                             calc = 0
  393.                         end
  394.                         print("-5 ")
  395.                    
  396.                 end
  397.                 elseif (b == 4 and c == 10 and state ~= "NONE") then
  398.                 if (state == "-"  or state == ">>") then
  399.  
  400.                         calc = calc - 1
  401.                         if calc < 0 then
  402.                             calc = 0
  403.                         end
  404.                         print("-1 ")
  405.  
  406.                 end
  407.                 elseif (b == 21 and c == 10 and state ~= "NONE") then
  408.                 if (state == "-"  or state == ">>") then
  409.  
  410.                         calc = calc + 1
  411.                         if calc > minUserMoney and (state == "-"  or state == ">>") then
  412.                             calc = minUserMoney
  413.                         end
  414.                         print("+1 ")
  415.  
  416.                 end
  417.                 elseif (b == 22 and c == 10 and state ~= "NONE") then
  418.                     if (state == "-"  or state == ">>") then
  419.                         calc = calc + 5
  420.                         if calc > minUserMoney  then
  421.                             calc = minUserMoney
  422.                            
  423.                         end
  424.                         print("+5 ")
  425.                     end
  426.                 elseif (b == 23 and c == 10 and state ~= "NONE") then
  427.                 if (state == "-"  or state == ">>")  then
  428.                    
  429.                         calc = minUserMoney
  430.                         print("MAX")
  431.                    
  432.                 end
  433.                 elseif (b >= 6 and b <= 19 and c == 10) then
  434.                     if (state == "-" ) then
  435.                         print(uID)
  436.                         pipe.makeRequest(uID,calc)
  437.                         minUserMoney = minUserMoney - calc
  438.                         users[minUser]["Money"] =  users[minUser]["Money"] - calc
  439.                         save(users)
  440.                         calc = 0
  441.                         state = "NONE"
  442.                     elseif (state == "-" ) then
  443.                         cbut(2,gy,"Noch gesperrt")
  444.                         calc = 0
  445.                         state = "NONE"
  446.                     end
  447.                 end
  448.             end
  449.         end
  450.    
  451.     end
  452.     firstRun = 1
  453.  
  454. end
Add Comment
Please, Sign In to add comment