Exemplo n.º 1
0
        /// <exception cref="AggregateException">
        ///     The task was canceled. The
        ///     <see cref="P:System.AggregateException.InnerExceptions" /> collection contains a
        ///     <see cref="T:System.Threading.Tasks.TaskCanceledException" /> object. -or-An exception was thrown during the
        ///     execution of the task. The <see cref="P:System.AggregateException.InnerExceptions" /> collection contains
        ///     information about the exception or exceptions.
        /// </exception>
        public static async Task <ServerStatus> GetServerInfoAsync()
        {
            var pinger = new ServerPinger("Origind", "d1.natapp.cc", 25333, PingVersion.MC_Current);
            var result = await pinger.GetStatus();

            return(result);
        }
Exemplo n.º 2
0
        string ServerPlayers(string name = null)
        {
            if (name == null)
            {
                return($"请指定服务器的名称. 当前存在的服务器配置有 [{Config.Instance.ServerInfos.GetMixed(Group).Select(i => i.ServerName).Connect()}] 你可以使用 [添加服务器配置 名字 IP].");
            }

            var info = Config.Instance.GetServerInfo(Group, name);

            if (info == null)
            {
                return($"没有找到这样的服务器. 当前存在的服务器配置有 [{Config.Instance.ServerInfos.GetMixed(Group).Select(i => i.ServerName).Connect()}] 你可以使用 [添加服务器配置 名字 IP].");
            }

            try
            {
                var stat = ServerPinger.GetStatus(info.ServerUrl).Result;
                if (stat == null)
                {
                    return("在获取服务器信息时出了差错. 这好像是因为服务器不在线, 要不然就是服务器地址敲错了.");
                }

                return($"这个服务器在线! \r\n" +
                       $"服务器版本: {stat.version.name}\r\n" +
                       $"玩家{stat.players.online}/{stat.players.max}: {stat.players.sample?.Select(p => p.name).Connect()}" +
                       (stat.modinfo == null ? "" : $"\r\n共有 {stat.modinfo.modlist.Count} 个 Models."));
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
                return("在获取服务器信息时出了差错. 这好像是因为服务器不在线, 要不然就是服务器地址敲错了.");
            }
        }
Exemplo n.º 3
0
        private void PingServer(ServerPinger pinger)
        {
            string         error = "";
            ServerPingData ping  = pinger.PingServer(Data.IP, Data.Port, out error);

            Ping = ping;
            //If no error
            if (error == string.Empty)
            {
                //Set the controls with the recieved data
                Stats.Text = ping.Online + "/" + ping.MaxOnline;
                Motd.Text  = ping.Description;

                Stats.TextColor = onlineColor;
            }
            else
            {
                //Error text
                Stats.Text      = "X";
                Stats.TextColor = offlineColor;

                Motd.Text = error;
            }
            Stats.Left = (ClientWidth - (int)Manager.Skin.Fonts["Default14"].Resource.MeasureString(Stats.Text).X) - 4 - 32;
        }
Exemplo n.º 4
0
 string AddAllGroupConfig(string name, string url)
 {
     if (ServerPinger.GetStatus(url) == null)
     {
         return("无法在这个时候访问这个服务器. 请检查你的参数是否正确, 如果你非要添加这个服务器不可, 请使用命令 [强行添加服务器配置].");
     }
     return(AddAllGroupConfigForce(name, url));
 }
        private static void Init(ServerInfo serverInfo)
        {
            var stat = ServerPinger.GetStatus(serverInfo.ServerUrl).Result;

            serverInfo.LastTimeOnline = stat != null;
            serverInfo.LastPlayers    = stat?.players?.sample == null ? new HashSet <string>() : new HashSet <string>(stat.players.sample.Select(info => info.name));
            serverInfo.Inited         = true;
        }
Exemplo n.º 6
0
        public ServerDataControl(Manager manager, ServerPinger pinger, ServerSaveData server)
            : base(manager)
        {
            //Setup
            Passive     = false;
            Height      = 76;
            ClientWidth = 450 - 16;
            Data        = server;

            //Background "gradient" image
            //TODO: Make an actual control. not a statusbar
            Gradient = new StatusBar(manager);
            Gradient.Init();
            Gradient.Alpha = .8f;
            Add(Gradient);

            //Add controls
            Name = new Label(Manager)
            {
                Width = this.Width, Text = server.Name, Left = 4, Top = 4, Font = FontSize.Default14, Alignment = Alignment.TopLeft
            };
            Name.Init();
            Add(Name);

            Stats = new Label(Manager)
            {
                Width = this.Width, Text = string.Empty, Alignment = Alignment.TopLeft, Top = 4, Font = FontSize.Default14,
            };
            Stats.Init();
            Add(Stats);

            Motd = new Label(Manager)
            {
                Width = this.Width, Left = 4, Top = Name.Bottom + 8, Font = FontSize.Default8, Alignment = Alignment.TopLeft
            };
            Motd.Init();
            Motd.Text   = "Querying server for data...";
            Motd.Height = (int)Manager.Skin.Fonts["Default8"].Height * 2;
            Add(Motd);

            Host = new Label(Manager)
            {
                Width = this.Width, Text = server.GetHostString(), Alignment = Alignment.TopLeft, Left = 4, Top = Motd.Bottom, TextColor = Color.LightGray
            };
            Host.Init();
            Add(Host);

            //Ping the server for stats on a seperate thread
            new Thread(() => PingServer(pinger)).Start();
        }
        public static void Tick()
        {
            var serverInfos = Config.Instance.ServerInfos.SelectMany(set => set.Value, (pair, info) => new { group = pair.Key, info })
                              .Where(info => (info.info.MonitorPlayer || info.info.Monitor) && info.group != "AllGroup").ToArray();

            foreach (var serverInfo in serverInfos.Where(info => !info.info.Inited).Select(info => info.info))
            {
                Init(serverInfo);
            }

            foreach (var serverInfo in serverInfos)
            {
                var info        = serverInfo.info;
                var lastPlayers = info.LastPlayers;
                var group       = serverInfo.group;
                var stat        = ServerPinger.GetStatus(info.ServerUrl).Result;
                var serverUp    = stat != null;
                if (serverUp != info.LastTimeOnline && info.Monitor)
                {
                    group.SendGroup(serverUp ? $"{info.ServerName} 服务器上线啦!" : $"{info.ServerName} 服务器: {info.BoomMessage}");
                    info.LastTimeOnline = serverUp;
                }

                if (stat?.players?.sample != null && info.MonitorPlayer)
                {
                    var currentPlayers = new HashSet <string>(stat.players.sample.Select(p => p.name));
                    if (!currentPlayers.SetEquals(lastPlayers))
                    {
                        var newPlayers  = currentPlayers.Except(lastPlayers).ToArray();
                        var guedPlayers = lastPlayers.Except(currentPlayers).ToArray();
                        if (newPlayers.Any())
                        {
                            group.SendGroup($"{info.ServerName}: {newPlayers.Connect()} 进入了服务器.");
                        }

                        if (guedPlayers.Any())
                        {
                            group.SendGroup($"{info.ServerName}: {guedPlayers.Connect()} 摸了.");
                        }
                    }

                    info.LastPlayers = currentPlayers;
                }
            }
        }
