2014-10-05 103 views
-1

我正在爲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

回答

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

這會工作得在你想要的方式,並允許更多的變化和擴展。當然,從中讀取是一個不同的故事,我會使用一個函數來查找和發送授權碼,否則無法使用。

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

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[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