-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeminiImporter.js
More file actions
760 lines (637 loc) · 22.8 KB
/
GeminiImporter.js
File metadata and controls
760 lines (637 loc) · 22.8 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/**
* This script allows players and GMs to import Gemini characters into Roll20. The Gemini Companion app is needed to generate the JSON the importer uses and
* is available on the Apple App Store and Google Play.
*
* Based off the BeyondImporter_5eOGL script - https://github.com/RobinKuiper/Roll20APIScripts/tree/master/BeyondImporter_5eOGL
*
* Version: 1.0.0
* Authors: sentry0
* Contact: https://app.roll20.net/users/6148080/sentry0
* Git: https://github.com/slackdaystudio/gemini-importer
*/
(function () {
const script_name = "GeminiImporter";
const state_name = "GEMINIIMPORTER";
const debug = false;
const style =
"margin-left: 0px; overflow: hidden; background-color: #fff; border: 1px solid #000; padding: 5px; border-radius: 5px;color: #000";
const buttonStyle =
"background-color: #000; border: 1px solid #292929; border-radius: 3px; padding: 5px; color: #fff; text-align: center; float: right;";
let caller = {};
let object;
on("ready", function () {
checkInstall();
log(script_name + " Ready! Command: !gemini");
if (debug) {
sendChat(script_name, script_name + " Ready!", null, { noarchive: true });
}
});
on("chat:message", (msg) => {
if (msg.type !== "api") {
return;
}
let args = msg.content.split(/ --(help|config|reset|import) ?/g);
let command = args.shift().substring(1).trim();
caller = getObj("player", msg.playerid);
if (command !== "gemini") {
return;
}
let importData = "";
if (args.length < 1) {
sendHelpMenu(caller);
return;
}
argLoop: for (let i = 0; i < args.length; i += 2) {
let k = args[i].trim();
let v = args[i + 1] != null ? args[i + 1].trim() : null;
switch (k.toLowerCase()) {
case "import":
importData = v;
break argLoop;
case "config":
if (args.length > 0) {
let setting = v.split("|");
let key = setting.shift();
let value =
setting[0] === "true"
? true
: setting[0] === "false"
? false
: setting[0] === "[NONE]"
? ""
: setting[0];
if (
key === "prefix" &&
value.charAt(0) !== "_" &&
value.length > 0
) {
value = `${value} `;
}
if (
key === "suffix" &&
value.charAt(0) !== "_" &&
value.length > 0
) {
value = ` ${value}`;
}
state[state_name][caller.id].config[key] = value;
}
sendConfigMenu(caller);
break argLoop;
case "reset":
state[state_name][caller] = {};
setDefaults(true);
sendConfigMenu(caller);
break argLoop;
default:
sendHelpMenu(caller);
break argLoop;
}
}
if (importData === "") {
return;
}
let character = JSON.parse(importData);
sendChat(
script_name,
`<div style="${style}">Import of <b>${character.name}</b> is starting.</div>`,
null,
{ noarchive: true }
);
// these are automatically sorted into attributes that are written individually, in alphabetical order
// and other attributes that are then written as a bulk write, but all are written before repeating_attributes
let single_attributes = {};
// these are written in one large write once everything else is written
// NOTE: changing any stats after all these are imported would create a lot of updates, so it is
// good that we write these when all the stats are done
let repeating_attributes = {};
object = null;
// Remove characters with the same name if overwrite is enabled.
if (state[state_name][caller.id].config.overwrite) {
let objects = findObjs(
{
_type: "character",
name:
state[state_name][caller.id].config.prefix +
character.name +
state[state_name][caller.id].config.suffix,
},
{ caseInsensitive: true }
);
if (objects.length > 0) {
object = objects[0];
for (let i = 1; i < objects.length; i++) {
objects[i].remove();
}
}
}
if (object === null) {
// Create character object
object = createObj("character", {
name:
state[state_name][caller.id].config.prefix +
character.name +
state[state_name][caller.id].config.suffix,
inplayerjournals: playerIsGM(msg.playerid)
? state[state_name][caller.id].config.inplayerjournals
: msg.playerid,
controlledby: playerIsGM(msg.playerid)
? state[state_name][caller.id].config.controlledby
: msg.playerid,
});
}
object.set("bio", character.background.replace(/\r?\n|\r/g, "<br />"));
addGeneralInfo(character, single_attributes);
addPoints(character, single_attributes);
addAttributesAndSkills(character, single_attributes);
addSpecializations(character, repeating_attributes);
addSecondaryAttributes(character, single_attributes);
addAdvantagesOrDisadvantages(character.advantages, repeating_attributes);
addAdvantagesOrDisadvantages(
character.disadvantages,
repeating_attributes,
true
);
addPowers(character, repeating_attributes);
addMartialArts(character, repeating_attributes);
addEquipment(character, single_attributes);
addNotes(character, single_attributes);
setAttrs(object.id, single_attributes);
setAttrs(object.id, repeating_attributes);
// Weaksauce
setTimeout(() => {
sendChat(
script_name,
`<div style="${style}">Import of <b>${character.name}</b> has completed.</div>`,
null,
{ noarchive: true }
);
}, 5000);
});
const addGeneralInfo = (character, single_attributes) => {
Object.assign(single_attributes, {
charactername: character.name,
occupation: character.occupation,
species: character.species,
gender: character.gender,
age: character.age,
height: character.height,
weight: character.weight,
});
};
const addPoints = (character, single_attributes) => {
Object.assign(single_attributes, {
attributepoints: character.creationPoints.attributes,
skillpoints: character.creationPoints.skills,
specializationpoints: character.creationPoints.specializations,
advantagepoints: character.creationPoints.advantages,
powerpoints: character.creationPoints.powers,
martialartpoints: character.creationPoints.martialArts,
totalpoints: Object.values(character.creationPoints).reduce(
(a, b) => a + (b || 0),
0
),
});
};
const addAttributesAndSkills = (character, single_attributes) => {
let attributes = {};
let key = "";
for (const attribute of character.attributes) {
key = attribute.name.toLowerCase();
attributes[key] = attribute.dice;
attributes[`${key}pip`] = attribute.pips;
for (const skill of attribute.skills) {
key = skill.name.toLowerCase().replace(/\s/g, "");
attributes[`${key}dice`] = skill.dice;
attributes[`${key}pip`] = skill.pips;
}
}
Object.assign(single_attributes, attributes);
};
const addSpecializations = (character, repeating_attributes) => {
let row;
let attributes;
let dice;
let i = 0;
for (const specialization of character.skillSpecializations) {
attributes = {};
dice = specialization.dieCode.split("D");
if (i === 0) {
attributes["specializationname"] = specialization.name;
attributes["rootskill"] = specialization.baseSkill;
attributes["specializationdice"] = dice[0];
attributes["specializationpips"] =
dice.length === 2 ? dice[1].substring(1) : 0;
} else {
row = getRepeatingRowIds(
"specialization",
"specializationname",
specialization.name,
0
);
attributes[`repeating_addspecialization_${row}_addspecializationname`] =
specialization.name;
attributes[`repeating_addspecialization_${row}_addrootskill`] =
specialization.baseSkill;
attributes[`repeating_addspecialization_${row}_addspecializationdice`] =
dice[0];
attributes[`repeating_addspecialization_${row}_addspecializationpips`] =
dice.length === 2 ? dice[1].substring(1) : 0;
}
Object.assign(repeating_attributes, attributes);
i++;
}
};
const addSecondaryAttributes = (character, single_attributes) => {
let attributes = {};
attributes["move"] = `${character.secondaryAttributes.move}m`;
attributes["evasion"] = character.calculatedAttributes.evasion;
attributes["resolve"] = character.calculatedAttributes.resolve;
attributes["soak"] = character.calculatedAttributes.soak;
attributes["bp"] = character.calculatedAttributes.bodyPoints;
attributes["cp"] = character.secondaryAttributes.characterPoints;
attributes["fp"] = character.secondaryAttributes.fatePoints;
Object.assign(single_attributes, attributes);
};
const addAdvantagesOrDisadvantages = (
list,
repeating_attributes,
isDisadvantage = false
) => {
const prefix = isDisadvantage ? "dis" : "";
let row;
let attributes;
let i = 0;
for (const item of list) {
attributes = {};
if (i === 0) {
attributes[`${prefix}advantagename`] = item.name;
attributes[`${prefix}advantagedetails`] = item.details;
attributes[`${prefix}advantagecost`] = Array.isArray(item.cost)
? item.cost[0]
: item.cost;
} else {
row = getRepeatingRowIds(
`${prefix}advantages`,
`${prefix}advantagename`,
item.name,
0
);
attributes[
`repeating_add${prefix}advantages_` +
row +
`_add${prefix}advantagename`
] = item.name;
attributes[
`repeating_add${prefix}advantages_` +
row +
`_add${prefix}advantagedetails`
] = item.details;
attributes[
`repeating_add${prefix}advantages_` +
row +
`_add${prefix}advantagecost`
] = Array.isArray(item.cost) ? item.cost[0] : item.cost;
}
Object.assign(repeating_attributes, attributes);
i++;
}
};
const addPowers = (character, repeating_attributes) => {
let row;
let attributes;
let ranks;
let i = 0;
for (const power of character.powers) {
attributes = {};
ranks = power.cost.split("/");
if (i === 0) {
attributes["powername"] = power.name;
attributes["powerdetails"] = getPowerDetails(power);
attributes["powerranks"] = ranks[0];
attributes["powereffectiveranks"] = ranks[1];
attributes["powertotalranks"] = ranks[2];
} else {
row = getRepeatingRowIds("powers", "powername", power.name, 0);
attributes[`repeating_addpowers_${row}_addpowername`] = power.name;
attributes[`repeating_addpowers_${row}_addpowerdetails`] =
getPowerDetails(power);
attributes[`repeating_addpowers_${row}_addpowerranks`] = ranks[0];
attributes[`repeating_addpowers_${row}_addpowereffectiveranks`] =
ranks[1];
attributes[`repeating_addpowers_${row}_addpowertotalranks`] = ranks[2];
}
Object.assign(repeating_attributes, attributes);
i++;
}
};
const addMartialArts = (character, repeating_attributes) => {
let row;
let attributes;
let i = 0;
for (const maneuver of character.martialArts) {
attributes = {};
if (i === 0) {
attributes["movename"] = maneuver.name;
attributes["movecost"] = maneuver.cost;
attributes["strikebonus"] = maneuver.strike;
attributes["evasionbonus"] = maneuver.evasion;
attributes["rangebonus"] = maneuver.range;
attributes["movedamage"] = maneuver.damage;
attributes["movenotes"] = maneuver.notes || "";
} else {
row = getRepeatingRowIds("move", "movename", maneuver.name, 0);
attributes[`repeating_addmoves_${row}_addmovename`] = maneuver.name;
attributes[`repeating_addmoves_${row}_addmovecost`] = maneuver.cost;
attributes[`repeating_addmoves_${row}_addstrikebonus`] =
maneuver.strike;
attributes[`repeating_addmoves_${row}_addevasionbonus`] =
maneuver.evasion;
attributes[`repeating_addmoves_${row}_addrangebonus`] = maneuver.range;
attributes[`repeating_addmoves_${row}_addmovedamage`] = maneuver.damage;
attributes[`repeating_addmoves_${row}_addmovenotes`] =
maneuver.notes || "";
}
Object.assign(repeating_attributes, attributes);
i++;
}
};
const addEquipment = (character, single_attributes) => {
let attributes = {};
attributes["equipment"] = character.equipment;
Object.assign(single_attributes, attributes);
};
const addNotes = (character, single_attributes) => {
let attributes = {};
attributes["notes"] = character.notes;
Object.assign(single_attributes, attributes);
};
const getPowerDetails = (power) => {
let details = "";
details += power.details.name;
if (power.details.enhancements.length >= 1) {
details += `; ${power.details.enhancements.join("; ")}`;
}
if (power.details.limitations.length >= 1) {
details += `; ${power.details.limitations.join("; ")}`;
}
return details;
};
const checkInstall = () => {
if (!_.has(state, state_name)) {
state[state_name] = state[state_name] || {};
}
setDefaults();
};
const setDefaults = (reset) => {
const defaults = {
overwrite: false,
debug: false,
prefix: "",
suffix: "",
inplayerjournals: "",
controlledby: "",
};
let playerObjects = findObjs({
_type: "player",
});
playerObjects.forEach((player) => {
if (!state[state_name][player.id]) {
state[state_name][player.id] = {};
}
if (!state[state_name][player.id].config) {
state[state_name][player.id].config = defaults;
}
for (const item in defaults) {
if (!state[state_name][player.id].config.hasOwnProperty(item)) {
state[state_name][player.id].config[item] = defaults[item];
}
}
if (!state[state_name][player.id].config.hasOwnProperty("firsttime")) {
if (!reset) {
sendConfigMenu(player, true);
}
state[state_name][player.id].config.firsttime = false;
}
});
};
const sendConfigMenu = (player, first) => {
let playerid = player.id;
let prefix =
state[state_name][playerid].config.prefix !== ""
? state[state_name][playerid].config.prefix
: "[NONE]";
let prefixButton = makeButton(
prefix,
"!gemini --config prefix|?{prefix}",
buttonStyle
);
let suffix =
state[state_name][playerid].config.suffix !== ""
? state[state_name][playerid].config.suffix
: "[NONE]";
let suffixButton = makeButton(
suffix,
"!gemini --config suffix|?{suffix}",
buttonStyle
);
let overwriteButton = makeButton(
state[state_name][playerid].config.overwrite,
`!gemini --config overwrite|${!state[state_name][playerid].config
.overwrite}`,
buttonStyle
);
let debugButton = makeButton(
state[state_name][playerid].config.debug,
`!gemini --config debug|${!state[state_name][playerid].config.debug}`,
buttonStyle
);
let listItems = [
`<span style="float: left; margin-top: 6px;">Overwrite:</span> ${overwriteButton}<br /><small style="clear: both; display: inherit;">This option will overwrite an existing character sheet with a matching character name. I recommend making a backup copy just in case.</small>`,
`<span style="float: left; margin-top: 6px;">Prefix:</span> ${prefixButton}`,
`<span style="float: left; margin-top: 6px;">Suffix:</span> ${suffixButton}`,
`<span style="float: left; margin-top: 6px;">Debug:</span> ${debugButton}`,
];
let list = `<b>Importer</b>${makeList(
listItems,
"overflow: hidden; list-style: none; padding: 0; margin: 0;",
"overflow: hidden; margin-top: 5px;"
)}`;
let inPlayerJournalsButton = makeButton(
player.get("displayname"),
"",
buttonStyle
);
let controlledByButton = makeButton(
player.get("displayname"),
"",
buttonStyle
);
if (playerIsGM(playerid)) {
let players = "";
let playerObjects = findObjs({
_type: "player",
});
for (let i = 0; i < playerObjects.length; i++) {
players += `|${playerObjects[i]["attributes"]["_displayname"]},${playerObjects[i].id}`;
}
let ipj =
state[state_name][playerid].config.inplayerjournals == ""
? "[NONE]"
: state[state_name][playerid].config.inplayerjournals;
if (ipj != "[NONE]" && ipj != "all") {
ipj = getObj("player", ipj).get("displayname");
}
inPlayerJournalsButton = makeButton(
ipj,
`!gemini --config inplayerjournals|?{Player|None,[NONE]|All Players,all${players}}`,
buttonStyle
);
let cb =
state[state_name][playerid].config.controlledby == ""
? "[NONE]"
: state[state_name][playerid].config.controlledby;
if (cb != "[NONE]" && cb != "all") {
cb = getObj("player", cb).get("displayname");
}
controlledByButton = makeButton(
cb,
`!gemini --config controlledby|?{Player|None,[NONE]|All Players,all${players}}`,
buttonStyle
);
}
let sheetListItems = [
`<span style="float: left; margin-top: 6px;">In Player Journal:</span> ${inPlayerJournalsButton}`,
`<span style="float: left; margin-top: 6px;">Player Control Permission:</span> ${controlledByButton}`,
];
let sheetList = `<hr><b>Character Sheet</b>${makeList(
sheetListItems,
"overflow: hidden; list-style: none; padding: 0; margin: 0;",
"overflow: hidden; margin-top: 5px;"
)}`;
let debug = "";
let resetButton = makeButton(
"Reset",
"!gemini --reset",
`${buttonStyle} margin: auto; width: 90%; display: block; float: none;`
);
let title_text = first
? `${script_name} First Time Setup`
: `${script_name} Config`;
let text = `<div style="${style}">${makeTitle(
title_text
)}${list}${sheetList}${debug}<hr>${resetButton}</div>`;
sendChat(script_name, `/w "${player.get("displayname")}" ${text}`, null, {
noarchive: true,
});
};
const sendHelpMenu = (player, first) => {
let listItems = [
'<span style="text-decoration: underline; font-size: 90%;">!gemini --help</span><br />Shows this menu.',
'<span style="text-decoration: underline; font-size: 90%;">!gemini --config</span><br />Shows the configuration menu. (GM only)',
'<span style="text-decoration: underline; font-size: 90%;">!gemini --import [CHARACTER JSON]</span><br />Imports a character from the Gemini app.',
];
let text = `<div style="${style}">`;
text += makeTitle(`${script_name} Help`);
text += "<ol>";
text += "<li>Export your character from the Gemini app</li>";
text +=
"<li>Open the exported file and copy the file contents to your clipboard</li>";
text +=
"<li>Run the command `!gemini --import {}` making sure you replace `{}` with the contents of your clipboard</li>";
text += "</ol>";
text += "<hr>";
text += `<b>Commands:</b> ${makeList(
listItems,
"list-style: none; padding: 0; margin: 0;"
)}`;
text += "</div>";
sendChat(script_name, `/w "${player.get("displayname")}" ${text}`, null, {
noarchive: true,
});
};
const makeTitle = (title) => {
return `<h3 style="margin-bottom: 10px;color: #000">${title}</h3>`;
};
const makeButton = (title, href, style) => {
return `<a style="${style}" href="${href}">${title}</a>`;
};
const makeList = (items, listStyle, itemStyle) => {
let list = `<ul style="${listStyle}">`;
items.forEach((item) => {
list += `<li style="${itemStyle}">${item}</li>`;
});
list += "</ul>";
return list;
};
const getRepeatingRowIds = (section, attribute, matchValue, index) => {
let ids = [];
if (state[state_name][caller.id].config.overwrite) {
let matches = findObjs({
type: "attribute",
characterid: object.id,
}).filter((attr) => {
return (
attr.get("name").indexOf("repeating_add" + section) !== -1 &&
attr.get("name").indexOf(attribute) !== -1 &&
attr.get("current") == matchValue
);
});
for (const i in matches) {
let row = matches[i]
.get("name")
.replace(`repeating_add${section}_`, "")
.replace(`_${attribute}`, "");
ids.push(row);
}
if (ids.length === 0) {
ids.push(generateRowID());
}
} else {
ids.push(generateRowID());
}
if (index === null) {
return ids;
} else {
return ids[index] == null && index >= 0 ? generateRowID() : ids[index];
}
};
const generateRowID = () => {
"use strict";
return generateUUID().replace(/_/g, "Z");
};
const generateUUID = (function () {
let a = 0,
b = [];
return function () {
let c = new Date().getTime() + 0,
d = c === a;
a = c;
for (var e = new Array(8), f = 7; 0 <= f; f--) {
e[f] =
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".charAt(
c % 64
);
c = Math.floor(c / 64);
}
c = e.join("");
if (d) {
for (f = 11; 0 <= f && 63 === b[f]; f--) {
b[f] = 0;
}
b[f]++;
} else {
for (f = 0; 12 > f; f++) {
b[f] = Math.floor(64 * Math.random());
}
}
for (f = 0; 12 > f; f++) {
c +=
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".charAt(
b[f]
);
}
return c;
};
})();
})();