diff --git a/migrations/20140831_increase_gems_for_previous_contributions.js b/migrations/20140831_increase_gems_for_previous_contributions.js index b8e30691ae..081da935c4 100644 --- a/migrations/20140831_increase_gems_for_previous_contributions.js +++ b/migrations/20140831_increase_gems_for_previous_contributions.js @@ -1,15 +1,16 @@ // IMPORTANT: // // run like this to capture all output: +// // node 20140831_increase_gems_for_previous_contributions.js > 20140831_increase_gems_for_previous_contributions_output.txt var migrationName = '20140831_increase_gems_for_previous_contributions'; -// author: Alys (d904bd62-da08-416b-a816-ba797c9ee265) /** * https://github.com/HabitRPG/habitrpg/issues/3933 * Increase Number of Gems for Contributors + * author: Alys (d904bd62-da08-416b-a816-ba797c9ee265) * * Increase everyone's gems per their contribution level. * Originally they were given 2 gems per tier. @@ -30,50 +31,102 @@ var mongo = require('mongoskin'); var _ = require('lodash'); -/////////////////// UNCOMMENT *ONE* OF THESE LINES: /////////////////// -// var dbUsers = mongo.db('lefnire:mAdn3s5s@charlotte.mongohq.com:10015/habitrpg_large?auto_reconnect').collection('users'); // @lefnire production? -// var dbUsers = mongo.db('localhost:27017/habitrpg_old?auto_reconnect').collection('users'); // @lefnire habitrpg_old -// var dbUsers = mongo.db('localhost:27017/habitrpg?auto_reconnect').collection('users'); // for local testing by script author (e.g., vagrant install) +/////////////////// UNCOMMENT ONE OF THESE LINES: /////////////////// +// var dbUsers = mongo.db('lefnire:mAdn3s5s@charlotte.mongohq.com:10015/habitrpg_large?auto_reconnect').collection('users'); // @lefnire habitrpg_large +// var dbUsers = mongo.db('localhost:27017/habitrpg_old?auto_reconnect').collection('users'); // @lefnire habitrpg_old +// var dbUsers = mongo.db('localhost:27017/habitrpg?auto_reconnect').collection('users'); // for local testing by script author (e.g., vagrant install) +if (typeof dbUsers == 'undefined') { exiting(1, 'Uncomment one of the "var dbUsers" lines!'); } +var query = { $and: [ + { 'migration': {$ne: migrationName} }, + { 'contributor.level': {$gt: 0} }, + { 'contributor.level': {$lt: 9} } +]}; + var fields = {'migration':1, 'contributor.level':1, 'auth.local.username':1, 'balance':1, - }; +}; -dbUsers.findEach({ $and: [ - { 'contributor.level': {$gt:0} }, - { 'contributor.level': {$lt:9} } -]}, fields, {batchSize:250}, function(err, user){ - if (!user) err = '!user'; - if (err) {return console.error(err);} +var userResults = {}; // each key is a UUID, each value is a string + // describing what changed for that user + +console.warn('Updating users...'); +var progressCount = 50; +var count = 0; +dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) { + if (err) { return exiting(1, 'ERROR! ' + err); } + if (!user) { + console.warn('All users found. Fetching final balances...'); + return fetchFinalBalances(); + } + count++; var set = {'migration': migrationName}; var tier = user.contributor.level; var extraGems = tier; // tiers 1,2,3 - if (tier > 3) { - extraGems = 3 + (tier - 3) * 2; - } - if (tier == 8) { - extraGems = 11; // mods (tier 8) are tier 7 in terms of gems awarded - } + if (tier > 3) { extraGems = 3 + (tier - 3) * 2; } + if (tier == 8) { extraGems = 11; } extraBalance = extraGems / 4; set['balance'] = user.balance + extraBalance; - // Display current state of user: - console.log("\n" + user._id + " " + user.auth.local.username + ":\n" + - " contrib tier : " + tier + "\n" + - " balance before : " + user.balance + "\n" + - " balance (gems) added : " + extraBalance + " (" + - extraGems + ")" + "\n" + - " expected balance after: " + (user.balance + extraBalance)); - - //console.log(JSON.stringify(user, null, " ")); - //console.log("set: " + JSON.stringify(set, null, " ")); + // Capture current state of user: + userResults[user._id] = + user._id + ' ' + user.auth.local.username + ':\n' + + ' contrib tier : ' + tier + '\n' + + ' balance before : ' + user.balance + '\n' + + ' balance (gems) added : ' + extraBalance + ' (' + + extraGems + ')' + '\n' + + ' expected balance after: ' + (user.balance + extraBalance) + '\n'; + // Update user: dbUsers.update({_id:user._id}, {$set:set, $inc:{_v:1}}); - - // No progress counter because we print output for every user. + if (count%progressCount == 0) console.warn(count + ' ' + user._id); }); + + +function fetchFinalBalances() { + var query = {_id: {$in: Object.keys(userResults)}}; + var fields = { + 'auth.local.username':1, + 'balance':1, + }; + + var count1 = 0; + dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) { + if (err) { return exiting(1, 'ERROR! ' + err); } + if (!user) { + console.warn('All final balances found.'); + return displayData(); + } + count1++; + userResults[user._id] = userResults[user._id] + + user._id + ' ' + user.auth.local.username + ':\n' + + ' actual balance after : ' + user.balance + '\n'; + if (count1%progressCount == 0) console.warn(count1 + ' ' + user._id); + }); +} + + +function displayData() { + _.each(userResults, function(text, uuid) { + console.log(text); // text contains uuid + }); + console.log('\n' + count + + ' users processed (should be roughly 335 according to the Hall)\n'); + return exiting(0); +} + + +function exiting(code, msg) { + code = code || 0; // 0 = success + if (code && !msg) { msg = 'ERROR!'; } + if (msg) { + if (code) { console.error(msg); } + else { console.log( msg); } + } + process.exit(code); +} diff --git a/migrations/20140831_increase_gems_for_previous_contributions_FINAL_CHECK.js b/migrations/20140831_increase_gems_for_previous_contributions_FINAL_CHECK.js deleted file mode 100644 index d11e1b29fc..0000000000 --- a/migrations/20140831_increase_gems_for_previous_contributions_FINAL_CHECK.js +++ /dev/null @@ -1,43 +0,0 @@ -// IMPORTANT: -// -// run like this to capture all output: -// node 20140831_increase_gems_for_previous_contributions_FINAL_CHECK.js > 20140831_increase_gems_for_previous_contributions_FINAL_CHECK_output.txt - - - -// 20140831_increase_gems_for_previous_contributions_FINAL_CHECK.js -// author: Alys (d904bd62-da08-416b-a816-ba797c9ee265) - -/** - * Run this script immediately after - * 20140831_increase_gems_for_previous_contributions.js - * to capture the actual gem balance in case we need it later for - * handling complaints that the right number of gems weren't added. - */ - -var mongo = require('mongoskin'); -var _ = require('lodash'); - -/////////////////// UNCOMMENT *ONE* OF THESE LINES: /////////////////// -// var dbUsers = mongo.db('lefnire:mAdn3s5s@charlotte.mongohq.com:10015/habitrpg_large?auto_reconnect').collection('users'); // @lefnire production? -// var dbUsers = mongo.db('localhost:27017/habitrpg_old?auto_reconnect').collection('users'); // @lefnire habitrpg_old -var dbUsers = mongo.db('localhost:27017/habitrpg?auto_reconnect').collection('users'); // for local testing by script author (e.g., vagrant install) - - -var fields = { - 'auth.local.username':1, - 'balance':1, - }; - -dbUsers.findEach({ $and: [ - { 'contributor.level': {$gt:0} }, - { 'contributor.level': {$lt:9} } -]}, fields, {batchSize:250}, function(err, user){ - if (!user) err = '!user'; - if (err) {return console.error(err);} - - console.log("\n" + user._id + " " + user.auth.local.username + ":\n" + - " actual balance after : " + user.balance); - - // No progress counter because we print output for every user. -});