diff --git a/Dealer/2.0/Dealer.js b/Dealer/2.0/Dealer.js new file mode 100644 index 0000000000..271ff8bbc1 --- /dev/null +++ b/Dealer/2.0/Dealer.js @@ -0,0 +1,287 @@ +// Dealer +// Last Updated: 2019-09-03 +// A script to deal and take cards to selected users from specified decks. +// Syntax is !deal --[give,take] [number of cards as integer] --[deck name]|[card name] +on('ready', () => { + const version = '2.0'; + + const processInlinerolls = (msg) => { + if(_.has(msg,'inlinerolls')){ + return _.chain(msg.inlinerolls) + .reduce(function(m,v,k){ + let ti=_.reduce(v.results.rolls,function(m2,v2){ + if(_.has(v2,'table')){ + m2.push(_.reduce(v2.results,function(m3,v3){ + m3.push(v3.tableItem.name); + return m3; + },[]).join(', ')); + } + return m2; + },[]).join(', '); + m['$[['+k+']]']= (ti.length && ti) || v.results.total || 0; + return m; + },{}) + .reduce(function(m,v,k){ + return m.replace(k,v); + },msg.content) + .value(); + } else { + return msg.content; + } + }; + + + log('-=> Dealer v' + version + ' <=-'); + + //Interface elements + + const openReport = `
!deal --[give,take] [#] --[deck name]|[Card Name] --[ids|Player_id]
!deal --give 5 --Playing Cards
!deal --give --Playing Cards|Six of Hearts
--ids|[player_id]
at the end of a command. You can get a list of player ids in the campaign by using the command !deal --players
!deal --give --Inspiration
!deal --take --Inspiration
!deal --give
or !deal --give --Playing Cards
!deal --take
--ids|ID1,ID2...
--ids|ID1
!deal --players
.");
+ return;
+ }
+
+
+}
+
+
+ //get parameter and use default of 'give' if parameter is missing or malformed
+ const args = processInlinerolls(msg).split(/\s+--/);
+
+ if (args.length < 2) {
+ if (args[0] !== '!deal') {
+ sendMessage('Malformed Command', 'Please use !deal --[give/take] --[Deckname] --ids|player_ids.');
+ return;
+ } else {
+ args[1] = 'give';
+ }
+ }
+ let action = args[1].split(/\s+/)[0];
+
+
+ let numCards = args[1].split(/\s+/)[1];
+ numCards = Number((Number.isInteger(Number(numCards))) ? numCards : 1);
+
+ const actions = ['give', 'take'];
+ let cardAction = 'give';
+ if (action && actions.includes(action)) {
+ cardAction = action;
+ }
+ let choices = args[2] || 'Playing Cards';
+
+
+ let deckChoice = choices.split(/\|/)[0] || 'Playing Cards';
+ let cardChoice = choices.split(/\|/)[1] || '';
+
+
+
+ //getid of deck
+ let theDeck = findObjs({
+ _type: "deck",
+ name: deckChoice
+ })[0];
+
+ //test if deck exists
+ if (!theDeck) {
+ sendMessage('No Such Deck', 'Create a deck named ' + deckChoice + '. If the intent is an Inspiration deck, it must be an infinite deck of one card only.');
+ return;
+ }
+
+
+ let deckID = theDeck.id;
+ let deckCards = theDeck.get('_currentDeck');
+
+
+
+ if (msg.selected){
+
+ if (msg.selected.length > 1) {
+ sendMessage('Multiple Tokens', 'Please select only one token. It must be controlled by a specific player, or represent a player-controlled character.');
+ return;
+ }
+ }
+
+ if (msg.selected){
+ let token = getObj(msg.selected[0]._type, msg.selected[0]._id);
+
+ //assign associated character to a variable
+ if (!token.get('represents')) {
+ sendMessage('No Player Specified', 'This token does not represent a player character. Only players get cards.');
+ return;
+ }
+ let character = getObj("character", token.get('represents'));
+
+ //Get owner IDs of each -- Not needed at this point
+ // If the token represents a character, get the character's controller, otherwise the token's
+ let ownerids = (token.get('controlledby').split(','));
+
+
+ if (character) {
+ ownerids = (character.get('controlledby').split(','));
+ }
+ //reduces to one ownerid that is not ['all']
+ ownerid = ownerids.filter(s => s !== 'all')[0];
+ }
+
+ if (IDs !==''){
+ ownerid = IDs;
+ }
+ if (undefined===ownerid || ownerid===''){
+ sendMessage ('No Player ID specified', 'Dealer does not know whom to send this card to.');
+ return;
+ }
+
+ // give card to player
+ // If the ownerid is undefined (no valid controller) explain and exit
+ if (!ownerid) {
+ sendMessage('Needs a Player Controller', 'If a token represents a character controlled by \'All Players\', an individual player must be also be specified. If there are multiple controllers, only the first will get inspiration.');
+ return;
+ }
+
+ //If a card is specified by name
+ if (cardChoice !== '') {
+ let theCard = findObjs({
+ _type: 'card',
+ name: cardChoice,
+ _deckid: deckID
+ })[0];
+ if (theCard !== undefined) {
+ do {
+ let specificCardID = theCard.id;
+ if (cardAction == 'give') {
+ giveCardToPlayer(specificCardID, ownerid);
+ numCards--;
+ } else {
+ takeCardFromPlayer(ownerid, {cardid: specificCardID})
+ numCards--;
+ }
+ }
+ while (numCards > 0);
+ return;
+ } else {
+ sendMessage('No Such Card', 'There does not seem to be a card named ' + cardChoice + ' in the deck ' + deckChoice);
+ return;
+ }
+ }
+
+
+ //If this is a random card
+ do {
+
+ //get id of card
+ let cardid = drawCard(deckID);
+
+ if (!cardid) {
+ shuffleDeck(deckID);
+ cardid = drawCard(deckID);
+ }
+ // get playerId of Token controller
+ //assign selected token to a variable
+
+ switch (cardAction) {
+ case 'take':
+
+ let hand = findObjs({
+ type: 'hand',
+ parentid: ownerid
+ })[0];
+ let theHand = hand.get('currentHand');
+
+ cardid = (theHand.split(',').filter(x => deckCards.split(',').includes(x)))[0];
+
+ if (theHand.length !== 0 && cardid !== undefined) {
+
+ takeCardFromPlayer(ownerid, {
+ cardid: cardid
+ });
+ } else {
+ let deckName = theDeck.get('name');
+ sendMessage('Deck Empty', token.get('name') + ' has no cards left to take from the ' + deckName + ' deck.');
+ }
+
+ break;
+ default:
+ giveCardToPlayer(cardid, ownerid);
+ break;
+ }
+
+ numCards--;
+ }
+ while (numCards > 0);
+ }
+ });
+});
diff --git a/Dealer/Dealer.js b/Dealer/Dealer.js
index 7cfdc78822..271ff8bbc1 100644
--- a/Dealer/Dealer.js
+++ b/Dealer/Dealer.js
@@ -3,19 +3,128 @@
// A script to deal and take cards to selected users from specified decks.
// Syntax is !deal --[give,take] [number of cards as integer] --[deck name]|[card name]
on('ready', () => {
- const version = '1.2.0';
+ const version = '2.0';
+
+ const processInlinerolls = (msg) => {
+ if(_.has(msg,'inlinerolls')){
+ return _.chain(msg.inlinerolls)
+ .reduce(function(m,v,k){
+ let ti=_.reduce(v.results.rolls,function(m2,v2){
+ if(_.has(v2,'table')){
+ m2.push(_.reduce(v2.results,function(m3,v3){
+ m3.push(v3.tableItem.name);
+ return m3;
+ },[]).join(', '));
+ }
+ return m2;
+ },[]).join(', ');
+ m['$[['+k+']]']= (ti.length && ti) || v.results.total || 0;
+ return m;
+ },{})
+ .reduce(function(m,v,k){
+ return m.replace(k,v);
+ },msg.content)
+ .value();
+ } else {
+ return msg.content;
+ }
+ };
+
+
log('-=> Dealer v' + version + ' <=-');
+
+ //Interface elements
+
+ const openReport = `!deal --[give,take] [#] --[deck name]|[Card Name] --[ids|Player_id]
!deal --give 5 --Playing Cards
!deal --give --Playing Cards|Six of Hearts
--ids|[player_id]
at the end of a command. You can get a list of player ids in the campaign by using the command !deal --players
!deal --give --Inspiration
!deal --take --Inspiration
!deal --give
or !deal --give --Playing Cards
!deal --take
--ids|ID1,ID2...
--ids|ID1
!deal --players
.");
+ return;
+ }
+
+
+}
//get parameter and use default of 'give' if parameter is missing or malformed
- const args = msg.content.split(/\s+--/);
+ const args = processInlinerolls(msg).split(/\s+--/);
if (args.length < 2) {
if (args[0] !== '!deal') {
- sendChat('Deal', '/w gm Malformed command. Please use !deal --[give/take] --[Deckname].');
+ sendMessage('Malformed Command', 'Please use !deal --[give/take] --[Deckname] --ids|player_ids.');
return;
} else {
args[1] = 'give';
@@ -48,7 +157,7 @@ on('ready', () => {
//test if deck exists
if (!theDeck) {
- sendChat('Deal', '/w gm Create a deck named ' + deckChoice + '. If the intent is an Inspiration deck, it must be an infinite deck of one card only.');
+ sendMessage('No Such Deck', 'Create a deck named ' + deckChoice + '. If the intent is an Inspiration deck, it must be an infinite deck of one card only.');
return;
}
@@ -58,16 +167,20 @@ on('ready', () => {
- if (msg.selected.length > 1) {
- sendChat('Deal', '/w gm Please select only one token. It must represent player-controlled character.');
+ if (msg.selected){
+
+ if (msg.selected.length > 1) {
+ sendMessage('Multiple Tokens', 'Please select only one token. It must be controlled by a specific player, or represent a player-controlled character.');
return;
+ }
}
+ if (msg.selected){
let token = getObj(msg.selected[0]._type, msg.selected[0]._id);
//assign associated character to a variable
if (!token.get('represents')) {
- sendChat('Deal', '/w gm This token does not represent a player character. Only players get cards.');
+ sendMessage('No Player Specified', 'This token does not represent a player character. Only players get cards.');
return;
}
let character = getObj("character", token.get('represents'));
@@ -82,12 +195,20 @@ on('ready', () => {
}
//reduces to one ownerid that is not ['all']
ownerid = ownerids.filter(s => s !== 'all')[0];
+ }
+ if (IDs !==''){
+ ownerid = IDs;
+ }
+ if (undefined===ownerid || ownerid===''){
+ sendMessage ('No Player ID specified', 'Dealer does not know whom to send this card to.');
+ return;
+ }
// give card to player
// If the ownerid is undefined (no valid controller) explain and exit
if (!ownerid) {
- sendChat('deal', '/w gm If a token represents a character controlled by \'All Players\', an individual player must be also be specified. If there are multiple controllers, only the first will get inspiration.');
+ sendMessage('Needs a Player Controller', 'If a token represents a character controlled by \'All Players\', an individual player must be also be specified. If there are multiple controllers, only the first will get inspiration.');
return;
}
@@ -98,24 +219,26 @@ on('ready', () => {
name: cardChoice,
_deckid: deckID
})[0];
-
if (theCard !== undefined) {
do {
let specificCardID = theCard.id;
-
+ if (cardAction == 'give') {
giveCardToPlayer(specificCardID, ownerid);
numCards--;
+ } else {
+ takeCardFromPlayer(ownerid, {cardid: specificCardID})
+ numCards--;
+ }
}
while (numCards > 0);
return;
} else {
- sendChat('deal', '/w gm There does not seem to be a card named ' + cardChoice + ' in the deck ' + deckChoice);
+ sendMessage('No Such Card', 'There does not seem to be a card named ' + cardChoice + ' in the deck ' + deckChoice);
return;
}
}
-
//If this is a random card
do {
@@ -147,7 +270,7 @@ on('ready', () => {
});
} else {
let deckName = theDeck.get('name');
- sendChat('deal', '/w gm ' + token.get('name') + ' has no cards left to take from the ' + deckName + ' deck.');
+ sendMessage('Deck Empty', token.get('name') + ' has no cards left to take from the ' + deckName + ' deck.');
}
break;