-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathscript.js
More file actions
210 lines (187 loc) · 5.99 KB
/
Copy pathscript.js
File metadata and controls
210 lines (187 loc) · 5.99 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
const WelcomePackage = (() => {
'use strict';
const CREATE_CHARACTER_CMD = '!welcomePackageCreateCharacter';
const observers = [];
let isReady = false;
/**
* Report an error to the GM and log its stack trace.
* @param err The error.
*/
function _error(err) {
log(`WelcomePackage ERROR: ${err.message}`);
log(err.stack);
sendChat('WelcomePackage API',
`/w gm ERROR: ${err.message} --- See API console log for details.`);
}
/**
* Event handler for when a player logs into the game.
* @private
* @param {Player} player
*/
function _handlePlayerOnline(player) {
if (player.get('_online') && !playerIsGM(player.get('_id')))
createPlayerCharacter(player);
}
/**
* Registers a function to be notified when a character is created
* @param {function} func
*/
function onAddCharacter(func){
if('function' === typeof func){
observers.push(func);
}
}
/**
* Notifies all registered functions when a character is created
* @param {Character} character
*/
function notifyAddCharacter(character){
observers.forEach((f)=>f(character));
}
/**
* Creates a character creation macro for a player if they don't already
* have the macro.
* @param {Player} player
*/
function addCharacterCreateMacro() {
const macroName = 'CreateACharacter';
let allPlayers = findObjs({
_type: 'player'
});
_.each(allPlayers, player => {
let playerId = player.get('_id');
let macro = findObjs({
_type: 'macro',
_playerid: playerId,
name: macroName
})[0];
if(!macro) {
log(`WelcomePackage: Creating "CreateACharacter" macro for player ` +
`${player.get('_displayname')}`);
createObj('macro', {
_playerid: playerId,
name: macroName,
action: `${CREATE_CHARACTER_CMD} ?{Character Name:}`
});
}
});
}
/**
* Creates a character for a player if they don't already have any characters.
* @param {Player} player
* @param {string} [name] The name of the character. If this is provided,
* the character will be created regardless of whether
* the player has a character already.
*/
function createPlayerCharacter(player, name) {
let playerId = player.get('_id');
let who = player.get('_displayname');
// Check if there are any characters already controlled by the player.
let characters = findObjs({
_type: 'character'
}).filter(character => {
return character.get('controlledby').includes(playerId);
});
// Create the new character if there are none, or if a name has been
// provided for a character being explicitly created through the macro.
if(characters.length === 0 || name) {
if(!name) {
log(`WelcomePackage: Creating first-time character for player ` +
`${player.get('_displayname')}`);
name = `${who}'s character`;
}
let character = createObj('character', {
controlledby: playerId,
inplayerjournals: 'all',
name
});
// Disable the Charactermancer for the new character by creating
// the "version" attribute. This will be set to its correct value later
// by the character sheet's worker.
/*
createObj('attribute', {
name: 'mancer_confirm_flag',
characterid: character.id,
current: '1'
});
createObj('attribute', {
name: 'l1mancer_status',
characterid: character.id,
current: 'completed'
});
*/
setTimeout(() => {
notifyAddCharacter(character);
showCharacterLink(who, character);
}, 1000);
}
}
/**
* Shows a player the link to their new character with a welcoming message.
* @param {string} who The player's display name.
* @param {Character} character
*/
function showCharacterLink(who, character) {
const html = `
<div class="menu" style="background: #fff; border: solid 1px #a88cd5; border-radius: 5px; font-weight: bold; margin-bottom: 1em; overflow: hidden;">
<div class="menuHeader" style="background: #a88cd5 ; color: #fff ; text-align: center;">Welcome!</div>
<div class="menuBody" style="padding: 5px; text-align: center">
<div>A blank character has been created for you to start building: </div>
<a href="http://journal.roll20.net/character/${character.get('_id')}" style="color: #a08;">${character.get('name')}
</div>
</div>
`;
sendChat('Welcome Package', '/w ' + who + ' ' + html);
}
// When a player logs in, create a character for them if they don't have one.
on('change:player:_online', player => {
if(isReady)
_handlePlayerOnline(player);
});
on('ready', () => {
try {
// Create the global macro for the script (visible to all players)
// if it doesn't already exist.
addCharacterCreateMacro();
log('*** Initialized Welcome Package v1.3.3 ***');
isReady = true;
// Wait some amount of time to give other scripts a chance to finish
// loading. Then process any pending logged in players.
setTimeout(() => {
try {
// Once we're loaded, create a character for any player that doesn't
// have one.
let players = findObjs({
_type: 'player'
});
_.each(players, player => {
_handlePlayerOnline(player);
});
}
catch (err) {
_error(err);
}
}, 5000);
}
catch (err) {
_error(err);
}
});
/**
* Process chat commands.
*/
on('chat:message', msg => {
let playerId = msg.playerid;
if(msg.content.startsWith(CREATE_CHARACTER_CMD)) {
let player = getObj('player', playerId);
let argv = msg.content.split(' ');
let name = argv.slice(1).join(' ');
createPlayerCharacter(player, name);
}
});
return {
OnAddCharacter: onAddCharacter,
onAddCharacter
};
})();
_.noop(WelcomePackage);