const bot = require('./../bot');
const influx = require('./../../influx');
const request = require('request-promise-native');
let ingame;
let guild;
const updateIngame = async function(){
  if(!guild && !ingame)return;
  /*
  let playing = await request('http://localhost/v0/discord/online');
  playing = JSON.parse(playing);
  */
  let members = await guild.fetchMembers();
  console.log(typeof members)
  let playing = guild.members.filterArray(function (m) {
        if (m.presence.game && m.presence.game.name)
            if (m.presence.game.name.toLowerCase().includes("battlefield 2"))
                return m;
    });
  playing = playing.map(m => m.user.id);
  console.log(playing);
  let toRemove = ingame.members.filter(function (m){
      if(!playing.includes(m.user.id))
      {
        console.log("Not playing : "+ m.user.id)
        return m;
      }
    });
  await Promise.all(toRemove.map(async function(m){
      await m.removeRole(ingame);
   }));
   await Promise.all(playing.map(async function(m){
      const member = await guild.fetchMember(m);
      if(!member.roles.get(ingame.id))
        await member.addRole(ingame);
   }));
  influx.writePoints([
          {
            measurement: 'statistics',
            fields: { count: playing.length},
            tags: {type:'ingame'}
          }
        ]).catch(console.log);
};
bot.on('ready',()=>{
    guild =  bot.guilds.get("184536578654339072");
    ingame = guild.roles.get("322233107489226764");
});
setInterval(updateIngame,5000)
/**
bot.on('presenceUpdate',async function(om,m){
    if(m.guild.id != guild.id) return;
    if (om.presence.game && om.presence.game.name)
            if (om.presence.game.name.toLowerCase().includes("battlefield 2"))
                await m.removeRole(ingame);
    if (m.presence.game && m.presence.game.name)
            if (m.presence.game.name.toLowerCase().includes("battlefield 2"))
                await m.addRole(ingame);
});
*/

const bot = require('./../bot');
const influx = require('./../../influx');
const request = require('request-promise-native');
let ingame;
let guild;
const updateIngame = async function(){
  if(!guild && !ingame)return;
  /*
  let playing = await request('http://localhost/v0/discord/online');
  playing = JSON.parse(playing);
  */
  guild = await guild.fetchMembers();
  let playing = guild.members.filterArray(function (m) {
        if (m.presence.game && m.presence.game.name)
            if (m.presence.game.name.toLowerCase().includes("battlefield 2"))
                return m;
    });
  playing = playing.map(m => m.user.id);
  console.log(playing);
  let toRemove = ingame.members.filter(function (m){
      if(!playing.includes(m.user.id))
      {
        console.log("Not playing : "+ m.user.id)
        return m;
      }
    });
  await Promise.all(toRemove.map(async function(m){
      await m.removeRole(ingame);
   }));
   await Promise.all(playing.map(async function(m){
      const member = await guild.fetchMember(m);
      if(!member.roles.get(ingame.id))
        await member.addRole(ingame);
   }));
  influx.writePoints([
          {
            measurement: 'statistics',
            fields: { count: playing.length},
            tags: {type:'ingame'}
          }
        ]).catch(console.log);
};
bot.on('ready',()=>{
    guild =  bot.guilds.get("184536578654339072");
    ingame = guild.roles.get("322233107489226764");
});
setInterval(updateIngame,5000)
/**
bot.on('presenceUpdate',async function(om,m){
    if(m.guild.id != guild.id) return;
    if (om.presence.game && om.presence.game.name)
            if (om.presence.game.name.toLowerCase().includes("battlefield 2"))
                await m.removeRole(ingame);
    if (m.presence.game && m.presence.game.name)
            if (m.presence.game.name.toLowerCase().includes("battlefield 2"))
                await m.addRole(ingame);
});
*/

update guild obj with full cache