I again have been racking my brain trying increase the functions of some of my nightbot games such as cast a spell. The original code is not mine but it does function as written
$(user) Casts a Spell on $(touser) it backfires and turns them into a $(eval a=[Bull Frog,Swing Set,Prickly Cactus,Sasquatch,Pumpkin,73 Pinto,Wet Noodle];a[Math.floor(Math.random()*a.length)])!
I was attempting to add a % to cast so say 20% of the time it would WORK and the other 80% of the time it would display one of the backfire results. To which I found a hit or miss code
$(user) Throws a dart at the bullseye $(eval var roll = Math.floor(Math.random() * 100) + 1; if (roll <= 20) { “YOU HIT THE Bulls Eye! GREAT SHOT” } else { “U MISSED. Please try Again” })
and with help here from great people that works as intended…
Now comes the part I just can’t seem to get an understanding for. Ideally I want to combine the code from both of these into one function the following is as far as I got after a few days of driving myself batty I thought I would ask here…
Following is my horrible attempt at combining the two codes into a working one…
$(user) Casts a Spell on $(touser) $(eval var roll = Math.floor(Math.random() * 100) + 1; if (roll <= 20) { “The SPELL WORKS and turns $(tuser) into a God/Godess” } else { it backfires and turns $(touser) into a $(eval a=[Bull Frog,Swing Set,Prickly Cactus,Sasquatch,Pumpkin,73 Pinto,Wet Noodle];a[Math.floor(Math.random()*a.length)] })
$(eval r = Math.floor(Math.random() * 5) + 1; b = ['Bull Frog', 'Swing Set', 'Prickly Cactus', 'Sasquatch', 'Pumpkin', '73 Pinto', 'Wet Noodle']; `$(user) Casts a Spell on $(touser)... ${r === 1 ? 'The SPELL WORKS and turns $(touser) into a Deity!' : 'It backfires and turns $(touser) into a ' + b[Math.floor(Math.random() * b.length)] + '.'}`;)
20/80 has the same ratio as 1/4, that’s why I’m only generating a random number up to 5 and check for r === 1, that simplifies the code a bit.
Also, I took the liberty to replace “God/Goddess” with “Deity” as it’s gender neutral, but feel free to revert my change.
I ended up finally making an even more optioned version that I made work by constructing it from one of your past efforts. Once upon a time I could do this in my sleep but I am very grateful for your help…
Here is that code I put together using 3 sets arguments by your simple guidance
$(user) Casts a Spell on $(touser) and it $(eval const a = [Works but ,Works PERFECTLY sort of ,Fails yet ]; const b = [bounces off the wall and ,hits the ceiling and ,Fizzles out and ]; const c = [turns $(user) into a frog,turns $(touser) into a Sasquatch,Bursts into flames, Turns $(user) and $(touser) into GODS]; a[Math.floor(Math.random()*a.length)] + b[Math.floor(Math.random()*b.length)] + c[Math.floor(Math.random()*c.length)])
the only thing I think it is missing at this point is a chance for it to choose between the $(user) and $(touser) on the results without putting it in the last eval set. The code is nearing 500 characters so I am not sure it is in the cards. Again I thank you for your efforts.
Well you can always save a lot of space by storing data and/or code on a Pastebin (I recommend making an account to be able to edit the paste later, the paste must not be private):
What goes in the paste:
const randomPick = (array) => array[Math.floor(Math.random() * array.length)];
const spellSuccess = ['Works, but', 'Works PERFECTLY, sort of', 'Fails, yet'];
const spellDirection = ['bounces off the wall', 'hits the ceiling', 'Fizzles out'];
let spellTarget = [u, t, `${u} and ${t}`, null];
let spellOutcome = {
'withTarget': ['Bull Frog', 'Swing Set', 'Prickly Cactus', 'Sasquatch', 'Pumpkin', '73 Pinto', 'Wet Noodle', 'Deity'],
'withoutTarget': ['Bursts into flames']
};
spellTarget = randomPick(spellTarget);
spellTarget === null ? spellOutcome = spellOutcome.withoutTarget : spellOutcome = spellOutcome.withTarget;
`${u} Casts a Spell on ${t} and it ${randomPick(spellSuccess)} ${randomPick(spellDirection)} and ${spellTarget === null ? '' : 'turns ' + spellTarget + ' into a '}${randomPick(spellOutcome)}!`;
And then the command code would be:
$(eval const u = '$(user)'; const t = '$(touser)'; $(urlfetch json https://pastebin.com/raw/XXXXXXXX))