Run migrations! First version of MULTISESSION_MODE=3. @ic/@ooc not working yet.

This commit is contained in:
Griatch 2014-08-04 15:32:48 +02:00
parent 3014909feb
commit 1e41be797b
5 changed files with 206 additions and 18 deletions

View file

@ -41,6 +41,54 @@ _GA = object.__getattribute__
_SA = object.__setattr__
_DA = object.__delattr__
# the sessid_max is based on the length of the db_sessid csv field (excluding commas)
_SESSID_MAX = 16 if settings.MULTISESSION_MODE > 2 else 1
class SessidHandler(object):
"""
Handles the get/setting of the sessid
comma-separated integer field
"""
def __init__(self, obj):
self.obj = obj
self._cache = set()
self._recache()
def _recache(self):
self._cache = set(int(val) for val in (_GA(self.obj, "db_sessid") or "").split(",") if val)
def get(self):
"Returns a single integer or a list"
return self._cache if _SESSID_MAX > 1 else self._cache[0] if self._cache else None
def add(self, sessid):
"Add sessid to handler"
_cache = self._cache
if len(_cache) >= _SESSID_MAX:
return False
_cache.add(int(sessid))
_SA(self.obj, "db_sessid", ",".join(str(val) for val in _cache))
_GA(self.obj, "save")(update_fields=["db_sessid"])
return True
def remove(self, sessid):
"Remove sessid from handler"
_cache = self._cache
if sessid in _cache:
_cache.remove(sessid)
_SA(self.obj, "db_sessid", ",".join(str(val) for val in _cache))
_GA(self.obj, "save")(update_fields=["db_sessid"])
def clear(self):
"Clear sessids"
self._cache = set()
_SA(self.obj, "db_sessid", None)
_GA(self.obj, "save")(update_fields=["db_sessid"])
def count(self):
"Return amount of sessions connected"
return len(self._cache)
#------------------------------------------------------------
#
@ -101,11 +149,11 @@ class ObjectDB(TypedObject):
# will automatically save and cache the data more efficiently.
# If this is a character object, the player is connected here.
db_player = models.ForeignKey("players.PlayerDB", blank=True, null=True, verbose_name='player', on_delete=models.SET_NULL,
db_player = models.ForeignKey("players.PlayerDB", null=True, verbose_name='player', on_delete=models.SET_NULL,
help_text='a Player connected to this object, if any.')
# the session id associated with this player, if any
db_sessid = models.IntegerField(null=True, verbose_name="session id",
help_text="unique session id of connected Player, if any.")
db_sessid = models.CommaSeparatedIntegerField(null=True, max_length=32, verbose_name="session id",
help_text="csv list of session ids of connected Player, if any.")
# The location in the game world. Since this one is likely
# to change often, we set this with the 'location' property
# to transparently handle Typeclassing.
@ -142,6 +190,10 @@ class ObjectDB(TypedObject):
def nicks(self):
return NickHandler(self)
@lazy_property
def sessid(self):
return SessidHandler(self)
def _at_db_player_postsave(self):
"""
This hook is called automatically after the player field is saved.
@ -224,7 +276,6 @@ class ObjectDB(TypedObject):
_GA(_GA(self, "dbobj"), "save")(upate_fields=["db_location"])
location = property(__location_get, __location_set, __location_del)
class Meta:
"Define Django meta options"
verbose_name = "Object"
@ -500,8 +551,8 @@ class ObjectDB(TypedObject):
except Exception:
logger.log_trace()
session = _SESSIONS.session_from_sessid(sessid if sessid else _GA(self, "sessid"))
if session:
sessions = _SESSIONS.session_from_sessid([sessid] if sessid else make_iter(_GA(self, "sessid").get()))
for session in sessions:
session.msg(text=text, **kwargs)
def msg_contents(self, message, exclude=None, from_obj=None, **kwargs):