From cf2e2fb6c6070c87ce09bf175e7b491773e50c25 Mon Sep 17 00:00:00 2001 From: "Aaron C. Meadows" Date: Mon, 20 Apr 2015 01:12:00 -0500 Subject: [PATCH 1/3] Squashed 'InitiativeAssistant/' content from commit a65ee45 git-subtree-dir: InitiativeAssistant git-subtree-split: a65ee4527f8d1ba2577f4e14519e2160767fc067 --- InitiativeAssistant.js | 269 +++++++++++++++++++++++++++++++++++++++++ package.json | 17 +++ 2 files changed, 286 insertions(+) create mode 100644 InitiativeAssistant.js create mode 100644 package.json diff --git a/InitiativeAssistant.js b/InitiativeAssistant.js new file mode 100644 index 0000000000..b56ae2ac26 --- /dev/null +++ b/InitiativeAssistant.js @@ -0,0 +1,269 @@ +// Github: https://github.com/shdwjk/Roll20API/blob/master/InitiativeAssistant/InitiativeAssistant.js +// By: The Aaron, Arcane Scriptomancer +// Contact: https://app.roll20.net/users/104025/the-aaron + +var InitiativeAssistant = InitiativeAssistant || (function() { + 'use strict'; + + var version = '0.1.0', + lastUpdate = 1429509755, + schemaVersion = 0.1, + + checkInstall = function() { + log('-=> InitiativeAssistant v'+version+' <=- ['+(new Date(lastUpdate*1000))+']'); + + if( ! _.has(state,'InitiativeAssistant') || state.InitiativeAssistant.version !== schemaVersion) { + log(' > Updating Schema to v'+schemaVersion+' <'); + state.InitiativeAssistant = { + version: schemaVersion + }; + } + }, + + ch = function (c) { + var entities = { + '<' : 'lt', + '>' : 'gt', + "'" : '#39', + '@' : '#64', + '{' : '#123', + '|' : '#124', + '}' : '#125', + '[' : '#91', + ']' : '#93', + '"' : 'quot', + '-' : 'mdash', + ' ' : 'nbsp' + }; + + if(_.has(entities,c) ){ + return ('&'+entities[c]+';'); + } + return ''; + }, + + showHelp = function(who) { + sendChat('','/w '+who+' ' + +'
' + +'
' + +'InitiativeAssistant v'+version + +'
' + +'
' + +'

Provides an easy interface to adding players into the initiative, particularly if they are manually rolling.

' + +'
' + +'Commands' + +'
' + +'!init-assist [ [--'+ch('<')+'name fragment'+ch('>')+'|'+ch('<')+'number'+ch('>')+'] ...] | --help' + +'
' + +'

Adds one or more characters to the initiative order.

' + +'
    ' + +'
  • ' + +''+ch('<')+'name fragment'+ch('>')+' '+ch('-')+' A part of the name of the character to add. This can be the full name, or just a few letters. Case-insensitive.' + +'
  • ' + +'
  • ' + +''+ch('<')+'number'+ch('>')+' '+ch('-')+' A number or inline roll representing the character'+ch("'")+'s initiative score.' + +'
  • ' + +'
' + +'
' + +'
' + +'
' + ); + }, + + + keyFormat = function(text) { + return text.toLowerCase().replace(/\s+/,''); + }, + + handleInput = function(msg_orig) { + var msg = _.clone(msg_orig), + output='', + redos={}, + keys=[], + lookup, + chars, + args, + who, + to; + + if (msg.type !== "api" || !playerIsGM(msg.playerid)) { + return; + } + who=getObj('player',msg.playerid).get('_displayname').split(' ')[0]; + + if(_.has(msg,'inlinerolls')){ + msg.content = _.chain(msg.inlinerolls) + .reduce(function(m,v,k){ + m['$[['+k+']]']=v.results.total || 0; + return m; + },{}) + .reduce(function(m,v,k){ + return m.replace(k,v); + },msg.content) + .value(); + } + + args = msg.content + .replace(/\n/g, ' ') + .replace(/(\{\{(.*?)\}\})/g," $2 ") + .split(/\s+--/); + + switch(args.shift()) { + case '!init-assist': + if( !args.length || _.contains(args,'--help')) { + showHelp(who); + return; + } + + lookup = _.reduce(args,function(m,p){ + var parts=p.split(/\|/), + key=keyFormat(parts[0]); + + keys.push(key); + + m[key]={ + key: key, + input: parts[0], + init: parts[1] + }; + return m; + },{}); + + chars = _.reduce( + filterObjs(function(obj){ + return ('character' === obj.get('type')) && (_.reduce(keys,function(m,k){ + return m || (-1 !== keyFormat(obj.get('name')||'').indexOf(k)); + },false)); + }), + function(m,c){ + var ckey=keyFormat(c.get('name')||''), + key=_.find(keys,function(k){ + return (-1 !== ckey.indexOf(k)); + }); + m[key] = (m[key] ? m[key].push(c) && m[key] : [c]); + return m; + }, + {} + ); + + to=JSON.parse(Campaign().get('turnorder')) || []; + _.each(keys, function(k){ + var char; + if(chars[k]) { + log(chars[k]); + if(1 === chars[k].length) { + char = findObjs({ + type: 'graphic', + pageid: Campaign().get('playerpageid'), + subtype: 'token', + represents: chars[k][0].id + })[0]; + if(char) { + to = _.reject(to, function(i){ + return char.id === i.id; + }); + to.push({ + id: char.id, + pr: lookup[k].init + }); + } else { + lookup[k].matches=chars[k]; + redos.NT=(redos.NT ? redos.NT.push(lookup[k]) && redos.NT : [lookup[k]]); + } + } else { + lookup[k].matches=chars[k]; + redos.DUP=(redos.DUP ? redos.DUP.push(lookup[k]) && redos.DUP : [lookup[k]]); + } + } else { + redos.NM=(redos.NM ? redos.NM.push(lookup[k]) && redos.NM : [lookup[k]]); + } + }); + Campaign().set({ + turnorder: JSON.stringify(to) + }); + + _.each(redos,function(rs,k){ + var params=[]; + switch(k){ + case 'NT': + output+= + '
'+ + '

