示例#1
0
 /// <summary>
 /// Resets the data.
 /// </summary>
 private void ResetData() {
     this.status         = ServerStatus.Down;
     this.name           = "";
     this.players.Clear();
     this.maxPlayers     = 0;
     this.privateClients = 0;
     this.map            = null;
     this.ping           = 0;
 }
示例#2
0
        /// <summary>
        /// Sends a request to the server to refresh the fields value of this Server instance.
        /// </summary>
        public void Refresh() {
            ResetData();

            String qstatXml = QStatUtil.GetQStatOutput(this.host, this.port);

            var xdoc = XDocument.Parse(qstatXml);
            XElement xserver;
            try {
                xserver = xdoc.Elements().Elements().ElementAt(0);
            } catch {
                // Server host or server port is probably in the wrong format, thus the XML response is not complete

                this.status = ServerStatus.Down;
                return;
            }

            // Get server status
            this.status = xserver.Attribute("status").Value == "UP" ? ServerStatus.Up : ServerStatus.Down;

            if (this.status != ServerStatus.Up) {
                // Server is not up, no need to continue process
                return;
            }

            // Server is up, load the rest of the data
            this.name         = xserver.Element("name").Value;
            this.map          = new Map(xserver.Element("map").Value);
            this.playersCount = int.Parse(xserver.Element("numplayers").Value);
            this.maxPlayers   = int.Parse(xserver.Element("maxplayers").Value);
            this.ping         = int.Parse(xserver.Element("ping").Value);
            
            // Private clients
            foreach (XElement xrule in xserver.Element("rules").Elements()) {
                if (xrule.Attribute("name").Value == "sv_privateClients") {
                    // Found the right rule!
                    this.privateClients = int.Parse(xrule.Value);
                }
            }

            // Players
            foreach (XElement xplayer in xserver.Element("players").Elements()) {
                this.players.Add(new Player(
                    int.Parse(xplayer.Element("score").Value), 
                    int.Parse(xplayer.Element("ping").Value), 
                    xplayer.Element("name").Value));
            }
        }