public static Player[] FindPlayers( [NotNull] Player player, [NotNull] string name, SearchOptions options ) { if( player == null ) throw new ArgumentNullException( "player" ); if( name == null ) throw new ArgumentNullException( "name" ); bool includeHidden = (options & SearchOptions.IncludeHidden) != 0; bool includeSelf = (options & SearchOptions.IncludeSelf) != 0; bool suppressEvent = (options & SearchOptions.SuppressEvent) != 0; bool returnSelf = (options & SearchOptions.ReturnSelfIfOnlyMatch) != 0; // Repeat last-used player name if( name == "-" ) { if( player.LastUsedPlayerName != null ) { name = player.LastUsedPlayerName; } else { return new Player[0]; } } // in case someone tries to use the "!" prefix in an online-only search if( name.Length > 0 && name[0] == '!' ) { name = name.Substring( 1 ); } bool foundSelf = false; List<Player> results = new List<Player>(); Player[] tempList = Players; foreach( Player otherPlayer in tempList ) { if( otherPlayer == null || !includeHidden && !player.CanSee( otherPlayer ) ) { continue; } if( otherPlayer.Name.Equals( name, StringComparison.OrdinalIgnoreCase ) ) { if( !includeSelf && otherPlayer == player ) { foundSelf = true; } else { results.Clear(); results.Add( otherPlayer ); break; } } else if( otherPlayer.Name.StartsWith( name, StringComparison.OrdinalIgnoreCase ) ) { if( !includeSelf && otherPlayer == player ) { foundSelf = true; } else { results.Add( otherPlayer ); } } } // set LastUsedPlayerName if we found one result if( results.Count == 1 ) { player.LastUsedPlayerName = results[0].Name; } // raise the SearchingForPlayer event if( !suppressEvent ) { var h = SearchingForPlayer; if( h != null ) { var e = new SearchingForPlayerEventArgs( player, name, results, options ); h( null, e ); } } // special behavior for ReturnSelfIfOnlyMatch flag if( results.Count == 0 && !includeSelf && foundSelf && returnSelf ) { results.Add( player ); } return results.ToArray(); }
/// <summary> Finds a player by name, using autocompletion. /// Returns ALL matching players, including hidden ones. </summary> /// <param name="namePart"> Full or partial player name. </param> /// <param name="options"> Search options (recognizes SuppressEvent). </param> /// <returns> An array of matches. List length of 0 means "no matches"; /// 1 is an exact match; over 1 for multiple matches. </returns> /// <exception cref="ArgumentNullException"></exception> public static Player[] FindPlayers( [NotNull] string namePart, SearchOptions options) { if( namePart == null ) throw new ArgumentNullException( "namePart" ); bool suppressEvent = (options & SearchOptions.SuppressEvent) != 0; Player[] tempList = Players; List<Player> results = new List<Player>(); for( int i = 0; i < tempList.Length; i++ ) { if( tempList[i] == null ) continue; if( tempList[i].Name.Equals( namePart, StringComparison.OrdinalIgnoreCase ) ) { results.Clear(); results.Add( tempList[i] ); break; } else if( tempList[i].Name.StartsWith( namePart, StringComparison.OrdinalIgnoreCase ) ) { results.Add( tempList[i] ); } } if( !suppressEvent ) { var h = SearchingForPlayer; if( h != null ) { var e = new SearchingForPlayerEventArgs( null, namePart, results, options ); h( null, e ); } } return results.ToArray(); }
/// <summary> Finds a player by name, using autocompletion. Does not include hidden players. </summary> /// <param name="player"> Player who initiated the search. /// Used to determine whether others are hidden or not. </param> /// <param name="name"> Full or partial name of the search target. </param> /// <param name="includeSelf"> Whether player themself should be considered in name autocompletion. /// NOTE: When includeSelf is false, player's own name matches the given name, and no other matches were found, this method returns player as the only result. </param> /// <param name="includeHidden"> Whether hidden players should be considered in the search. </param> /// <param name="raiseEvent"> Whether to raise Server.SearchingForPlayer event. </param> /// <returns> An array of matches. Array length of 0 means "no matches"; 1 is an exact match or single partial match; over 1 for multiple matches. </returns> public static Player[] FindPlayers( [NotNull] Player player, [NotNull] string name, bool includeSelf, bool includeHidden, bool raiseEvent ) { if( player == null ) throw new ArgumentNullException( "player" ); if( name == null ) throw new ArgumentNullException( "name" ); if( name == "-" ) { if( player.LastUsedPlayerName != null ) { name = player.LastUsedPlayerName; } else { return new Player[0]; } } player.LastUsedPlayerName = name; List<Player> results = new List<Player>(); Player[] tempList = Players; foreach( Player otherPlayer in tempList ) { if( otherPlayer == null || !includeHidden && !player.CanSee( otherPlayer ) || !includeSelf && otherPlayer == player ) { continue; } if( otherPlayer.Name.Equals( name, StringComparison.OrdinalIgnoreCase ) ) { results.Clear(); results.Add( otherPlayer ); break; } else if( otherPlayer.Name.StartsWith( name, StringComparison.OrdinalIgnoreCase ) ) { results.Add( otherPlayer ); } } if( results.Count == 0 ) { if( player.Name.StartsWith( name, StringComparison.OrdinalIgnoreCase ) ) { results.Add( player ); } } if( raiseEvent ) { var h = SearchingForPlayer; if( h != null ) { var e = new SearchingForPlayerEventArgs( player, name, results, includeHidden, includeSelf ); h( null, e ); } } if( results.Count == 1 ) { player.LastUsedPlayerName = results[0].Name; } return results.ToArray(); }
/// <summary> Finds a player by name, using autocompletion. Does not include hidden players. </summary> /// <param name="player"> Player who initiated the search. /// Used to determine whether others are hidden or not. </param> /// <param name="name"> Full or partial name of the search target. </param> /// <param name="raiseEvent"> Whether to raise Server.SearchingForPlayer event. </param> /// <returns> An array of matches. List length of 0 means "no matches"; /// 1 is an exact match; over 1 for multiple matches. </returns> public static Player[] FindPlayers( [NotNull] Player player, [NotNull] string name, bool raiseEvent ) { if( player == null ) throw new ArgumentNullException( "player" ); if( name == null ) throw new ArgumentNullException( "name" ); if( name == "-" ) { if( player.LastUsedPlayerName != null ) { name = player.LastUsedPlayerName; } else { return new Player[0]; } } player.LastUsedPlayerName = name; List<Player> results = new List<Player>(); Player[] tempList = Players; for( int i = 0; i < tempList.Length; i++ ) { if( tempList[i] == null || !player.CanSee( tempList[i] ) ) continue; if( tempList[i].Name.Equals( name, StringComparison.OrdinalIgnoreCase ) ) { results.Clear(); results.Add( tempList[i] ); break; } else if( tempList[i].Name.StartsWith( name, StringComparison.OrdinalIgnoreCase ) ) { results.Add( tempList[i] ); } } if( raiseEvent ) { var h = SearchingForPlayer; if( h != null ) { var e = new SearchingForPlayerEventArgs( player, name, results ); h( null, e ); } } if( results.Count == 1 ) { player.LastUsedPlayerName = results[0].Name; } return results.ToArray(); }
/// <summary> Finds a player by name, using autocompletion. /// Returns ALL matching players, including hidden ones. </summary> /// <returns> An array of matches. List length of 0 means "no matches"; /// 1 is an exact match; over 1 for multiple matches. </returns> public static Player[] FindPlayers( [NotNull] string name, bool raiseEvent ) { if( name == null ) throw new ArgumentNullException( "name" ); Player[] tempList = Players; List<Player> results = new List<Player>(); for( int i = 0; i < tempList.Length; i++ ) { if( tempList[i] == null ) continue; if( tempList[i].Name.Equals( name, StringComparison.OrdinalIgnoreCase ) ) { results.Clear(); results.Add( tempList[i] ); break; } else if( tempList[i].Name.StartsWith( name, StringComparison.OrdinalIgnoreCase ) ) { results.Add( tempList[i] ); } } if( raiseEvent ) { var handler = SearchingForPlayer; if( handler != null ) { var e = new SearchingForPlayerEventArgs( null, name, results ); handler( null, e ); } } return results.ToArray(); }