|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | module ::Rollmaster |
| 4 | + SELECTOR_QUERY = ".bb-rollmaster[data-notation]" |
| 5 | + |
4 | 6 | class HandleCookedPostProcess |
5 | 7 | def self.process(doc, post) |
6 | 8 | # Add your processing logic here |
7 | 9 |
|
8 | | - # parse the post content |
| 10 | + # { raw, dom }[] |
| 11 | + roll_elements = [] |
| 12 | + |
| 13 | + # parse the post content for rolls and flatten |
| 14 | + doc |
| 15 | + .css(SELECTOR_QUERY) |
| 16 | + .each do |roll_element| |
| 17 | + original_notation = roll_element.attribute("data-notation").value |
| 18 | + |
| 19 | + next if original_notation.blank? |
| 20 | + |
| 21 | + original_notation |
| 22 | + .split(/\n/) |
| 23 | + .each do |notation| |
| 24 | + if notation.strip.empty? |
| 25 | + roll_elements << { raw: "", dom: roll_element } # let us keep empty lines |
| 26 | + else |
| 27 | + roll_elements << { raw: notation, dom: roll_element } |
| 28 | + end |
| 29 | + end |
| 30 | + roll_element.content = "" # clear the original notation |
| 31 | + end |
| 32 | + |
| 33 | + return if roll_elements.empty? |
| 34 | + |
| 35 | + # { raw, dom, formatted, result, error }[] |
| 36 | + roll_elements.each do |element| |
| 37 | + notation = element[:raw] |
| 38 | + next if notation.blank? |
| 39 | + |
| 40 | + element.merge!(process_roll(notation)) |
| 41 | + end |
| 42 | + |
| 43 | + # { raw, dom, formatted, result, error, id }[] |
| 44 | + match_rolls(roll_elements, post) if post.id? |
| 45 | + save_rolls(roll_elements, post) |
9 | 46 |
|
10 | | - # check for existing dice rolls associated with post |
| 47 | + roll_elements |
| 48 | + .group_by { |e| e[:dom] } |
| 49 | + .each do |dom, rolls| |
| 50 | + content = |
| 51 | + rolls.map do |e| |
| 52 | + if e[:error] |
| 53 | + e[:raw] |
| 54 | + elsif e[:raw].empty? |
| 55 | + "" |
| 56 | + else |
| 57 | + e[:raw] + ": " + e[:result] # TODO: consider decorating with spans |
| 58 | + end |
| 59 | + end |
| 60 | + dom.content = CGI.unescapeHTML(content.join("\n")) |
| 61 | + dom["data-roll-id"] = rolls.map { |e| e[:id] }.join(",") if rolls.any? { |e| e[:id] } |
| 62 | + end |
| 63 | + |
| 64 | + true |
| 65 | + end |
| 66 | + |
| 67 | + def self.process_roll(notation) |
| 68 | + begin |
| 69 | + formatted = Rollmaster::DiceEngine.format_notation(notation).first |
| 70 | + final = Rollmaster::DiceEngine.roll(notation).first |
| 71 | + { error: false, formatted: formatted, result: final } |
| 72 | + rescue Rollmaster::DiceEngine::RollError => e |
| 73 | + Rails.logger.warn("Rollmaster: Error formatting notation for post #{post.id}: #{e.message}") |
| 74 | + { error: true, formatted: nil, result: e.message } |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def self.match_rolls(rolls, post) |
| 79 | + existing_rolls = Rollmaster::Roll.where(post_id: post.id).to_a |
| 80 | + return if existing_rolls.empty? |
| 81 | + |
| 82 | + rolls |
| 83 | + .reject { |r| r[:raw].empty? || r[:error] } |
| 84 | + .each do |roll| |
| 85 | + existing_roll_idx = |
| 86 | + existing_rolls.index { |r| r.raw == roll[:raw] || r.notation == roll[:formatted] } |
| 87 | + next if existing_roll_idx.nil? |
| 88 | + |
| 89 | + existing_roll = existing_rolls[existing_roll_idx] |
| 90 | + roll[:id] = existing_roll.id |
| 91 | + roll[:result] = existing_roll.result # use existing roll result |
| 92 | + existing_rolls.delete_at(existing_roll_idx) |
| 93 | + end |
| 94 | + end |
11 | 95 |
|
12 | | - # create new dice rolls for any new ones |
| 96 | + def self.save_rolls(rolls, post) |
| 97 | + rolls |
| 98 | + .reject { |r| r[:raw].empty? || r[:error] } |
| 99 | + .each do |roll| |
| 100 | + if roll[:id] |
| 101 | + existing_roll = Rollmaster::Roll.find(roll[:id]) |
| 102 | + existing_roll.update!(raw: roll[:raw], notation: roll[:formatted]) |
| 103 | + else |
| 104 | + new_roll = |
| 105 | + Rollmaster::Roll.create!( |
| 106 | + post_id: post.id, |
| 107 | + raw: roll[:raw], |
| 108 | + notation: roll[:formatted], |
| 109 | + result: roll[:result], |
| 110 | + ) |
| 111 | + roll[:id] = new_roll.id |
| 112 | + end |
| 113 | + end |
13 | 114 | end |
14 | 115 | end |
15 | 116 | end |
0 commit comments