Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use-strict';
- const {MongoClient} = require('mongodb');
- const {id} = require('battlefy-utils');
- const _ = require('lodash');
- const { parse } = require('json2csv');
- const fs = require('fs');
- async function main() {
- // const db = await MongoClient.connect('mongodb://localhost:27017/anduril-development'); //local
- // const db = await MongoClient.connect('mongodb://10.0.201.33:27017/anduril'); //hawaii
- const db = await MongoClient.connect('mongodb://172.16.32.97:27000/anduril-prod'); //prod
- const {_id: organizationID} = await db.collection('organizations').findOne({
- slug: 'hsesports'
- });
- const hstournaments = await db.collection('tournaments').find({
- organizationID,
- deletedAt: {$exists: false}
- }, {
- _id: true,
- stageIDs: true
- }).toArray();
- const stageIDs = hstournaments.map((x) => x.stageIDs[0]);
- const account = [];
- for (let i = 0; i < 2; i++) {
- // for (let i = 0; i < stageIDs.length; i++) {
- const stageID = stageIDs[i];
- if (stageID) {
- const stage = await db.collection('stages').findOne({
- _id: id(stageID),
- deletedAt: {$exists: false}
- }, {
- teamIDs: true
- });
- if (stage) {
- const team = await db.collection('teams').find({
- _id: {$in: stage.teamIDs},
- deletedAt: {$exists: false}
- }, {
- userID: true
- }).toArray();
- // console.log("TEAM", team);
- const userID = _.map(team, 'userID');
- console.log("USERID", userID);
- if (userID) {
- const accountID = await db.collection('users').find({
- _id: {$in: id(userID)}
- }, {
- accounts: true
- }).toArray();
- // console.log("USERSSS ACCOUNTS", accountID);
- const info = _.map(accountID, 'accounts.battlenet');
- account.push(info);
- }
- }
- }
- }
- // MAKE SURE THE COLUMNS ARE SORTED AS BATTLETAG AND ACCOUNTID
- const names = _.flattenDeep(account);
- console.log("LAST", names);
- ConvertToCSV(names);
- // fs.writeFileSync('lala.csv', parse({names}, {fields: ['accountID', 'battleTag'], delimiter: '\t'}), 'utf-8');
- }
- function ConvertToCSV(objArray) {
- const array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
- let str = '';
- for (let i = 0; i < array.length; i++) {
- let line = '';
- for (const index in array[i]) {
- if (line != '') line += ',';
- line += array[i][index];
- }
- str += `${line }\r\n`;
- }
- // console.log(typeof str);
- fs.writeFileSync('lala.csv', str, 'utf-8');
- }
- const start = Date.now();
- main()
- .then((res) => {
- console.log(`process finised in ${Date.now() - start}`); // eslint-disable-line no-console
- process.exit(0);
- })
- .catch((err) => {
- console.log(err); // eslint-disable-line no-console
- console.log(err.stack); // eslint-disable-line no-console
- process.exit(1);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement