Esempio 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);
        }
Esempio 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("在获取服务器信息时出了差错. 这好像是因为服务器不在线, 要不然就是服务器地址敲错了.");
            }
        }
Esempio n. 3
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;
        }
        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;
                }
            }
        }