forked from HabitRPG/habitica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20140829_change_headAccessory_to_eyewear.js
75 lines (64 loc) · 2.91 KB
/
20140829_change_headAccessory_to_eyewear.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// node .migrations/20140829_change_headAccessory_to_eyewear.js
var migrationName = '20140829_change_headAccessory_to_eyewear';
var authorName = 'Alys'; // in case script author needs to know when their ...
var authorUuid = 'd904bd62-da08-416b-a816-ba797c9ee265'; //... own data is done
/**
* https://github.com/HabitRPG/habitrpg/issues/3645
*/
var mongo = require('mongoskin');
var _ = require('lodash');
var liveUsers = mongo.db('localhost:27017/habitrpg2?auto_reconnect').collection('users');
var fields = {'migration':1,
'items.gear.costume.headAccessory':1,
'items.gear.equipped.headAccessory':1,
'items.gear.owned.headAccessory_special_wondercon_black':1,
'items.gear.owned.headAccessory_special_wondercon_red':1,
'items.gear.owned.headAccessory_special_summerRogue':1,
'items.gear.owned.headAccessory_special_summerWarrior':1
};
var progressCount = 1000;
var count = 0;
liveUsers.findEach({ $and: [
{ migration: {$ne:migrationName} },
{ $or: [
{'items.gear.owned.headAccessory_special_summerRogue': {'$exists':true}},
{'items.gear.owned.headAccessory_special_summerWarrior':{'$exists':true}},
{'items.gear.owned.headAccessory_special_wondercon_red':{'$exists':true}},
{'items.gear.owned.headAccessory_special_wondercon_black':{'$exists':true}}
]}
]}, fields, {batchSize:250}, function(err, user){
count++;
if (!user) err = '!user';
if (err) {return console.error(err);}
var set = {'migration': migrationName};
var unset = {};
var oldToNew = {
'headAccessory_special_summerRogue': 'eyewear_special_summerRogue',
'headAccessory_special_summerWarrior': 'eyewear_special_summerWarrior',
'headAccessory_special_wondercon_red': 'eyewear_special_wondercon_red',
'headAccessory_special_wondercon_black':'eyewear_special_wondercon_black'
};
// items.gear.costume, items.gear.equipped:
_.each(['costume','equipped'],function(type){
_.each(oldToNew,function(newName,oldName){
if (user.items.gear[type].headAccessory === oldName) {
unset['items.gear.'+type+'.headAccessory'] = "";
set['items.gear.'+type+'.eyewear'] = newName;
}
});
});
// items.gear.owned:
_.each(oldToNew,function(newName,oldName){
if (oldName in user.items.gear.owned) {
unset['items.gear.owned.'+oldName] = "";
set['items.gear.owned.'+newName] = user.items.gear.owned[oldName];
}
});
//console.log(JSON.stringify(user, null, " "));
//console.log("set: " + JSON.stringify(set, null, " "));
//console.log("unset: " + JSON.stringify(unset, null, " "));
liveUsers.update({_id:user._id}, {$set:set, $unset:unset, $inc:{_v:1}});
if (count%progressCount == 0) console.log(count + ' ' + user._id);
if (user._id == '9') console.log('lefnire processed');
if (user._id == authorUuid) console.log(authorName + ' processed');
});