Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. def pmatch(name, player=None, match_all=False, match_account=True, connected_only=False):
  2.     """
  3.      Provides a complete solution to matching players by Account or
  4.      Character, by name, alias, id or dbref and by connected status.
  5.      
  6.      
  7.      Args:
  8.        name           : the desired player name or alias.
  9.        player         : the enactor, needed to match 'self' and 'me'.
  10.        match_all      : return all matches, otherwise require a unique match.
  11.        match_account  : do the lookup with AccountDB, otherwise ObjectDB.
  12.        connected_only : filter out disconnected players.
  13.        
  14.      Returns:
  15.        Either a QuerySet, object reference, or None.
  16.        
  17.      Credits:
  18.        Darren Wheeler : Darren@Seventh Sea MUSH
  19.    """
  20.  
  21.     # If the name starts with a '*' then force an account lookup.    
  22.     if name.startswith('*'):
  23.         name = name.lstrip('*')
  24.         match_account = True
  25.    
  26.     if name.startswith('#'):
  27.         name = name.lstrip('#')
  28.  
  29.     # -------------------------------------    
  30.     # First let's handle lookup by id/dbref
  31.     # -------------------------------------    
  32.     if name.isnumeric():
  33.         try:
  34.             i = int(name)
  35.         except:
  36.             return None
  37.         if match_account:
  38.             try:
  39.                 matches = AccountDB.objects.get(pk=i)
  40.             except:
  41.                 return None
  42.             return matches if inherits_from(matches, "typeclasses.accounts.Account")  else None
  43.         else:
  44.             try:
  45.                 matches = ObjectDB.objects.get(pk=i)
  46.             except:
  47.                 return None
  48.                
  49.         return matches if matches.has_account else None
  50.     else:                
  51.         # -------------------------------------
  52.         # Now let's handle lookup by name/alias
  53.         # -------------------------------------
  54.        
  55.         # Accept 'me' and 'self' when 'player' is passed.
  56.         if player and name.lower() in ('me', 'self'):
  57.             if match_account:
  58.                 return player.account if hasattr(player, 'account') else player
  59.             else:
  60.                 return player if hasattr(player, 'account') else None
  61.  
  62.         if match_account:
  63.             # Account lookup (by username).
  64.             matches = AccountDB.objects.filter(username__startswith=name)
  65.             if not matches:
  66.                 # If no match, try a lookup by alias.
  67.                 matches = AccountDB.objects.filter(
  68.                         db_tags__db_tagtype__iexact="alias",
  69.                         **{"db_tags__db_key__iexact": name})
  70.         else:
  71.             # Character lookup (by db_key).
  72.             matches = ObjectDB.objects.filter(db_key__startswith=name)
  73.             if not matches:
  74.                 # If no matches, try a lookup by alias.
  75.                 matches = ObjectDB.objects.filter(
  76.                         db_tags__db_tagtype__iexact="alias",
  77.                         **{"db_tags__db_key__iexact": name})
  78.  
  79.         # Finally, the moment of truth!
  80.        
  81.         if matches:
  82.             if connected_only:
  83.                 # If we found any matches and we're looking for connected
  84.                 # players only, filter disconnected players out of the QuerySet.
  85.                 matches = matches.filter(db_is_connected=True)
  86.            
  87.             # If match_all is true then we want to return all matches, otherwise
  88.             # we return the first match if there is precisely one match,
  89.             # otherwise we return None.        
  90.             return matches if match_all else matches.first() if matches.count() == 1 else None
  91.  
  92.         return Non
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement