Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
どどんとふのチャットログからダイスロール部分を抽出するスクリプト
require 'yaml'
require 'json'
lines = ARGF.readlines(chomp: true)
json_lines = lines.map { |line|
left_bracket_index = line.index('[')
line[left_bracket_index..-1]
}
hashes = json_lines.map { |line|
JSON.parse(line)
}
messages = hashes.map{ |hash|
hash[1]['message']
}
sharps = messages.grep(/\A###/)
inner_hashes = sharps.map { |message|
JSON.parse(message.sub(/\A###.+?###/, ''))
}
chat_messages = inner_hashes.map{ |hash|
hash['chatMessage']
}.compact
multi_lines = chat_messages.select { |m|
m.chomp.lines.length > 1
}
dice_commands = multi_lines.
map { |m|
second_line = m.lines[1]
matches = second_line.match(/\A(.+?):/)
game_id = matches ? matches[1].strip : nil
[game_id, m.lines[0].strip]
}.
reject { |game_id, _| game_id.nil? }.
sort_by { |game_id, _| game_id }
game_id_messages = dice_commands.reduce({}) { |acc, (game_id, message)|
acc[game_id] ||= []
acc[game_id] << message
acc
}
YAML.dump(game_id_messages, $stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment