Skip to content

Instantly share code, notes, and snippets.

@ysakasin
Last active December 31, 2020 08:02
Show Gist options
  • Save ysakasin/11e59b01ed329bb810492b3eea9a7dde to your computer and use it in GitHub Desktop.
Save ysakasin/11e59b01ed329bb810492b3eea9a7dde to your computer and use it in GitHub Desktop.
require "strscan"
p ARGV
files = Dir[ARGV[0]]
p files
def dump(class_name, data)
ret = ""
ret += "[[ test ]]\n"
ret += "game_system = #{class_name.inspect}\n"
ret += "input = #{data[:input].inspect}\n"
ret += "output = #{data[:expected].inspect}\n"
ret += "secret = true\n" if data[:secret]
ret += "rands = #{data[:rands]}\n"
return ret
end
def format_rands(rands)
if rands.nil? || rands.empty?
return "[]"
end
body = rands.map { |r| " { sides = #{r[1]}, value = #{r[0]} }," }.join("\n")
return ["[", body, "]"].join("\n")
end
def parse(source)
scanner = StringScanner.new(source)
scanner.scan(/input:\n/)
input = scanner.scan_until(/output:/).delete_suffix("output:").strip
output = scanner.scan_until(/rand:/).delete_suffix("rand:").strip
rands = scanner.scan(/\d+\/\d+(, \d+\/\d+)/)
rands = rands&.split(", ")&.map {|x| x.split("/").map(&:to_i)} || []
{
input: input.chomp,
expected: output.sub(/#+secret dice#+/, ""),
secret: output.match?(/#+secret dice#+/),
rands: format_rands(rands)
}
end
files.each do |path|
class_name = File.basename(path, ".txt")
source = File.read(path)
dataSetSources = source
.gsub("\r\n", "\n")
.tr("\r", "\n")
.split("============================\n")
.map(&:chomp)
toml = dataSetSources.map do |dataSetSource|
data = parse(dataSetSource)
out = dump(class_name, data)
end
puts toml.join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment