Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. 'use-strict';
  2.  
  3. const {MongoClient} = require('mongodb');
  4. const {id} = require('battlefy-utils');
  5. const _ = require('lodash');
  6. const { parse } = require('json2csv');
  7. const fs = require('fs');
  8.  
  9.  
  10. async function main() {
  11. // const db = await MongoClient.connect('mongodb://localhost:27017/anduril-development'); //local
  12. // const db = await MongoClient.connect('mongodb://10.0.201.33:27017/anduril'); //hawaii
  13. const db = await MongoClient.connect('mongodb://172.16.32.97:27000/anduril-prod'); //prod
  14.  
  15. const {_id: organizationID} = await db.collection('organizations').findOne({
  16. slug: 'hsesports'
  17. });
  18.  
  19. const hstournaments = await db.collection('tournaments').find({
  20. organizationID,
  21. deletedAt: {$exists: false}
  22. }, {
  23. _id: true,
  24. stageIDs: true
  25. }).toArray();
  26.  
  27. const stageIDs = hstournaments.map((x) => x.stageIDs[0]);
  28.  
  29. const account = [];
  30.  
  31. for (let i = 0; i < 2; i++) {
  32. // for (let i = 0; i < stageIDs.length; i++) {
  33. const stageID = stageIDs[i];
  34. if (stageID) {
  35. const stage = await db.collection('stages').findOne({
  36. _id: id(stageID),
  37. deletedAt: {$exists: false}
  38. }, {
  39. teamIDs: true
  40. });
  41. if (stage) {
  42. const team = await db.collection('teams').find({
  43. _id: {$in: stage.teamIDs},
  44. deletedAt: {$exists: false}
  45. }, {
  46. userID: true
  47. }).toArray();
  48. // console.log("TEAM", team);
  49. const userID = _.map(team, 'userID');
  50. console.log("USERID", userID);
  51. if (userID) {
  52. const accountID = await db.collection('users').find({
  53. _id: {$in: id(userID)}
  54. }, {
  55. accounts: true
  56. }).toArray();
  57. // console.log("USERSSS ACCOUNTS", accountID);
  58. const info = _.map(accountID, 'accounts.battlenet');
  59. account.push(info);
  60. }
  61. }
  62. }
  63. }
  64.  
  65. // MAKE SURE THE COLUMNS ARE SORTED AS BATTLETAG AND ACCOUNTID
  66. const names = _.flattenDeep(account);
  67. console.log("LAST", names);
  68. ConvertToCSV(names);
  69. // fs.writeFileSync('lala.csv', parse({names}, {fields: ['accountID', 'battleTag'], delimiter: '\t'}), 'utf-8');
  70. }
  71.  
  72. function ConvertToCSV(objArray) {
  73. const array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
  74. let str = '';
  75.  
  76. for (let i = 0; i < array.length; i++) {
  77. let line = '';
  78. for (const index in array[i]) {
  79. if (line != '') line += ',';
  80.  
  81. line += array[i][index];
  82. }
  83.  
  84. str += `${line }\r\n`;
  85. }
  86. // console.log(typeof str);
  87. fs.writeFileSync('lala.csv', str, 'utf-8');
  88. }
  89.  
  90. const start = Date.now();
  91.  
  92. main()
  93. .then((res) => {
  94. console.log(`process finised in ${Date.now() - start}`); // eslint-disable-line no-console
  95. process.exit(0);
  96. })
  97. .catch((err) => {
  98. console.log(err); // eslint-disable-line no-console
  99. console.log(err.stack); // eslint-disable-line no-console
  100. process.exit(1);
  101. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement