Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 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.  
  43.             if connected_only and not matches.is_connected:
  44.                 return None
  45.                                
  46.             return matches if inherits_from(matches, "typeclasses.accounts.Account")  else None
  47.         else:
  48.             try:
  49.                 matches = ObjectDB.objects.get(pk=i)
  50.             except:
  51.                 return None
  52.  
  53.             if connected_only and not matches.is_connected:
  54.                 return None
  55.                                
  56.         return matches if matches.has_account else None
  57.     else:                
  58.         # -------------------------------------
  59.         # Now let's handle lookup by name/alias
  60.         # -------------------------------------
  61.        
  62.         # Accept 'me' and 'self' when 'player' is passed.
  63.         if player and name.lower() in ('me', 'self'):
  64.             if match_account:
  65.                 return player.account if hasattr(player, 'account') else player
  66.             else:
  67.                 return player if hasattr(player, 'account') else None
  68.  
  69.         if match_account:
  70.             # Account lookup (by username).
  71.             matches = AccountDB.objects.filter(username__startswith=name)
  72.             if not matches:
  73.                 # If no match, try a lookup by alias.
  74.                 matches = AccountDB.objects.filter(
  75.                         db_tags__db_tagtype__iexact="alias",
  76.                         **{"db_tags__db_key__iexact": name})
  77.         else:
  78.             # Character lookup (by db_key).
  79.             matches = ObjectDB.objects.filter(db_key__startswith=name)
  80.             if not matches:
  81.                 # If no matches, try a lookup by alias.
  82.                 matches = ObjectDB.objects.filter(
  83.                         db_tags__db_tagtype__iexact="alias",
  84.                         **{"db_tags__db_key__iexact": name})
  85.  
  86.         # Finally, the moment of truth!
  87.        
  88.         if matches:
  89.             if connected_only:
  90.                 # If we found any matches and we're looking for connected
  91.                 # players only, filter disconnected players out of the QuerySet.
  92.                 if match_account:
  93.                     matches = matches.filter(db_is_connected=True)
  94.                 else:
  95.                     matches = matches.filter(db_account__db_is_connected=True)
  96.            
  97.             # If match_all is true then we want to return all matches, otherwise
  98.             # we return the first match if there is precisely one match,
  99.             # otherwise we return None.        
  100.             return matches if match_all else matches.first() if matches.count() == 1 else None
  101.  
  102.         return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement