Some minor pep8 fixes and refactoring.

This commit is contained in:
Griatch 2017-04-11 08:35:30 +02:00
parent 63f4f04d77
commit a9bee74b37

View file

@ -46,12 +46,13 @@ from evennia import syscmdkeys
from evennia.utils.evmenu import EvMenu from evennia.utils.evmenu import EvMenu
from evennia.utils.utils import random_string_from_module from evennia.utils.utils import random_string_from_module
## Constants # Constants
RE_VALID_USERNAME = re.compile(r"^[a-z]{3,}$", re.I) RE_VALID_USERNAME = re.compile(r"^[a-z]{3,}$", re.I)
LEN_PASSWD = 6 LEN_PASSWD = 6
CONNECTION_SCREEN_MODULE = settings.CONNECTION_SCREEN_MODULE CONNECTION_SCREEN_MODULE = settings.CONNECTION_SCREEN_MODULE
## Menu notes (top-level functions) # Menu notes (top-level functions)
def start(caller): def start(caller):
"""The user should enter his/her username or NEW to create one. """The user should enter his/her username or NEW to create one.
@ -67,22 +68,16 @@ def start(caller):
text += "\n\nEnter your username or |yNEW|n to create a new account." text += "\n\nEnter your username or |yNEW|n to create a new account."
options = ( options = (
{"key": "", {"key": "",
"goto": "start", "goto": "start"},
}, {"key": "new",
{ "goto": "create_account"},
"key": "new",
"goto": "create_account",
},
{"key": "quit", {"key": "quit",
"goto": "quit" "goto": "quit"},
}, {"key": "_default",
{ "goto": "username"})
"key": "_default",
"goto": "username",
},
)
return text, options return text, options
def username(caller, string_input): def username(caller, string_input):
"""Check that the username leads to an existing player. """Check that the username leads to an existing player.
@ -100,34 +95,25 @@ def username(caller, string_input):
Try another name or leave empty to go back. Try another name or leave empty to go back.
""".strip("\n")).format(string_input) """.strip("\n")).format(string_input)
options = ( options = (
{ {"key": "",
"key": "", "goto": "start"},
"goto": "start", {"key": "_default",
}, "goto": "username"})
{
"key": "_default",
"goto": "username",
},
)
else: else:
caller.ndb._menutree.player = player caller.ndb._menutree.player = player
text = "Enter the password for the {} account.".format(player.name) text = "Enter the password for the {} account.".format(player.name)
# Disables echo for the password # Disables echo for the password
caller.msg("", options={"echo": False}) caller.msg("", options={"echo": False})
options = ( options = (
{ {"key": "",
"key": "",
"exec": lambda caller: caller.msg("", options={"echo": True}), "exec": lambda caller: caller.msg("", options={"echo": True}),
"goto": "start", "goto": "start"},
}, {"key": "_default",
{ "goto": "ask_password"})
"key": "_default",
"goto": "ask_password",
},
)
return text, options return text, options
def ask_password(caller, string_input): def ask_password(caller, string_input):
"""Ask the user to enter the password to this player. """Ask the user to enter the password to this player.
@ -147,8 +133,8 @@ def ask_password(caller, string_input):
password_attempts = menutree.password_attempts \ password_attempts = menutree.password_attempts \
if hasattr(menutree, "password_attempts") else 0 if hasattr(menutree, "password_attempts") else 0
bans = ServerConfig.objects.conf("server_bans") bans = ServerConfig.objects.conf("server_bans")
banned = bans and (any(tup[0] == player.name.lower() for tup in bans) \ banned = bans and (any(tup[0] == player.name.lower() for tup in bans) or
or any(tup[2].match(caller.address) for tup in bans if tup[2])) any(tup[2].match(caller.address) for tup in bans if tup[2]))
if not player.check_password(string_input): if not player.check_password(string_input):
# Didn't enter a correct password # Didn't enter a correct password
@ -167,16 +153,11 @@ def ask_password(caller, string_input):
""".strip("\n")) """.strip("\n"))
# Loops on the same node # Loops on the same node
options = ( options = (
{ {"key": "",
"key": "",
"exec": lambda caller: caller.msg("", options={"echo": True}), "exec": lambda caller: caller.msg("", options={"echo": True}),
"goto": "start", "goto": "start"},
}, {"key": "_default",
{ "goto": "ask_password"})
"key": "_default",
"goto": "ask_password",
},
)
elif banned: elif banned:
# This is a banned IP or name! # This is a banned IP or name!
string = dedent(""" string = dedent("""
@ -196,6 +177,7 @@ def ask_password(caller, string_input):
return text, options return text, options
def create_account(caller): def create_account(caller):
"""Create a new account. """Create a new account.
@ -205,13 +187,11 @@ def create_account(caller):
""" """
text = "Enter your new account name." text = "Enter your new account name."
options = ( options = (
{ {"key": "_default",
"key": "_default", "goto": "create_username"},)
"goto": "create_username",
},
)
return text, options return text, options
def create_username(caller, string_input): def create_username(caller, string_input):
"""Prompt to enter a valid username (one that doesnt exist). """Prompt to enter a valid username (one that doesnt exist).
@ -231,15 +211,10 @@ def create_username(caller, string_input):
""".strip("\n")).format(string_input) """.strip("\n")).format(string_input)
# Loops on the same node # Loops on the same node
options = ( options = (
{ {"key": "",
"key": "", "goto": "start"},
"goto": "start", {"key": "_default",
}, "goto": "create_username"})
{
"key": "_default",
"goto": "create_username",
},
)
elif not RE_VALID_USERNAME.search(string_input): elif not RE_VALID_USERNAME.search(string_input):
text = dedent(""" text = dedent("""
|rThis username isn't valid.|n |rThis username isn't valid.|n
@ -248,15 +223,10 @@ def create_username(caller, string_input):
Enter another username or leave blank to go back. Enter another username or leave blank to go back.
""".strip("\n")) """.strip("\n"))
options = ( options = (
{ {"key": "",
"key": "", "goto": "start"},
"goto": "start", {"key": "_default",
}, "goto": "create_username"})
{
"key": "_default",
"goto": "create_username",
},
)
else: else:
# a valid username - continue getting the password # a valid username - continue getting the password
menutree.playername = string_input menutree.playername = string_input
@ -265,14 +235,12 @@ def create_username(caller, string_input):
# Redirects to the creation of a password # Redirects to the creation of a password
text = "Enter this account's new password." text = "Enter this account's new password."
options = ( options = (
{ {"key": "_default",
"key": "_default", "goto": "create_password"},)
"goto": "create_password",
},
)
return text, options return text, options
def create_password(caller, string_input): def create_password(caller, string_input):
"""Ask the user to create a password. """Ask the user to create a password.
@ -284,16 +252,11 @@ def create_password(caller, string_input):
menutree = caller.ndb._menutree menutree = caller.ndb._menutree
text = "" text = ""
options = ( options = (
{ {"key": "",
"key": "",
"exec": lambda caller: caller.msg("", options={"echo": True}), "exec": lambda caller: caller.msg("", options={"echo": True}),
"goto": "start", "goto": "start"},
}, {"key": "_default",
{ "goto": "create_password"})
"key": "_default",
"goto": "create_password",
},
)
password = string_input.strip() password = string_input.strip()
playername = menutree.playername playername = menutree.playername
@ -338,11 +301,13 @@ def create_password(caller, string_input):
return text, options return text, options
def quit(caller): def quit(caller):
caller.sessionhandler.disconnect(caller, "Goodbye! Logging off.") caller.sessionhandler.disconnect(caller, "Goodbye! Logging off.")
return "", {} return "", {}
## Other functions # Other functions
def _formatter(nodetext, optionstext, caller=None): def _formatter(nodetext, optionstext, caller=None):
"""Do not display the options, only the text. """Do not display the options, only the text.
@ -354,7 +319,8 @@ def _formatter(nodetext, optionstext, caller=None):
""" """
return nodetext return nodetext
## Commands and CmdSets
# Commands and CmdSets
class UnloggedinCmdSet(CmdSet): class UnloggedinCmdSet(CmdSet):
"Cmdset for the unloggedin state" "Cmdset for the unloggedin state"