text: const fs = require("fs");
  const path = require('path');
  const config = require('../config/config.json')
  const leaderboards = require('./leaderboards')
  const util = require('./miscUtil')
  const db = require('../db/db')
  module.exports = {
      genPackList
  }
  
  async function main() {
      // await new Promise(resolve => setTimeout(resolve, 5000))
      let serverArr = await genPackList()
      console.log('PUSH PUSH PUSH')
      console.log(`FUCK: %o`, serverArr)
      let networkStats = await leaderboards.networkLeaderboards(serverArr)
      let networkServer = {
          name: 'Network',
          players: networkStats[0],
          numPlayers: networkStats[1]
      }
      serverArr.push(networkServer)
      console.log(networkServer)
      let usersArr = await userBuilder(networkServer)
      pushToUserDB(usersArr)
      pushToServerDB(serverArr)
  }
  main()
  
  
  function genPackList() {
      // Checks directories to see if server.properties is there, if so adds to array of valid servers
      // var currentPacks = []
      var serverPlayersArr = []
      let rootPath = path.resolve(config.stats.path)
      console.log(`Root path: ${rootPath}`)
      var dirs = fs.readdirSync(rootPath, {
              withFileTypes: true
          })
          .filter(dirent => dirent.isDirectory())
      // console.log(dirs)
      dirs.forEach(dir => {
          dir = dir.name
          console.log(`Looking in ${dir}`)
          let newPath = path.join(rootPath, dir)
          console.log(newPath)
          let subDirs = fs.readdirSync(newPath)
          console.log(subDirs)
          subDirs.forEach(dir => {
              console.log(dir)
              console.log(newPath)
              let path2 = path.join(newPath, dir)
              let search = fs.readdirSync(path2)
              console.log(path2)
  
              if (search.includes('server.properties')) {
                  console.log(`Server Properties found for ${dir}!`)
                  let playerArr = playersList(path2)
                  let i = {
                      name: dir,
                      players: playerArr[0],
                      numPlayers: playerArr[1]
                  }
                  serverPlayersArr.push(i)
                  // let pack = {
                  //     name: dir,
                  //     active: true,
                  //     path: path2,
                  //     numPlayers: playerArr[1],
                  //     players: leaderboards.packLeaderboards(playerArr[0])
                  // }
  
                  // currentPacks.push(pack)
              }
          })
      })
  
      return serverPlayersArr
  }
  
  function playersList(filepath) {
      var players = []
      let users = require(path.join(filepath, 'usercache.json'))
      users.forEach(user => {
          let stats = playersStats(filepath, user.uuid)
          console.log(stats)
          if (stats != 'No Time Recorded') {
              let player = {
                  userName: user.name,
                  uuid: user.uuid,
                  timePlayedSeconds: stats.timePlayed.secondsPlayed,
                  stats: stats.timePlayed.fancyTimePlayed
              }
              players.push(player)
          }
      })
      return [players, players.length]
  }
  
  function playersStats(filepath, uuid) {
      let statsPath = path.join(filepath, '/world/stats/', `${uuid}.json`)
      console.log(statsPath)
      var stats = {
          timePlayed: {
              rawTimePlayed: null,
              fancyTimePlayed: null
          }
      }
      try {
          let playerStats = JSON.parse(fs.readFileSync(statsPath, 'utf8'))
          if (playerStats['stat.playOneMinute']) {
              let playerTime = util.convertTime(playerStats['stat.playOneMinute'])
              stats.timePlayed.rawTimePlayed = playerTime[0]
              stats.timePlayed.fancyTimePlayed = playerTime[1]
              stats.timePlayed.secondsPlayed = playerTime[2]
              return stats
          } else {
              let playerTime = util.convertTime(playerStats.stats['minecraft:custom']['minecraft:play_one_minute'])
              stats.timePlayed.rawTimePlayed = playerTime[0]
              stats.timePlayed.fancyTimePlayed = playerTime[1]
              stats.timePlayed.secondsPlayed = playerTime[2]
              return stats
          }
      } catch (error) {
          stats = 'No Time Recorded'
          return stats
      }
  }
  
  async function pushToDB(serverArr) {
      for (const server of serverArr) {
          let done = await db.serverHandler(server)
      }
  }
  
  async function pushToServerDB(input) {
      console.log(input)
      for (const server of input) {
          for (const player of server.players) {
              let done = await db.serverTableManager(server.name, player)
          }
      }
  
  }
  
  async function pushToUserDB(users) {
      for (const user of users) {
          let done = await db.userHandler(user)
      }
  }
  
  async function userBuilder(netServer) {
      let users = []
  
      for (const user of netServer.players) {
  
          let serversPlayed = await db.findAllServersForUser(user.uuid)
          let serverStats = await db.pullNetStats(user.uuid)
  
          let newUser = {
              uuid: user.uuid,
              userName: user.userName,
              discordID: null,
              servers: serversPlayed[0],
              stats: serverStats.stats,
              badges: serversPlayed[1]
          }
          users.push(newUser)
      }
      return users
  }
  
  async function badgeGen(input) {
  
  }