-
Notifications
You must be signed in to change notification settings - Fork 1
/
npcimport.js
113 lines (99 loc) · 4.33 KB
/
npcimport.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
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
// NPC Import
//
// lorem ipsum dolor sit amet
//
// Github: https://github.com/nesges/roll20-char-export/npcimport.js
// by: Thomas Nesges <[email protected]>
// Version: 0.1
on('ready',function() {
'use strict';
log('.oO( npcengineer_import v0.0 )');
on("chat:message", function(msg) {
if (msg.type == "api" && msg.content.indexOf("!n") === 0) {
var param = msg.content.split(" ");
var name = param[1];
param.splice(0,1);
// JSON format: extracts of masqs 1d6adventurers compendium (http://www.masq.net/search/label/1d6A%20Compendium) converted to json using http://www.utilities-online.info/xmltojson
var doc = JSON.parse(param.join(''));
for(var key in doc) {
var npc = doc[key];
name = v(npc, 'name/#text');
// create character
var character = createObj('character', {
avatar: '',
name: name,
bio: '',
gmnotes: '',
archived: false,
inplayerjournals: 'all',
controlledby: 'all',
});
attr(character.id, 'npc', 1);
attr(character.id, 'npc_name', name);
var attrvalues = {
npc_type: v(npc,'size/#text') + ' ' + v(npc,'type/#text') + (typeof v(npc,'alignment/#text') != 'undefined' ? ', '+v(npc,'alignment/#text') : ''),
npc_ac: v(npc,'ac/#text'),
npc_actype: v(npc,'actext/#text'),
hp: [ v(npc,'hp/#text'), v(npc,'hp/#text') ],
npc_hpformula: v(npc,'hd/#text'),
npc_speed: v(npc,'speed/#text'),
strength_base: v(npc,'abilities/strength/score/#text'),
dexterity_base: v(npc,'abilities/dexterity/score/#text'),
constitution_base: v(npc,'abilities/constitution/score/#text'),
intelligence_base: v(npc,'abilities/intelligence/score/#text'),
wisdom_base: v(npc,'abilities/wisdom/score/#text'),
charisma_base: v(npc,'abilities/charisma/score/#text'),
};
for(var attrname in attrvalues) {
if(Array.isArray(attrvalues[attrname])) {
if(attrvalues[attrname].length==1) {
attr(character.id, attrname, attrvalues[attrname][0]);
} else if(attrvalues[attrname].length==2) {
attr(character.id, attrname, attrvalues[attrname][0], attrvalues[attrname][1]);
} else {
logChat("ERROR: array "+attrname+" too large");
}
} else {
attr(character.id, attrname, attrvalues[attrname]);
}
}
}
logChat('done');
}
});
function attr(characterid, name, current, max) {
if(typeof current != 'undefined' && typeof max != 'undefined') {
createObj('attribute', {
_characterid: characterid,
name: name,
current: current,
max: max
});
} else if(typeof max != 'undefined') {
createObj('attribute', {
_characterid: characterid,
name: name,
max: max
});
} else if(typeof current != 'undefined') {
createObj('attribute', {
_characterid: characterid,
name: name,
current: current
});
}
}
// v implements a simple path description for json structures
function v(npc, pathString) {
var path = pathString.split('/');
var o = npc;
for(var i=0; i<path.length; i++) {
o = o[path[i]];
}
return o;
}
function logChat(message) {
log(message.replace(/<.*?>/g, ''));
sendChat('NPC Import', message);
}
});