badnames = {} -- table of ridiculous names for _, v in pairs ({ "New", "God", "Admin" }) do badnames [v] = true end function NewConnection (line, client) local name Send (client, messages.welcome) -- get name while true do Send (client, "Your name? (Enter 'new' to create a new character) ...", true) name = coroutine.yield () local ok, err = validname (name) if ok then name = capitalize (name) if name == "New" then break end -- special name if yes_no (client, "You are called " .. name .. ", right? ...") then break end -- they agreed to that name else Send (client, err) end -- good name entered end -- while getting name local char -- the character s/he is if name == "New" then char = NewCharacter (client) else char = ExistingCharacter (client, name) end if not char then Send (client, "Press to restart the connection process ...") return end print (char.name, "has entered the game.") Send (client, "Welcome to " .. config.mudname) char.connected_time = os.time () char.client = client -- point our character to the controlling client client.char = char -- tell the client which character it is char.playing = true -- they are on now client.handler = CommandHandler end -- NewConnection function NewCharacter (client) local password -- get new name while true do Send (client, "What would you like to be called? ...", true) name = coroutine.yield () local ok, err = validname (name) if ok then name = capitalize (name) if badnames [name] then -- can't be called New, etc. Send (client, "That name is not available.") elseif playerexists (name) then Send (client, "There is already a character called " .. name) elseif yes_no (client, "You want to be called " .. name .. ", right? ...") then break end -- they agreed to that name else -- bad name Send (client, err) end -- good name entered end -- while getting name while true do password = GetPassword (client, "Please choose a password for " .. name .. " ...") local password2 = GetPassword (client, "Please re-enter your password to confirm it ...") if password == password2 then break else Send (client, "Passwords do not match!") end -- passwords not matching end -- getting password and confirmation Send (client, "Please choose all sort of other things ...") -- new character here char = {} char.name = name char.password = password -- should hash this char.create_time = os.time () chars [name] = char -- add to known characters table client.name = name SaveCharacter (name) -- serialize it return char end -- NewCharacter function ExistingCharacter (client, name) local password if chars [name] then Send (client, name .. " is already playing.") return end -- already playing if not playerexists (name) then Send (client, name .. " does not exist.") return end -- no such player LoadCharacter (name) local char = chars [name] client.name = name -- tprint (char) char.playing = false for try = 1, config.max_password_tries do password = GetPassword (client, "Please enter your password ...") if char.password ~= password then Send (client, "Incorrect password!") else return char end -- if end -- trying 3 times -- kill them RemoveClient (client.socket) end -- ExistingCharacter -- gets password from player, checks not empty, and is minimum required length function GetPassword (client, prompt) local password while true do Send (client, prompt, true) password = trim (coroutine.yield ()) if string.len (password) == 0 then Send (client, "Password must be entered.") elseif string.len (password) < config.min_password_length then Send (client, "Password too short, must be at least " .. config.min_password_length .. " characters.") else return password end -- checks end -- getting password end -- GetPassword