|
24 | 24 | });
|
25 | 25 | }
|
26 | 26 |
|
27 |
| - function processLoadout(data) { |
| 27 | + function processLoadout(data, version) { |
28 | 28 | if (!_.isUndefined(data)) {
|
29 |
| - _loadouts.splice(0); |
| 29 | + if (version === 'v3.0') { |
| 30 | + var ids = data['loadouts-v3.0']; |
| 31 | + _loadouts.splice(0); |
30 | 32 |
|
31 |
| - // Remove null loadouts. |
32 |
| - data = _.filter(data, function(primitive) { |
33 |
| - return !_.isNull(primitive); |
34 |
| - }); |
| 33 | + _.each(ids, function(id) { |
| 34 | + _loadouts.push(hydrate(data[id])); |
| 35 | + }); |
| 36 | + } else { |
| 37 | + _loadouts.splice(0); |
35 | 38 |
|
36 |
| - _.each(data, function(primitive) { |
37 |
| - // Add id to loadout. |
38 |
| - _loadouts.push(hydrate(primitive)); |
39 |
| - }); |
| 39 | + // Remove null loadouts. |
| 40 | + data = _.filter(data, function(primitive) { |
| 41 | + return !_.isNull(primitive); |
| 42 | + }); |
| 43 | + |
| 44 | + _.each(data, function(primitive) { |
| 45 | + // Add id to loadout. |
| 46 | + _loadouts.push(hydrate(primitive)); |
| 47 | + }); |
| 48 | + } |
40 | 49 | } else {
|
41 | 50 | _loadouts = _loadouts.splice(0);
|
42 | 51 | }
|
|
48 | 57 |
|
49 | 58 | // Avoids the hit going to data store if we have data already.
|
50 | 59 | if (getLatest || _.size(_loadouts) === 0) {
|
51 |
| - chrome.storage.sync.get('loadouts-v2.0', function(data) { |
52 |
| - if (_.isUndefined(data['loadouts-v2.0'])) { |
53 |
| - chrome.storage.sync.get('loadouts', function(oldData) { |
54 |
| - processLoadout((oldData.loadouts) ? oldData.loadouts : undefined); |
55 |
| - saveLoadouts(_loadouts); |
56 |
| - }) |
| 60 | + chrome.storage.sync.get(null, function(data) { |
| 61 | + // if (_.isUndefined(data['loadouts-v2.0'])) { |
| 62 | + // chrome.storage.sync.get('loadouts', function(oldData) { |
| 63 | + // processLoadout((oldData.loadouts) ? oldData.loadouts : undefined); |
| 64 | + // saveLoadouts(_loadouts); |
| 65 | + // }) |
| 66 | + // } else { |
| 67 | + // processLoadout((data['loadouts-v2.0']) ? data['loadouts-v2.0'] : undefined); |
| 68 | + // } |
| 69 | + |
| 70 | + if (_.has(data, 'loadouts-v3.0')) { |
| 71 | + processLoadout(data, 'v3.0'); |
| 72 | + } else if (_.has(data, 'loadouts-v2.0')) { |
| 73 | + processLoadout(data['loadouts-v2.0'], 'v2.0'); |
| 74 | + |
| 75 | + saveLoadouts(_loadouts); |
57 | 76 | } else {
|
58 |
| - processLoadout((data['loadouts-v2.0']) ? data['loadouts-v2.0'] : undefined); |
| 77 | + processLoadout(undefined); |
59 | 78 | }
|
60 | 79 |
|
61 | 80 | deferred.resolve(_loadouts);
|
|
86 | 105 | });
|
87 | 106 | })
|
88 | 107 | .then(function(loadoutPrimitives) {
|
89 |
| - chrome.storage.sync.set({ 'loadouts-v2.0': loadoutPrimitives }, function(e) { |
| 108 | + var data = { |
| 109 | + 'loadouts-v3.0': [] |
| 110 | + }; |
| 111 | + |
| 112 | + _.each(loadoutPrimitives, function(l) { |
| 113 | + data['loadouts-v3.0'].push(l.id); |
| 114 | + data[l.id] = l; |
| 115 | + }); |
| 116 | + |
| 117 | + chrome.storage.sync.set(data, function(e) { |
90 | 118 | deferred.resolve(loadoutPrimitives);
|
91 | 119 | });
|
92 | 120 |
|
|
105 | 133 | loadouts.splice(index, 1);
|
106 | 134 | }
|
107 | 135 |
|
| 136 | + chrome.storage.sync.remove(loadout.id.toString(), function() {}); |
| 137 | + |
108 | 138 | return (loadouts);
|
109 | 139 | })
|
110 | 140 | .then(function(_loadouts) {
|
|
144 | 174 | var hydration = {
|
145 | 175 | 'v1.0': hydratev1d0,
|
146 | 176 | 'v2.0': hydratev2d0,
|
147 |
| - 'default': hydratev2d0 |
| 177 | + 'v3.0': hydratev3d0, |
| 178 | + 'default': hydratev3d0 |
148 | 179 | }
|
149 | 180 |
|
150 | 181 | // v1.0 did not have a 'version' property so if it fails, we'll assume.
|
151 | 182 | return (hydration[(loadout.version)] || hydration['v1.0'])(loadout);
|
152 | 183 | }
|
153 | 184 |
|
| 185 | + function hydratev3d0(loadoutPrimitive) { |
| 186 | + var result = { |
| 187 | + id: loadoutPrimitive.id, |
| 188 | + name: loadoutPrimitive.name, |
| 189 | + classType: (_.isUndefined(loadoutPrimitive.classType) ? -1 : loadoutPrimitive.classType), |
| 190 | + version: 'v3.0', |
| 191 | + items: {} |
| 192 | + }; |
| 193 | + |
| 194 | + _.each(loadoutPrimitive.items, function(itemPrimitive) { |
| 195 | + var item = _.clone(dimItemService.getItem({ |
| 196 | + id: itemPrimitive.id, |
| 197 | + hash: itemPrimitive.hash |
| 198 | + })); |
| 199 | + |
| 200 | + if (item) { |
| 201 | + var discriminator = item.type.toLowerCase(); |
| 202 | + |
| 203 | + item.equipped = itemPrimitive.equipped; |
| 204 | + |
| 205 | + result.items[discriminator] = (result.items[discriminator] || []); |
| 206 | + result.items[discriminator].push(item); |
| 207 | + } |
| 208 | + }); |
| 209 | + |
| 210 | + return result; |
| 211 | + } |
| 212 | + |
154 | 213 | function hydratev2d0(loadoutPrimitive) {
|
155 | 214 | var result = {
|
156 | 215 | id: loadoutPrimitive.id,
|
157 | 216 | name: loadoutPrimitive.name,
|
158 | 217 | classType: (_.isUndefined(loadoutPrimitive.classType) ? -1 : loadoutPrimitive.classType),
|
159 |
| - version: 'v2.0', |
| 218 | + version: 'v3.0', |
160 | 219 | items: {}
|
161 | 220 | };
|
162 | 221 |
|
|
184 | 243 | id: uuid2.newguid(),
|
185 | 244 | name: loadoutPrimitive.name,
|
186 | 245 | classType: -1,
|
187 |
| - version: 'v2.0', |
| 246 | + version: 'v3.0', |
188 | 247 | items: {}
|
189 | 248 | };
|
190 | 249 |
|
|
209 | 268 | id: loadout.id,
|
210 | 269 | name: loadout.name,
|
211 | 270 | classType: loadout.classType,
|
212 |
| - version: 'v2.0', |
| 271 | + version: 'v3.0', |
213 | 272 | items: []
|
214 | 273 | };
|
215 | 274 |
|
|
0 commit comments