电脑操作系统不能使用fs登录系统API

问题描述:

我正在为Computercraft操作Windows 8模拟操作系统,而我的登录系统无法正常工作。我一直试图弄清楚过去一小时左右的情况,这真的令人沮丧。电脑操作系统不能使用fs登录系统API

这里的登录代码:

-- 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() 

userConfig.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" 
+0

它不起作用?预期的结果是什么?究竟发生了什么? – 2014-10-06 02:55:26

+0

@ColonelThirtyTwo它可以打印我想要的所有内容,但在输入凭据后,似乎无法检查它们是否正确。我将编辑问题以包含屏幕截图和“userConfig.cfg”文件。 – user3377520 2014-10-06 20:38:15

+0

对不起,我的声望对屏幕不够高,但如果您有电脑设备或模拟器,您可以测试代码,只需确保文件位于代码中定义的正确目录中即可。 – user3377520 2014-10-06 20:58:12

的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" 
      } 
} 

这会工作得在你想要的方式,并允许更多的变化和扩展。当然,从中读取是一个不同的故事,我会使用一个函数来查找和发送授权码,否则无法使用。

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 
+0

主要是因为我无法评论David的帖子,所以我没有Oeed:P 是的,你的版本可以工作David但是他使用的格式看起来很像一张表格会使它工作。 – Flamanis 2014-10-12 10:11:09

为了扩展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[24]替换userConfig.readLine(24)之类的东西。


或者,你可以检查出io libraryCC's implementation。这是一个标准的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之上,所以最好移除中间人。

希望这会有所帮助。

+1

修正了龙的用户名。我以为你是Oeed,因为他说他会看这个帖子,哈哈 – skwerlman 2014-10-12 10:15:37