-1

我正在为 Computercraft 开发 Windows 8 模拟操作系统,但我的登录系统无法正常工作。在过去的一个小时左右,我一直在试图弄清楚,这真的很令人沮丧。

这是登录代码:

    -- Log-in and User Definition Service

    --- Variables

    userExists = fs.exists("/Users/.config/userConfig.cfg")
    termx, termy = term.getSize()
    halfx = math.floor(termx*0.5)
    halfy = math.floor(termy*0.5)

    prompt = "Welcome"
    uprompt = "Username: "
    pprompt = "Password: "

    userConfig = fs.open("/Users/.config/userConfig.cfg", "r")
    edituserConfig = fs.open("/Users/.config/userConfig.cfg", "w")
    --- Functions

    function login(user)
      if user == "admin" then
        term.setCursorPos(1,1)
        term.setTextColor(256)
        print (user)

      elseif user == "guest" then
        print (user)

      else
        print ("nil")

      end
    end

    function userCheck()
      if userExists == true then
        term.clear()
        term.setBackgroundColor(8)
        term.setCursorPos(halfx-0.5*#prompt, halfy - 4)
        term.clear()

        print (prompt)

        term.setCursorPos((halfx-#uprompt), (halfy - 2))
        write (uprompt)
        term.setCursorPos((halfx-#uprompt), (halfy - 1))
        write (pprompt)
        term.setCursorPos((halfx), (halfy - 2))
        upin = read()
        term.setCursorPos((halfx), (halfy - 1))
        ppin = read("*")

        if upin == userConfig.readLine(21) and ppin == userConfig.readLine(24) then
          print ("ADMIN")

        elseif upin == userConfig.readLine(33) and ppin == userConfig.readLine(36) then
          login("guest")

        end

      elseif userExists == false then


      elseif userExists == nil then

      end 
    end

    --- Main

    userCheck()

用户配置.cfg:

    -- Format as follows:
    --
    --  (name):
    --    
    --    (prop):
    --    "(value)"
    --
    --    (prop):
    --    "(value)"
    --
    --    (prop):
    --    "(value)"
    --
    --
    --  (name):
    --    [etc.]

    Admin:

      user:
      "Admin"

      pass:
      "admin"

      auth:
      "1"


    Guest:

      user:
      "Admin"

      pass:
      nil

      auth:
      "0"


    Root:

      user:
      nil

      pass:
      nil

      auth:
      "2"
4

2 回答 2

1

要扩展 Dragon53535 的答案:

这是我将文件读入表格的快速例程:

local function fileToTable(path)
  -- first we make sure that the path can be opened
  assert(fs.exists(path), path..' does not exist!')
  assert(not fs.isDir(path), path..' is a directory!')

  local tSrc = {}
  local inFile = fs.open(path, 'r')

  -- we set line to a non-nil value
  local line = ''

  -- we continue the loop until line is nil
  while line do

    -- we read a line from the file
    line = inFile.readLine()

    -- now we save the value of line to our table
    -- line will be nil at EOF
    tSrc[#tSrc+1] = line
  end

  inFile.close()
  return tSrc
end

运行后userConfig = fileToTable('/Users/.config/userConfig.cfg'),您将替换userConfig.readLine(24)userConfig[24].


或者,您可以查看CC 的io实现。它是一个标准的 Lua 库(尽管在 CC 中它是一个 fs 包装器),因此可以更轻松地将代码移出 CC。特别是,io.lines()在这里会有所帮助。

上面的代码重写使用io.lines

local function fileToTable(path)
  -- first we make sure that the path can be opened
  assert(fs.exists(path), path..' does not exist!')
  assert(not fs.isDir(path), path..' is a directory!')

  local tSrc = {}

  -- iterate over file
  -- io.lines is neat, since it opens and closes the file automatically
  for line in io.lines(path) do
    tSrc[#tSrc+1] = line
  end

  return tSrc
end

如您所见,这要小得多(只有 9 行代码!)并且更易于管理。它不是我在 CC 中首选的解决方案的原因是它io位于 . 之上fs,因此最好删除中间人。

希望这可以帮助。

于 2014-10-12T10:04:14.333 回答
1

readLine 不接受参数,只读取下一行。您最好的选择是使用表和 textutils.serialize 将其全部写入文件,然后在读取时使用 textutils.unserialize 将其放入表中。

行政:

  user:
  "Admin"

  pass:
  "admin"

  auth:
  "1"

可以写在表格中,例如

{
  Admin = {
            user = "Admin"

            pass = "admin"

            auth = "1"
          }

  Guest = {
            user = "Admin"

            pass = nil

            auth = "0"
          }
}

这将以您想要的方式工作,并允许更多的可变性和扩展。当然,从中读取是一个不同的故事,我会使用一个函数来查找并发送回验证码,或者如果它不起作用,则返回 nil。

local function findUsers(username,password)

    --This will read the file and put all it's data inside a table, and then close it.
    local file = fs.open("userConfig.cfg","r")
    local userRanks = textutils.unserialize(file.readAll())
    file.close()

    --To read all the data in a table, i'm going to do this.
    for a,v in pairs(userRanks) do
        if type(v) == "table" then
            if userRanks[a].user == username and userRanks[a].pass == password then
                return userRanks[a].auth
            end
        end
    end

    --[[If we look through the entire file table, and the username and password aren't the same 
        as on file, then we say we couldn't find it by returning nil]]--
    return nil
end

现在对于您的输入区域,您所要做的就是当他们输入用户名和密码时,然后再调用它,如果允许您拥有验证码

local auth = findUsers(upin,ppin)

--If they inputted an actual username and password
if auth ~= nil then

    --If the auth code from the rank is "1"
    if auth == "1" then
       --Do whatever stuff you want
    elseif auth == "2" then
       --Do whatever other stuff for this auth code
    end
elseif auth == nil then
    print("Oh no you inputted an invalid username and/or password, please try again!"
end
于 2014-10-08T02:40:56.960 回答