import fetch from 'flumm-fetch-cookies';
import { SourceQuerySocket } from 'source-server-query';
import config from "../config.mjs";

const ssq = new SourceQuerySocket();

const api = ip => `${config.apis.gameserver.api}${ip}`;

export default async bot => {
  
  return [{
    name: "gs",
    call: /^(\.|\/)gs/i,
    clients: ['tg','irc'],
    f: async e => {
      for(const ip of config.apis.gameserver.ips) {
        const servers = (await (await fetch(api(ip))).json()).response.servers;

        servers.forEach(async s => {
          const [ _server, _port ] = s.addr.split(':');
          const info = await ssq.info(_server, _port, 1000);
          const players = await ssq.players(_server, _port, 1000);

          e.reply(`${info.name} (${info.folder}) - map: ${info.map} - players ${info.players}/${info.max_players}${info.players > 0 ? ": " + players.map(p => p.name).join(", ") : ""}`);
        });
      }
    }
  }];
};