Please Add Tokens

'+ + '
'+ + _.map(rs, function(r){ + var c = r.matches[0]; + params.push('--'+r.key+'|'+r.init); + return '
'+ + ''+ + ''+c.get("name")+''+ + '
'+ + '
'; + }).join(' ')+ + '
'+ + '

After adding tokens for the above characters: Add Turn(s)

'+ + '
'; + break; + case 'DUP': + output+= + '
'+ + '

Which One?

'+ + '
'+ + _.map(rs, function(r){ + return '

'+ + rs.input+ + '

'+ + '
'+ + _.map(r.matches,function(c){ + var button='Pick'; + return '
'+ + ''+ + button+ + ''+c.get("name")+''+ + '
'+ + '
'; + }).join('')+ + '
'; + }).join(' ')+ + '

'+ + '
'; + break; + case 'NM': + output+= + '
'+ + '

No Matching Characters

'+ + '
'+ + _.map(rs, function(r){ + return '
'+ + r.input+ + '
'; + }).join(' ')+ + '
'+ + '
'; + break; + } + }); + if(output){ + sendChat('Initiative Assistant','/w '+who+' '+output); + } + + break; + } + }, + + registerEventHandlers = function() { + on('chat:message', handleInput); + }; + + return { + CheckInstall: checkInstall, + RegisterEventHandlers: registerEventHandlers + }; + +}()); + +on('ready',function() { + 'use strict'; + + InitiativeAssistant.CheckInstall(); + InitiativeAssistant.RegisterEventHandlers(); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000000..a6f92cc84d --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "InitiativeAssistant", + "version": "0.1", + "description": "Provides an easy interface to adding players to the inititaive order.", + "authors": "The Aaron", + "roll20userid": "104025", + "dependencies": { + }, + "modifies": { + "state.InitiativeAssistant": "read,write", + "campaign.turnorder": "read,write", + "character.name": "read", + "token.represents": "read" + }, + "conflicts": [ + ] +} From 1cb5080ff14d68c0bf0698b6bc502d55582c9a7f Mon Sep 17 00:00:00 2001 From: "Aaron C. Meadows" Date: Mon, 20 Apr 2015 01:24:54 -0500 Subject: [PATCH 2/3] Squashed 'InitiativeAssistant/' changes from a65ee45..1890e37 1890e37 prod-InitiativeAssistant: Updated prod version of InitiativeAssistant at version 0.1.1. git-subtree-dir: InitiativeAssistant git-subtree-split: 1890e37c8babfd12e73d81ad9c097af98e179a4f --- InitiativeAssistant.js | 7 +++---- package.json | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/InitiativeAssistant.js b/InitiativeAssistant.js index b56ae2ac26..710e12a1c6 100644 --- a/InitiativeAssistant.js +++ b/InitiativeAssistant.js @@ -5,8 +5,8 @@ var InitiativeAssistant = InitiativeAssistant || (function() { 'use strict'; - var version = '0.1.0', - lastUpdate = 1429509755, + var version = '0.1.1', + lastUpdate = 1429510954, schemaVersion = 0.1, checkInstall = function() { @@ -150,7 +150,6 @@ var InitiativeAssistant = InitiativeAssistant || (function() { _.each(keys, function(k){ var char; if(chars[k]) { - log(chars[k]); if(1 === chars[k].length) { char = findObjs({ type: 'graphic', @@ -210,7 +209,7 @@ var InitiativeAssistant = InitiativeAssistant || (function() { '
'+ _.map(rs, function(r){ return '

'+ - rs.input+ + r.input+ '

'+ '
'+ _.map(r.matches,function(c){ diff --git a/package.json b/package.json index a6f92cc84d..bb262d63e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "InitiativeAssistant", - "version": "0.1", + "version": "0.1.1", "description": "Provides an easy interface to adding players to the inititaive order.", "authors": "The Aaron", "roll20userid": "104025", From 60f129c4d5f66ff82befee251621452d5313bc41 Mon Sep 17 00:00:00 2001 From: "Aaron C. Meadows" Date: Mon, 20 Apr 2015 10:58:44 -0500 Subject: [PATCH 3/3] Squashed 'InitiativeAssistant/' changes from 1890e37..0d22730 0d22730 prod-InitiativeAssistant: Updated prod version of InitiativeAssistant at version 0.1.2. git-subtree-dir: InitiativeAssistant git-subtree-split: 0d22730760f7fe972622d7322792e69e256f24f9 --- InitiativeAssistant.js | 91 +++++++++++++++++++++++++++++++++++++++--- package.json | 2 +- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/InitiativeAssistant.js b/InitiativeAssistant.js index 710e12a1c6..4ac227822f 100644 --- a/InitiativeAssistant.js +++ b/InitiativeAssistant.js @@ -5,21 +5,40 @@ var InitiativeAssistant = InitiativeAssistant || (function() { 'use strict'; - var version = '0.1.1', - lastUpdate = 1429510954, - schemaVersion = 0.1, + var version = '0.1.2', + lastUpdate = 1429545338, + schemaVersion = 0.2, + sorters = { + 'None': function(to) { + return to; + }, + 'Ascending': function(to){ + return _.sortBy(to,function(i){ + return (i.pr); + }); + }, + 'Descending': function(to){ + return _.sortBy(to,function(i){ + return (-i.pr); + }); + } + }, checkInstall = function() { - log('-=> InitiativeAssistant v'+version+' <=- ['+(new Date(lastUpdate*1000))+']'); + log('-=> InitiativeAssistant v'+version+' <=- ['+(new Date(lastUpdate*1000))+']'); if( ! _.has(state,'InitiativeAssistant') || state.InitiativeAssistant.version !== schemaVersion) { log(' > Updating Schema to v'+schemaVersion+' <'); state.InitiativeAssistant = { - version: schemaVersion + version: schemaVersion, + config: { + sortOption: 'None' + } }; } }, + ch = function (c) { var entities = { '<' : 'lt', @@ -42,6 +61,22 @@ var InitiativeAssistant = InitiativeAssistant || (function() { return ''; }, + getConfigOption_SortOptions = function() { + var text = state.InitiativeAssistant.config.sortOption; + return '
'+ + 'Sort Options is currently '+ + text+ + '.'+ + '
'+ + _.map(_.keys(sorters),function(so){ + return ''+ + so+ + ''; + }).join(' ')+ + '
'+ + '
'; + }, + showHelp = function(who) { sendChat('','/w '+who+' ' +'
' @@ -66,6 +101,7 @@ var InitiativeAssistant = InitiativeAssistant || (function() { +'' +'
' +'
' + +getConfigOption_SortOptions() +'

' ); }, @@ -178,7 +214,7 @@ var InitiativeAssistant = InitiativeAssistant || (function() { } }); Campaign().set({ - turnorder: JSON.stringify(to) + turnorder: JSON.stringify(sorters[state.InitiativeAssistant.config.sortOption](to)) }); _.each(redos,function(rs,k){ @@ -245,6 +281,49 @@ var InitiativeAssistant = InitiativeAssistant || (function() { sendChat('Initiative Assistant','/w '+who+' '+output); } + break; + case '!init-assist-config': + if(_.contains(args,'--help')) { + showHelp(who); + return; + } + if(!args.length) { + sendChat('','/w '+who+' ' + +'
' + +'
' + +'InitiativeAssistant v'+version + +'
' + +getConfigOption_SortOptions() + +'
' + ); + return; + } + _.each(args,function(a){ + var opt=a.split(/\|/), + msg=''; + switch(opt.shift()) { + case 'sort-option': + if(sorters[opt[0]]) { + state.InitiativeAssistant.config.sortOption=opt[0]; + } else { + msg='
Error: Not a valid sort method: '+opt[0]+'
'; + } + sendChat('','/w '+who+' ' + +'
' + +msg + +getConfigOption_SortOptions() + +'
' + ); + break; + + default: + sendChat('','/w '+who+' ' + +'
Unsupported Option:
'+a+'' + ); + } + + }); + break; } }, diff --git a/package.json b/package.json index bb262d63e8..2e190df1b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "InitiativeAssistant", - "version": "0.1.1", + "version": "0.1.2", "description": "Provides an easy interface to adding players to the inititaive order.", "authors": "The Aaron", "roll20userid": "104025",