-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuckRecovery.js
More file actions
198 lines (168 loc) · 4.54 KB
/
Copy pathLuckRecovery.js
File metadata and controls
198 lines (168 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* jshint undef: true */
/*
globals
state,
sendChat,
randomInteger,
_,
on
*/
var luckRecovery = luckRecovery || (function ()
{
'use strict';
let version = '1.0'; //Next version will attempt to add the value to the character sheet.
let sentAlready = false;
/**
* Parses the input parameters to retrive the character name.
* @param inputParams
* @returns {T}
*/
let getCharName = function (inputParams)
{
let charName = '';
for (let i = 0; i < inputParams.length - 1; i++)
{
charName = charName + ' ' + inputParams[i];
}
return charName.trim();
}
/**
* Utility function to determine if a variable is a number.
* @param obj
* @returns {boolean}
*/
let isNumeric = function (obj)
{
return !isNaN(obj - parseFloat(obj));
}
/**
* Display in chat the result of the luck recovery for the given character and roll.
* @param charName
* @param currLuck
* @param rolld100
* @param luck1Roll
* @param luck2Roll
* @param newluck
*/
let showNewLuck = function (charName, currLuck)
{
log("showNewLuck `" + charName + "` `" + currLuck + "`");
let rolld100 = randomInteger(100); // Default to a random roll.
let luck1Roll = randomInteger(10);
let luck2Roll = randomInteger(10);
let luck3Roll = randomInteger(10);
let luckPass = luck1Roll + 5;
let luckFail = luck1Roll + luck2Roll + 10;
let prefixMsg = '<br>Your starting luck is ' + currLuck + ".";
prefixMsg += "<br>You rolled a " + rolld100 + '.';
let newLuck = currLuck;
if (rolld100 <= currLuck)
{
newLuck += luckPass;
prefixMsg = prefixMsg + '<br>👎 You succeeded on your Luck roll;<br> ∴ you add ' + luckPass + ' (1d10 + 5) to your current luck. ';
prefixMsg = prefixMsg + '<br>Your d10 roll was ' + luck1Roll + ".";
}
else
{
newLuck += luckFail;
prefixMsg = prefixMsg + '<br>👍 You failed on your Luck roll;<br> ∴ you add ' + luckFail + ' (2d10 + 10) to your current luck.';
prefixMsg = prefixMsg + '<br>Your 2d10 rolls were ' + luck1Roll + " and " + luck2Roll + ".";
}
prefixMsg += "<br>🎲 Your new luck value is <b>" + newLuck + "</b>.";
// lucky trait
prefixMsg = prefixMsg + '<br>If you have the Lucky Trait, your d10 roll was ' + luck3Roll + ".";
var luckyNewLuck = newLuck + luck3Roll;
prefixMsg += "<br>🍀 Your Lucky Trait new luck is <b>" + luckyNewLuck + "</b>.";
sendChat(charName + "\'s Luck Recovery", prefixMsg);
}
/**
* Performs the actual logic to determine the luck recovery.
* @param fumbleParams
*/
let parseLuckRecovery = function (inputParams)
{
let charName = getCharName(inputParams);
let paramslen = inputParams.length;
// No parameters so display the default chart and a random roll.
if (paramslen === 0)
{
//showFumble(chart, rolled);
return;
}
let lastParam = inputParams[paramslen - 1];
// Single parameter that is numeric so set the roll value to the parameter and use the default fumble chart.
if (paramslen >= 2 && isNumeric(lastParam))
{
let currLuck = parseFloat(lastParam);
showNewLuck(charName, currLuck);
}
else
{
let msg = charName + ': Invalid parameters given, or something went wrong. GM take over. ' + paramslen + ' ' + lastParam;
sendChat('Luck Recovery', msg);
}
}
/**
* Handle chat events
*
* @param {object} msg
*/
let handleChatMessage = function (msg)
{
// For runtime debugging
if (sentAlready)
{
return;
}
// Is the message the `!fumble` command?
if (msg.type !== "api")
{
return;
}
if (msg.content.substr(0, 13) !== "!luckRecovery")
{
//sendChat(' returning msg.type and content', msg.type + " " + msg.content.substr(0,12));
//sentAlready = true;
return;
}
else
{
//sendChat(' continuing msg.type and content', msg.type + " " + msg.content);
//sentAlready = true;
}
let content = msg.content;
let words = content.split(' '); // Split each word into an array.
words.shift(); // Remove "!luckRecovery"
parseLuckRecovery(words); // Parse the input parameters.
}
/**
* Start luckRecovery and handle chat events.
*/
let init = function ()
{
log('Starting Pulp Luck Recovery v' + version);
on("chat:message", function (msg)
{
handleChatMessage(msg)
}
);
};
log('luckRecovery object');
let ret =
{
init: init
};
return ret;
}
());
/**
* Fires when the page has loaded.
*/
on("ready", function ()
{
'use strict';
log('Initializing luck recovery');
luckRecovery.init();
log('init ready');
}
);