示例#1
0
        public static async Task <SwatServer> GetSwatServerFromDatabaseAsync(this DBService db, string name)
        {
            if (name == null)
            {
                return(null);
            }

            SwatServer server = null;

            await db.ExecuteCommandAsync(async (cmd) => {
                cmd.CommandText = "SELECT * FROM gf.swat_servers WHERE name = @name LIMIT 1;";
                cmd.Parameters.Add(new NpgsqlParameter <string>("name", name));

                using (var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false)) {
                    if (await reader.ReadAsync().ConfigureAwait(false))
                    {
                        server = new SwatServer(
                            (string)reader["name"],
                            (string)reader["ip"],
                            (int)reader["joinport"],
                            (int)reader["queryport"]
                            );
                    }
                }
            });

            return(server);
        }
示例#2
0
        public async Task QueryAsync(CommandContext ctx,
                                     [Description("Registered name or IP.")] string name,
                                     [Description("Query port")] int queryport = 10481)
        {
            if (queryport <= 0 || queryport > 65535)
            {
                throw new InvalidCommandUsageException("Port range invalid (must be in range [1, 65535])!");
            }

            SwatServer server = await this.Database.GetSwatServerFromDatabaseAsync(name.ToLowerInvariant());

            if (server == null)
            {
                throw new CommandFailedException("Server with given name is not registered.");
            }

            SwatServerInfo info = await SwatServerInfo.QueryIPAsync(server.Ip, server.QueryPort);

            if (info != null)
            {
                await ctx.RespondAsync(embed : info.ToDiscordEmbed(this.ModuleColor));
            }
            else
            {
                await this.InformFailureAsync(ctx, "No reply from server.");
            }
        }
示例#3
0
        public static Task AddSwatServerAsync(this DBService db, SwatServer server)
        {
            return(db.ExecuteCommandAsync(cmd => {
                cmd.CommandText = "INSERT INTO gf.swat_servers(ip, joinport, queryport, name) VALUES (@ip, @joinport, @queryport, @name) ON CONFLICT(ip) DO UPDATE SET name = EXCLUDED.name;";
                cmd.Parameters.Add(new NpgsqlParameter <string>("ip", server.Ip));
                cmd.Parameters.Add(new NpgsqlParameter <int>("joinport", server.JoinPort));
                cmd.Parameters.Add(new NpgsqlParameter <int>("queryport", server.QueryPort));
                cmd.Parameters.Add(new NpgsqlParameter <string>("name", server.Name));

                return cmd.ExecuteNonQueryAsync();
            }));
        }
示例#4
0
        public async Task QueryAsync(CommandContext ctx,
                                     [Description("Registered name.")] string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new InvalidCommandUsageException("Name missing.");
            }

            SwatServer server = await this.Database.GetSwatServerFromDatabaseAsync(name.ToLowerInvariant());

            if (server == null)
            {
                throw new CommandFailedException("Server with such name isn't found in the database.");
            }

            await this.InformAsync(ctx, $"IP: {Formatter.Bold($"{server.Ip}:{server.JoinPort}")}");
        }
            public async Task AddAsync(CommandContext ctx,
                                       [Description("Name.")] string name,
                                       [Description("IP.")] CustomIPFormat ip,
                                       [Description("Query port")] int queryport = 10481)
            {
                if (string.IsNullOrWhiteSpace(name))
                {
                    throw new InvalidCommandUsageException("Invalid name.");
                }

                if (queryport <= 0 || queryport > 65535)
                {
                    throw new InvalidCommandUsageException("Port range invalid (must be in range [1, 65535])!");
                }

                await this.Database.AddSwatServerAsync(SwatServer.FromIP(ip.Content, queryport, name));

                await this.InformAsync(ctx, "Server added. You can now query it using the name provided.", important : false);
            }
示例#6
0
        public async Task StartCheckAsync(CommandContext ctx,
                                          [Description("IP.")] CustomIPFormat ip,
                                          [Description("Query port")] int queryport = 10481)
        {
            if (queryport <= 0 || queryport > 65535)
            {
                throw new InvalidCommandUsageException("Port range invalid (must be in range [1, 65535])!");
            }

            if (SwatSpaceCheckService.IsListening(ctx.Channel))
            {
                throw new CommandFailedException("Already checking space in this channel!");
            }

            var server = SwatServer.FromIP(ip.Content, queryport);

            SwatSpaceCheckService.AddListener(server, ctx.Channel);

            await this.InformAsync(ctx, $"Starting space listening on {server.Ip}:{server.JoinPort}... Use command {Formatter.Bold("swat stopcheck")} to stop the check.", important : false);
        }
示例#7
0
        public async Task QueryAsync(CommandContext ctx,
                                     [Description("Server IP.")] CustomIPFormat ip,
                                     [Description("Query port")] int queryport = 10481)
        {
            if (queryport <= 0 || queryport > 65535)
            {
                throw new InvalidCommandUsageException("Port range invalid (must be in range [1, 65535])!");
            }

            var            server = SwatServer.FromIP(ip.Content, queryport);
            SwatServerInfo info   = await SwatServerInfo.QueryIPAsync(server.Ip, server.QueryPort);

            if (info != null)
            {
                await ctx.RespondAsync(embed : info.ToDiscordEmbed(this.ModuleColor));
            }
            else
            {
                await this.InformFailureAsync(ctx, "No reply from server.");
            }
        }
示例#8
0
        public static void AddListener(SwatServer server, DiscordChannel channel)
        {
            if (_listeners.Count > 10 || _listeners.Any(kvp => kvp.Value.Count > 10))
            {
                throw new Exception("Maximum amount of simultanous checks reached. Please try again later.");
            }

            lock (_lock) {
                _listeners.AddOrUpdate(
                    server,
                    new ConcurrentHashSet <DiscordChannel>()
                {
                    channel
                },
                    (k, v) => {
                    v.Add(channel);
                    return(v);
                }
                    );
            }

            Start();
        }
示例#9
0
        public async Task StartCheckAsync(CommandContext ctx,
                                          [Description("Registered name.")] string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new InvalidCommandUsageException("Name/IP missing.");
            }

            if (SwatSpaceCheckService.IsListening(ctx.Channel))
            {
                throw new CommandFailedException("Already checking space in this channel!");
            }

            SwatServer server = await this.Database.GetSwatServerFromDatabaseAsync(name.ToLowerInvariant());

            if (server == null)
            {
                throw new CommandFailedException("Server with given name is not registered.");
            }

            SwatSpaceCheckService.AddListener(server, ctx.Channel);

            await this.InformAsync(ctx, $"Starting space listening on {server.Ip}:{server.JoinPort}... Use command {Formatter.Bold("swat stopcheck")} to stop the check.", important : false);
        }