Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions app/controllers/rollmaster/examples_controller.rb

This file was deleted.

20 changes: 20 additions & 0 deletions app/controllers/rollmaster/roll_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module ::Rollmaster
class RollController < ::ApplicationController
requires_plugin PLUGIN_NAME

# GET /rollmaster/roll
# @param [Array<String>] diceRolls array of dice rolls to be
# @return [Hash] results array of dice rolls
# @example URL /rollmaster/roll?diceRolls[]=2d6&diceRolls[]=1d20
def roll
begin
result = ::Rollmaster::DiceEngine.roll(*params[:diceRolls])
render json: { result: result }
rescue ::Rollmaster::DiceEngine::RollError => e
raise Discourse::InvalidParameters, e.message
end
end
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

Rollmaster::Engine.routes.draw do
get "/examples" => "examples#index"
get "/roll" => "roll#roll"
# define routes here
end

Expand Down
20 changes: 17 additions & 3 deletions lib/rollmaster/dice_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

require "mini_racer"

module Rollmaster
module ::Rollmaster
class DiceEngine
class RollError < StandardError
end

@mutex = Mutex.new
@ctx_init = Mutex.new
@ctx = nil
Expand All @@ -20,15 +23,26 @@ def self.roll(*diceRolls)
context = v8
result = context.call("roll", *diceRolls)
end
if result.is_a?(Hash) && result["type"] == "error"
raise Rollmaster::DiceEngine::RollError.new(result["msg"])
end
result
end

def self.attach_function(ctx)
ctx.eval <<~JS
function roll(...diceRolls) {
const roller = new rpgDiceRoller.DiceRoller;
roller.roll(...diceRolls);
return JSON.parse(JSON.stringify(roller.log))
try {
roller.roll(...diceRolls);
return JSON.parse(JSON.stringify(roller.log))
} catch (e) {
return {
type: "error",
name: e.name,
msg: e.message,
};
}
}
JS
end
Expand Down
6 changes: 6 additions & 0 deletions lib/rollmaster/handle_cooked_post_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module ::Rollmaster
class HandleCookedPostProcess
def self.process(doc, post)
# Add your processing logic here

# parse the post content

# check for existing dice rolls associated with post

# create new dice rolls for any new ones
end
end
end
4 changes: 2 additions & 2 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module ::Rollmaster

after_initialize do
# Code which should run after Rails has finished booting
# # I don't think this is needed, but it doesn't hurt to be safe
# PrettyText.reset_context()
# I don't think this is needed, but it doesn't hurt to be safe
::Rollmaster::DiceEngine.reset_context

on(:post_process_cooked) { |doc, post| ::Rollmaster::HandleCookedPostProcess.process(doc, post) }
# TODO: consider :chat_message_processed as well
Expand Down
12 changes: 10 additions & 2 deletions scripts/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ const { DiceRoller } = require("@dice-roller/rpg-dice-roller");

function roll(...diceRolls) {
const roller = new DiceRoller();
roller.roll(...diceRolls);
return roller.log;
try {
roller.roll(...diceRolls);
return roller.log;
} catch (e) {
return {
type: "error",
name: e.name,
msg: e.message,
};
}
}

const r = repl.start({ prompt: "> " });
Expand Down
20 changes: 19 additions & 1 deletion spec/lib/rollmaster/dice_engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
expect(result.size).to eq(dice_rolls.size)
expect(result.all? { |r| r.is_a?(Hash) }).to be(true)
end

it "raises a RollError for invalid dice rolls" do
dice_rolls = ["invalid_roll"]

expect { described_class.roll(*dice_rolls) }.to raise_error(Rollmaster::DiceEngine::RollError)
end

it "raises a RollError for empty dice rolls" do
dice_rolls = []

expect { described_class.roll(*dice_rolls) }.to raise_error(Rollmaster::DiceEngine::RollError)
end

it "raises a RollError for nil dice rolls" do
dice_rolls = nil

expect { described_class.roll(*dice_rolls) }.to raise_error(Rollmaster::DiceEngine::RollError)
end
end

describe ".reset_context" do
Expand Down Expand Up @@ -60,7 +78,7 @@
end
end

xdescribe ".attach_function" do
describe ".attach_function" do
it "attaches the roll function to the context" do
context = MiniRacer::Context.new
described_class.attach_function(context)
Expand Down
40 changes: 40 additions & 0 deletions spec/requests/rollmaster/roll_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

RSpec.describe Rollmaster::RollController, type: :request do
before { SiteSetting.rollmaster_enabled = true }

describe "GET /rollmaster/roll" do
let(:endpoint) { "/rollmaster/roll.json" }

context "when valid dice rolls are provided" do
it "returns the correct results" do
get "/rollmaster/roll.json", params: { diceRolls: %w[2d6 1d20] }
expect(response).to have_http_status(:ok)
json = response.parsed_body
expect(json["result"]).to be_an(Array)
expect(json["result"].size).to eq(2)
end
end

context "when no dice rolls are provided" do
it "raises an invalid parameters error" do
get "/rollmaster/roll.json"
expect(response.status).to eq(400)
end
end

context "when an invalid dice roll is provided" do
before do
allow(::Rollmaster::DiceEngine).to receive(:roll).and_raise(
::Rollmaster::DiceEngine::RollError,
"Invalid dice roll",
)
end

it "raises an invalid parameters error" do
get "/rollmaster/roll.json", params: { diceRolls: ["invalid"] }
expect(response.status).to eq(400)
end
end
end
end