Advertisement
Guest User

startup

a guest
Mar 3rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.37 KB | None | 0 0
  1. --[[
  2.  
  3. Monitor -> front
  4. Logistic Pipe -> right
  5. Player Sensor -> bottom
  6. Inventory Sensor -> left
  7.  
  8.  
  9. --]]
  10.  
  11. os.pullEvent = os.pullEventRaw
  12.  
  13. mon = peripheral.wrap("front")
  14.  
  15. term.clear()
  16. term.setCursorPos(1,1)
  17. mon.clear()
  18. mon.setCursorPos(1,1)
  19. mon.setTextScale(0.5)
  20.  
  21.  if fs.isDir("users") ~= true then
  22.    fs.makeDir("users")
  23.  end
  24.  if fs.isDir("data") ~= true then
  25.    fs.makeDir("data")
  26.  end
  27.  
  28. os.loadAPI("ocs/apis/sensor")
  29.  
  30. pipe = peripheral.wrap("right")
  31.  
  32. ------------------------------------
  33.  
  34. local offset = {
  35.   X = 4,
  36.   Y = 0,
  37.   Z = 0
  38. }
  39.  
  40. local radius = 6
  41.  
  42.  
  43. function distance(pos)
  44.   local xd = pos.X - offset.X
  45.   local yd = pos.Y - offset.Y
  46.   local zd = pos.Z - offset.Z
  47.   return math.sqrt(xd*xd + yd*yd + zd*zd)
  48. end
  49.  
  50.  
  51. function player()
  52. local tablePCount = {}
  53. local tablePlayer = {}
  54. local proximity = sensor.wrap("bottom")
  55. local targets = proximity.getTargets()
  56.   for k, v in pairs(targets) do
  57.         if distance(v.Position) < radius then
  58.           table.insert(tablePCount, "1")
  59.           table.insert(tablePlayer, k)
  60.         end
  61.   end
  62.  
  63.   if sum(tablePCount) == 1 then
  64.    return tablePlayer[1]
  65.   else
  66.    local player = 0
  67.    return player
  68.   end
  69. end
  70.  
  71.  
  72. function sum(data)
  73.  local sum = 0
  74.   for k, v in pairs(data) do
  75.     sum = sum + v
  76.   end
  77.  return sum
  78. end
  79.  
  80.  
  81. function xPos(txt)
  82.   local l = string.len(txt)
  83.   local a, b = mon.getSize()
  84.   local xpos = math.floor((a - l)/2 + 1)
  85.   return xpos
  86. end
  87.  
  88. function pos(x,y)
  89.  return mon.setCursorPos(x,y)
  90. end
  91.  
  92.  
  93. function loadpw(user)
  94.   local file = fs.open("users/" .. user, "r")
  95.     if file then
  96.      local password = tostring(file.readLine())
  97.      file.close()
  98.      return password
  99.     else
  100.      return nil
  101.     end
  102. end
  103.  
  104.  
  105. function savepw(p, pw)
  106.   file = fs.open("users/" .. p, "w")
  107.   file.writeLine(pw)
  108.   file.close()
  109. end
  110.  
  111.  
  112. function button(x, y, size, lenght, color_txt, color_bg, txt)
  113.   for yPos = y, y+size-1 do
  114.     pos(x, yPos)
  115.     mon.setBackgroundColor(color_bg)
  116.     mon.write(string.rep(" ", lenght))
  117.   end
  118.    local ypos = math.floor(y + (size/2))
  119.    local l = string.len(txt)
  120.    local xpos = math.floor((lenght - l)/2 + x)
  121.    pos(xpos, ypos)
  122.    mon.setTextColor(color_txt)
  123.    mon.write(txt)
  124.   mon.setTextColor(colors.white)
  125.   mon.setBackgroundColor(colors.black)
  126. end
  127.  
  128.  
  129. function register()
  130. cap = math.random(10000, 99999)
  131.  
  132.   mon.clear()
  133.   mon.setBackgroundColor(colors.black)
  134.   mon.setTextColor(colors.white)
  135.   pos(1,1)
  136.   mon.write("Bitte benutze nicht deine Minecraft Passwort!")
  137.   pos(1,3)
  138.   mon.write("ID:")
  139.   pos(1,5)
  140.   mon.write("Passwort: ")
  141.   pos(23,7)
  142.   mon.setBackgroundColor(colors.white)
  143.   mon.setTextColor(colors.black)
  144.   mon.write(string.rep(" ", 7))
  145.   pos(23,8)
  146.   mon.write(" " .. cap .. " ")
  147.   pos(23,9)
  148.   mon.write(string.rep(" ", 7))
  149.   mon.setBackgroundColor(colors.black)
  150.   mon.setTextColor(colors.white)
  151.   pos(1,11)
  152.   mon.write("Captcha: ")
  153.  
  154.   pos(23,5)
  155.   password = read()
  156.   pos(23,11)
  157.   captcha = tonumber(read())
  158. end
  159.  
  160.  
  161. function registerbuttons()
  162.   button(4, 16, 3, 14, colors.white, colors.orange, "Zuruecksetzen")
  163.   button(20, 16, 3, 14, colors.white, colors.red, "Abbrechen")
  164.   button(36, 16, 3, 14, colors.white, colors.green, "Abschliessen")
  165.   pos(23,3)
  166.   mon.write(player())
  167.   pos(23,5)
  168.  
  169.   while true do
  170.    local event, side, X, Y = os.pullEvent("monitor_touch")
  171.    if X >= 4 and X <= 17 and Y >= 16 and Y <= 18 then
  172.          pos(1,14)
  173.          mon.setTextColor(colors.orange)
  174.          mon.write("Felder werden zurueckgesetzt")
  175.          sleep(2)
  176.          parallel.waitForAll(register, registerbuttons)
  177.    elseif X >= 20 and X <= 33 and Y >= 16 and Y <= 18 and button == 1 then
  178.          pos(1,14)
  179.          mon.setTextColor(colors.red)
  180.          mon.write("Registrierung abgebrochen!")
  181.          sleep(2)
  182.          startscreen()
  183.    elseif X >= 36 and X <= 49 and Y >= 16 and Y <= 18 and button == 1 then
  184.       if captcha == cap and string.len(password) > 4 then
  185.          savepw(player(), password)
  186.          pos(1,14)
  187.          mon.setTextColor(colors.lime)
  188.          mon.write("Registrierung erfolgreich!")
  189.          sleep(1)
  190.          startscreen()
  191.        elseif password == nil then
  192.          pos(1,14)
  193.          mon.setTextColor(colors.red)
  194.          mon.write("Passwort zu kurz! (Mindstlaenge: 5)")
  195.          sleep(3)
  196.          parallel.waitForAll(register, registerbuttons)
  197.        elseif string.len(password) < 5 then
  198.          pos(1,14)
  199.          mon.setTextColor(colors.red)
  200.          mon.write("Passwort zu kurz! (Mindstlaenge: 5)")
  201.          sleep(3)
  202.          parallel.waitForAll(register, registerbuttons)
  203.        elseif captcha ~= cap then
  204.          pos(1,14)
  205.          mon.setTextColor(colors.red)
  206.          mon.write("Captcha falsch!")
  207.          sleep(3)
  208.          parallel.waitForAll(register, registerbuttons)
  209.        end
  210.    end
  211.   end
  212. end
  213.  
  214. function status()
  215. pos(1,1)
  216. mon.clearLine()
  217. mon.write("ID: " .. player())
  218. --[[ if loadpw(player()) ~= nil then
  219.    pos(27,1)
  220.    mon.write("Status: registriert")
  221.  else
  222.    pos(27,1)
  223.    mon.write("Status: nicht registriert")
  224.  end--]]
  225. end
  226.  
  227.  
  228. function request(id, amount, data)
  229.  id = pipe.getItemIdentifierIDFor(id, data or 0)
  230.  pipe.makeRequest(id, amount)
  231. end
  232.  
  233. function startscreen()
  234. mon.clear()
  235.  status()
  236.  
  237.   while true do
  238.    if loadpw(player()) ~= nil then
  239.      button(18, 8, 3, 15, colors.white, colors.green, "Login")
  240.    else
  241.      button(18, 8, 3, 15, colors.white, colors.green, "Registrierung")
  242.    end
  243.  
  244.    local event, side, X, Y = os.pullEvent("monitor_touch")
  245.    if X >= 18 and X <= 32 and Y >= 8 and Y <= 10 and loadpw(player()) ~= nil then
  246.          login()
  247.    elseif X >= 18 and X <= 32 and Y >= 8 and Y <= 10 and loadpw(player()) == nil then
  248.          parallel.waitForAll(register, registerbuttons)
  249.    end
  250.   end
  251. end
  252.  
  253. function storage(itemName)
  254.  local tableItem = {}
  255.  local prox = sensor.wrap("left")
  256.  local targets = prox.getTargets()
  257.  for k, v in pairs(targets) do
  258.   local details = prox.getTargetDetails(k)
  259.   if details.Name == "Diamond Chest" then
  260.    for i = 1, 108, 1 do
  261.     local item = details.Slots[i].Name
  262.     local amount = details.Slots[i].Size
  263.       if item == itemName then
  264.         table.insert(tableItem, amount)
  265.       end
  266.    end
  267.   end
  268.  end
  269.  return tonumber(sum(tableItem))
  270. end
  271.  
  272.  
  273. function saveFile()
  274.   local file = fs.open("data/" .. player(), "w")
  275.   file.writeLine("" .. OakWood)
  276.   file.close()
  277. end
  278.  
  279. function loadFile()
  280.   local file = fs.open("data/" .. player(), "r")
  281.    if file then
  282.     OakWood = tonumber(file.readLine())
  283.     file.close()
  284.    else
  285.     OakWood = 512
  286.    end
  287. end
  288.  
  289.  
  290. function selection()
  291.  mon.clear()
  292.  pos(xPos("Auswahl"), 3)
  293.  mon.setTextColor(colors.lime)
  294.  mon.write("Auswahl")
  295.  
  296.  button(2, 5, 3, 8, colors.white, colors.brown, "Wood")
  297.  button(28, 21, 3, 8, colors.white, colors.red, "Exit")
  298.  
  299.   while true do
  300.    status()
  301.    local event, side, X, Y = os.pullEvent("monitor_touch")
  302.    if X ~= nil and Y ~= nil then
  303.    if X >= 2 and X <= 9 and Y >= 5 and Y <= 7 and player() ~= 0 then
  304.          loadFile()
  305.          wahl("Oak Wood", OakWood, 17)
  306.    elseif X >= 28 and X <= 35 and Y >= 21 and Y <= 23 then
  307.          os.reboot()
  308.    else
  309.     term.clear()
  310.     print("nope")
  311.    end
  312.    end
  313.   end
  314. end
  315.  
  316. function touchpad(x,y)
  317.  
  318. mon.setTextColor(colors.lightGray)
  319. for yPos = y,(y+11), 2 do
  320.  pos(x,yPos)
  321.  mon.clearLine()
  322.  mon.write("+---+---+---+")
  323. end
  324.  pos(x,(y+1))
  325.  mon.clearLine()
  326.  mon.write("|")
  327.  pos((x+12),(y+1))
  328.  mon.write("|")
  329.  
  330. for yPos = (y+3),(y+9), 2 do
  331.  pos(x,yPos)
  332.  mon.write("|")
  333.  pos((x+4),yPos)
  334.  mon.write("|")
  335.  pos((x+8),yPos)
  336.  mon.write("|")
  337.  pos((x+12),yPos)
  338.  mon.write("|")
  339. end
  340. mon.setTextColor(colors.white)
  341.  
  342. local x2 = x + 2
  343. local y2 = y + 3
  344.  
  345. for i = 1,3 do
  346.  pos(x2,y2)
  347.  mon.write("" .. i)
  348.  x2 = x2 + 4
  349. end
  350.  
  351. local x2 = x + 2
  352. local y2 = y + 5
  353.  
  354. for i = 4,6 do
  355.  pos(x2,y2)
  356.  mon.write("" .. i)
  357.  x2 = x2 + 4
  358. end
  359.  
  360. local x2 = x + 2
  361. local y2 = y + 7
  362.  
  363. for i = 7,9 do
  364.  pos(x2,y2)
  365.  mon.write("" .. i)
  366.  x2 = x2 + 4
  367. end
  368.  
  369. pos((x+6),(y+9))
  370. mon.write("0")
  371.  
  372. pos((x+1),(y+9))
  373. mon.setTextColor(colors.red)
  374. mon.write("DEL")
  375.  
  376. pos((x+9),(y+9))
  377. mon.setTextColor(colors.green)
  378. mon.write("ENT")
  379. mon.setTextColor(colors.white)
  380.  
  381. button(28, 21, 3, 8, colors.white, colors.red, "Exit")
  382.  
  383.  
  384. local tableNum = {}
  385.  
  386. while true do
  387.  local event, side, X, Y = os.pullEvent("monitor_touch")
  388. if event == "monitor_touch" then
  389.    if X >= (x+1) and X <= (x+3) and Y == (y+3) then -- 1
  390.       table.insert(tableNum, tonumber(1) )
  391.    elseif X >= (x+5) and X <= (x+7) and Y == (y+3) then -- 2
  392.       table.insert(tableNum, tonumber(2) )
  393.    elseif X >= (x+9) and X <= (x+11) and Y == (y+3) then -- 3
  394.       table.insert(tableNum, tonumber(3) )
  395.    elseif X >= (x+1) and X <= (x+3) and Y == (y+5) then -- 4
  396.       table.insert(tableNum, tonumber(4) )
  397.    elseif X >= (x+5) and X <= (x+7) and Y == (y+5) then -- 5
  398.       table.insert(tableNum, tonumber(5) )
  399.    elseif X >= (x+9) and X <= (x+11) and Y == (y+5) then -- 6
  400.       table.insert(tableNum, tonumber(6) )
  401.    elseif X >= (x+1) and X <= (x+3) and Y == (y+7) then -- 7
  402.       table.insert(tableNum, tonumber(7) )
  403.    elseif X >= (x+5) and X <= (x+7) and Y == (y+7) then -- 8
  404.       table.insert(tableNum, tonumber(8) )
  405.    elseif X >= (x+9) and X <= (x+11) and Y == (y+7) then -- 9
  406.       table.insert(tableNum, tonumber(9) )
  407.    elseif X >= (x+5) and X <= (x+9) and Y == (y+9) then -- 0
  408.       table.insert(tableNum, tonumber(0) )
  409.    elseif X >= (x+1) and X <= (x+3) and Y == (y+9) then -- DELETE
  410.       tableNum = {}
  411.       pos((x+1),(y+1))
  412.       mon.write(string.rep(" ", 11))
  413.    elseif X >= 28 and X <= 35 and Y >= 21 and Y <= 23 then
  414.          os.reboot()
  415.    end
  416.  
  417.    local inputStr = ""
  418.     for k,v in pairs( tableNum ) do
  419.       inputStr = tonumber(inputStr .. v)
  420.     end
  421.  
  422.    if X >= (x+9) and X <= (x+11) and Y == (y+9) then -- ENTER
  423.       return inputStr
  424.    end
  425.  
  426.    if string.len(inputStr) > 11 then
  427.      return inputStr
  428.    end
  429.  
  430.    pos((x+1),(y+1))
  431.    mon.write("" .. inputStr)
  432.  end
  433. end
  434. end
  435.  
  436.  
  437.  
  438. function wahl(itemName, amountStor, id, data)
  439.  mon.clear()
  440.  pos(1,1)
  441.  mon.write("Derzeitge Wahl: " .. itemName)
  442.  pos(1,3)
  443.  mon.write("Im Lager vorhanden: " .. storage(itemName))
  444.  pos(1,5)
  445.  mon.write("Dir noch zur Verfuegung stehend: " .. amountStor)
  446.  pos(1,8)
  447.  mon.write("Wie viel " .. itemName)
  448.  pos(1,9)
  449.  mon.write("moechtest du ausgeben?")
  450.  local amount = tonumber(touchpad(1,14))
  451. if amount ~= nil then
  452.  if amount <= storage(itemName) and amount <= amountStor then
  453.     request(id, amount, data or 0)
  454.     OakWood = amountStor - amount
  455.     saveFile()
  456.     pos(1,1)
  457.     mon.clear()
  458.     mon.write(amount .. " " .. itemName .. " liegen nun")
  459.     pos(1,2)
  460.     mon.write("rechts im ME-System")
  461.     sleep(2)
  462.     selection()
  463.  elseif amount > storage(itemName) then
  464.     pos(1,11)
  465.     mon.clearLine()
  466.     mon.write("Im Lager ist nicht genuegend")
  467.     pos(1,12)
  468.     mon.clearLine()
  469.     mon.write(itemName .. " vorhanden.")
  470.     sleep(2)
  471.     selection()
  472.  elseif amount > amountStor then
  473.     pos(1,11)
  474.     mon.clearLine()
  475.     mon.write("Du hast fuer diese Woche bereits")
  476.     pos(1,12)
  477.     mon.clearLine()
  478.     mon.write("den maximalen Betrag abgeholt.")
  479.     sleep(2)
  480.     selection()
  481.  end
  482. else
  483. pos(1,11)
  484. mon.clearLine()
  485. mon.setTextColor(colors.red)
  486. mon.write("Gebe bitte eine Zahl ein.")
  487. mon.setTextColor(colors.white)
  488. sleep(2)
  489. wahl(itemName, amountStor, id, data)
  490. end
  491.  
  492. end
  493.  
  494. function login()
  495.  mon.clear()
  496.  pos(xPos("Login"), 3)
  497.  mon.setTextColor(colors.lime)
  498.  mon.write("Login")
  499.  pos(1,6)
  500.  mon.setTextColor(colors.white)
  501.  mon.write("ID: ")
  502.  pos(23,6)
  503.  mon.write(player())
  504.  pos(1,8)
  505.  mon.clearLine()
  506.  mon.write("Passwort:")
  507.  sleep(0.5)
  508.  passwort = nil
  509.  parallel.waitForAll(input, loginbuttons)
  510. end
  511.  
  512. function input(x,y)
  513.  pos(23,8)
  514.  passwort = tostring(read())
  515. end
  516.  
  517. function loginbuttons()
  518.   button(20, 16, 3, 14, colors.white, colors.red, "Abbrechen")
  519.   button(36, 16, 3, 14, colors.white, colors.green, "Login")
  520.   user = player()
  521.   pos(23,8)
  522.  
  523.  while true do
  524.    local event, side, X, Y = os.pullEvent("monitor_touch")
  525.    if X >= 20 and X <= 33 and Y >= 16 and Y <= 18 then
  526.     startscreen()
  527.    elseif X >= 36 and X <= 49 and Y >= 16 and Y <= 18 then
  528.     if passwort == loadpw(user) then
  529.        selection()
  530.     else
  531.        pos(1,11)
  532.        mon.setTextColor(colors.red)
  533.        mon.write("Falsches Passwort")
  534.        passwort = nil
  535.        sleep(2)
  536.        login()
  537.     end
  538.   end
  539.  end
  540. end
  541.  
  542.  
  543. while true do
  544.   selection()
  545. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement