diff options
author | Nat Lasseter <user@4574.co.uk> | 2022-02-23 23:10:53 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2022-02-23 23:10:53 +0000 |
commit | 14d5b36dc88ed9ab23526cbfddf4e20653345a65 (patch) | |
tree | f33f9189621f782fc6a29c6357a388c882932436 | |
parent | 1aa60851a50376edc7d3b2b3dd2c7c41fe3b940c (diff) |
Rewrite for EPv2
-rwxr-xr-x | iada.rb | 127 | ||||
-rwxr-xr-x | iada_v1.rb | 137 |
2 files changed, 235 insertions, 29 deletions
@@ -6,35 +6,52 @@ def make_roll(dX = 100) rand(100_000_000 * dX) % dX end -def human_result(name, roll, vs, fr, prefix = "") +def calc_sups(roll, success = true) + case roll + when 0 + success = true + when 99 + success = false + else + success = roll <= vs + end + + (success ? roll : (99 - roll)) / 33 +end + +def calc_crit(roll) + roll / 10 == roll % 10 +end + +def human_result(name, data, prefix = "") + roll, vs, fr, flip, up, down = data + case roll when 0 crit = true success = true - diff = [vs - roll, 0].max when 99 crit = true success = false - diff = (vs - roll).abs else - crit = roll.to_s[0] == roll.to_s[1] + crit = calc_crit(roll) success = roll <= vs - diff = (vs - roll).abs end - sig = diff >= 30 - supsig = diff >= 60 + sups = calc_sups(roll, vs) + + sups += 1 if up + crit = false if down prefix + "#{name} rolled `#{roll}` against `#{vs}`" + "#{fr.empty? ? "" : " for \"#{fr.join(' ')}\""}. " + - "That is #{(!crit && sig && success) ? "an" : "a"} " + - "_#{crit ? "**critical** " : ""}" + - "#{sig && !supsig ? (success ? "excellent " : "severe ") : ""}" + - "#{supsig ? (success ? "exceptional " : "horrific ") : ""}" + - "#{success ? "success" : "failure"}_ " + - - "by `#{diff}` (#{diff / 10} Mo#{success ? "S" : "F"})." + "That is " + + (sups > 1 ? "two " : "a ") + + (sups > 0 ? "_superior_ " : "") + + (crit ? "_**critical**_ " : "") + + (success ? "success" : "failure") + + (sups > 1 ? (success ? "es" : "s") : "") end last_roll = {} @@ -45,39 +62,91 @@ bot.command(:vs, description: "Roll a d100 (0-99) against a target number, with vs = vs.to_i roll = make_roll - last_roll[event.author.id] = [roll, vs, fr] + data = [roll, vs, fr, false, false, false] + last_roll[event.author.id] = data - res = human_result(event.author.display_name, roll, vs, fr) + res = human_result(event.author.display_name, data) puts res res end bot.command(:again, description: "Repeat your last vs roll.") do |event| - roll, vs, fr = last_roll[event.author.id] + data = last_roll[event.author.id] + roll, vs, fr, flip, up, down = data if roll.nil? res = "You need to roll something first, #{event.author.display_name}!" else roll = make_roll - last_roll[event.author.id] = [roll, vs, fr] + data = [roll, vs, fr, false, false, false] + last_roll[event.author.id] = data - res = human_result(event.author.display_name, roll, vs, fr, "Repeat: ") + res = human_result(event.author.display_name, data , "Repeat: ") end puts res res end -bot.command(:moxie, description: "Perform a moxie digit swap on your last vs roll.") do |event| - roll, vs, fr = last_roll[event.author.id] +bot.command(:flipflop, description: "Swap the digits on your last vs roll.") do |event| + data = last_roll[event.author.id] + roll, vs, fr, flip, up, down = data if roll.nil? res = "You need to roll something first, #{event.author.display_name}!" + else if up + res = "You have already upgraded this success, #{event.author.display_name}." + else if up + res = "You have already downgraded this failure, #{event.author.display_name}." else roll = ((roll % 10) * 10) + (roll / 10) - last_roll[event.author.id] = [roll, vs, fr] + last_roll[event.author.id] = [roll, vs, fr, !flip, up, down] + + res = human_result(event.author.display_name, data, (flip ? "Unflip-flop: " : "Flip-flop: ")) + end + + puts res + res +end + +bot.command(:upgrade, description: "Increase the number of superiors on your last vs roll.") do |event| + data = last_roll[event.author.id] + roll, vs, fr, flip, up, down = data + + if roll.nil? + res = "You need to roll something first, #{event.author.display_name}!" + else if flip + res = "You have already flip-flopped this roll, #{event.author.display_name}." + else if down + res = "You have already downgraded this failure, #{event.author.display_name}." + else if calc_sups(roll, vs) > 1 + res = "This is already two superior successes, #{event.author.display_name}!" + else + last_roll[event.author.id] = [roll, vs, fr, flip, !up, down] + + res = human_result(event.author.display_name, data, (up ? "Unupgrade: " : "Upgrade: ")) + end + + puts res + res +end + +bot.command(:downgrade, description: " last vs roll.") do |event| + data = last_roll[event.author.id] + roll, vs, fr, flip, up, down = data + + if roll.nil? + res = "You need to roll something first, #{event.author.display_name}!" + else if flip + res = "You have already flip-flopped this roll, #{event.author.display_name}." + else if up + res = "You have already upgraded this success, #{event.author.display_name}." + else if !calc_crit(roll) + res = "This isn't a critical failure, #{event.author.display_name}." + else + last_roll[event.author.id] = [roll, vs, fr, flip, up, !down] - res = human_result(event.author.display_name, roll, vs, fr, "Moxie swap: ") + res = human_result(event.author.display_name, data, (down ? "Undowngrade: " : "Downgrade: ")) end puts res @@ -85,25 +154,25 @@ bot.command(:moxie, description: "Perform a moxie digit swap on your last vs rol end bot.command(:remind, description: "Get a reminder of the result of your last vs roll.") do |event| - roll, vs, fr = last_roll[event.author.id] + data = last_roll[event.author.id] if roll.nil? res = "You need to roll something first, #{event.author.display_name}!" else - res = human_result(event.author.display_name, roll, vs, fr, "Reminder: ") + res = human_result(event.author.display_name, data, "Reminder: ") end puts res res end -bot.command(:init, description: "Roll a d10 (1-10) and add an init modifier, with two tie-break d10 (0-9) rolloffs.") do |event, mod| +bot.command(:init, description: "Roll a d6 (1-6) and add an init modifier, with two tie-break d6 (0-5) rolloffs.") do |event, mod| mod = mod.to_i - roll = make_roll(10) + 1 + roll = make_roll(6) + 1 tot = roll + mod - d1 = make_roll(10) - d2 = make_roll(10) + d1 = make_roll(6) + d2 = make_roll(6) res = "#{event.author.display_name} rolled `#{roll}` and scored `#{tot}.#{d1}#{d2}` for initiative." puts res diff --git a/iada_v1.rb b/iada_v1.rb new file mode 100755 index 0000000..6bfa417 --- /dev/null +++ b/iada_v1.rb @@ -0,0 +1,137 @@ +#!/usr/bin/env ruby + +require 'discordrb' + +def make_roll(dX = 100) + rand(100_000_000 * dX) % dX +end + +def human_result(name, roll, vs, fr, prefix = "") + case roll + when 0 + crit = true + success = true + diff = [vs - roll, 0].max + when 99 + crit = true + success = false + diff = (vs - roll).abs + else + crit = roll.to_s[0] == roll.to_s[1] + success = roll <= vs + diff = (vs - roll).abs + end + + sig = diff >= 30 + supsig = diff >= 60 + + prefix + "#{name} rolled `#{roll}` against `#{vs}`" + + "#{fr.empty? ? "" : " for \"#{fr.join(' ')}\""}. " + + + "That is #{(!crit && sig && success) ? "an" : "a"} " + + "_#{crit ? "**critical** " : ""}" + + "#{sig && !supsig ? (success ? "excellent " : "severe ") : ""}" + + "#{supsig ? (success ? "exceptional " : "horrific ") : ""}" + + "#{success ? "success" : "failure"}_ " + + + "by `#{diff}` (#{diff / 10} Mo#{success ? "S" : "F"})." +end + +last_roll = {} + +bot = Discordrb::Commands::CommandBot.new token: ENV['DISCORD_TOKEN'], client_id: ENV['DISCORD_ID'], prefix: '!' + +bot.command(:vs, description: "Roll a d100 (0-99) against a target number, with optional comment.") do |event, vs, *fr| + vs = vs.to_i + roll = make_roll + + last_roll[event.author.id] = [roll, vs, fr] + + res = human_result(event.author.display_name, roll, vs, fr) + puts res + res +end + +bot.command(:again, description: "Repeat your last vs roll.") do |event| + roll, vs, fr = last_roll[event.author.id] + + if roll.nil? + res = "You need to roll something first, #{event.author.display_name}!" + else + roll = make_roll + last_roll[event.author.id] = [roll, vs, fr] + + res = human_result(event.author.display_name, roll, vs, fr, "Repeat: ") + end + + puts res + res +end + +bot.command(:moxie, description: "Perform a moxie digit swap on your last vs roll.") do |event| + roll, vs, fr = last_roll[event.author.id] + + if roll.nil? + res = "You need to roll something first, #{event.author.display_name}!" + else + roll = ((roll % 10) * 10) + (roll / 10) + last_roll[event.author.id] = [roll, vs, fr] + + res = human_result(event.author.display_name, roll, vs, fr, "Moxie swap: ") + end + + puts res + res +end + +bot.command(:remind, description: "Get a reminder of the result of your last vs roll.") do |event| + roll, vs, fr = last_roll[event.author.id] + + if roll.nil? + res = "You need to roll something first, #{event.author.display_name}!" + else + res = human_result(event.author.display_name, roll, vs, fr, "Reminder: ") + end + + puts res + res +end + +bot.command(:init, description: "Roll a d10 (1-10) and add an init modifier, with two tie-break d10 (0-9) rolloffs.") do |event, mod| + mod = mod.to_i + roll = make_roll(10) + 1 + tot = roll + mod + + d1 = make_roll(10) + d2 = make_roll(10) + + res = "#{event.author.display_name} rolled `#{roll}` and scored `#{tot}.#{d1}#{d2}` for initiative." + puts res + res +end + +bot.command(:unitest, description: "Perform a uniformity test for peace of mind.") do |event| + t = 10_000_000 + u = t / 100 + + a = Array.new(100, 0) + t.times { a[make_roll] += 1 } + + d2 = a.map { |o| (o - u) ** 2 } + s2 = d2.sum / 100.0 + s = s2 ** 0.5 + + res = "#{event.author.display_name} requested a uniformity test. #{t} d100s were rolled and counted. The standard deviation was #{"%.2f" % s}, which means 68% of the possible outcomes each appeared within #{s.ceil} of the expected #{u} times, 95% within #{(s*2).ceil}, and 99.7% within #{(s*3).ceil}." + puts res + res +end + +bot.command(:reseed, description: "Reseed the PRNG if you're feeling particularly superstitious about the rolls.") do |event| + srand + + res = "#{event.author.display_name} requested the PRNG be reseeded." + puts res + res +end + +bot.run |