ID:158465
 
Basically I have a basic login system that is buggy as you can probally notice. If you can point out some bugs and also help me by telling me how to create a Auto-Save every so long and also make it only allow one person to have the name desired.

E.G. If key 1 makes there name Jeff then key 2 cant make there name Jeff.

Here is what I got so far:
world
name = "Harry Potter - The Beginning | Version 5.2"
hub = "Chrislee123.HPTB"
hub_password = "hp"
status = "<font size=1><font face=verdana><font color=red>Main Server | Come and Join | Version 5.2"
view= 11
client
command_prompt = "HPTB / Chrislee123."
script = "<STYLE>BODY {background: black; color: white}</STYLE>"
command_text = "say "

mob/Login()
usr.loc = locate(15,15,2)
mob/Logout()
..()
world << "<b><font color = blue>[usr.name]([usr.key]) Logged out!</font></b>"
var/savefile/F=new("players/[src.key].sav")
Write(F)
del(src)

turf/Full
density = 1
layer = 999
icon = 'Images/login.jpg'
turf/Loginstuff/New
density = 1
layer = 999
Click()
usr.NewPlayer()
mob
proc
NewPlayer()
if(fexists("players/[src.key].sav"))
fdel("players/[src.key].sav")
var/name = input("Enter your desired name.","Enter your name.") as text|null
if(length(name) < 2)
alert("Your name must be longer than 2 letters!")
return
if(length(name) > 20)
alert("Your name can not be longer then 20 letters!")
return
usr.name="[html_encode(name)]"
world << "<b><font color = blue>[usr.name]([usr.key]) Logged in for the First Time!</font></b>"
alert("Head into the Great Hall and wear the sorting hat and also head to Diagon Alley to get hair.","HPTB")
usr.loc = locate(24,20,1)
usr.icon = 'base.dmi'
usr.icon_state = "base"
usr.client.view=7
if(usr.key == "Chrislee123" || usr.key == "")
usr.verbs += typesof(/mob/MGM/verb)
else
..()
turf/Loginstuff/Load
density = 1
layer = 999
Click()
usr.LoadPlayer()
mob
proc
LoadPlayer()
if(fexists("players/[src.key].sav"))
var/savefile/F=new("players/[src.key].sav")
Read(F)
src << "<b><font color = white>Savefile loaded, welcome back."
world << "<b><font color = blue>[usr.name]([usr.key]) logged in!"
src.client.view=7
if(src.key == "Chrislee123" || src.key == "")
src.verbs += typesof(/mob/MGM/verb)
else
..()
else
src<<"<b><font color = white>You have no savefile"
turf/Loginstuff/Delete
density = 1
layer=999
Click()
usr.DeleteChar()
mob
proc
DeleteChar()
if(fexists("players/[src.key].sav"))
var/sure=alert(src,"Are you sure you want to delete your character?","Confirmation","No","Yes")
if(sure=="Yes")
sleep(0)
fdel("players/[src.key].sav")
src << "Character Deleted"
else if(sure=="No")
return
else
return
else
src << "You do not have a character saved on this server."
mob/
Read(var/savefile/F)
var
X
Y
Z
xverbs
F["x"] >> X
F["y"] >> Y
F["z"] >> Z
F >> xverbs
..()
src.loc = locate(X,Y,Z)
verbs += xverbs
Write(var/savefile/F)
F["x"] << x
F["y"] << y
F["z"] << z
F << verbs
..()
double post/bump

