From 155e45a8cf5f4d8ab4b06e62f88590359de3b11d Mon Sep 17 00:00:00 2001 From: Lucian Date: Sun, 26 Feb 2017 15:53:31 +0100 Subject: [PATCH] fix(startup): Scale timeout to size of campaign Still trying to fix #341 --- lib/entry-point.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/entry-point.js b/lib/entry-point.js index 3c301e2..791e659 100644 --- a/lib/entry-point.js +++ b/lib/entry-point.js @@ -65,24 +65,25 @@ roll20.on('ready', () => { logger.info('-=> ShapedScripts %%GULP_INJECT_VERSION%% <=-'); Migrator.migrateShapedConfig(myState, logger); const character = roll20.createObj('character', { name: 'SHAPED_VERSION_TESTER' }); - setTimeout(() => { - roll20.createAttrWithWorker(character.id, 'sheet_opened', 1, () => { - const version = roll20.getAttrByName(character.id, 'version'); - setTimeout(() => { - _.invoke(roll20.findObjs({ type: 'character', name: 'SHAPED_VERSION_TESTER' }), 'remove'); - }, 1000); - logger.info('Detected sheet version as : $$$', version); - if (utils.versionCompare(version, MINIMUM_SHEET_VERSION) < 0) { - reporter.reportError(`Incompatible sheet version. You need at least version ${MINIMUM_SHEET_VERSION} to use ` + - 'this script.'); - return; - } - const ed = new EventDispatcher(roll20, errorHandler, logger, reporter); - const cw = new ChatWatcher(roll20, logger, ed); - const commandProc = makeCommandProc('shaped', roll20, errorHandler, ed, version); - getModuleList().forEach(module => module.configure(roll20, reporter, logger, myState, commandProc, cw, ed)); - }); - }, 400); + const campaignSize = roll20.findObjs({}).length; + const delay = Math.max(1000, Math.floor(campaignSize / 30)); + logger.debug('Campaign size: $$$', campaignSize); + roll20.createAttrWithWorker(character.id, 'sheet_opened', 1, () => { + const version = roll20.getAttrByName(character.id, 'version'); + setTimeout(() => { + _.invoke(roll20.findObjs({ type: 'character', name: 'SHAPED_VERSION_TESTER' }), 'remove'); + }, delay); + logger.info('Detected sheet version as : $$$', version); + if (utils.versionCompare(version, MINIMUM_SHEET_VERSION) < 0) { + reporter.reportError(`Incompatible sheet version. You need at least version ${MINIMUM_SHEET_VERSION} to use ` + + 'this script.'); + return; + } + const ed = new EventDispatcher(roll20, errorHandler, logger, reporter); + const cw = new ChatWatcher(roll20, logger, ed); + const commandProc = makeCommandProc('shaped', roll20, errorHandler, ed, version); + getModuleList().forEach(module => module.configure(roll20, reporter, logger, myState, commandProc, cw, ed)); + }); }); module.exports = {