Last active
October 31, 2020 17:45
-
-
Save ysakasin/65aad913ec9542a5d43b0d2dbdec89f3 to your computer and use it in GitHub Desktop.
BCDice v3でevalの戻り値を結果用インスタンスにする案
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "rands" | |
require_relative "result" | |
module BCDice | |
class Evaluator | |
class << self | |
def eval(command) | |
new(command).eval | |
end | |
end | |
def initialize(command) | |
@command = command | |
end | |
def eval(randomizer: Randomizer.new) | |
@command = Preprocessor.process(@command, self, randomizer) | |
result = AddDice.eval(self, randomizer) || eval_game_system_specific_command(randomizer) | |
return nil unless result | |
result.rands = randomizer.rands | |
result | |
end | |
private | |
def eval_game_system_specific_command(randomizer); end | |
end | |
module GameSystem | |
class Cthulhu < Evaluator | |
def eval_game_system_specific_command(randomizer) | |
m = /^CC<=(\d+)$/.match(@command) | |
return nil unless m | |
target_number = m[1].to_i | |
result = Result.new | |
dice = randomizer.roll_once(100) | |
result.condition = dice <= target_number | |
sequence = [ | |
"(#{@command})", | |
dice, | |
result.success? ? "成功" : "失敗" | |
] | |
result.text = sequence.join(" > ") | |
result | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module BCDice | |
class Randomizer | |
attr_reader :rands | |
def initialize | |
@rands = [] | |
end | |
def roll_once(sides) | |
value = Kernel.rand(sides) + 1 | |
@rands.push(value) | |
value | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Result | |
def initialize | |
@secret = false | |
@success = false | |
@failure = false | |
@critical = false | |
@fumble = false | |
end | |
attr_accessor :text, :rands | |
attr_writer :success, :failure, :critical, :fumble | |
def secret?; @secret end | |
def success?; @success end | |
def failure?; @failure end | |
def critical?; @critical end | |
def fumble?; @fumble end | |
def condition=(condition) | |
@success = condition | |
@failure = !condition | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment