Skip to content

Commit

Permalink
move login code to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Munro committed Jan 30, 2016
1 parent b42b9bf commit ff8581e
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 102 deletions.
121 changes: 121 additions & 0 deletions login.lua
@@ -0,0 +1,121 @@
local location = require "location"
local command = require "command"
local room = require "room"
local race = require "race"
local lyaml = require "lyaml"

local login = {}
local loginmt = {}

-- prompt to select a mob as part of the login process.
function loginmt:selectmob()
self.player:ask("By what name do you wish to be known (list for existing characters)? ", function(args)
local mobname = args[1]
if mobname == "list" then
local list = "Your alts:\n"
for i, m in pairs(self.player.mobs) do
list = list .. m .. "\n"
end
self.player:send(list)
self:selectmob()
else
for i, m in pairs(self.player.mobs) do
if m == mobname then
self.player:load(mobname)
self.player:send("Welcome back.")
location:addmob(self.player.mob.id, room.START_ROOM)
command.look(self.player)
return
end
end
self.player:load(mobname)
if self.player.mob then
self.player:send("You do not possess that mob.")
self.player.mob = nil
return
else
local racecallback = function(args)
if race.list[args[1]] then
local race = args[1]
self.player:send("Ok. Welcome to luamud.")
self.player.mob = mob:new(mobname, race)
self.player.mob.isnpc = false
table.insert(self.player.mobs, self.player.mob.name)
location:addmob(self.player.mob.id, room.START_ROOM)
self.player:save()
command.look(self.player)
elseif args[1] == "help" then
else
self.player:ask("That's not a valid race. What race are you? ", racecallback)
end
end
self.player:ask("New character. What race are you? ", racecallback)
end
end
end)
end

-- New account - set a password
function loginmt:newaccountpassword()
self.player:ask("Please enter a password: ", function(args)
local p1 = table.concat(args, " ")
if p1:len() < 5 then
self.player:send("Passwords must be greater than 5 characters in length.")
self:newaccountpassword()
else
self.player:ask("Confirm your password: ", function(args)
local p2 = table.concat(args, " ")
if p1 == p2 then
self.player.password = p1
self.player:send("Confirmed!")
self.player:save()
self:selectmob()
else
self.player:send("Passwords do not match. Try again.")
self:newaccountpassword()
end
end)
end
end)
end

-- prompt the player for a login username and password if the account already
-- exists. Otherwise,
function loginmt:prompt()
self.player:ask("What is your login username (not in-game character)? ", function(args)
self.player.username = args[1]
local f = io.open("data/players/" .. self.player.username .. ".yaml", "r")
if f == nil then
self.player:send("New account.")
self:newaccountpassword()
else
local data = lyaml.load(f:read("*all"))
f:close()
self.player:ask("Password: ", function(args)
local password = args[1]
if data["password"] == password then
self.player:send("Success!")
self.player.password = password
self.player.mobs = data["mobs"]
self:selectmob()
else
self.player:send("Login failure.")
self.player.client:close()
end
end)
end
end)
end

function login:new(player)
local l = {
player = player
}

setmetatable(l, loginmt)
loginmt.__index = loginmt

return l
end

return login
112 changes: 10 additions & 102 deletions player.lua
@@ -1,24 +1,24 @@
local mob = require "mob"
local race = require "race"
local lyaml = require "lyaml"
local room = require "room"
local location = require "location"
local command = require "command"
local item = require "item"
local location = require "location"
local login = require "login"

local player = {}
local playermt = {}

-- send a message to the player
function playermt:send(message)
self.client:send(message .. "\n")
end

-- send a message to the player, and capture their next input for the provided
-- callback function.
function playermt:ask(message, callback)
self.client:send(message)
self.callback = callback
end

-- saves the player and the mob the player is logged in as.
function playermt:save()
if self.mob then
local f = io.open("data/mobs/" .. self.mob.name .. ".yaml", "w")
Expand All @@ -38,6 +38,7 @@ function playermt:save()
f:close()
end

-- load a mob.
function playermt:load(mobname)
local f = io.open("data/mobs/" .. mobname .. ".yaml", "r")
local data = lyaml.load(f:read("*all"))
Expand All @@ -48,106 +49,12 @@ function playermt:load(mobname)
item.inv[self.mob.id] = data["inv"]
end

-- prompt the player.
function playermt:prompt()
self.client:send("\n" .. self.mob.hp .. "hp " .. self.mob.mana .. "mana " .. self.mob.mv .. "mv> ")
end

function playermt:selectmob()
self:ask("By what name do you wish to be known (list for existing characters)? ", function(args)
local mobname = args[1]
if mobname == "list" then
local list = "Your alts:\n"
for i, m in pairs(self.mobs) do
list = list .. m .. "\n"
end
self:send(list)
self:selectmob()
else
for i, m in pairs(self.mobs) do
if m == mobname then
self:load(mobname)
self:send("Welcome back.")
location:addmob(self.mob.id, room.START_ROOM)
command.look(self)
return
end
end
self:load(mobname)
if self.mob then
self:send("You do not possess that mob.")
self.mob = nil
return
else
local racecallback = function(args)
if race.list[args[1]] then
local race = args[1]
self:send("Ok. Welcome to luamud.")
self.mob = mob:new(mobname, race)
self.mob.isnpc = false
table.insert(self.mobs, self.mob.name)
location:addmob(self.mob.id, room.START_ROOM)
self:save()
command.look(self)
elseif args[1] == "help" then
else
self:ask("That's not a valid race. What race are you? ", racecallback)
end
end
self:ask("New character. What race are you? ", racecallback)
end
end
end)
end

function playermt:enterpassword()
self:ask("Please enter a password: ", function(args)
local p1 = table.concat(args, " ")
if p1:len() < 5 then
self:send("Passwords must be greater than 5 characters in length.")
self:enterpassword()
else
self:ask("Confirm your password: ", function(args)
local p2 = table.concat(args, " ")
if p1 == p2 then
self.password = p1
self:send("Confirmed!")
self:save()
self:selectmob()
else
self:send("Passwords do not match. Try again.")
self:enterpassword()
end
end)
end
end)
end

function playermt:loginprompt()
self:ask("What is your login username (not in-game character)? ", function(args)
self.username = args[1]
local f = io.open("data/players/" .. self.username .. ".yaml", "r")
if f == nil then
self:send("New account.")
self:enterpassword()
else
local data = lyaml.load(f:read("*all"))
f:close()
self:ask("Password: ", function(args)
local password = args[1]
if data["password"] == password then
self:send("Success!")
self.password = password
self.mobs = data["mobs"]
self:selectmob()
else
self:send("Login failure.")
self.client:close()
end
end)
end
end)
end

-- create a new player table.
function player:new(client)
local p = {
username = "",
Expand All @@ -158,7 +65,8 @@ function player:new(client)

setmetatable(p, playermt)
playermt.__index = playermt
p:loginprompt()

login:new(p):prompt()

return p
end
Expand Down

0 comments on commit ff8581e

Please sign in to comment.