Further cleanup of source; making class methods _private for clarity in the API.
This commit is contained in:
parent
fc156b5a54
commit
c8df141e89
18 changed files with 607 additions and 588 deletions
|
|
@ -2,12 +2,13 @@
|
|||
The managers for the custom Player object and permissions.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import datetime
|
||||
from functools import update_wrapper
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from src.typeclasses.managers import returns_typeclass_list, returns_typeclass, TypedObjectManager
|
||||
from src.utils import logger
|
||||
__all__ = ("PlayerManager",)
|
||||
|
||||
#
|
||||
# Player Manager
|
||||
|
|
@ -35,16 +36,16 @@ def returns_player_list(method):
|
|||
players.append(user.get_profile())
|
||||
except Exception:
|
||||
# there is something wrong with get_profile. But
|
||||
# there is a 1-1 relation between Users-Players, so we
|
||||
# there is a 1-1 relation between Users-Players, so we
|
||||
# try to go the other way instead.
|
||||
from src.players.models import PlayerDB
|
||||
match = PlayerDB.objects.filter(user=user)
|
||||
if match:
|
||||
players.append(match[0])
|
||||
else:
|
||||
logger.log_trace("No connection User<->Player, maybe database was partially reset?")
|
||||
logger.log_trace("No connection User<->Player, maybe database was partially reset?")
|
||||
return players
|
||||
return update_wrapper(func, method)
|
||||
return update_wrapper(func, method)
|
||||
|
||||
def returns_player(method):
|
||||
"""
|
||||
|
|
@ -57,18 +58,18 @@ def returns_player(method):
|
|||
if match:
|
||||
return match[0]
|
||||
else:
|
||||
return None
|
||||
return None
|
||||
return update_wrapper(func, method)
|
||||
|
||||
class PlayerManager(TypedObjectManager):
|
||||
"""
|
||||
This PlayerManager implements methods for searching
|
||||
This PlayerManager implements methods for searching
|
||||
and manipulating Players directly from the database.
|
||||
|
||||
Evennia-specific search methods (will return Characters if
|
||||
possible or a Typeclass/list of Typeclassed objects, whereas
|
||||
Django-general methods will return Querysets or database objects):
|
||||
|
||||
|
||||
dbref (converter)
|
||||
dbref_search
|
||||
get_dbref_range
|
||||
|
|
@ -83,14 +84,14 @@ class PlayerManager(TypedObjectManager):
|
|||
get_player_from_name
|
||||
player_search (equivalent to ev.search_player)
|
||||
swap_character
|
||||
|
||||
|
||||
"""
|
||||
def num_total_players(self):
|
||||
"""
|
||||
Returns the total number of registered users/players.
|
||||
"""
|
||||
return self.count()
|
||||
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_connected_players(self):
|
||||
"""
|
||||
|
|
@ -99,7 +100,7 @@ class PlayerManager(TypedObjectManager):
|
|||
return [player for player in self.all() if player.sessions]
|
||||
|
||||
@returns_typeclass_list
|
||||
@returns_player_list
|
||||
@returns_player_list
|
||||
def get_recently_created_players(self, days=7):
|
||||
"""
|
||||
Returns a QuerySet containing the player User accounts that have been
|
||||
|
|
@ -145,8 +146,8 @@ class PlayerManager(TypedObjectManager):
|
|||
players = self.filter(user__username=uname)
|
||||
if players:
|
||||
return players[0]
|
||||
return None
|
||||
|
||||
return None
|
||||
|
||||
# @returns_typeclass_list
|
||||
# def get_players_with_perm(self, permstring):
|
||||
# """
|
||||
|
|
@ -159,7 +160,7 @@ class PlayerManager(TypedObjectManager):
|
|||
# @returns_typeclass_list
|
||||
# def get_players_with_group(self, groupstring):
|
||||
# """
|
||||
# Returns all players belonging to the given group.
|
||||
# Returns all players belonging to the given group.
|
||||
# """
|
||||
# return [player.user for player in self.all()
|
||||
# if player.has_group(groupstring)]
|
||||
|
|
@ -167,9 +168,9 @@ class PlayerManager(TypedObjectManager):
|
|||
@returns_typeclass_list
|
||||
def player_search(self, ostring):
|
||||
"""
|
||||
Searches for a particular player by name or
|
||||
Searches for a particular player by name or
|
||||
database id.
|
||||
|
||||
|
||||
ostring = a string or database id.
|
||||
"""
|
||||
ostring = ostring.lstrip("*")
|
||||
|
|
@ -178,7 +179,7 @@ class PlayerManager(TypedObjectManager):
|
|||
matches = self.filter(id=dbref)
|
||||
if matches:
|
||||
return matches
|
||||
return self.filter(user__username__iexact=ostring)
|
||||
return self.filter(user__username__iexact=ostring)
|
||||
|
||||
def swap_character(self, player, new_character, delete_old_character=False):
|
||||
"""
|
||||
|
|
@ -192,7 +193,7 @@ class PlayerManager(TypedObjectManager):
|
|||
return False
|
||||
|
||||
# do the swap
|
||||
old_character = player.character
|
||||
old_character = player.character
|
||||
if old_character:
|
||||
old_character.player = None
|
||||
try:
|
||||
|
|
@ -202,8 +203,7 @@ class PlayerManager(TypedObjectManager):
|
|||
# recover old setup
|
||||
old_character.player = player
|
||||
player.character = old_character
|
||||
return False
|
||||
return False
|
||||
if delete_old_character:
|
||||
old_character.delete()
|
||||
return True
|
||||
|
||||
return True
|
||||
|
|
|
|||
|
|
@ -53,6 +53,8 @@ from src.utils import logger, utils
|
|||
from src.commands.cmdsethandler import CmdSetHandler
|
||||
from src.commands import cmdhandler
|
||||
|
||||
__all__ = ("PlayerAttribute", "PlayerNick", "PlayerDB")
|
||||
|
||||
_AT_SEARCH_RESULT = utils.mod_import(*settings.SEARCH_AT_RESULT.rsplit('.', 1))
|
||||
|
||||
#------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
"""
|
||||
Typeclass for Player objects
|
||||
|
||||
Note that this object is primarily intended to
|
||||
store OOC information, not game info! This
|
||||
Note that this object is primarily intended to
|
||||
store OOC information, not game info! This
|
||||
object represents the actual user (not their
|
||||
character) and has NO actual precence in the
|
||||
character) and has NO actual precence in the
|
||||
game world (this is handled by the associated
|
||||
character object, so you should customize that
|
||||
instead for most things).
|
||||
|
|
@ -12,12 +12,12 @@ instead for most things).
|
|||
"""
|
||||
from django.conf import settings
|
||||
from src.typeclasses.typeclass import TypeClass
|
||||
|
||||
__all__ = ("Player",)
|
||||
CMDSET_OOC = settings.CMDSET_OOC
|
||||
|
||||
class Player(TypeClass):
|
||||
"""
|
||||
Base typeclass for all Players.
|
||||
Base typeclass for all Players.
|
||||
"""
|
||||
def __init__(self, dbobj):
|
||||
"""
|
||||
|
|
@ -27,7 +27,7 @@ class Player(TypeClass):
|
|||
can connect to a Character Object in order to "enter" the
|
||||
game.
|
||||
|
||||
Player Typeclass API:
|
||||
Player Typeclass API:
|
||||
|
||||
* Available properties (only available on initiated typeclass objects)
|
||||
|
||||
|
|
@ -38,22 +38,22 @@ class Player(TypeClass):
|
|||
dbobj (Player, read-only) - link to database model. dbobj.typeclass points back to this class
|
||||
typeclass (Player, read-only) - this links back to this class as an identified only. Use self.swap_typeclass() to switch.
|
||||
date_created (string) - time stamp of object creation
|
||||
permissions (list of strings) - list of permission strings
|
||||
permissions (list of strings) - list of permission strings
|
||||
|
||||
user (User, read-only) - django User authorization object
|
||||
obj (Object) - game object controlled by player. 'character' can also be used.
|
||||
sessions (list of Sessions) - sessions connected to this player
|
||||
is_superuser (bool, read-only) - if the connected user is a superuser
|
||||
|
||||
* Handlers
|
||||
* Handlers
|
||||
|
||||
locks - lock-handler: use locks.add() to add new lock strings
|
||||
db - attribute-handler: store/retrieve database attributes on this self.db.myattr=val, val=self.db.myattr
|
||||
ndb - non-persistent attribute handler: same as db but does not create a database entry when storing data
|
||||
ndb - non-persistent attribute handler: same as db but does not create a database entry when storing data
|
||||
scripts - script-handler. Add new scripts to object with scripts.add()
|
||||
cmdset - cmdset-handler. Use cmdset.add() to add new cmdsets to object
|
||||
nicks - nick-handler. New nicks with nicks.add().
|
||||
|
||||
|
||||
* Helper methods
|
||||
|
||||
msg(outgoing_string, from_obj=None, data=None)
|
||||
|
|
@ -68,12 +68,12 @@ class Player(TypeClass):
|
|||
* Hook methods
|
||||
|
||||
basetype_setup()
|
||||
at_player_creation()
|
||||
at_player_creation()
|
||||
|
||||
- note that the following hooks are also found on Objects and are
|
||||
usually handled on the character level:
|
||||
|
||||
at_init()
|
||||
at_init()
|
||||
at_cmdset_get()
|
||||
at_first_login()
|
||||
at_post_login()
|
||||
|
|
@ -86,11 +86,11 @@ class Player(TypeClass):
|
|||
"""
|
||||
super(Player, self).__init__(dbobj)
|
||||
|
||||
## methods inherited from database model
|
||||
## methods inherited from database model
|
||||
|
||||
def msg(self, outgoing_string, from_obj=None, data=None):
|
||||
"""
|
||||
Evennia -> User
|
||||
"""
|
||||
Evennia -> User
|
||||
This is the main route for sending data back to the user from the server.
|
||||
|
||||
outgoing_string (string) - text data to send
|
||||
|
|
@ -106,7 +106,7 @@ class Player(TypeClass):
|
|||
|
||||
new_character (Object) - character/object to swap to
|
||||
delete_old_character (bool) - delete the old character when swapping
|
||||
|
||||
|
||||
Returns: True/False depending on if swap suceeded or not.
|
||||
"""
|
||||
return self.dbobj.swap_character(new_character, delete_old_character=delete_old_character)
|
||||
|
|
@ -117,44 +117,44 @@ class Player(TypeClass):
|
|||
lets its typeclass execute the command. Evennia also calls
|
||||
this method whenever the player sends a command on the command line.
|
||||
|
||||
Argument:
|
||||
Argument:
|
||||
raw_string (string) - raw command input
|
||||
|
||||
Returns Deferred - this is an asynchronous Twisted object that will
|
||||
not fire until the command has actually finished executing. To overload
|
||||
this one needs to attach callback functions to it, with addCallback(function).
|
||||
this one needs to attach callback functions to it, with addCallback(function).
|
||||
This function will be called with an eventual return value from the command
|
||||
execution.
|
||||
execution.
|
||||
|
||||
This return is not used at all by Evennia by default, but might be useful
|
||||
for coders intending to implement some sort of nested command structure.
|
||||
for coders intending to implement some sort of nested command structure.
|
||||
"""
|
||||
self.dbobj.execute_cmd(raw_string)
|
||||
|
||||
def search(self, ostring, global_search=False, attribute_name=None, use_nicks=False,
|
||||
def search(self, ostring, global_search=False, attribute_name=None, use_nicks=False,
|
||||
location=None, ignore_errors=False, player=False):
|
||||
"""
|
||||
This method mimicks object.search if self.character is set. Otherwise only
|
||||
other Players can be searched with this method.
|
||||
other Players can be searched with this method.
|
||||
"""
|
||||
self.dbobj.search(ostring, global_search=global_search, attribute_name=attribute_name, use_nicks=use_nicks,
|
||||
self.dbobj.search(ostring, global_search=global_search, attribute_name=attribute_name, use_nicks=use_nicks,
|
||||
location=location, ignore_errors=ignore_errors, player=player)
|
||||
|
||||
|
||||
def is_typeclass(self, typeclass, exact=False):
|
||||
"""
|
||||
Returns true if this object has this type
|
||||
OR has a typeclass which is an subclass of
|
||||
the given typeclass.
|
||||
|
||||
|
||||
typeclass - can be a class object or the
|
||||
python path to such an object to match against.
|
||||
|
||||
python path to such an object to match against.
|
||||
|
||||
exact - returns true only if the object's
|
||||
type is exactly this typeclass, ignoring
|
||||
parents.
|
||||
|
||||
Returns: Boolean
|
||||
"""
|
||||
Returns: Boolean
|
||||
"""
|
||||
return self.dbobj.is_typeclass(typeclass, exact=exact)
|
||||
|
||||
def swap_typeclass(self, new_typeclass, clean_attributes=False, no_default=True):
|
||||
|
|
@ -162,18 +162,18 @@ class Player(TypeClass):
|
|||
This performs an in-situ swap of the typeclass. This means
|
||||
that in-game, this object will suddenly be something else.
|
||||
Player will not be affected. To 'move' a player to a different
|
||||
object entirely (while retaining this object's type), use
|
||||
object entirely (while retaining this object's type), use
|
||||
self.player.swap_object().
|
||||
|
||||
Note that this might be an error prone operation if the
|
||||
Note that this might be an error prone operation if the
|
||||
old/new typeclass was heavily customized - your code
|
||||
might expect one and not the other, so be careful to
|
||||
might expect one and not the other, so be careful to
|
||||
bug test your code if using this feature! Often its easiest
|
||||
to create a new object and just swap the player over to
|
||||
that one instead.
|
||||
that one instead.
|
||||
|
||||
Arguments:
|
||||
new_typeclass (path/classobj) - type to switch to
|
||||
Arguments:
|
||||
new_typeclass (path/classobj) - type to switch to
|
||||
clean_attributes (bool/list) - will delete all attributes
|
||||
stored on this object (but not any
|
||||
of the database fields such as name or
|
||||
|
|
@ -185,10 +185,10 @@ class Player(TypeClass):
|
|||
no_default - if this is active, the swapper will not allow for
|
||||
swapping to a default typeclass in case the given
|
||||
one fails for some reason. Instead the old one
|
||||
will be preserved.
|
||||
Returns:
|
||||
will be preserved.
|
||||
Returns:
|
||||
boolean True/False depending on if the swap worked or not.
|
||||
|
||||
|
||||
|
||||
"""
|
||||
self.dbobj.swap_typeclass(new_typeclass, clean_attributes=clean_attributes, no_default=no_default)
|
||||
|
|
@ -200,52 +200,52 @@ class Player(TypeClass):
|
|||
accessing_obj (Object)- object trying to access this one
|
||||
access_type (string) - type of access sought
|
||||
default (bool) - what to return if no lock of access_type was found
|
||||
"""
|
||||
"""
|
||||
return self.dbobj.access(accessing_obj, access_type=access_type, default=default)
|
||||
|
||||
def check_permstring(self, permstring):
|
||||
"""
|
||||
This explicitly checks the given string against this object's
|
||||
'permissions' property without involving any locks.
|
||||
|
||||
|
||||
permstring (string) - permission string that need to match a permission on the object.
|
||||
(example: 'Builders')
|
||||
"""
|
||||
return self.dbobj.check_permstring(permstring)
|
||||
|
||||
## player hooks
|
||||
|
||||
|
||||
def basetype_setup(self):
|
||||
"""
|
||||
This sets up the basic properties for a player.
|
||||
This sets up the basic properties for a player.
|
||||
Overload this with at_player_creation rather than
|
||||
changing this method.
|
||||
|
||||
changing this method.
|
||||
|
||||
"""
|
||||
# the text encoding to use.
|
||||
self.db.encoding = "utf-8"
|
||||
|
||||
|
||||
# A basic security setup
|
||||
self.locks.add("examine:perm(Wizards)")
|
||||
self.locks.add("edit:perm(Wizards)")
|
||||
self.locks.add("delete:perm(Wizards)")
|
||||
self.locks.add("boot:perm(Wizards)")
|
||||
self.locks.add("boot:perm(Wizards)")
|
||||
self.locks.add("msg:all()")
|
||||
|
||||
# The ooc player cmdset
|
||||
# The ooc player cmdset
|
||||
self.cmdset.add_default(CMDSET_OOC, permanent=True)
|
||||
self.cmdset.outside_access = False
|
||||
|
||||
self.cmdset.outside_access = False
|
||||
|
||||
def at_player_creation(self):
|
||||
"""
|
||||
This is called once, the very first time
|
||||
the player is created (i.e. first time they
|
||||
register with the game). It's a good place
|
||||
to store attributes all players should have,
|
||||
like configuration values etc.
|
||||
"""
|
||||
like configuration values etc.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def at_init(self):
|
||||
"""
|
||||
|
|
@ -257,7 +257,7 @@ class Player(TypeClass):
|
|||
happens the moment the player logs in or reconnects after a
|
||||
reload.
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
||||
# Note that the hooks below also exist in the character object's
|
||||
# typeclass. You can often ignore these and rely on the character
|
||||
|
|
@ -292,7 +292,7 @@ class Player(TypeClass):
|
|||
"""
|
||||
Called at the end of the login
|
||||
process, just before letting
|
||||
them loose.
|
||||
them loose.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
|
@ -307,29 +307,29 @@ class Player(TypeClass):
|
|||
"""
|
||||
Called when any text is emitted to this
|
||||
object. If it returns False, no text
|
||||
will be sent automatically.
|
||||
will be sent automatically.
|
||||
"""
|
||||
return True
|
||||
return True
|
||||
|
||||
def at_message_send(self, message, to_object):
|
||||
"""
|
||||
Called whenever this object tries to send text
|
||||
to another object. Only called if the object supplied
|
||||
itself as a sender in the msg() call.
|
||||
itself as a sender in the msg() call.
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
||||
def at_server_reload(self):
|
||||
"""
|
||||
This hook is called whenever the server is shutting down for restart/reboot.
|
||||
This hook is called whenever the server is shutting down for restart/reboot.
|
||||
If you want to, for example, save non-persistent properties across a restart,
|
||||
this is the place to do it.
|
||||
this is the place to do it.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_server_shutdown(self):
|
||||
"""
|
||||
This hook is called whenever the server is shutting down fully (i.e. not for
|
||||
a restart).
|
||||
This hook is called whenever the server is shutting down fully (i.e. not for
|
||||
a restart).
|
||||
"""
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue