Advertisement
MagmaLP

mit Top List

May 25th, 2024 (edited)
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.18 KB | None | 0 0
  1. os.loadAPI("ocs/apis/sensor")
  2. s = sensor.wrap("top")
  3. rednet.open("back")
  4. term.setBackgroundColor(32768)
  5. term.setCursorPos(1,1)
  6. term.clear()
  7.  
  8. ------------------------------------------------------------
  9. term.setTextColor(colors.yellow)
  10. print("Item Trading System")
  11. print("~~~~~~~~~~~~~~~~~~~")
  12. print(" ")
  13. term.setTextColor(colors.red)
  14. print(" ")
  15. print("please come closer !!!")
  16. print("please come closer !!!")
  17. print("please come closer !!!")
  18. print(" ")
  19. print("Always stand inside the white circle")
  20. print("on the floor, that goes around the Computer.")
  21. print(" ")
  22. print(" ")
  23. print(" ")
  24. print(" ")
  25. term.setTextColor(colors.lightGray)
  26. print("Shop build by MagmaLP")
  27. print("System programmed by MagmaLP")
  28. print("01.01.2023")
  29. print("In case of errors please report to MagmaLP")
  30. ------------------------------------------------------------
  31.  
  32. local itemList = { "HV Solar Panel", "MV Solar Panel", "LV Solar Panel", "ME Cable", "ME Storage Bus", "ME Import Bus",
  33.                  "ME Export Bus",
  34.                  "ME Controller", "ME Access Terminal", "ME Drive", "Re-Enriched Uranium Cell", "ME 64k Storage", "ME 16k Storage",
  35.                  "ME 4k Storage" }
  36.  
  37. function calc_color(color)
  38.     return math.pow(2, color - 1)
  39. end
  40.  
  41. function msg(gap, line, color, groundb, text)
  42.     term.setBackgroundColor(calc_color(groundb))
  43.     term.setCursorPos(gap,line)
  44.     term.setTextColor(calc_color(color))
  45.     text = term.write(text)
  46. end
  47.  
  48. file = fs.open("local/", "w")           --name local löschen
  49. file.write("                                ")
  50. file.close()
  51.  
  52. m0 = 0
  53. m1 = 0
  54. m2 = 0
  55. m3 = 0
  56. m4 = 0
  57. m5 = 0
  58. m6 = 0
  59. m7 = 0
  60. m8 = 0
  61. m9 = 0
  62. m10 = 0
  63. m11 = 0
  64. m12 = 0
  65. m13 = 0
  66. m14 = 0
  67. m15 = 0
  68. m16 = 0
  69. m17 = 0
  70. M = {m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, m16, m17}
  71.  
  72. b1 = 0
  73. b2 = 0
  74. b3 = 8000
  75. b4 = 1000
  76. b5 = 150
  77. b6 = 10
  78. b7 = 125
  79. b8 = 125
  80. b9 = 120
  81. b10 = 250
  82. b11 = 150
  83. b12 = 65
  84. b13 = 3
  85. b14 = 5000
  86. b15 = 2000
  87. b16 = 1000
  88. b17 = 0
  89. B = {b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17}
  90.  
  91. function kaufen()
  92.  
  93.         local event, button, X, Y = os.pullEvent("mouse_click")
  94.         for i = 3,13 do                                             --item von zeile 3-13
  95.             if X >= 40 and X <= 42 and Y == i then                  -- +1
  96.                 if M[i] < 60 then                                   -- max 60
  97.                     M[i] = M[i] + 1
  98.                     c = c + B[i]
  99.                     msg(44,i,04,16,""..M[i].."")
  100.                     msg(42,19,06,16,""..c.."$")
  101.                     break
  102.                 end
  103.             end
  104.             if X >= 47 and X <= 49 and Y == i then                  -- -1
  105.                 if M[i] > 0 then
  106.                     M[i] = M[i] - 1
  107.                     c = c - B[i]
  108.                     msg(44,i,04,16,""..M[i].." ")
  109.                     msg(42,19,06,16,""..c.."$     ")
  110.                     break
  111.                 end
  112.             end
  113.         end
  114.        
  115.         for i = 14,16 do                                            --item von zeile 14-16
  116.             if X >= 40 and X <= 42 and Y == i then                  -- +1
  117.                 if M[i] < 1 then                                    -- max 1
  118.                     M[i] = M[i] + 1
  119.                     c = c + B[i]
  120.                     msg(44,i,04,16,""..M[i].."")
  121.                     msg(42,19,06,16,""..c.."$")
  122.                     break
  123.                 end
  124.             end
  125.             if X >= 47 and X <= 49 and Y == i then                  -- -1
  126.                 if M[i] > 0 then
  127.                     M[i] = M[i] - 1
  128.                     c = c - B[i]
  129.                     msg(44,i,04,16,""..M[i].." ")
  130.                     msg(42,19,06,16,""..c.."$     ")
  131.                     break
  132.                 end
  133.             end
  134.         end
  135.        
  136.         c = tonumber(c)
  137.         money = tonumber(money)        
  138.  
  139.         if X >= 11 and X <= 21 and Y == 19 then                 --Abbrechen
  140.             os.reboot()  
  141.                    
  142.         elseif X >= 01 and X <= 08 and Y == 19 then             --Kaufen    
  143.             if c > money then                                   --zu wenig Geld
  144.                 msg(41,19,01,15," "..c.."$ ")
  145.                 sleep(0.25)
  146.                 msg(41,19,06,16," "..c.."$ ")
  147.                 sleep(0.25)
  148.                 msg(41,19,01,15," "..c.."$ ")
  149.                 sleep(0.25)
  150.                 msg(41,19,06,16," "..c.."$ ")
  151.                 sleep(0.5)
  152.                
  153.             elseif c <= money then                              --genug Geld
  154.                 money = money - c
  155.            
  156.                 file = fs.open("local/" , "r")              --read user
  157.                 name = file.readLine()
  158.                 file.close()
  159.                 file = fs.open("user/" ..name.. "/", "w")
  160.                 file.write(money)
  161.                 file.close()
  162.            
  163.                 if fs.exists("spendmoney/" ..name) == false then          --create spendmoney directory
  164.                     file = fs.open("spendmoney/" ..name.. "/", "w")
  165.                     file.write(0.0)
  166.                     file.close()
  167.                 end
  168.            
  169.                 file = fs.open("spendmoney/" ..name, "r")             --read user of spendmoney directory
  170.                 oldspendmoney = file.readLine()
  171.                 file.close()
  172.                
  173.                 spendmoney = oldspendmoney + c
  174.                
  175.                 file = fs.open("spendmoney/" ..name.. "/", "w")
  176.                 file.write(spendmoney)
  177.                 file.close()
  178.                    
  179.                 package = {}
  180.                 for i,amount in pairs(M) do     --sende Daten an turtle
  181.                     local a = {}
  182.                        
  183.                     if amount > 0 then
  184.                         local num = i - 2
  185.                         a = {
  186.                             ["Slot"] = num,
  187.                             ["Amount"] = amount
  188.                         }
  189.                         table.insert(package, a)
  190.                         writeToFile(num, amount)
  191.                     end
  192.                 end
  193.                 rednet.open("back")
  194.                 rednet.send(11894,textutils.serialize(package))  
  195.                 os.reboot()
  196.              end
  197.         end
  198. --    end
  199.     kaufen()  
  200. end
  201.  
  202. function writeToFile(item, amount)
  203.     local file = http.get("http://www.atomuhr.de/uhrzeit/")
  204.     local file2 = file.readAll()
  205.     local length = file2.len(file2)
  206.     local start = length - 40
  207.     local endString = length - 22
  208.     local time = string.sub(file2, start, endString)
  209.                
  210.     file = fs.open("kaufliste", "a")
  211.     file.write("\n", " ")
  212.     file.write(time.." ")
  213.     file.write(name.." ")
  214.     file.write(itemList[item].." "..amount)
  215.     file.close()
  216. end
  217.  
  218. while true do
  219.     for name,info in pairs(s.getTargets()) do               --detect user
  220.         if fs.exists("user/" ..name) == false then          --create user
  221.             file = fs.open("user/" ..name.. "/", "w")
  222.             file.write(0.0)
  223.             money = "0.0"
  224.             file.close()
  225.         else
  226.             file = fs.open("user/" ..name, "r")             --read user
  227.             money = file.readLine()
  228.             file.close()
  229.         end
  230.        
  231.         file = fs.open("local/", "w")           --name local speichern
  232.         file.write(name)
  233.         file.close()
  234.        
  235.         for a = 1,20 do
  236.             for b = 1,51 do
  237.                 msg(b,a,16,16," ")
  238.             end
  239.         end
  240.        
  241.         msg(01,01,06,16,"< Account of "..name.." >")   msg(36,01,06,16," >  "..money.."$        ")      --überschrift
  242.         msg(01,02,04,16,"+-------------------------------------------------+")
  243.        
  244.         msg(06,07,01,15,"                  ")   msg(30,07,01,15,"                ")         --mainscreen
  245.         msg(06,08,01,15,"  deposit money   ")   msg(30,08,01,15,"   buy items    ")
  246.         msg(06,09,01,15,"                  ")   msg(30,09,01,15,"                ")
  247.        
  248.         msg(02,13,09,16,"This PC exchanges items for virtual money")
  249.         msg(02,14,09,16,"(saved on this PC)")
  250.         msg(02,15,09,16,"in order to buy other items with it.")
  251.        
  252.         msg(01,19,01,16,"                   >> Top-List <<                       ")
  253.        
  254.         local event, button, X, Y = os.pullEvent("mouse_click")
  255.         if X >= 06 and X <= 23 and Y >= 07 and Y <= 09 then                                 --Verkauf Screen
  256.             for a = 3,20 do
  257.                 for b = 1,50 do
  258.                     msg(b,a,16,16," ")
  259.                 end
  260.             end
  261.             sleep(0.5)
  262.             msg(02,06,01,15,"                                                 ")
  263.             msg(02,07,01,15," click here to see a list of all sellable items  ")
  264.             msg(02,08,01,15,"                                                 ")
  265.  
  266.             msg(02,12,09,16,"Throw in all items on the left and confirm with")
  267.             msg(02,13,09,16,"the button below. The money is stored permanently ")
  268.             msg(02,14,09,16,"until you buy something with it.")
  269.            
  270.             msg(40,16,01,15,"          ")
  271.             msg(40,17,01,15," confirm  ")   msg(32,17,01,16,"----->")
  272.             msg(40,18,01,15,"          ")
  273.            
  274.             msg(01,19,16,01,"back")
  275.            
  276.             local event, button, X, Y = os.pullEvent("mouse_click")
  277.             if X >= 03 and X <= 48 and Y >= 06 and Y <= 08 then         --item list button
  278.                 for a = 3,20 do
  279.                     for b = 1,50 do
  280.                         msg(b,a,16,16," ")
  281.                     end
  282.                 end
  283.                
  284.                 msg(02,04,09,16,"1x Shard of Minium =   25$         (64x = 1600$)")
  285.                 msg(02,05,09,16,"1x Quartz Crystal  =   25$         (64x = 1600$)")
  286.                 msg(02,06,09,16,"1x Lead Ore        =    4$         (64x = 256$)")
  287.                 msg(02,07,09,16,"1x Silver Ore      =    3$         (64x = 192$)")
  288.                 msg(02,08,09,16,"1x Tungsten Ore    =   40$         (64x = 2560$)")
  289.                 msg(02,09,09,16,"1x Ferrouse Ore    =   50$         (64x = 3200$)")
  290.                 msg(02,10,09,16,"1x Ghast Tear      =  120$         (64x = 7680$)")
  291.                 msg(02,11,09,16,"1x Shiny Ingot     =  500$         (64x = 32000$)")
  292.                 msg(02,12,09,16,"1x Wither Skull    = 2000$")
  293.                 msg(02,13,09,16,"1x Nether Star     = 6000$")
  294.                
  295.                 msg(01,19,16,01,"back")                
  296.                 local event, button, X, Y = os.pullEvent("mouse_click")
  297.                 if X >= 01 and X <= 04 and Y == 19 then                 --back button
  298.                     os.reboot()
  299.                 end
  300.             elseif X >= 40 and X <= 49 and Y >= 16 and Y <= 18 then     --confirm button
  301.                 rednet.send(11900,1)
  302.                 x, y, z = rednet.receive()
  303.                 money = y + money
  304.                 local file = fs.open("user/" ..name.. "/", "w")
  305.                 file.write(money)
  306.                 file.close()
  307.             elseif X >= 01 and X <= 04 and Y == 19 then                 --back button2
  308.                     os.reboot()
  309.             end
  310.                
  311.         elseif X >= 30 and X <= 45 and Y >= 07 and Y <= 09 then         --kaufen Screen
  312.  
  313.             for a = 3,20 do
  314.                 for b = 1,50 do
  315.                     msg(b,a,16,16," ")
  316.                 end
  317.             end
  318.             c = 0.0
  319.             msg(01,03,04,16,"| 8000$ HV Solar Panel")               msg(51,03,04,16,"|")
  320.             msg(01,04,04,16,"| 1000$ MV Solar Panel")               msg(51,04,04,16,"|")
  321.             msg(01,05,04,16,"|  150$ LV Solar Panel")               msg(51,05,04,16,"|")
  322.             msg(01,06,04,16,"|   10$ ME Cable         ")            msg(51,06,04,16,"|")
  323.             msg(01,07,04,16,"|  125$ ME Storage Bus   ")            msg(51,07,04,16,"|")
  324.             msg(01,08,04,16,"|  125$ ME Import Bus    ")            msg(51,08,04,16,"|")
  325.             msg(01,09,04,16,"|  120$ ME Export Bus    ")            msg(51,09,04,16,"|")
  326.             msg(01,10,04,16,"|  250$ ME Controller    ")            msg(51,10,04,16,"|")
  327.             msg(01,11,04,16,"|  150$ ME Access Terminal")           msg(51,11,04,16,"|")
  328.             msg(01,12,04,16,"|   65$ ME Drive")                     msg(51,12,04,16,"|")
  329.             msg(01,13,04,16,"|    3$ Re-Enriched Uranium Cell")     msg(51,13,04,16,"|")
  330.             msg(01,14,04,16,"| 5000$ ME 64k Storage   ")            msg(51,14,04,16,"|")
  331.             msg(01,15,04,16,"| 2000$ ME 16k Storage   ")            msg(51,15,04,16,"|")
  332.             msg(01,16,04,16,"| 1000$ ME  4k Storage   ")            msg(51,16,04,16,"|")
  333.             msg(01,17,04,16,"|                        ")            msg(51,17,04,16,"|")
  334.             msg(01,18,04,16,"+-------------------------------------------------+")
  335.             msg(01,19,01,15,"  Buy   ") msg(11,19,01,15,"  Cancel   ")
  336.             msg(34,19,04,16,"Amount:")  msg(42,19,06,16,""..c.."$")  
  337.  
  338.             for i = 3,16 do                     -- +für zeile 3-16
  339.                 msg(40,i,01,15," + ")  
  340.             end
  341.             for i = 3,16 do                     -- 0für zeile 3-16
  342.                 msg(44,i,04,16,"0")
  343.             end
  344.             for i = 3,16 do                     -- -für zeile 3-16
  345.                 msg(47,i,01,15," - ")
  346.             end  
  347.             kaufen()
  348.         elseif X >= 19 and X <= 34 and Y >= 18 and Y <= 19 then         --Top-List Screen
  349.             for a = 1,20 do
  350.                 for b = 1,50 do
  351.                     msg(b,a,16,16," ")
  352.                 end
  353.             end
  354.            
  355.             msg(01,01,06,16,"                 Money - Top List                  ")      --überschrift 1
  356.             msg(01,02,04,16,"+-------------------------------------------------+")
  357.            
  358.             -- Funktion zum Einlesen des Inhalts einer Datei und Umwandeln in eine Zahl
  359.             local function readNumberFromFile(fileName)
  360.                 local file = fs.open(fileName, "r")
  361.                 if file then
  362.                     local content = file.readAll()
  363.                     file.close()
  364.                     return tonumber(content)
  365.                 end
  366.                 return nil
  367.             end
  368.  
  369.             -- Verzeichnisname
  370.             local directory = "user"
  371.  
  372.             -- Liste aller Dateien im Verzeichnis
  373.             local files = fs.list(directory)
  374.  
  375.             -- Tabelle zur Speicherung der Spieler und ihrer Punkte
  376.             local scores = {}
  377.  
  378.             -- Durchlaufen aller Dateien im Verzeichnis
  379.             for _, file in ipairs(files) do
  380.                 local filePath = fs.combine(directory, file)
  381.                 local score = readNumberFromFile(filePath)
  382.                 if score then
  383.                     table.insert(scores, {name = file, score = score})
  384.                 end
  385.             end
  386.  
  387.             -- Funktion zum Sortieren der Scores
  388.             local function compareScores(a, b)
  389.                 return a.score > b.score
  390.             end
  391.  
  392.             -- Sortieren der Scores
  393.             table.sort(scores, compareScores)
  394.  
  395.             -- Ausgabe der Top 5 Spieler
  396.             for i = 1, math.min(6, #scores) do
  397.                 print(i .. ". " .. scores[i].name .. " - " .. scores[i].score)
  398.             end
  399.            
  400.             msg(01,10,06,16,"              Money spend - Top List               ")      --überschrift 2
  401.             msg(01,11,04,16,"+-------------------------------------------------+")
  402.            
  403.             -- Funktion zum Einlesen des Inhalts einer Datei und Umwandeln in eine Zahl
  404.             local function readNumberFromFile(fileName)
  405.                 local file = fs.open(fileName, "r")
  406.                 if file then
  407.                     local content = file.readAll()
  408.                     file.close()
  409.                     return tonumber(content)
  410.                 end
  411.                 return nil
  412.             end
  413.  
  414.             -- Verzeichnisname
  415.             local directory = "spendmoney"
  416.  
  417.             -- Liste aller Dateien im Verzeichnis
  418.             local files = fs.list(directory)
  419.  
  420.             -- Tabelle zur Speicherung der Spieler und ihrer Punkte
  421.             local scores = {}
  422.  
  423.             -- Durchlaufen aller Dateien im Verzeichnis
  424.             for _, file in ipairs(files) do
  425.                 local filePath = fs.combine(directory, file)
  426.                 local score = readNumberFromFile(filePath)
  427.                 if score then
  428.                     table.insert(scores, {name = file, score = score})
  429.                 end
  430.             end
  431.  
  432.             -- Funktion zum Sortieren der Scores
  433.             local function compareScores(a, b)
  434.                 return a.score > b.score
  435.             end
  436.  
  437.             -- Sortieren der Scores
  438.             table.sort(scores, compareScores)
  439.  
  440.             -- Ausgabe der Top 5 Spieler
  441.             for i = 1, math.min(6, #scores) do
  442.                 print(i .. ". " .. scores[i].name .. " - " .. scores[i].score)
  443.             end
  444.            
  445.             msg(01,19,16,01,"back")                
  446.             local event, button, X, Y = os.pullEvent("mouse_click")
  447.             if X >= 01 and X <= 04 and Y == 19 then                 --back button
  448.                 os.reboot()
  449.             end
  450.         end    
  451.     end
  452. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement