def __set_obj(self, value): """ Set player or obj to their right database field. If a dbref is given, assume ObjectDB. """ try: value = _GA(value, "dbobj") except AttributeError: pass if isinstance(value, (basestring, int)): from src.objects.models import ObjectDB value = to_str(value, force_string=True) if (value.isdigit() or value.startswith("#")): dbid = dbref(value, reqhash=False) if dbid: try: value = ObjectDB.objects.get(id=dbid) except ObjectDoesNotExist: # maybe it is just a name that happens to look like a dbid pass if value.__class__.__name__ == "PlayerDB": fname = "db_player" _SA(self, fname, value) else: fname = "db_obj" _SA(self, fname, value) # saving the field _GA(self, "save")(update_fields=[fname])
def _set_foreign(cls, fname, value): "Setter only used on foreign key relations, allows setting with #dbref" try: value = _GA(value, "dbobj") except AttributeError: pass if isinstance(value, (basestring, int)): value = to_str(value, force_string=True) if (value.isdigit() or value.startswith("#")): # we also allow setting using dbrefs, if so we try to load the matching object. # (we assume the object is of the same type as the class holding the field, if # not a custom handler must be used for that field) dbid = dbref(value, reqhash=False) if dbid: model = _GA(cls, "_meta").get_field(fname).model try: value = model._default_manager.get(id=dbid) except ObjectDoesNotExist: # maybe it is just a name that happens to look like a dbid pass _SA(cls, fname, value) # only use explicit update_fields in save if we actually have a # primary key assigned already (won't be set when first creating object) update_fields = [fname] if _GA(cls, "_get_pk_val")(_GA(cls, "_meta")) is not None else None _GA(cls, "save")(update_fields=update_fields)
def _set_foreign(cls, fname, value): "Setter only used on foreign key relations, allows setting with #dbref" if _GA(cls, "_is_deleted"): raise ObjectDoesNotExist( "Cannot set %s to %s: Hosting object was already deleted!" % (fname, value)) try: value = _GA(value, "dbobj") except AttributeError: pass if isinstance(value, (basestring, int)): value = to_str(value, force_string=True) if (value.isdigit() or value.startswith("#")): # we also allow setting using dbrefs, if so we try to load the matching object. # (we assume the object is of the same type as the class holding the field, if # not a custom handler must be used for that field) dbid = dbref(value, reqhash=False) if dbid: model = _GA(cls, "_meta").get_field(fname).model try: value = model._default_manager.get(id=dbid) except ObjectDoesNotExist: # maybe it is just a name that happens to look like a dbid pass _SA(cls, fname, value) # only use explicit update_fields in save if we actually have a # primary key assigned already (won't be set when first creating object) update_fields = [fname] if _GA(cls, "_get_pk_val")(_GA( cls, "_meta")) is not None else None _GA(cls, "save")(update_fields=update_fields)
def check_holds(objid): # helper function. Compares both dbrefs and keys/aliases. objid = str(objid) dbref = utils.dbref(objid) if dbref and any((True for obj in contents if obj.dbid == dbref)): return True objid = objid.lower() return any((True for obj in contents if obj.key.lower() == objid or objid in [al.lower() for al in obj.aliases]))
def check_holds(objid): # helper function. Compares both dbrefs and keys/aliases. objid = str(objid) dbref = utils.dbref(objid, reqhash=False) if dbref and any((True for obj in contents if obj.dbid == dbref)): return True objid = objid.lower() return any((True for obj in contents if obj.key.lower() == objid or objid in [al.lower() for al in obj.aliases.all()]))
def find_topicmatch(self, topicstr, exact=False): """ Searches for matching topics based on player's input. """ dbref = utils.dbref(topicstr) if dbref: return self.filter(id=dbref) topics = self.filter(db_key__iexact=topicstr) if not topics and not exact: topics = self.filter(db_key__istartswith=topicstr) if not topics: topics = self.filter(db_key__icontains=topicstr) return topics
def __location_set(self, location): "Set location, checking for loops and allowing dbref" if isinstance(location, (basestring, int)): # allow setting of #dbref dbid = dbref(location, reqhash=False) if dbid: try: location = ObjectDB.objects.get(id=dbid) except ObjectDoesNotExist: # maybe it is just a name that happens to look like a dbid pass try: def is_loc_loop(loc, depth=0): "Recursively traverse target location, trying to catch a loop." if depth > 10: return elif loc == self: raise RuntimeError elif loc == None: raise RuntimeWarning return is_loc_loop(_GA(_GA(loc, "dbobj"), "db_location"), depth + 1) try: is_loc_loop(location) except RuntimeWarning: pass # actually set the field _SA(_GA(self, "dbobj"), "db_location", _GA(location, "dbobj") if location else location) _GA(_GA(self, "dbobj"), "save")(update_fields=["db_location"]) except RuntimeError: errmsg = "Error: %s.location = %s creates a location loop." % ( self.key, location) logger.log_errmsg(errmsg) raise RuntimeError(errmsg) except Exception, e: errmsg = "Error (%s): %s is not a valid location." % (str(e), location) logger.log_errmsg(errmsg) raise Exception(errmsg)
def __location_set(self, location): "Set location, checking for loops and allowing dbref" if isinstance(location, (basestring, int)): # allow setting of #dbref dbid = dbref(location, reqhash=False) if dbid: try: location = ObjectDB.objects.get(id=dbid) except ObjectDoesNotExist: # maybe it is just a name that happens to look like a dbid pass try: def is_loc_loop(loc, depth=0): "Recursively traverse target location, trying to catch a loop." if depth > 10: return elif loc == self: raise RuntimeError elif loc == None: raise RuntimeWarning return is_loc_loop(_GA(_GA(loc, "dbobj"), "db_location"), depth + 1) try: is_loc_loop(location) except RuntimeWarning: pass # actually set the field _SA(_GA(self, "dbobj"), "db_location", _GA(location, "dbobj") if location else location) _GA(_GA(self, "dbobj"), "save")(update_fields=["db_location"]) except RuntimeError: errmsg = "Error: %s.location = %s creates a location loop." % (self.key, location) logger.log_errmsg(errmsg) raise RuntimeError(errmsg) except Exception, e: errmsg = "Error (%s): %s is not a valid location." % (str(e), location) logger.log_errmsg(errmsg) raise Exception(errmsg)
def func(self): "Setup the irc-channel mapping" if not settings.IRC_ENABLED: string = """IRC is not enabled. You need to activate it in game/settings.py.""" self.msg(string) return if 'list' in self.switches: # show all connections ircbots = [ bot.typeclass for bot in PlayerDB.objects.filter( db_is_bot=True, username__startswith="ircbot-") ] if ircbots: from src.utils.evtable import EvTable table = EvTable("{wdbid{n", "{wbotname{n", "{wev-channel{n", "{wirc-channel{n", maxwidth=78) for ircbot in ircbots: ircinfo = "%s (%s:%s)" % (ircbot.db.irc_channel, ircbot.db.irc_network, ircbot.db.irc_port) table.add_row(ircbot.id, ircbot.db.irc_botname, ircbot.db.ev_channel, ircinfo) self.caller.msg(table) else: self.msg("No irc bots found.") return if ('disconnect' in self.switches or 'remove' in self.switches or 'delete' in self.switches): botname = "ircbot-%s" % self.lhs matches = PlayerDB.objects.filter(db_is_bot=True, username=botname) dbref = utils.dbref(self.lhs) if not matches and dbref: # try dbref match matches = PlayerDB.objects.filter(db_is_bot=True, id=dbref) if matches: matches[0].delete() self.msg("IRC connection destroyed.") else: self.msg( "IRC connection/bot could not be removed, does it exist?") return if not self.args or not self.rhs: string = "Usage: @irc2chan[/switches] <evennia_channel> = <ircnetwork> <port> <#irchannel> <botname>" self.msg(string) return channel = self.lhs self.rhs = self.rhs.replace('#', ' ') # to avoid Python comment issues try: irc_network, irc_port, irc_channel, irc_botname = \ [part.strip() for part in self.rhs.split(None, 3)] irc_channel = "#%s" % irc_channel except Exception: string = "IRC bot definition '%s' is not valid." % self.rhs self.msg(string) return botname = "ircbot-%s" % irc_botname # create a new bot bot = PlayerDB.objects.filter(username__iexact=botname) if bot: # re-use an existing bot bot = bot[0].typeclass if not bot.is_bot: self.msg("Player '%s' already exists and is not a bot." % botname) return else: bot = create.create_player(botname, None, None, typeclass=bots.IRCBot) bot.start(ev_channel=channel, irc_botname=irc_botname, irc_channel=irc_channel, irc_network=irc_network, irc_port=irc_port) self.msg("Connection created. Starting IRC bot.")
def func(self): "Setup the irc-channel mapping" if not settings.IRC_ENABLED: string = """IRC is not enabled. You need to activate it in game/settings.py.""" self.msg(string) return if 'list' in self.switches: # show all connections ircbots = [bot.typeclass for bot in PlayerDB.objects.filter(db_is_bot=True, username__startswith="ircbot-")] if ircbots: from src.utils.evtable import EvTable table = EvTable("{wdbid{n", "{wbotname{n", "{wev-channel{n", "{wirc-channel{n", maxwidth=78) for ircbot in ircbots: ircinfo = "%s (%s:%s)" % (ircbot.db.irc_channel, ircbot.db.irc_network, ircbot.db.irc_port) table.add_row(ircbot.id, ircbot.db.irc_botname, ircbot.db.ev_channel, ircinfo) self.caller.msg(table) else: self.msg("No irc bots found.") return if('disconnect' in self.switches or 'remove' in self.switches or 'delete' in self.switches): botname = "ircbot-%s" % self.lhs matches = PlayerDB.objects.filter(db_is_bot=True, username=botname) dbref = utils.dbref(self.lhs) if not matches and dbref: # try dbref match matches = PlayerDB.objects.filter(db_is_bot=True, id=dbref) if matches: matches[0].delete() self.msg("IRC connection destroyed.") else: self.msg("IRC connection/bot could not be removed, does it exist?") return if not self.args or not self.rhs: string = "Usage: @irc2chan[/switches] <evennia_channel> = <ircnetwork> <port> <#irchannel> <botname>" self.msg(string) return channel = self.lhs self.rhs = self.rhs.replace('#', ' ') # to avoid Python comment issues try: irc_network, irc_port, irc_channel, irc_botname = \ [part.strip() for part in self.rhs.split(None, 3)] irc_channel = "#%s" % irc_channel except Exception: string = "IRC bot definition '%s' is not valid." % self.rhs self.msg(string) return botname = "ircbot-%s" % irc_botname # create a new bot bot = PlayerDB.objects.filter(username__iexact=botname) if bot: # re-use an existing bot bot = bot[0].typeclass if not bot.is_bot: self.msg("Player '%s' already exists and is not a bot." % botname) return else: bot = create.create_player(botname, None, None, typeclass=bots.IRCBot) bot.start(ev_channel=channel, irc_botname=irc_botname, irc_channel=irc_channel, irc_network=irc_network, irc_port=irc_port) self.msg("Connection created. Starting IRC bot.")