Sha256: 581ff9e829bfa36a11c84079883edc98297bafa33f47a6d3fe6b1b93738cc1cc

Contents?: true

Size: 1.78 KB

Versions: 8

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require 'cureutils/logic/base_logic'

# Singleton class for cure echo
class EchoLogic < BaseLogic
  module EchoMode
    TRANSFORM = 1
    ATTACK = 2
  end

  def initialize
    super
    @cure_name = 'echo'
    @message_mode = EchoMode::TRANSFORM
    @style_priority = []
  end

  def precure(cure_name = false)
    @cure_name = cure_name || 'echo'
  end

  def msg_transform(flag = true)
    @message_mode = EchoMode::TRANSFORM if flag
  end

  def msg_attack(flag = false)
    @message_mode = EchoMode::ATTACK if flag
  end

  def nosleep(flag = true)
    Rubicure::Girl.sleep_sec = 1
    Rubicure::Girl.sleep_sec = 0 if flag
  end

  def style(style)
    @style_priority << style.to_sym if style
  end

  def print_results
    return 1 unless existing_precure?

    precure = Cure.send(@cure_name.to_sym)
    if @message_mode == EchoMode::TRANSFORM
      original_transform(precure)
    elsif @message_mode == EchoMode::ATTACK
      precure = original_transform(precure)
      precure.attack!
    end
    0
  end

  private

  def original_transform(precure)
    return precure.transform! unless transformable?(precure)

    chosen_style = @style_priority.find do |s|
      precure.transform_styles.include?(s)
    end
    if chosen_style
      precure.transform! chosen_style
    else
      default_style, _entity = precure.transform_styles.first
      precure.transform! default_style
    end
  end

  def transformable?(precure)
    return false if precure.transform_calls.nil?

    return true if precure.key?(:transform_styles)

    false
  end

  def existing_precure?
    found_precure = Rubicure::Girl.config.find do |k, _v|
      k == @cure_name.to_sym
    end
    if found_precure
      true
    else
      @err.puts "No such precure #{@cure_name}"
      false
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cureutils-1.7.0 lib/cureutils/logic/echo_logic.rb
cureutils-1.6.0 lib/cureutils/logic/echo_logic.rb
cureutils-1.5.0 lib/cureutils/logic/echo_logic.rb
cureutils-1.4.0 lib/cureutils/logic/echo_logic.rb
cureutils-1.3.1 lib/cureutils/logic/echo_logic.rb
cureutils-1.3.0 lib/cureutils/logic/echo_logic.rb
cureutils-1.2.0 lib/cureutils/logic/echo_logic.rb
cureutils-1.1.1 lib/cureutils/logic/echo_logic.rb