Skip to content

Commit e021212

Browse files
authored
Naming Day and Take This migrations (#7838)
* feat(migrations): Naming Day and Take This * fix(migration): more goodies, better query * fix(migration): query in wrong migration * fix(migration): don't award goodies repeatedly * fix(migrations): move logic to query * fix(migrations): better field lookup * fix(migrations): use timeouts
1 parent 6804125 commit e021212

5 files changed

Lines changed: 160 additions & 15 deletions

File tree

migrations/20160731_naming_day.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
var migrationName = '20160731_naming_day.js';
2+
var authorName = 'Sabe'; // in case script author needs to know when their ...
3+
var authorUuid = '7f14ed62-5408-4e1b-be83-ada62d504931'; //... own data is done
4+
5+
/*
6+
* Award Royal Purple Gryphon pet to Royal Purple Gryphon mount owners, mount to everyone else
7+
*/
8+
9+
var mongo = require('mongoskin');
10+
11+
var connectionString = 'mongodb://localhost:27017/habitrpg?auto_reconnect=true'; // FOR TEST DATABASE
12+
13+
var dbUsers = mongo.db(connectionString).collection('users');
14+
15+
// specify a query to limit the affected users (empty for all users):
16+
var query = {
17+
'migration':{$ne:migrationName},
18+
'auth.timestamps.loggedin':{$gt:new Date('2016-07-30')} // Extend timeframe each run of migration
19+
};
20+
21+
// specify fields we are interested in to limit retrieved data (empty if we're not reading data):
22+
var fields = {
23+
'items.mounts': 1
24+
};
25+
26+
console.warn('Updating users...');
27+
var progressCount = 1000;
28+
var count = 0;
29+
dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) {
30+
if (err) { return exiting(1, 'ERROR! ' + err); }
31+
if (!user) {
32+
console.warn('All appropriate users found and modified.');
33+
setTimeout(displayData, 300000);
34+
return;
35+
}
36+
count++;
37+
38+
// specify user data to change:
39+
var set = {};
40+
var inc = {};
41+
inc = {
42+
'achievements.habiticaDays': 1,
43+
'items.food.Cake_Skeleton': 1,
44+
'items.food.Cake_Base': 1,
45+
'items.food.Cake_CottonCandyBlue': 1,
46+
'items.food.Cake_CottonCandyPink': 1,
47+
'items.food.Cake_Shade': 1,
48+
'items.food.Cake_White': 1,
49+
'items.food.Cake_Golden': 1,
50+
'items.food.Cake_Zombie': 1,
51+
'items.food.Cake_Desert': 1,
52+
'items.food.Cake_Red': 1
53+
};
54+
if (user.items.mounts['Gryphon-RoyalPurple']) {
55+
set = {'migration':migrationName, 'items.pets.Gryphon-RoyalPurple':5};
56+
} else {
57+
set = {'migration':migrationName, 'items.mounts.Gryphon-RoyalPurple':true};
58+
}
59+
60+
dbUsers.update({_id:user._id}, {$set:set, $inc:inc});
61+
62+
if (count%progressCount == 0) console.warn(count + ' ' + user._id);
63+
if (user._id == authorUuid) console.warn(authorName + ' processed');
64+
});
65+
66+
67+
function displayData() {
68+
console.warn('\n' + count + ' users processed\n');
69+
return exiting(0);
70+
}
71+
72+
73+
function exiting(code, msg) {
74+
code = code || 0; // 0 = success
75+
if (code && !msg) { msg = 'ERROR!'; }
76+
if (msg) {
77+
if (code) { console.error(msg); }
78+
else { console.log( msg); }
79+
}
80+
process.exit(code);
81+
}
82+

