Exemplo n.º 1
0
        public static bool TryGetPlayer(string identifier, out Players.Player targetPlayer, out string error, bool includeOffline = false)
        {
            targetPlayer = null;
            if (identifier.StartsWith("'"))
            {
                if (identifier.EndsWith("'"))
                {
                    identifier = identifier.Substring(1, identifier.Length - 2);
                }
                else
                {
                    error = "missing ' after playername";
                    return(false);
                }
            }
            if (identifier.Length < 1)
            {
                error = "no playername given";
                return(false);
            }

            // try to find by steamID first
            ulong steamid;

            if (ulong.TryParse(identifier, out steamid))
            {
                Steamworks.CSteamID csteamid = new Steamworks.CSteamID(steamid);
                if (csteamid.IsValid())
                {
                    NetworkID networkId = new NetworkID(csteamid);
                    error = "";
                    if (Players.TryGetPlayer(networkId, out targetPlayer))
                    {
                        return(true);
                    }
                    else
                    {
                        targetPlayer = null;
                    }
                }
            }

            // try to find by hash code
            var m = Regex.Match(identifier, @"#(?<hash>\d{8})");
            int givenHash;

            if (m.Success && int.TryParse(m.Groups["hash"].Value, out givenHash))
            {
                foreach (Players.Player player in Players.PlayerDatabase.Values)
                {
                    if (!includeOffline && player.ConnectionState != Players.EConnectionState.Connected)
                    {
                        continue;
                    }
                    if (player.ID.steamID.GetHashCode() == givenHash)
                    {
                        if (targetPlayer == null)
                        {
                            targetPlayer = player;
                        }
                        else
                        {
                            targetPlayer = null;
                            error        = "duplicate hash code, please use full SteamID";
                            return(false);
                        }
                    }
                }

                if (targetPlayer != null)
                {
                    error = "";
                    return(true);
                }
            }

            // try to find by string closest match
            Players.Player closestMatch = null;
            int            closestDist  = int.MaxValue;

            foreach (var player in Players.PlayerDatabase.Values)
            {
                if (!includeOffline && player.ConnectionState != Players.EConnectionState.Connected)
                {
                    continue;
                }
                if (player.Name != null)
                {
                    if (string.Equals(player.Name, identifier, StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (targetPlayer == null)
                        {
                            targetPlayer = player;
                        }
                        else
                        {
                            targetPlayer = null;
                            error        = "duplicate player name, pls use SteamID";
                            return(false);
                        }
                    }
                    else
                    {
                        int levDist = LevenshteinDistance.Compute(player.Name.ToLower(), identifier.ToLower());
                        if (levDist < closestDist)
                        {
                            closestDist  = levDist;
                            closestMatch = player;
                        }
                        else if (levDist == closestDist)
                        {
                            closestMatch = null;
                        }
                    }
                }
            }

            if (targetPlayer != null)
            {
                error = "";
                return(true);
            }
            else if (closestMatch != null && (closestDist < closestMatch.Name.Length * 0.2))
            {
                error        = "";
                targetPlayer = closestMatch;
                Log.Write($"Name '{identifier}' did not match, picked closest match '{targetPlayer.Name}' instead");
                return(true);
            }
            error = "player not found";
            return(false);
        }