ID:272807
 
Here's my thought process of how to do a PIN number for accessing bank accounts in-game and whatnot, but I just can't help but think that there's a better way :\ Is there? Or have I pretty much got the best way of doing such a thing?

mob/var
PIN_Number

mob/verb
New_PIN()
if(!PIN_Number)
var/a = input("Please enter a four digit number to be used as your PIN number.","New PIN") as null|num
if(!a)
alert("Canceled...","New PIN")
return
if(length("[a]")<4 || length("[a]")>4)
alert("Your PIN number must 4 digits in length.\nProcess canceled.","ERROR")
return
PIN_Number = a
alert("PIN number successfully assigned.","New PIN - Sucess!")
else if(PIN_Number)
var/b = input("For security, please enter your current PIN number.","Security") as null|num
if(!b)
alert("Cenceled...","Security")
return
if(b == PIN_Number)
var/c = input("Now please enter the new PIN you would like to use.","New PIN") as null|num
if(!c)
alert("Canceled...","New PIN")
return
if(length("[c]")<4 || length("[c]")>4)
alert("Your PIN number must 4 digits in length.\nProcess canceled.","ERROR")
return
PIN_Number = c
alert("PIN number successfully changed.","New PIN - Success!")
else
alert("Incorrect PIN number.","ERROR")
return

Unlock()
var/a = input("Please enter your PIN number","Unlock") as null|num
if(!a)
alert("Cenceled...","Unlock")
return
if(a == PIN_Number) alert("You have successfully entered your PIN number.","Unlock - Success!")
else alert("Incorrect PIN number.","ERROR")
You can always turn THIS
if(length("[a]")<4 || length("[a]")>4)
INTO
if(length("[a]") != 4)


You can make "else if(PIN_Number)" to just "else" since the first if() checks if it is FALSE.

But other then those minor things, that's pretty much the same I would do.
In response to GhostAnime
I like having specific conditions in my programming, and "else if()" settles that. Is there really ant significant difference or reason I should use "else" instead?

if(length("[a]") != 4)

Thanks for that one though, I totally forgot I could do that haha.
In response to GhostAnime
I just thought of this. >_>

Would it be better to use a list var defined under a Banker mob to hold peoples' keys with their associated PIN numbers? I forgot to describe the purpose of the PIN number system above haha xD
mob/Banker
var/list/PIN_Numbers = list()
verb/New_Change_PIN()
set name = "Create/Change PIN"
set src in oview(1)
if(!(usr.key in PIN_Numbers))
var/a = input as null|num
if(!a) return
PIN_Numbers += usr.key
PIN_Numbers[usr.key] = a
//...
In response to Spunky_Girl
Spunky_Girl wrote:
Is there really ant significant difference or reason I should use "else" instead?

This should be obvious; if you use 'else' the code under it will run on the condition that the previous if() <small>(and else-ifs when applicable)</small> didn't pass their condition (and so didn't execute), but if you use else-if then in addition to checking the if()s before it, it checks an additional condition - and the condition you're checking is very superfluous because the fact it's true is already established - since that part of the code wouldn't execute if it isn't.
if(MyCondition)
//...
else if(!MyCondition) //this if() always evaluates true
In response to Spunky_Girl
Spunky_Girl wrote:
Would it be better to use a list var defined under a Banker mob to hold peoples' keys with their associated PIN numbers?

By assumption, probably not. Is there any reason the Banker mob needs to store the PINs locally? Are players meant to possibly have different/separate PINs per every Banker mob?
Is there any reason to separate this data (also making saving it extra work)? You could as well use a global [associative] list(s) to store any var you use that belong to something - but in an object-oriented language, you don't have to.

I forgot to describe the purpose of the PIN number system above haha xD

Yes, I believe you did... ;P
In response to Kaioken
Ah, I didn't think of that haha. I'll just stick with my original way then xD
You don't even have to check the length of a string in this case, but could simply check if the number is in a range of values.
And you could make use of modular programming ;)

mob
var
PIN_Number = 0
locked = 0
contact = "bank manager/host"

verb
New_PIN()
if(PIN_Number)
if(confirm_PIN(usr))
set_PIN(usr)
else
set_PIN(usr)
Unlock()
if(PIN_Number)
if(confirm_PIN(usr))
alert("You have accessed your bank account.", "Confirmation")
/*access bank account*/
proc
confirm_PIN(var/mob/m)
if(locked)
alert("Your bank account has been locked for security reasons, please consult your local [contact]", "PIN change")
return 0
var/tries = 3
while(tries > 0)
var/a = input(m, "Please enter your current PIN number.", "PIN confirmation") as null|num
if(!a)
alert(m, "Confirmation canceled.", "PIN confirmation")
return 0
else if(a == PIN_Number)
return 1
else
alert(m, "Incorrect PIN number.", "PIN confirmation")
tries --
alert(m, "You have entered the wrong PIN number thrice, bank account locked.", "PIN confirmation")
locked = 1
return 0

set_PIN(var/mob/m)
var/a = 0
a = input(m, "Please enter a four digit number to be used as your PIN number.","PIN-Setup") as null|num
if((a in 1000 to 9999) && (a == round(a))) //Edit: Sorry, I was dumb and forgot about dezimals, but those would have busted you as well :p
PIN_Number = a
alert(m, "PIN number successfully assigned.","PIN-Setup - Sucessful!")
else
(!a) ? alert(m, "PIN-Setup canceled.", "PIN-Setup") : alert(m, "Your PIN was malformated, please ensure that it has 4 digits.\nProcess canceled.","PIN-Setup")


I included three tries, followed by a lock for confirming your PIN, just for the fun of it.
In response to Schnitzelnagler
Like I said, it was just a thought process that I had tested to see if it would actually work, and it did. I just wanted to see if there was a different/better way of doing such a thing.

By the time you had posted, I had already made it more modular ^-^ But thanks for the decimal insight; I had totally forgotten about decimals >.<

One thing I did notice, was that you did 1000 to 9999. Bad, because what if the person would want to use 0000 as their PIN number? Actually, would 0000 even return anything? I'd have to text embed it is my guess, so that it does return what I want, then use text2num() or something.

EDIT
I also spotted a bug in your "retry" system you have set yup there. If a player were to click on the verb before hitting that 3 limit every time, then they could get passed it, and never hit a locked PIN number :\