Skip to content

Instantly share code, notes, and snippets.

@ysakasin
Last active August 9, 2020 17:10
Show Gist options
  • Save ysakasin/1464a15828f4beeb2988b61f924bacb9 to your computer and use it in GitHub Desktop.
Save ysakasin/1464a15828f4beeb2988b61f924bacb9 to your computer and use it in GitHub Desktop.
module BCDice
VERSION = "3.0.0b"
# @return [Class]
def self.game_system_class(id)
# ...
end
end
class BCDice::GameSystem::Base
def initialize
@randomizer = Randomizer.new
@secret = false
end
def id
self.class::ID
end
def name
self.class::NAME
end
# @return [String, nil]
def eval(command)
@secret = match_secret?(command)
res = eval_game_system_specific_command(command)
if res
return res
end
[AddDice, RerollDice, Choice].each do |klass|
common = klass.new(@randomizer, this)
res = common.eval(command)
if res
return res
end
end
return nil
end
# @return [Boolean]
def secret?
@secret
end
# @return [Array<Array<(Integer, Integer)>>]
def rand_results
@randomizer.rand_results
end
private
# @return [nil]
def eval_game_system_specific_command(command); end
end
def BCDice::GameSystem::Cthulhu < BCDice::GameSystem::Base
ID = "Cthulhu"
NAME = "クトゥルフ神話"
private
# @return [String]
# @return [nil]
def eval_game_system_specific_command(command)
if command.start_with?("CC")
return skill_check(command)
else
return nil
end
end
# @return [String]
def skill_check(command)
value = roll_d(1, 100)
m = /^CC<=(\d+)$/.match(command)
if m
target = m[1].to_i
suffix = value <= target ? "成功" : "失敗"
return ":(1D100) > #{value} > #{suffix}"
else
return ":(1D100) > #{value}"
end
end
end
class AddDice
def initialize(randomizer, game_system)
@randomizer = randomizer
@game_system = game_system
end
# @return [String]
# @return [nil]
def eval(command)
#
end
end
game_system_class = BCDice.game_system_class("Cthulhu") #=> BCDice::GameSystem::Cthulhu
game_system_class.ID #=> "Cthulhu"
game_system_class.NAME #=> "クトゥルフ神話"
game_system_class.HELP_MESSAGE #=> ...
game_system = game_system_class.new
game_system.eval("CC<=70 目星") #=> "(1D100) > 60 > 成功"
game_system.eval("1D6 SANチェック") #=> "(1D6) > 5"
game_system.eval("2B6") #=> "(2B6) > [5,2]"
game_system.secret? #=> false
game_system.id #=> "Cthulhu"
game_system.name #=> "クトゥルフ神話"
game_system.help_message #=> ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment