예제 #1
0
        /// <summary>
        /// Purges the client.
        /// </summary>
        private void PurgeClient()
        {
            if (CurrentServerInfo != null)
            {
                CurrentServerInfo = null;
            }

            Client?.Dispose();
            HostnameUpdated?.Invoke(this, new HostnameEventArgs()
            {
                NewHostname = "", Timestamp = DateTime.Now
            });
            CurrentPlayerCountUpdated?.Invoke(this, new PlayerCountEventArgs()
            {
                PlayerCount = 0
            });
        }
예제 #2
0
        /// <summary>
        /// Called when [get players].
        /// </summary>
        /// <param name="packet">The packet.</param>
        private void OnGetPlayers(Packet packet)
        {
            try
            {
                List <Player> players = new List <Player>();
                string        message = packet.DataAsString();
                if (message.Trim() == "No Players Connected")
                {
                    CurrentPlayerCountUpdated?.Invoke(this, new PlayerCountEventArgs()
                    {
                        PlayerCount = players.Count
                    });
                    PlayersUpdated?.Invoke(this, new PlayersEventArgs()
                    {
                        Players = players
                    });
                    ConsoleLogUpdated?.Invoke(this, new ConsoleLogEventArgs()
                    {
                        Message   = "Server Response: No Players Connected",
                        Timestamp = packet.Timestamp
                    });
                    return;
                }
                string   str        = packet.DataAsString();
                string[] lines      = str.Split('\n');
                string[] cleanLines = lines.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
                foreach (string line in cleanLines)
                {
                    string   lineData     = line.Replace("...", "");
                    string[] split1       = lineData.Split(new char[] { '.' }, 2);
                    string[] split2       = split1[1].Split(new char[] { ',' }, 2);
                    string   playerNumber = split1[0].Trim();
                    string   name         = split2[0].Trim();
                    string   steamId      = split2[1].Trim();

                    players.Add(new Player
                    {
                        PlayerNumber = int.Parse(playerNumber),
                        Name         = name,
                        SteamID      = ulong.Parse(steamId)
                    });
                }

                CurrentPlayerCountUpdated?.Invoke(this, new PlayerCountEventArgs()
                {
                    PlayerCount = players.Count
                });
                PlayersUpdated?.Invoke(this, new PlayersEventArgs()
                {
                    Players = players
                });
                ConsoleLogUpdated?.Invoke(this, new ConsoleLogEventArgs()
                {
                    Message   = "Server Response: Player List Updated",
                    Timestamp = packet.Timestamp
                });
            }
            catch (Exception ex)
            {
                _logger.LogInformation("Exception in OnGetPlayers: {exception}", ex.Message);
            }
        }