-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.gs
347 lines (282 loc) · 12.5 KB
/
code.gs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Star Trek Timelines Datasheet Updater
*
* Updates the datasheet generated by datacore.app directly from JSON data contained in a google drive file.
*
* Released under an Apache 3.0 licence
*/
/* Used to identify the file on Google Drive that contains data from disruptorbeam.com/player */
const GOOGLE_DRIVE_FILEID = 'INSERT_ID_HERE';
const IMPORT_FUNCS = [
{ name : 'crew', func : importCrewData_ },
{ name : 'item', func : importItemData_ },
{ name : 'voyage', func : importVoyageData_ },
{ name : 'event', func : importEventData_ }
];
const doImport_ = (index) => importData_(IMPORT_FUNCS[index], fetchData_())
const crewDataImport = () => doImport_(0);
const itemDataImport = () => doImport_(1);
const voyageDataImport = () => doImport_(2);
const eventDataImport = () => doImport_(3);
/**
* Adds menu items for importing data
* @param {Event} e The onOpen event.
*/
function onOpen(e) {
// Add a custom menu to the spreadsheet.
let menu = SpreadsheetApp.getUi()
.createMenu('Import data')
.addItem('Import all data', 'importAllData')
.addSeparator();
IMPORT_FUNCS.forEach(info => menu.addItem(`Import ${info.name} data`, `${info.name}DataImport`));
menu.addToUi();
}
fetchData_ = () => JSON.parse(DriveApp.getFileById(GOOGLE_DRIVE_FILEID)
.getBlob()
.getDataAsString());
const BATCH_IMPORT_ERROR = 363;
const OCCUPIED_RANGE_NOTE = "Data sheet used";
const OCCUPIED_PROMPT = `The sheet you are using already contains data that will be deleted.
Do you want to do this?`;
function importAllData() {
let successes = 0;
IMPORT_FUNCS.forEach(info => {
try {
let data = fetchData_();
importData_(info, data, true);
++successes;
} catch (e) {
if (e != BATCH_IMPORT_ERROR)
throw e;
}
});
let message = successes ? `Successfully imported ${successes} data sets` :
'Please import data set individually before using batch update';
SpreadsheetApp.getUi().alert(message);
}
function importData_(info, data = null, batch = false) {
let ss = SpreadsheetApp.getActiveSpreadsheet();
let rangeName = info.name + "DataImport";
let range = ss.getRangeByName(rangeName);
if (range == null && !batch) {
range = ss.getActiveSheet().getRange(1, 1);
if (range.getSheet().getLastRow() > 0) {
let ui = SpreadsheetApp.getUi();
let response = ui.alert(OCCUPIED_PROMPT, ui.ButtonSet.YES_NO);
if (response != ui.Button.YES)
return;
}
ss.setNamedRange(rangeName, range);
}
if (range == null)
throw BATCH_IMPORT_ERROR;
if (info[0] == 'crew' && sheet.getLastRow() > 1) {
// Attempt to find frozen crew in sheet
let crewNames = sheet.getSheetValues(2, 1, sheet.getLastRow()).flat();
let archetypeIds = sheet.getSheetValues(2, sheet.getLastColumn(), sheet.getLastRow()).flat();
for (i in parsedData.player.character.stored_immortals) {
let immortal = parsedData.player.character.stored_immortals[i];
let crewIndex = archetypeIds.indexOf(immortal.id);
if (crewIndex >= 0) {
// Add to active crew
data.crew.push({
'name' : crewNames[crewIndex],
'frozen' : true,
'copies' : immortal.quantity});
}
}
}
let newContent = info.func(data);
range.getSheet().clearContents();
range.getSheet().getRange(1, 1, newContent.length, newContent[0].length).setValues(newContent);
}
function createColumnUpdaters_(immortalHeaders) {
const playerSkill = (skill, type) => {
let name = skill + '_skill';
if (type != 'core')
type = 'range_' + type;
return (data, id) => {
let skills = new Object(data.skills);
if (!Object.keys(skills).includes(name))
return 0;
return data.skills[name][type];
};
};
const value = val => (d, id) => val;
const fromPlayer = path => {
let indicies = path.split('/');
return (data, id) => {
let retVal = data;
for (index in indicies)
retVal = retVal[indicies[index]];
return retVal;
};
};
const fromImmortals = name => {
let index = immortalHeaders[1].indexOf(name);
return (pd, id) => id[index];
};
const immortalSkill = (skill, type) => {
let index = immortalHeaders[0].indexOf(skill) + immortalHeaders[1].indexOf(type)
- immortalHeaders[0].indexOf("Command");
return (pd, id) => id[index];
};
const fromImmortalsNum = name => {
let index = immortalHeaders[1].indexOf(name);
return (pd, id) => parseInt(id[index].slice(1));
};
let rarityIndex = immortalHeaders[1].indexOf('Stars');
let collectionsIndex = immortalHeaders[1].indexOf('Collection');
let rarityMap = ["", ", Common", ", Uncommon", ", Rare"];
const collections = (pd, id) => [id[collectionsIndex], id[rarityIndex] <= 3 ? rarityMap[id[rarityIndex]] : ""].join(', ');
return [[
fromPlayer('name'), value(true), fromPlayer('short_name'),
fromPlayer('max_rarity'), fromPlayer('rarity'), fromPlayer('level'), value(0),
(data, id) => { let values = []; for (equip in data.equipment) {values.push(data.equipment[equip][0]); }; return values.join(' '); },
fromPlayer('equipment_rank'), fromImmortals('Portal'), collections,
fromImmortalsNum('VOY Rank'), fromImmortals('GPairs'),
playerSkill('command', 'core'), playerSkill('command', 'min'), playerSkill('command', 'max'),
playerSkill('diplomacy', 'core'), playerSkill('diplomacy', 'min'), playerSkill('diplomacy', 'max'),
playerSkill('engineering', 'core'), playerSkill('engineering', 'min'), playerSkill('engineering', 'max'),
playerSkill('medicine', 'core'), playerSkill('medicine', 'min'), playerSkill('medicine', 'max'),
playerSkill('science', 'core'), playerSkill('science', 'min'), playerSkill('science', 'max'),
playerSkill('security', 'core'), playerSkill('security', 'min'), playerSkill('security', 'max'),
fromImmortals('Traits'), fromPlayer('archetype_id')
// Not using ship stats for now
//fromPlayer('action/name'),
//(data, id) => ['Attack','Evasion','Accuracy'][data.action.boost_type],
//fromPlayer('action/bonus_amount'), fromPlayer('action/initial_cooldown'), fromPlayer('action/duration'), fromPlayer('action/cooldown'),
//value(''), value(''), value(''), value(''), value(''), value(''), value(''),
],
[
fromImmortals("Name"), fromPlayer('frozen'), fromImmortals("Variant"),
fromImmortals("Stars"), fromImmortals("Stars"), value(100), fromPlayer('copies'),
value(''), value(9), fromImmortals('Portal'), collections,
fromImmortalsNum('VOY Rank'), fromImmortals('GPairs'),
immortalSkill('Command', 'Base'), immortalSkill('Command', 'Min'), immortalSkill('Command', 'Max'),
immortalSkill('Diplomacy', 'Base'), immortalSkill('Diplomacy', 'Min'), immortalSkill('Diplomacy', 'Max'),
immortalSkill('Engineering', 'Base'), immortalSkill('Engineering', 'Min'), immortalSkill('Engineering', 'Max'),
immortalSkill('Medicine', 'Base'), immortalSkill('Medicine', 'Min'), immortalSkill('Medicine', 'Max'),
immortalSkill('Science', 'Base'), immortalSkill('Science', 'Min'), immortalSkill('Science', 'Max'),
immortalSkill('Security', 'Base'), immortalSkill('Security', 'Min'), immortalSkill('Security', 'Max'),
fromImmortals('Traits'), fromPlayer('archetype_id', '')
]];
}
class CrewMap extends Map {
add(character) {
if (this.has(character.name))
character.duplicate = this.get(character.name);
else
character.duplicate = false;
this.set(character.name, character);
}
}
/** Imports crew data from an JSON file on */
function importCrewData_(data) {
// Data from Timelines Backend Stats Super Hyper Edition
let immortalSheet = SpreadsheetApp.openById('1czPEYhyNszsx7BqzYsbicAHKK15w4hjTmYiSp_jKsgg')
.getSheetByName('Data');
let immortalHeaders = immortalSheet.getSheetValues(2, 1, 2, immortalSheet.getLastColumn());
let immortalData = immortalSheet.getSheetValues(4, 1, immortalSheet.getLastRow() - 4, immortalSheet.getLastColumn());
let [playerUpdaters, immortalUpdaters] = createColumnUpdaters_(immortalHeaders);
let crewMap = new CrewMap();
// Setup map of active crew
for (i in data.player.character.crew) {
let character = data.player.character.crew[i];
if (!character.in_buy_back_state) {
character.frozen = false;
character.copies = 1;
crewMap.add(character);
}
}
let outputData = new Array();
const headers = [
'Name', 'Have', 'Short name',
'Max rarity', 'Rarity', 'Level', 'Frozen',
'Equipment', 'Tier', 'In portal', 'Collections',
'Voyage Rank', 'Gauntlet Pairs',
'Command Core', 'Command Min', 'Command Max',
'Diplomacy Core', 'Diplomacy Min', 'Diplomacy Max',
'Engineering Core', 'Engineering Min', 'Engineering Max',
'Medicine Core', 'Medicine Min', 'Medicine Max',
'Science Core', 'Science Min', 'Science Max',
'Security Core', 'Security Min', 'Security Max',
'Traits', /* 'Action name', 'Boosts Amount',
'Initialize Duration', 'Cooldown', 'Bonus Ability',
'Trigger Uses per Battle (Not Used)', 'Handicap Type (Not Used)','Handicap Amount (Not Used)',
'Accuracy', 'Crit Bonus', 'Crit Rating', 'Evasion', 'Charge Phases (Not Used)', */
'Character ID'];
outputData.push(headers);
let crewOwned = 0;
let crewFrozen = 0;
for (let row = 2; row < immortalData.length; ++row) {
let crewName = immortalData[row][0];
//console.info('Looking at ' + characterName);
if (crewName == '')
continue;
let crew = crewMap.has(crewName) ? crewMap.get(crewName) : { name : crewName, frozen : false, copies : 0 };
while (crew) {
let outputRow = [];
let updaters = !crew.frozen && crew.copies ? playerUpdaters : immortalUpdaters;
for (let column = 0; column < headers.length; ++column)
outputRow.push(updaters[column](crew, immortalData[row]));
if (crew.frozen)
crewFrozen += crew.copies;
else
crewOwned += crew.copies;
outputData.push(outputRow);
crew = crew.duplicate;
}
}
//app.getUi().alert(`You own ${crewOwned} crew and have ${crewFrozen} in the freezer.`);
return outputData;
}
/** Import Name, Rarity and quantity owned of every item into active sheet */
function importItemData_(data) {
let items = data.player.character.items;
let outputMap = new CrewMap();
let output = [];
output.push(['Name', 'Basic', 'Common', 'Uncommon', 'Rare', 'Super rare', 'Legendary']);
for (i in items) {
let item = items[i];
if (!outputMap.has(item.name))
outputMap.set(item.name, [item.name, 0, 0, 0, 0, 0, 0]);
outputMap.get(item.name)[item.rarity + 1] = item.quantity;
}
let sortedKeys = Array.from(outputMap.keys()).sort();
for (key in sortedKeys)
output.push(outputMap.get(sortedKeys[key]));
return output;
}
/** Import Voyage data into active sheet */
function importVoyageData_(data) {
let output = [];
let voyageData = data.player.character.voyage_descriptions[0];
let skillTypeMap = new Map();
skillTypeMap.set(voyageData.skills.primary_skill, 'Primary');
skillTypeMap.set(voyageData.skills.secondary_skill, 'Secondary');
const parseTrait = trait => trait.replace('_', ' ')
.replace('doctor', 'Physician')
.replace(/\b(\w)/g, c => c.toUpperCase());
let current = data.player.character.voyage[0];
let voyager = current ? slot => current.crew_slots[slot].crew.name : slot => "None";
output.push(["Voyage Status:", "", current ? current.state : "Not out", "", "", ""]);
output.push(['', '', '', '', '', '']);
output.push(['Skill', 'Type', 'Trait 1', 'Voyager 1', 'Trait 2', 'Voyager 2']);
let skillRows = [];
for (let i = 0; i < voyageData.crew_slots.length; i += 2) {
let slot1 = voyageData.crew_slots[i];
let slot2 = voyageData.crew_slots[i+1];
let skill = slot1.skill;
skillRows.push([skill.charAt(0).toUpperCase() + skill.slice(1, -6),
skillTypeMap.get(skill),
parseTrait(slot1.trait),
voyager(i),
parseTrait(slot2.trait),
voyager(i+1)]);
}
skillRows.sort((a, b) => a[0].localeCompare(b[0]));
return [...output, ...skillRows];
}
function importEventData_(data, sheet) {
}