Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def pmatch(name, player=None, match_all=False, match_account=True, connected_only=False):
- """
- Provides a complete solution to matching players by Account or
- Character, by name, alias, id or dbref and by connected status.
- Args:
- name : the desired player name or alias.
- player : the enactor, needed to match 'self' and 'me'.
- match_all : return all matches, otherwise require a unique match.
- match_account : do the lookup with AccountDB, otherwise ObjectDB.
- connected_only : filter out disconnected players.
- Returns:
- Either a QuerySet, object reference, or None.
- Credits:
- Darren Wheeler : Darren@Seventh Sea MUSH
- """
- # If the name starts with a '*' then force an account lookup.
- if name.startswith('*'):
- name = name.lstrip('*')
- match_account = True
- if name.startswith('#'):
- name = name.lstrip('#')
- # -------------------------------------
- # First let's handle lookup by id/dbref
- # -------------------------------------
- if name.isnumeric():
- try:
- i = int(name)
- except:
- return None
- if match_account:
- try:
- matches = AccountDB.objects.get(pk=i)
- except:
- return None
- return matches if inherits_from(matches, "typeclasses.accounts.Account") else None
- else:
- try:
- matches = ObjectDB.objects.get(pk=i)
- except:
- return None
- return matches if matches.has_account else None
- else:
- # -------------------------------------
- # Now let's handle lookup by name/alias
- # -------------------------------------
- # Accept 'me' and 'self' when 'player' is passed.
- if player and name.lower() in ('me', 'self'):
- if match_account:
- return player.account if hasattr(player, 'account') else player
- else:
- return player if hasattr(player, 'account') else None
- if match_account:
- # Account lookup (by username).
- matches = AccountDB.objects.filter(username__startswith=name)
- if not matches:
- # If no match, try a lookup by alias.
- matches = AccountDB.objects.filter(
- db_tags__db_tagtype__iexact="alias",
- **{"db_tags__db_key__iexact": name})
- else:
- # Character lookup (by db_key).
- matches = ObjectDB.objects.filter(db_key__startswith=name)
- if not matches:
- # If no matches, try a lookup by alias.
- matches = ObjectDB.objects.filter(
- db_tags__db_tagtype__iexact="alias",
- **{"db_tags__db_key__iexact": name})
- # Finally, the moment of truth!
- if matches:
- if connected_only:
- # If we found any matches and we're looking for connected
- # players only, filter disconnected players out of the QuerySet.
- matches = matches.filter(db_is_connected=True)
- # If match_all is true then we want to return all matches, otherwise
- # we return the first match if there is precisely one match,
- # otherwise we return None.
- return matches if match_all else matches.first() if matches.count() == 1 else None
- return Non
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement