Skip to content

Commit 3d183ec

Browse files
committed
Added Cashmaster 0.4.1
1 parent bb5c395 commit 3d183ec

File tree

4 files changed

+595
-0
lines changed

4 files changed

+595
-0
lines changed

CashMaster/0.4.1/CashMaster.js

+267
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
/*
2+
3+
CASHMASTER
4+
5+
A currency management script for the D&D 5e OGL sheets on roll20.net.
6+
Please use `!cmhelp` for inline help and examples.
7+
8+
9+
10+
*/
11+
12+
on('ready', function () {
13+
'use strict';
14+
15+
var v="0.4.1";
16+
17+
var usd=25;
18+
/*
19+
Change this if you want to have a rough estimation of a character's wealth in USD.
20+
Set it to something between 25 and 50 (25 USD per 1gp).
21+
Set it to 0 to disable it completely.
22+
*/
23+
24+
var scname="CashMaster";
25+
26+
log(scname+" v"+v+" online. Select one or more party members, then use `!cmhelp` ");
27+
28+
on('chat:message', function(msg) {
29+
if (msg.type !== "api" && !playerIsGM(msg.playerid)) return;
30+
if (msg.content.startsWith("!cm")!== true) return;
31+
var partytotal = 0;
32+
var output = "/w gm &{template:desc} {{desc=<b>Party's cash overview</b><hr>";
33+
var partycounter = 0;
34+
var partymember = Object.entries(msg.selected).length;
35+
_.each(msg.selected, function(obj) {
36+
var token, character;
37+
token = getObj('graphic', obj._id);
38+
if (token) {
39+
character = getObj('character', token.get('represents'));
40+
}
41+
if (character) {
42+
partycounter++;
43+
var name = getAttrByName(character.id, "character_name");
44+
var pp = getattr(character.id, "pp")*1;
45+
var gp = getattr(character.id, "gp")*1;
46+
var ep = getattr(character.id, "ep")*1;
47+
var sp = getattr(character.id, "sp")*1;
48+
var cp = getattr(character.id, "cp")*1;
49+
var total = Math.round((pp*10+gp+ep*0.5+cp/100+sp/10)*10000)/10000;
50+
partytotal = total+partytotal;
51+
output+= "<b>"+name+"</b><br>has ";
52+
if (pp!=0) output+=pp+" platinum, ";
53+
if (gp!=0) output+=gp+" gold, ";
54+
if (ep!=0) output+=ep+" electrum, ";
55+
if (sp!=0) output+=sp+" silver, ";
56+
if (cp!=0) output+=cp+" copper.";
57+
58+
output+="<br>Converted, this character has ";
59+
if (usd>0) output+="<span title='Equals roughly "+(Math.round((total*usd)/5)*5)+" USD'>";
60+
output+=total+" gp";
61+
if (usd>0) output+="</span>";
62+
output+=" in total.<hr>";
63+
}
64+
});
65+
66+
partytotal=Math.round(partytotal*100,0)/100;
67+
68+
output+= "<b><u>Party total: "+partytotal+" gp</u></b>}}";
69+
sendChat (scname,output);
70+
71+
if (msg.content === "!cmhelp")
72+
73+
{
74+
sendChat (scname,"/w gm <h2>Usage</h2><p>First, select one or several party members.</p><p>Then use</p><ul><li><code>!cm</code> to get an <strong>overview</strong> over the party’s cash,</li><li><code>!cmshare</code> to <strong>share</strong> the money equally between party members, converting the amount into the best combination of gold, silver and copper (this should be used in smaller stores),</li><li><code>!cmconvert</code> to <strong>convert and share</strong> the money equally between party members, converting the amount into the best combination of platinum, gold, electrum, silver and copper (this should only be used in larger stores that have a fair amount of cash),</li><li><code>!cmadd [amount][currency]</code> to add/subtract an equal amount of money from each selected party member,</li><li><code>!cmhoard [amount][currency]</code> to share a certain amount of coins between the party members. Note that in this case, no conversion between the different coin types is made - if a party of 5 shares 4 pp, then 4 party members receive one pp each, and the last member won’t get anything.</li></ul><h3>Examples</h3><ol><li><code>!cm</code> will show a cash overview.</li><li><code>!cmshare</code> will collect all the money and share it evenly on the members, using gp, sp and cp only (pp and ep will be converted). Can also be used for one character to “exchange” money.</li><li><code>!cmconvert</code> - same as <code>!cmshare</code>, but will also use platinum and electrum.</li><li><code>!cmadd 50gp</code> will add 50 gp to every selected character.</li><li><code>!cmhoard 50gp</code> will (more or less evenly) distribute 50 gp among the party members.</li></ol><p><strong>Note:</strong> If you substract more coins than a character has, the coin value will become negative. Use <code>!cmshare</code> on that one character to balance the coins (see examples below).</p><h3>Advanced uses</h3><ol><li><strong>Changing multiple values at once:</strong> <code>!cmadd -1gp 10sp</code> will substract 1 gp and add 10 sp at the same time.</li><li><strong>Paying services:</strong> <code>!cmadd -6cp</code> will subtract 6cp from each selected party member. Use <code>!cmshare</code> or <code>!cmconvert</code> afterwards to balance the amount of coins (e.g. it will substract 1 sp and add 4 cp if the character didn’t have copper pieces before).</li></ol>");
75+
76+
}
77+
78+
if (msg.content === "!cmshare" || msg.content === "!cmconvert")
79+
{
80+
output="";
81+
var cashshare=partytotal/partycounter;
82+
var newcounter=0;
83+
var pps=Math.floor(cashshare/10);
84+
if (msg.content === "!cmshare") pps=0;
85+
var rest=cashshare-pps*10;
86+
var gps=Math.floor(rest);
87+
rest=(rest-gps)*2;
88+
var eps=Math.floor(rest);
89+
if (msg.content === "!cmshare") eps=0;
90+
rest=(rest-eps)*5;
91+
var sps=Math.floor(rest);
92+
rest=(rest-sps)*10;
93+
var cps=Math.round(rest);
94+
rest=(rest-cps)*partycounter;
95+
96+
sendChat (scname,"/w gm &{template:desc} {{desc=<b>Let's share this!</b><hr>Everyone receives the equivalent of <b>"+cashshare+" gp:</b> "+pps+" platinum, "+gps+" gold, "+eps+" electrum, "+sps+" silver, and "+cps+" copper.}}");
97+
98+
_.each(msg.selected, function(obj) {
99+
var token, character;
100+
newcounter++;
101+
token = getObj('graphic', obj._id);
102+
if (token) {
103+
character = getObj('character', token.get('represents'));
104+
}
105+
if (character) {
106+
setattr(character.id,"pp",pps);
107+
setattr(character.id,"gp",gps);
108+
setattr(character.id,"ep",eps);
109+
setattr(character.id,"sp",sps);
110+
// enough copper coins? If not, the last one in the group has to take the diff
111+
if (rest>0.999 && newcounter==partycounter) cps=cps+Math.round(rest);
112+
if (rest<-0.999 && newcounter==partycounter) cps=cps+Math.round(rest);
113+
setattr(character.id,"cp",cps);
114+
}
115+
116+
});
117+
118+
}
119+
120+
121+
if (msg.content.startsWith("!cmadd")== true)
122+
{
123+
124+
var ppg=/([0-9 -]+)pp/;
125+
var ppa=ppg.exec(msg.content);
126+
127+
var gpg=/([0-9 -]+)gp/;
128+
var gpa=gpg.exec(msg.content);
129+
130+
var epg=/([0-9 -]+)ep/;
131+
var epa=epg.exec(msg.content);
132+
133+
var spg=/([0-9 -]+)sp/;
134+
var spa=spg.exec(msg.content);
135+
136+
var cpg=/([0-9 -]+)cp/;
137+
var cpa=cpg.exec(msg.content);
138+
139+
output="";
140+
141+
_.each(msg.selected, function(obj) {
142+
var token, character;
143+
token = getObj('graphic', obj._id);
144+
if (token) {
145+
character = getObj('character', token.get('represents'));
146+
}
147+
if (character) {
148+
partycounter++;
149+
var name = getAttrByName(character.id, "character_name");
150+
var pp = getattr(character.id, "pp")*1;
151+
var gp = getattr(character.id, "gp")*1;
152+
var ep = getattr(character.id, "ep")*1;
153+
var sp = getattr(character.id, "sp")*1;
154+
var cp = getattr(character.id, "cp")*1;
155+
var total = Math.round((pp*10+gp+ep*0.5+cp/100+sp/10)*10000)/10000;
156+
partytotal = total+partytotal;
157+
output+="<br><b>"+name+"</b>";
158+
if (ppa) {setattr(character.id,"pp",parseInt(pp)+parseInt(ppa[1])); output+="<br> "+ppa[0];}
159+
if (gpa) {setattr(character.id,"gp",parseInt(gp)+parseInt(gpa[1])); output+="<br> "+gpa[0];}
160+
if (epa) {setattr(character.id,"ep",parseInt(ep)+parseInt(epa[1])); output+="<br> "+epa[0];}
161+
if (spa) {setattr(character.id,"sp",parseInt(sp)+parseInt(spa[1])); output+="<br> "+spa[0];}
162+
if (cpa) {setattr(character.id,"cp",parseInt(cp)+parseInt(cpa[1])); output+="<br> "+cpa[0];}
163+
164+
165+
}
166+
167+
});
168+
sendChat (scname,"/w gm &{template:desc} {{desc=<b>Cashing out - it's payday!</b><hr>"+output+"}}");
169+
170+
}
171+
172+
173+
174+
if (msg.content.startsWith("!cmhoard")== true)
175+
{
176+
177+
var ppg=/([0-9 -]+)pp/;
178+
var ppa=ppg.exec(msg.content);
179+
180+
var gpg=/([0-9 -]+)gp/;
181+
var gpa=gpg.exec(msg.content);
182+
183+
var epg=/([0-9 -]+)ep/;
184+
var epa=epg.exec(msg.content);
185+
186+
var spg=/([0-9 -]+)sp/;
187+
var spa=spg.exec(msg.content);
188+
189+
var cpg=/([0-9 -]+)cp/;
190+
var cpa=cpg.exec(msg.content);
191+
192+
output="";
193+
var partycounter = 0;
194+
195+
_.each(msg.selected, function(obj) {
196+
var token, character;
197+
token = getObj('graphic', obj._id);
198+
if (token) {
199+
character = getObj('character', token.get('represents'));
200+
}
201+
if (character) {
202+
partycounter++;
203+
var name = getAttrByName(character.id, "character_name");
204+
var pp = getattr(character.id, "pp")*1;
205+
var gp = getattr(character.id, "gp")*1;
206+
var ep = getattr(character.id, "ep")*1;
207+
var sp = getattr(character.id, "sp")*1;
208+
var cp = getattr(character.id, "cp")*1;
209+
210+
if (ppa !== null) var ppt=cashsplit(ppa[1],partymember,partycounter);
211+
if (gpa !== null) var gpt=cashsplit(gpa[1],partymember,partycounter);
212+
if (epa !== null) var ept=cashsplit(epa[1],partymember,partycounter);
213+
if (spa !== null) var spt=cashsplit(spa[1],partymember,partycounter);
214+
if (cpa !== null) var cpt=cashsplit(cpa[1],partymember,partycounter);
215+
216+
output+="<br><b>"+name+"</b>";
217+
if (ppa) {setattr(character.id,"pp",parseInt(pp)+parseInt(ppt)); output+="<br> "+ppt+"pp";}
218+
if (gpa) {setattr(character.id,"gp",parseInt(gp)+parseInt(gpt)); output+="<br> "+gpt+"gp";}
219+
if (epa) {setattr(character.id,"ep",parseInt(ep)+parseInt(ept)); output+="<br> "+ept+"ep";}
220+
if (spa) {setattr(character.id,"sp",parseInt(sp)+parseInt(spt)); output+="<br> "+spt+"sp";}
221+
if (cpa) {setattr(character.id,"cp",parseInt(cp)+parseInt(cpt)); output+="<br> "+cpt+"cp";}
222+
}
223+
224+
});
225+
sendChat (scname,"/w gm &{template:desc} {{desc=<b>You are splitting up the coins among you</b><hr>"+output+"}}");
226+
}
227+
228+
229+
});
230+
231+
});
232+
233+
234+
function cashsplit(c,m,x)
235+
{
236+
var ct = 0;
237+
var cr = 0;
238+
if (c !== null)
239+
{
240+
ct = Math.floor(c / m);
241+
cr = c % m;
242+
if (cr >= x) ct++;
243+
else if (c<0 && cr<0 && -cr<x) ct++;
244+
}
245+
return ct;
246+
}
247+
248+
function getattr(cid,att)
249+
{
250+
let attr = findObjs({type:'attribute',characterid:cid,name:att})[0];
251+
if(attr){
252+
let cur = attr.get('current'); // .get()
253+
// log(`${att}: ${cur}`);
254+
return cur;
255+
}
256+
}
257+
258+
function setattr(cid,att,val)
259+
{
260+
let attr = findObjs({type:'attribute',characterid:cid,name:att})[0];
261+
if(attr){
262+
// log(`${att}: ${cur}->${val}`);
263+
attr.setWithWorker({current: parseInt(val)}); // .set()
264+
}
265+
}
266+
267+

0 commit comments

Comments
 (0)