-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): Check against sheet version on startup
closes #12
- Loading branch information
Showing
29 changed files
with
751 additions
and
684 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use strict'; | ||
|
||
const _ = require('underscore'); | ||
|
||
module.exports = class ChatWatcher { | ||
constructor(roll20, logger, eventDispatcher) { | ||
this.roll20 = roll20; | ||
this.logger = logger; | ||
this.eventDispatcher = eventDispatcher; | ||
this.chatListeners = []; | ||
logger.wrapModule(this); | ||
eventDispatcher.registerEventHandler('chat:message', (msg) => { | ||
if (msg.type !== 'api') { | ||
this.triggerChatListeners(msg); | ||
} | ||
}); | ||
} | ||
|
||
registerChatListener(triggerFields, handler) { | ||
const matchers = []; | ||
if (triggerFields && !_.isEmpty(triggerFields)) { | ||
matchers.push((msg, options) => { | ||
this.logger.debug('Matching options: $$$ against triggerFields $$$', options, triggerFields); | ||
return _.intersection(triggerFields, _.keys(options)).length === triggerFields.length; | ||
}); | ||
} | ||
this.chatListeners.push({ matchers, handler }); | ||
} | ||
|
||
triggerChatListeners(msg) { | ||
const options = this.getRollTemplateOptions(msg); | ||
this.logger.debug('Roll template options: $$$', options); | ||
_.each(this.chatListeners, (listener) => { | ||
if (_.every(listener.matchers, matcher => matcher(msg, options))) { | ||
listener.handler(options, msg); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* @returns {*} | ||
*/ | ||
getRollTemplateOptions(msg) { | ||
if (msg.rolltemplate === '5e-shaped') { | ||
const regex = /\{\{(.*?)}}/g; | ||
let match; | ||
const options = {}; | ||
while ((match = regex.exec(ChatWatcher.processInlinerolls(msg)))) { | ||
if (match[1]) { | ||
const splitAttr = match[1].split('='); | ||
const propertyName = splitAttr[0].replace(/_([a-z])/g, (m, letter) => letter.toUpperCase()); | ||
options[propertyName] = splitAttr.length === 2 ? splitAttr[1].replace(/\^\{/, '') : ''; | ||
} | ||
} | ||
if (options.characterName) { | ||
options.character = this.roll20.findObjs({ | ||
_type: 'character', | ||
name: options.characterName, | ||
})[0]; | ||
} | ||
return options; | ||
} | ||
return {}; | ||
} | ||
|
||
static processInlinerolls(msg) { | ||
if (_.has(msg, 'inlinerolls')) { | ||
return _.chain(msg.inlinerolls) | ||
.reduce((previous, current, index) => { | ||
previous[`$[[${index}]]`] = current.results.total || 0; | ||
return previous; | ||
}, {}) | ||
.reduce((previous, current, index) => previous.replace(index.toString(), current), msg.content) | ||
.value(); | ||
} | ||
|
||
return msg.content; | ||
} | ||
|
||
get logWrap() { | ||
return 'ChatWatcher'; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict'; | ||
const _ = require('underscore'); | ||
|
||
module.exports = class EventDispatcher { | ||
|
||
constructor(roll20, errorHandler, logger, reporter) { | ||
this.roll20 = roll20; | ||
this.addedTokenIds = []; | ||
this.errorHandler = errorHandler; | ||
this.logger = logger; | ||
this.reporter = reporter; | ||
this.addTokenListeners = []; | ||
this.attributeChangeHandlers = {}; | ||
logger.wrapModule(this); | ||
roll20.on('add:token', this.handleAddToken.bind(this)); | ||
roll20.on('change:token', this.handleChangeTokenForAdd.bind(this)); | ||
roll20.on('chat:message', (msg) => { | ||
if (msg.playerid !== 'API') { | ||
reporter.setPlayer(msg.playerid); | ||
} | ||
}); | ||
roll20.on('change:attribute', (curr, prev) => { | ||
(this.attributeChangeHandlers[curr.get('name')] || []).forEach(handler => handler(curr, prev)); | ||
}); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
// Event Handlers | ||
///////////////////////////////////////////////// | ||
handleAddToken(token) { | ||
const represents = token.get('represents'); | ||
if (_.isEmpty(represents)) { | ||
return; | ||
} | ||
const character = this.roll20.getObj('character', represents); | ||
if (!character) { | ||
return; | ||
} | ||
this.addedTokenIds.push(token.id); | ||
|
||
// URGH. Thanks Roll20. | ||
setTimeout(() => { | ||
const addedToken = this.roll20.getObj('graphic', token.id); | ||
if (addedToken) { | ||
this.handleChangeTokenForAdd(addedToken); | ||
} | ||
}, 100); | ||
} | ||
|
||
handleChangeTokenForAdd(token) { | ||
if (_.contains(this.addedTokenIds, token.id)) { | ||
this.addedTokenIds = _.without(this.addedTokenIds, token.id); | ||
this.addTokenListeners.forEach(listener => listener(token)); | ||
// this.setTokenBarsOnDrop(token, true); | ||
} | ||
} | ||
|
||
registerEventHandler(eventType, handler) { | ||
if (eventType === 'add:token') { | ||
this.addTokenListeners.push(this.wrapHandler(handler)); | ||
} | ||
else { | ||
this.roll20.on(eventType, this.wrapHandler(handler)); | ||
} | ||
} | ||
|
||
registerAttributeChangeHandler(attributeName, handler) { | ||
this.attributeChangeHandlers[attributeName] = this.attributeChangeHandlers[attributeName] || []; | ||
this.attributeChangeHandlers[attributeName].push(this.wrapHandler(handler)); | ||
} | ||
|
||
wrapHandler(handler) { | ||
const self = this; | ||
return function handlerWrapper() { | ||
try { | ||
handler.apply(null, arguments); | ||
} | ||
catch (e) { | ||
self.errorHandler(e); | ||
} | ||
finally { | ||
self.logger.prefixString = ''; | ||
} | ||
}; | ||
} | ||
|
||
get logWrap() { | ||
return 'EventDispatcher'; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.