-1

Computercraft 用の Windows 8 モックアップ OS に取り組んでいますが、ログイン システムが機能しません。私は過去1時間ほどそれを理解しようとしてきましたが、本当にイライラしています.

ログインコードは次のとおりです。

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

2 に答える 2

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 に答える