/// <summary> Finds players by partial name (prefix). </summary> /// <param name="partialName"> Full or partial name of the player. </param> /// <param name="limit"> Maximum number of results to return. </param> /// <returns> A sequence of zero or more PlayerInfos whose names start with partialName. </returns> public IEnumerable <PlayerInfo> FindByPartialName(string partialName, int limit) { if (partialName == null) { throw new ArgumentNullException("partialName"); } using (PlayerDB.GetReadLock()) { MySqlCommand cmdExact = GetFindExactCommand(partialName); object playerIdOrNull = cmdExact.ExecuteScalar(); if (playerIdOrNull != null) { // An exact match was found, return it int id = (int)playerIdOrNull; return(new[] { FindPlayerInfoByID(id) }); } MySqlCommand cmdPartial = GetFindPartialCommand(partialName + "%", limit); using (MySqlDataReader reader = cmdPartial.ExecuteReader()) { List <PlayerInfo> results = new List <PlayerInfo>(); while (reader.Read()) { // If multiple matches were found, they'll be added to the list int id = reader.GetInt32(0); results.Add(FindPlayerInfoByID(id)); } // If no matches were found, the list will be empty return(results); } } }
/// <summary> Finds players by IP address. </summary> /// <param name="address"> Player's IP address. </param> /// <param name="limit"> Maximum number of results to return. </param> /// <returns> A sequence of zero or more PlayerInfos who have logged in from given IP. </returns> public IEnumerable <PlayerInfo> FindByIP(IPAddress address, int limit) { if (address == null) { throw new ArgumentNullException("address"); } using (PlayerDB.GetReadLock()) { MySqlCommand cmd = GetFindByIPCommand(address, limit); List <PlayerInfo> results = new List <PlayerInfo>(); using (MySqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { int id = reader.GetInt32(0); results.Add(FindPlayerInfoByID(id)); } } return(results); } }
/// <summary> Finds player by exact name. </summary> /// <param name="fullName"> Full, case-insensitive name of the player. </param> /// <returns> PlayerInfo if player was found, or null if not found. </returns> public PlayerInfo FindExact(string fullName) { if (fullName == null) { throw new ArgumentNullException("fullName"); } using (PlayerDB.GetReadLock()) { MySqlCommand cmd = GetFindExactCommand(fullName); object playerIdOrNull = cmd.ExecuteScalar(); if (playerIdOrNull == null) { return(null); } else { int id = (int)playerIdOrNull; return(FindPlayerInfoByID(id)); } } }
/// <summary> Searches for player names starting with namePart, returning just one or none of the matches. </summary> /// <param name="partialName"> Partial or full player name. </param> /// <param name="result"> PlayerInfo to output (will be set to null if no single match was found). </param> /// <returns> true if one or zero matches were found, false if multiple matches were found. </returns> public bool FindOneByPartialName(string partialName, out PlayerInfo result) { if (partialName == null) { throw new ArgumentNullException("partialName"); } using (PlayerDB.GetReadLock()) { MySqlCommand cmdExact = GetFindExactCommand(partialName); object playerIdOrNull = cmdExact.ExecuteScalar(); if (playerIdOrNull != null) { // An exact match was found, return it int id = (int)playerIdOrNull; result = FindPlayerInfoByID(id); return(true); } MySqlCommand cmdPartial = GetFindPartialCommand(partialName + "%", 2); using (MySqlDataReader reader = cmdPartial.ExecuteReader()) { if (!reader.Read()) { // zero matches found result = null; return(true); } int id = reader.GetInt32(0); if (!reader.Read()) { // one partial match found result = FindPlayerInfoByID(id); return(true); } // multiple partial matches found result = null; return(false); } } }
/// <summary> Finds player by name pattern. </summary> /// <param name="pattern"> Pattern to search for. /// Asterisk (*) matches zero or more characters. /// Question mark (?) matches exactly one character. </param> /// <param name="limit"> Maximum number of results to return. </param> /// <returns> A sequence of zero or more PlayerInfos whose names match the pattern. </returns> public IEnumerable <PlayerInfo> FindByPattern(string pattern, int limit) { if (pattern == null) { throw new ArgumentNullException("pattern"); } string processedPattern = pattern.Replace("_", "\\_") // escape underscores .Replace('*', '%') // zero-or-more-characters wildcard .Replace('?', '_'); // single-character wildcard using (PlayerDB.GetReadLock()) { MySqlCommand cmdPartial = GetFindPartialCommand(processedPattern, limit); using (MySqlDataReader reader = cmdPartial.ExecuteReader()) { List <PlayerInfo> results = new List <PlayerInfo>(); while (reader.Read()) { int id = reader.GetInt32(0); results.Add(FindPlayerInfoByID(id)); } return(results); } } }