Exemplo n.º 8
0
        public ServerWindow(Manager manager) : base(manager)
        {
            //Setup server pinger/query
            Pinger = new ServerPinger();
            //Setup the window
            CaptionVisible   = false;
            Caption.Text     = "Welcome to Bricklayer!";
            Description.Text = "An open source, fully moddable and customizable 2D\nbuilding game built with the community in mind.";
            Movable          = false;
            Resizable        = false;
            Width            = 450;
            Height           = 350;
            Shadow           = true;
            Center();

            //Player config
            NameLbl = new Label(Manager)
            {
                Left = 8, Top = TopPanel.Bottom + 10, Width = 64, Text = "Username:"******"Color:"
            };
            ColorLbl.Init();
            Add(ColorLbl);

            BodyClr = new ColorPicker(Manager)
            {
                Left = ColorLbl.Right + 4, Top = TopPanel.Bottom + 8, Width = 128, Saturation = GlobalSettings.ColorSaturation, Value = GlobalSettings.ColorValue
            };
            BodyClr.Init();
            BodyClr.ValueChanged += new TomShane.Neoforce.Controls.EventHandler(delegate(object o, TomShane.Neoforce.Controls.EventArgs e)
            {
                BodyImg.Color = BodyClr.SelectedColor;
                Game.MyColor  = BodyClr.SelectedColor;
                Game.MyHue    = BodyClr.Hue;
            });
            Add(BodyClr);

            BodyImg = new ImageBox(Manager)
            {
                Left = BodyClr.Right + 6, Top = TopPanel.Bottom + 8, Width = 18, Height = 18, Color = Game.MyColor, Image = ContentPack.Textures["entity\\body"]
            };
            BodyImg.Init();
            Add(BodyImg);

            SmileyImg = new ImageBox(Manager)
            {
                Left = BodyClr.Right + 6, Top = TopPanel.Bottom + 8, Width = 18, Height = 18, Image = ContentPack.Textures["entity\\smileys"], SourceRect = new Rectangle(0, 0, 18, 18)
            };
            SmileyImg.Init();
            SmileyImg.ToolTip.Text = "I love this color!";
            Add(SmileyImg);

            BodyClr.Hue = Game.MyHue;
            //Create main server list
            ServerListCtrl = new ControlList <ServerDataControl>(manager)
            {
                Left = 8, Top = TopPanel.Bottom + 34, Width = ClientWidth - 16, Height = ClientHeight - TopPanel.Height - BottomPanel.Height - 42
            };
            ServerListCtrl.Init();
            Add(ServerListCtrl);
            RefreshServerList();

            //Add BottomPanel controls
            JoinBtn = new Button(manager)
            {
                Text = "Connect", Left = 24, Top = 8, Width = 100,
            };
            JoinBtn.Init();
            JoinBtn.Click += new TomShane.Neoforce.Controls.EventHandler(delegate(object o, TomShane.Neoforce.Controls.EventArgs e)
            {
                //Logical place to save name/color settings
                IO.SaveSettings(new Settings()
                {
                    Username = Game.Username, Color = Game.MyHue, ContentPack = Game.ContentPackName, Resolution = new Point(Game.Resolution.Width, Game.Resolution.Height), UseVSync = Game.MainWindow.Manager.Graphics.SynchronizeWithVerticalRetrace
                });
                //Connect
                if (ServerListCtrl.Items.Count > 0)
                {
                    if ((ServerListCtrl.Items[ServerListCtrl.ItemIndex] as ServerDataControl).Ping != null && (ServerListCtrl.Items[ServerListCtrl.ItemIndex] as ServerDataControl).Ping.Error)
                    {
                        return;
                    }
                    //Create a world and connect
                    JoinBtn.Enabled = false;
                    JoinBtn.Text    = "Connecting...";
                    Game.NetManager.Connect(Servers[ServerListCtrl.ItemIndex].IP, Servers[ServerListCtrl.ItemIndex].Port);
                }
            });
            BottomPanel.Add(JoinBtn);

            AddBtn = new Button(manager)
            {
                Text = "Add", Left = JoinBtn.Right + 8, Top = 8, Width = 64,
            };
            AddBtn.Init();
            AddBtn.Click += new TomShane.Neoforce.Controls.EventHandler(delegate(object o, TomShane.Neoforce.Controls.EventArgs e)
            {
                AddServerDialog window = new AddServerDialog(manager, this, ServerListCtrl.ItemIndex, false, string.Empty, string.Empty);
                window.Init();
                Manager.Add(window);
                window.Show();
            });
            BottomPanel.Add(AddBtn);

            EditBtn = new Button(manager)
            {
                Text = "Edit", Left = AddBtn.Right + 8, Top = 8, Width = 64,
            };
            EditBtn.Init();
            EditBtn.Click += new TomShane.Neoforce.Controls.EventHandler(delegate(object o, TomShane.Neoforce.Controls.EventArgs e)
            {
                if (ServerListCtrl.Items.Count > 0)
                {
                    AddServerDialog window = new AddServerDialog(manager, this, ServerListCtrl.ItemIndex, true, Servers[ServerListCtrl.ItemIndex].Name, Servers[ServerListCtrl.ItemIndex].GetHostString());
                    window.Init();
                    Manager.Add(window);
                    window.Show();
                }
            });
            BottomPanel.Add(EditBtn);

            RemoveBtn = new Button(manager)
            {
                Text = "Remove", Left = EditBtn.Right + 8, Top = 8, Width = 64
            };
            RemoveBtn.Init();
            RemoveBtn.Click += new TomShane.Neoforce.Controls.EventHandler(delegate(object o, TomShane.Neoforce.Controls.EventArgs e)
            {
                if (ServerListCtrl.Items.Count > 0)
                {
                    MessageBox confirm = new MessageBox(manager, MessageBoxType.YesNo, "Are you sure you would like to remove\nthis server from your server list?", "[color:Red]Confirm Removal[/color]");
                    confirm.Init();
                    confirm.Closed += new WindowClosedEventHandler(delegate(object sender, WindowClosedEventArgs args)
                    {
                        if ((sender as Dialog).ModalResult == ModalResult.Yes) //If user clicked yes
                        {
                            Servers.RemoveAt(ServerListCtrl.ItemIndex);
                            ServerListCtrl.Items.RemoveAt(ServerListCtrl.ItemIndex);
                            IO.WriteServers(Servers);
                            RefreshServerList();
                        }
                    });
                    Manager.Add(confirm);
                    confirm.Show();
                }
            });
            BottomPanel.Add(RemoveBtn);

            RefreshBtn = new Button(manager)
            {
                Text = "Refresh", Left = RemoveBtn.Right + 8, Top = 8, Width = 64
            };
            RefreshBtn.Init();
            RefreshBtn.Click += new TomShane.Neoforce.Controls.EventHandler(delegate(object o, TomShane.Neoforce.Controls.EventArgs e)
            {
                RefreshServerList();
            });
            BottomPanel.Add(RefreshBtn);
            MainWindow.ScreenManager.FadeIn();
            validateUsername();
        }