Advertisement
Pixxel124

CC server

Mar 16th, 2023 (edited)
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 0 0
  1. serverID = os.getComputerID
  2. userIDs = {}
  3. userIDsN = 0
  4.  
  5. function manageUsers()
  6.    local menu = "\nManaging users:\n"
  7.    menu = menu .. "0. Back\n"
  8.    menu = menu .. "1. Show users\n"
  9.    menu = menu .. "2. Add users\n"
  10.    menu = menu .. "3. Remove users\n\n"
  11.    menu = menu .. "Your choice: "
  12.    io.write(menu)
  13.    choice = io.read("*l");
  14.    while choice ~= "0" do
  15.       if choice == "0" then
  16.          break
  17.       elseif choice == "1" then
  18.          showUsers()
  19.       elseif choice == "2" then
  20.          addUsers()
  21.       elseif choice == "3" then
  22.          removeUsers()
  23.       end
  24.       io.write(menu)
  25.       choice = io.read("*l");
  26.    end
  27. end
  28.  
  29. function showUsers()
  30.    io.write("\n")
  31.    for i,v in ipairs(userIDs) do
  32.       io.write("userID[" .. i .. "]: " .. v .. "\n")
  33.    end
  34. end
  35.  
  36. function addUsers()
  37.    local input = io.read("*l")
  38.    while input ~= "." do
  39.       local id = tonumber(input)
  40.       userIDsN = userIDsN + 1
  41.       userIDs[userIDsN] = id
  42.       input = io.read("*l")
  43.    end
  44.    updateUserIDsfile()
  45. end
  46.  
  47. function removeUsers()
  48.    local input = io.read("*l")
  49.    while input ~= "." do
  50.       local id = tonumber(id)
  51.       for i = id, userIDsN - 1 do
  52.          userIDs[i] = userIDs[i + 1]
  53.       end
  54.       userIDs[userIDsN] = nil
  55.       userIDsN = userIDsN - 1
  56.       input = io.read("*l")
  57.    end
  58.    updateUserIDsfile()
  59. end
  60.  
  61. function writeToFile(path,data)
  62.    local file = fs.open(path,"w")
  63.    file.write(textutils.serialise(data))
  64.    file.close()
  65. end
  66.  
  67. function readFromFile(path)
  68.    local file = fs.open(path,"r")
  69.    data = textutils.unserialise(file.readAll())
  70.    file.close()
  71.    return data
  72. end
  73.  
  74. function updateUserIDsfile()
  75.    writeToFile("data/userIDs",userIDs)
  76. end
  77.  
  78. function updateUserIDs()
  79.    if fs.exists("data/userIDs") then
  80.       userIDs = readFromFile("data/userIDs")
  81.       userIDsN = table.getn(userIDs)
  82.    end
  83. end
  84.  
  85. function init()
  86.    updateUserIDs()
  87. end
  88.  
  89. function loop()
  90.    init()
  91.    local menu = "\nMenu:\n"
  92.    menu = menu .. "0. Close\n"
  93.    menu = menu .. "1. Manage users\n\n"
  94.    menu = menu .. "Your choice: "
  95.    io.write(menu)
  96.    choice = io.read("*l");
  97.    while choice ~= "0" do
  98.       if choice == "0" then
  99.          break
  100.       elseif choice == "1" then
  101.          manageUsers()
  102.       elseif choice == "2" then
  103.          showUsers()
  104.       end
  105.       io.write(menu)
  106.       choice = io.read("*l");
  107.    end
  108. end
  109.  
  110. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement