diff options
author | David Gay <david@davidgay.org> | 2021-06-06 19:06:46 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-06-06 19:06:46 -0400 |
commit | e37402ff309311a14d7dd666d0d8b29517504017 (patch) | |
tree | 3d6604805e9004bc0c37130f451376e79a68c989 /app/lib | |
parent | 3622126380278d9bed8ea0e1e05a0bd1ea040596 (diff) |
Leviathans
Diffstat (limited to 'app/lib')
-rw-r--r-- | app/lib/activity_processor.rb | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/app/lib/activity_processor.rb b/app/lib/activity_processor.rb index c0eaab1..11f0db0 100644 --- a/app/lib/activity_processor.rb +++ b/app/lib/activity_processor.rb @@ -39,6 +39,18 @@ class ActivityProcessor when "xp" puts "Result: #{result}" handle_xp_result(result) + when "monster_spawn" + raise TooManyWoundsError unless @character.can_fight? + next if rand > (result[:chance] || 1) + + @results.push({ type: "br" }) + + monster_spawn = MonsterSpawn.where(location: Location.find_by_gid(result[:location])).select(&:alive?).first + raise MonsterSpawnError unless monster_spawn + + @results.push({ type: type, monster_spawn: monster_spawn }) + resolve_combat_with(monster_spawn) + break when "monster" raise TooManyWoundsError unless @character.can_fight? next if rand > (result[:chance] || 1) @@ -129,6 +141,10 @@ class ActivityProcessor @character.stop_activity @results.replace([{ type: "error", message: "You can't fight in your condition. You'll have to heal a wound." }]) + rescue MonsterSpawnError + @character.stop_activity + @results.replace([{ type: "error", + message: "There are no living leviathans here." }]) end private @@ -193,9 +209,16 @@ class ActivityProcessor end def resolve_combat_with(mon) + + monster_spawn = nil + if mon.is_a? MonsterSpawn + monster_spawn = mon + mon = monster_spawn.monster + end + char = @character char_hp = @character.max_hp - mon_hp = mon.max_hp + mon_hp = monster_spawn.present? ? monster_spawn.remaining_hp : mon.max_hp combat_message = ->(msg) { @results.push({ type: "message", body: "[#{char_hp}/#{char.max_hp}] #{msg}" }) } char_initiative = roll(20) + char.speed mon_initative = roll(20) + mon.speed @@ -244,6 +267,13 @@ class ActivityProcessor combat_message.call("#{target.name} evaded #{actor.name}'s attack.") end if char_hp < 1 || mon_hp < 1 + if monster_spawn + hp_lost = monster_spawn.remaining_hp - mon_hp + if hp_lost > 0 + monster_spawn.monster_spawn_combats.create(monster_spawn: monster_spawn, character: char, + hp_lost: monster_spawn.remaining_hp - mon_hp) + end + end if char_hp < 1 @results.push({ type: "message", body: "You were defeated! You retreat, wounded." }) char.wounds += 1 @@ -256,16 +286,21 @@ class ActivityProcessor end else @results.push({ type: "message", body: "You slew the #{mon.name}." }) - mon.whatnot[:awards]&.each do |award_data| - case award_data[:type] - when "title" - handle_title_result(award_data) - when "xp" - handle_xp_result(award_data) - when "item" - handle_item_result(award_data) - else - raise "Invalid award type string (#{award_data[:type]})" + if monster_spawn + char.stop_activity + return + else + mon.whatnot[:awards]&.each do |award_data| + case award_data[:type] + when "title" + handle_title_result(award_data) + when "xp" + handle_xp_result(award_data) + when "item" + handle_item_result(award_data) + else + raise "Invalid award type string (#{award_data[:type]})" + end end end end |