migrations/20160731_takeThis.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var migrationName = '20160731_takeThis.js';
2+
var authorName = 'Sabe'; // in case script author needs to know when their ...
3+
var authorUuid = '7f14ed62-5408-4e1b-be83-ada62d504931'; //... own data is done
4+
5+
/*
6+
* Award Take This Sword to Take This challenge participants who already own the Shield
7+
* and Take This Shield to the rest of the list
8+
*/
9+
10+
var mongo = require('mongoskin');
11+
12+
var connectionString = 'mongodb://localhost:27017/habitrpg?auto_reconnect=true'; // FOR TEST DATABASE
13+
14+
var dbUsers = mongo.db(connectionString).collection('users');
15+
16+
// specify a query to limit the affected users (empty for all users):
17+
var query = {
18+
'migration':{$ne:migrationName},
19+
'auth.timestamps.loggedin':{$gt:new Date('2016-07-30')}, // Extend timeframe each run of migration
20+
'challenges':{$in:['da8859b2-5c6e-4aa5-b8b2-8db93d5de9fc']}
21+
};
22+
23+
// specify fields we are interested in to limit retrieved data (empty if we're not reading data):
24+
var fields = {
25+
'items.gear.owned': 1
26+
};
27+
28+
console.warn('Updating users...');
29+
var progressCount = 1000;
30+
var count = 0;
31+
dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) {
32+
if (err) { return exiting(1, 'ERROR! ' + err); }
33+
if (!user) {
34+
console.warn('All appropriate users found and modified.');
35+
setTimeout(displayData, 300000);
36+
return;
37+
}
38+
count++;
39+
40+
// specify user data to change:
41+
var set = {};
42+
43+
if (typeof user.items.gear.owned.shield_special_takeThis !== 'undefined') {
44+
set = {'migration':migrationName, 'items.gear.owned.weapon_special_takeThis':false};
45+
} else {
46+
set = {'migration':migrationName, 'items.gear.owned.shield_special_takeThis':false};
47+
}
48+
49+
dbUsers.update({_id:user._id}, {$set:set});
50+
51+
if (count%progressCount == 0) console.warn(count + ' ' + user._id);
52+
if (user._id == authorUuid) console.warn(authorName + ' processed');
53+
});
54+
55+
56+
function displayData() {
57+
console.warn('\n' + count + ' users processed\n');
58+
return exiting(0);
59+
}
60+
61+
62+
function exiting(code, msg) {
63+
code = code || 0; // 0 = success
64+
if (code && !msg) { msg = 'ERROR!'; }
65+
if (msg) {
66+
if (code) { console.error(msg); }
67+
else { console.log( msg); }
68+
}
69+
process.exit(code);
70+
}
71+

migrations/new_stuff.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@ var authorUuid = '7f14ed62-5408-4e1b-be83-ada62d504931'; //... own data is done
66
* set the newStuff flag in all user accounts so they see a Bailey message
77
*/
88

9-
var dbserver = 'localhost:27017'; // FOR TEST DATABASE
10-
// var dbserver = 'username:password@ds031379-a0.mongolab.com:31379'; // FOR PRODUCTION DATABASE
11-
var dbname = 'habitrpg';
12-
139
var mongo = require('mongoskin');
14-
var _ = require('lodash');
1510

16-
var dbUsers = mongo.db(dbserver + '/' + dbname + '?auto_reconnect').collection('users');
11+
var connectionString = 'mongodb://localhost:27017/habitrpg?auto_reconnect=true'; // FOR TEST DATABASE
12+
13+
var dbUsers = mongo.db(connectionString).collection('users');
1714

1815
// specify a query to limit the affected users (empty for all users):
1916
var query = {
20-
'flags.newStuff':false
17+
'flags.newStuff':{$ne:true}
2118
};
2219

2320
// specify fields we are interested in to limit retrieved data (empty if we're not reading data):
2421
var fields = {
25-
'flags.newStuff':1
2622
};
2723

2824
console.warn('Updating users...');
@@ -32,7 +28,8 @@ dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) {
3228
if (err) { return exiting(1, 'ERROR! ' + err); }
3329
if (!user) {
3430
console.warn('All appropriate users found and modified.');
35-
return displayData();
31+
setTimeout(displayData, 300000);
32+
return;
3633
}
3734
count++;
3835

npm-shrinkwrap.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
"lcov-result-merger": "^1.0.2",
149149
"mocha": "^2.3.3",
150150
"mongodb": "^2.0.46",
151-
"mongoskin": "~0.6.1",
151+
"mongoskin": "~2.1.0",
152152
"nock": "^2.17.0",
153153
"phantomjs": "^1.9",
154154
"protractor": "^3.1.1",

0 commit comments

Comments
 (0)