No joke ive been at my computer all day yesterday waiting for a reply Ive been refreshing like every 15 mins and no help :( that makes me sad please someone help me im ready to help myself.
In response to Chrislee123
Personally I'd do it like this :

world
name = "Harry Potter - The Beginning | Version 5.2"
hub = "Chrislee123.HPTB"
hub_password = "hp" // maybe you should change your hub password that's pretty predictable xD
status = "<font size=1><font face=verdana><font color=red>Main Server | Come and Join | Version 5.2"
view= 11
client
command_prompt = "HPTB / Chrislee123."
script = "<STYLE>BODY {background: black; color: white}</STYLE>"
command_text = "say "

mob/Login()
loc = locate(15,15,2)
mob/Logout()
..()
world << "<b><font color = blue>[name]([key]) Logged out!</font></b>"
var/savefile/F=new("players/[key].sav")
Write(F)
del(src)

turf/Full
density = 1
layer = 999
icon = 'Images/login.jpg'

turf/Loginstuff/New
density = 1
layer = 999
Click()
if(fexists("players/[usr.key].sav"))
fdel("players/[usr.key].sav") // might want to ask if they want to overwrite instead of deleting it
var/name = input("Enter your desired name.","Enter your name.") as text|null
if(length(name) < 2 || length(name) > 20)
alert("Your name must be between 2 and 20 letters!")
return
usr.name="[html_encode(name)]"
world << "<b><font color = blue>[usr.name]([usr.key]) Logged in for the First Time!</font></b>"
alert("Head into the Great Hall and wear the sorting hat and also head to Diagon Alley to get hair.","HPTB")
usr.loc = locate(24,20,1)
usr.icon = 'base.dmi'
usr.icon_state = "base"
usr.client.view=7
if(usr.key == "Chrislee123" || usr.key == "") // bad way of handling this
usr.verbs += typesof(/mob/MGM/verb)

turf/Loginstuff/Load
density = 1
layer = 999
Click()
if(fexists("players/[usr.key].sav"))
var/savefile/F=new("players/[usr.key].sav")
Read(F)
usr << "<b><font color = white>Savefile loaded, welcome back."
world << "<b><font color = blue>[usr.name]([usr.key]) logged in!"
usr.client.view=7
if(usr.key == "Chrislee123" || usr.key == "") // bad way of handling this
usr.verbs += typesof(/mob/MGM/verb)
else usr<<"<b><font color = white>You have no savefile"

turf/Loginstuff/Delete
density = 1
layer=999
Click()
if(fexists("players/[usr.key].sav"))
switch(alert("Are you sure you want to delete your character?","Confirmation","No","Yes"))
if("Yes")
fdel("players/[usr.key].sav")
usr << "Character Deleted"
else return
else usr << "You do not have a character saved on this server."

mob
Read(var/savefile/F)
var
X
Y
Z
xverbs
F["x"] >> X
F["y"] >> Y
F["z"] >> Z
F >> xverbs
..()
src.loc = locate(X,Y,Z)
verbs += xverbs
Write(var/savefile/F)
F["x"] << x
F["y"] << y
F["z"] << z
F << verbs
..()


Like I said previously on chatters, no usr in procs.

If you're not going to call Load/New/Delete procs anywhere else, you could just define them in the Click() procs of the turfs.

One more thing, you shouldn't save a mob's icon and overlays, you should restore them upon login so you don't end up with a big savefile size.
In response to Andre-g1
To save, you should not call Write() directly, but rather you should output the mob to the savefile. Instead of src.Write(F) you would use F << src. You will also have to be prepared to read it properly as well: simply load the mob from the savefile into any variable, and it should log you into the mob. IE:

var/mob/M
F >> M


Your old mob will remain and should be deleted, though, if it wouldn't automatically be picked up by the garbage collector.

Anyway, you didn't address the issue of preventing duplicate names. To do that, you would do something like this:

//global list of taken names
var/list/names = list()

//when we save a mob, and it's a player, add its name to the names list, if it isn't already there
mob/Write(var/savefile/F)
if(key)
if(!names.Find(name))
names += name
//after we've added the name to the list, we should save it as well
var/savefile/N = new("names.sav")
N << names
..()

//load names in world/New()
world/New()
..()
if(fexists("names.sav"))
var/savefile/N = new("names.sav")
N >> names


That will maintain a list of used names. By only adding to it in mob/Write() instead of when we create a character, we prevent the case where a player is created, but never saved (due to the game crashing, perhaps), and having the name taken up when there is no character with that name. Alternatively, if you only saved in world/Del(), if the game crashed then names of players that were created and saved before the crash would be lost.

It's up to you to prevent somebody picking a name that's already in the names list, but that's simple anyway.
In response to Garthor
I actually didn't know you could load a mob like that, nice to know.

But is there something wrong with calling Write() or Read() directly ?
In response to Andre-g1
Yes: it does not properly resolve references to the mob, which can result in the proc crashing upon load under certain not-uncommon circumstances, such as simply having an obj in contents with a variable pointing to the mob (IE: an owner variable).
In response to Garthor
Hmm Garthor you totally lost me can you explain more simpler and throughly please.

I sort of get the duplicate name one but why adress it in save when it would be easier to do it in the create I don't want two people with the same name in the game or it will confuse everyone and also will mess the saves up.

The first bit your saying not you call the Write directly so can give me an example of both the Write and Read procs being called proply.
In response to Chrislee123
I already explained why: if you reserve the name the moment the character is created, then if the character never gets saved, that name is permanently lost.

And I already gave an example of how to save and load properly:

mob/verb/save()
var/savefile/F = new()
F << src

mob/verb/load()
if(fexists())
var/savefile/F = new()
var/mob/M
F >> M
del(src)
</DM>
In response to Garthor
ahhh-ok sorry ill post what I go so far in a sec and maybe we can look for bugs and ways to improve it :D Ahh got confused half way through when errors start poping up

world
name = "Harry Potter - The Beginning | Version 5.2"
hub = "Chrislee123.HPTB"
hub_password = "hp" // maybe you should change your hub password that's pretty predictable xD
status = "<font size=1><font face=verdana><font color=red>Main Server | Come and Join | Version 5.2"
view= 11
client
command_prompt = "HPTB / Chrislee123."
script = "<STYLE>BODY {background: black; color: white}</STYLE>"
command_text = "say "

mob/Login()
loc = locate(15,15,2)
mob/Logout()
..()
world << "<b><font color = blue>[name]([key]) Logged out!</font></b>"
var/savefile/F=new("players/[key].sav")
Write(F)
del(src)

turf/Full
density = 1
layer = 999
icon = 'Images/login.jpg'

turf/Loginstuff/New
density = 1
layer = 999
Click()
if(fexists("players/[usr.key].sav"))
fdel("players/[usr.key].sav") // might want to ask if they want to overwrite instead of deleting it
var/name = input("Enter your desired name.","Enter your name.") as text|null
if(length(name) < 2 || length(name) > 20)
alert("Your name must be between 2 and 20 letters!")
return
usr.name="[html_encode(name)]"
world << "<b><font color = blue>[usr.name]([usr.key]) Logged in for the First Time!</font></b>"
alert("Head into the Great Hall and wear the sorting hat and also head to Diagon Alley to get hair.","HPTB")
usr.loc = locate(24,20,1)
usr.icon = 'base.dmi'
usr.icon_state = "base"
usr.client.view=7
if(usr.key == "Chrislee123" || usr.key == "") // bad way of handling this
usr.verbs += typesof(/mob/MGM/verb)

turf/Loginstuff/Load
density = 1
layer = 999
Click()
if(fexists())
var/savefile/F = new()
var/mob/M
F >> M
del(src)

turf/Loginstuff/Delete
density = 1
layer=999
Click()
if(fexists("players/[usr.key].sav"))
switch(alert("Are you sure you want to delete your character?","Confirmation","No","Yes"))
if("Yes")
fdel("players/[usr.key].sav")
usr << "Character Deleted"
else return
else usr << "You do not have a character saved on this server."

mob
Read(var/savefile/F)
var
X
Y
Z
xverbs
F["x"] >> X
F["y"] >> Y
F["z"] >> Z
F >> xverbs
..()
src.loc = locate(X,Y,Z)
verbs += xverbs
Write(var/savefile/F)
if(key)
if(!names.Find(name))
names += name
var/savefile/N = new("names.sav")
N << names
N["x"] << x
N["y"] << y
N["z"] << z
N << verbs
..()


//global list of taken names
var/list/names = list()

//when we save a mob, and it's a player, add its name to the names list, if it isn't already there
mob/Write(var/savefile/F)
if(key)
if(!names.Find(name))
names += name
//after we've added the name to the list, we should save it as well
var/savefile/N = new("names.sav")
N << names
..()

//load names in world/New()
world/New()
..()
if(fexists("names.sav"))
var/savefile/N = new("names.sav")
N >> names