Class: Radventure::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/radventure/action.rb

Overview

Represents an ingame possible action

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, skill, dc, success, failure, success_cache = [], failure_cache = []) ⇒ Action

Action constructor

Parameters:

  • name (String)

    The general name of the action

  • skill (String)

    The name of the skill to test of the action

  • dc (Integer)

    The skill’s Difficulty Class of the action

  • success (String)

    The success text of the action

  • failure (String)

    The failure text of the action

  • success_cache (Array<Item, Money>) (defaults to: [])

    A list of items and money caches available when succeeded

  • failure_cache (Array<Item, Money>) (defaults to: [])

    A list of items and money caches available when failed



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/radventure/action.rb', line 31

def initialize(name, skill, dc, success, failure, success_cache = [], failure_cache = [])
  @name          = name.strip
  @skill         = skill.strip
  @dc            = dc.to_i
  @success       = success.strip
  @failure       = failure.strip
  @status        = nil
  @success_cache = success_cache
  @failure_cache = failure_cache
  @cache         = nil
end

Instance Attribute Details

#dcInteger (readonly)

Returns The the skill’s Difficulty Class of the action.

Returns:

  • (Integer)

    The the skill’s Difficulty Class of the action



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/radventure/action.rb', line 18

class Action
  attr_reader :name, :skill, :success, :failure, :status

  # Action constructor
  #
  # @param name [String] The general name of the action
  # @param skill [String] The name of the skill to test of the action
  # @param dc [Integer] The skill's Difficulty Class of the action
  # @param success [String] The success text of the action
  # @param failure [String] The failure text of the action
  # @param success_cache [Array<Item, Money>] A list of items and money caches available when succeeded
  # @param failure_cache [Array<Item, Money>] A list of items and money caches available when failed
  # @return [Action] The Action object
  def initialize(name, skill, dc, success, failure, success_cache = [], failure_cache = [])
    @name          = name.strip
    @skill         = skill.strip
    @dc            = dc.to_i
    @success       = success.strip
    @failure       = failure.strip
    @status        = nil
    @success_cache = success_cache
    @failure_cache = failure_cache
    @cache         = nil
  end

  # Try to roll for this action
  #
  # @param roll [Integer] The result of the roll
  # @return [Boolean] Whether the check succeeded or not
  def try(roll)
    if @status.nil?
      @status = roll.to_i >= @success
      @cache  = if @status
                  @success_cache
                else
                  @failure_cache
                end
    end

    @status
  end

  # Fetch rewards, if applicable
  #
  # @return [false, Array] Returns `false` if the action was not tried, or an array, otherwise
  def rewards
    if @status.nil?
      false
    else
      @cache
    end
  end

  # Retrieve a reward (and remove it) from the available rewards list
  #
  # @return [nil, Item, Money] Returns the removed reward from the rewards list, or `nil` if no rewards match
  def get_reward(index)
    if @cache.nil?
      nil
    else
      @cache.delete_at index
    end
  end
end

#failureString (readonly)

Returns The failure text of the action.

Returns:

  • (String)

    The failure text of the action



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/radventure/action.rb', line 18

class Action
  attr_reader :name, :skill, :success, :failure, :status

  # Action constructor
  #
  # @param name [String] The general name of the action
  # @param skill [String] The name of the skill to test of the action
  # @param dc [Integer] The skill's Difficulty Class of the action
  # @param success [String] The success text of the action
  # @param failure [String] The failure text of the action
  # @param success_cache [Array<Item, Money>] A list of items and money caches available when succeeded
  # @param failure_cache [Array<Item, Money>] A list of items and money caches available when failed
  # @return [Action] The Action object
  def initialize(name, skill, dc, success, failure, success_cache = [], failure_cache = [])
    @name          = name.strip
    @skill         = skill.strip
    @dc            = dc.to_i
    @success       = success.strip
    @failure       = failure.strip
    @status        = nil
    @success_cache = success_cache
    @failure_cache = failure_cache
    @cache         = nil
  end

  # Try to roll for this action
  #
  # @param roll [Integer] The result of the roll
  # @return [Boolean] Whether the check succeeded or not
  def try(roll)
    if @status.nil?
      @status = roll.to_i >= @success
      @cache  = if @status
                  @success_cache
                else
                  @failure_cache
                end
    end

    @status
  end

  # Fetch rewards, if applicable
  #
  # @return [false, Array] Returns `false` if the action was not tried, or an array, otherwise
  def rewards
    if @status.nil?
      false
    else
      @cache
    end
  end

  # Retrieve a reward (and remove it) from the available rewards list
  #
  # @return [nil, Item, Money] Returns the removed reward from the rewards list, or `nil` if no rewards match
  def get_reward(index)
    if @cache.nil?
      nil
    else
      @cache.delete_at index
    end
  end
end

#nameString (readonly)

Returns The general name of the action.

Returns:

  • (String)

    The general name of the action



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/radventure/action.rb', line 18

class Action
  attr_reader :name, :skill, :success, :failure, :status

  # Action constructor
  #
  # @param name [String] The general name of the action
  # @param skill [String] The name of the skill to test of the action
  # @param dc [Integer] The skill's Difficulty Class of the action
  # @param success [String] The success text of the action
  # @param failure [String] The failure text of the action
  # @param success_cache [Array<Item, Money>] A list of items and money caches available when succeeded
  # @param failure_cache [Array<Item, Money>] A list of items and money caches available when failed
  # @return [Action] The Action object
  def initialize(name, skill, dc, success, failure, success_cache = [], failure_cache = [])
    @name          = name.strip
    @skill         = skill.strip
    @dc            = dc.to_i
    @success       = success.strip
    @failure       = failure.strip
    @status        = nil
    @success_cache = success_cache
    @failure_cache = failure_cache
    @cache         = nil
  end

  # Try to roll for this action
  #
  # @param roll [Integer] The result of the roll
  # @return [Boolean] Whether the check succeeded or not
  def try(roll)
    if @status.nil?
      @status = roll.to_i >= @success
      @cache  = if @status
                  @success_cache
                else
                  @failure_cache
                end
    end

    @status
  end

  # Fetch rewards, if applicable
  #
  # @return [false, Array] Returns `false` if the action was not tried, or an array, otherwise
  def rewards
    if @status.nil?
      false
    else
      @cache
    end
  end

  # Retrieve a reward (and remove it) from the available rewards list
  #
  # @return [nil, Item, Money] Returns the removed reward from the rewards list, or `nil` if no rewards match
  def get_reward(index)
    if @cache.nil?
      nil
    else
      @cache.delete_at index
    end
  end
end

#skillString (readonly)

Returns The name of the skill to test of the action.

Returns:

  • (String)

    The name of the skill to test of the action



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/radventure/action.rb', line 18

class Action
  attr_reader :name, :skill, :success, :failure, :status

  # Action constructor
  #
  # @param name [String] The general name of the action
  # @param skill [String] The name of the skill to test of the action
  # @param dc [Integer] The skill's Difficulty Class of the action
  # @param success [String] The success text of the action
  # @param failure [String] The failure text of the action
  # @param success_cache [Array<Item, Money>] A list of items and money caches available when succeeded
  # @param failure_cache [Array<Item, Money>] A list of items and money caches available when failed
  # @return [Action] The Action object
  def initialize(name, skill, dc, success, failure, success_cache = [], failure_cache = [])
    @name          = name.strip
    @skill         = skill.strip
    @dc            = dc.to_i
    @success       = success.strip
    @failure       = failure.strip
    @status        = nil
    @success_cache = success_cache
    @failure_cache = failure_cache
    @cache         = nil
  end

  # Try to roll for this action
  #
  # @param roll [Integer] The result of the roll
  # @return [Boolean] Whether the check succeeded or not
  def try(roll)
    if @status.nil?
      @status = roll.to_i >= @success
      @cache  = if @status
                  @success_cache
                else
                  @failure_cache
                end
    end

    @status
  end

  # Fetch rewards, if applicable
  #
  # @return [false, Array] Returns `false` if the action was not tried, or an array, otherwise
  def rewards
    if @status.nil?
      false
    else
      @cache
    end
  end

  # Retrieve a reward (and remove it) from the available rewards list
  #
  # @return [nil, Item, Money] Returns the removed reward from the rewards list, or `nil` if no rewards match
  def get_reward(index)
    if @cache.nil?
      nil
    else
      @cache.delete_at index
    end
  end
end

#statusBoolean? (readonly)

Returns The current status of the action of the action.

Returns:

  • (Boolean, nil)

    The current status of the action of the action



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/radventure/action.rb', line 18

class Action
  attr_reader :name, :skill, :success, :failure, :status

  # Action constructor
  #
  # @param name [String] The general name of the action
  # @param skill [String] The name of the skill to test of the action
  # @param dc [Integer] The skill's Difficulty Class of the action
  # @param success [String] The success text of the action
  # @param failure [String] The failure text of the action
  # @param success_cache [Array<Item, Money>] A list of items and money caches available when succeeded
  # @param failure_cache [Array<Item, Money>] A list of items and money caches available when failed
  # @return [Action] The Action object
  def initialize(name, skill, dc, success, failure, success_cache = [], failure_cache = [])
    @name          = name.strip
    @skill         = skill.strip
    @dc            = dc.to_i
    @success       = success.strip
    @failure       = failure.strip
    @status        = nil
    @success_cache = success_cache
    @failure_cache = failure_cache
    @cache         = nil
  end

  # Try to roll for this action
  #
  # @param roll [Integer] The result of the roll
  # @return [Boolean] Whether the check succeeded or not
  def try(roll)
    if @status.nil?
      @status = roll.to_i >= @success
      @cache  = if @status
                  @success_cache
                else
                  @failure_cache
                end
    end

    @status
  end

  # Fetch rewards, if applicable
  #
  # @return [false, Array] Returns `false` if the action was not tried, or an array, otherwise
  def rewards
    if @status.nil?
      false
    else
      @cache
    end
  end

  # Retrieve a reward (and remove it) from the available rewards list
  #
  # @return [nil, Item, Money] Returns the removed reward from the rewards list, or `nil` if no rewards match
  def get_reward(index)
    if @cache.nil?
      nil
    else
      @cache.delete_at index
    end
  end
end

#successString (readonly)

Returns The success text of the action.

Returns:

  • (String)

    The success text of the action



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/radventure/action.rb', line 18

class Action
  attr_reader :name, :skill, :success, :failure, :status

  # Action constructor
  #
  # @param name [String] The general name of the action
  # @param skill [String] The name of the skill to test of the action
  # @param dc [Integer] The skill's Difficulty Class of the action
  # @param success [String] The success text of the action
  # @param failure [String] The failure text of the action
  # @param success_cache [Array<Item, Money>] A list of items and money caches available when succeeded
  # @param failure_cache [Array<Item, Money>] A list of items and money caches available when failed
  # @return [Action] The Action object
  def initialize(name, skill, dc, success, failure, success_cache = [], failure_cache = [])
    @name          = name.strip
    @skill         = skill.strip
    @dc            = dc.to_i
    @success       = success.strip
    @failure       = failure.strip
    @status        = nil
    @success_cache = success_cache
    @failure_cache = failure_cache
    @cache         = nil
  end

  # Try to roll for this action
  #
  # @param roll [Integer] The result of the roll
  # @return [Boolean] Whether the check succeeded or not
  def try(roll)
    if @status.nil?
      @status = roll.to_i >= @success
      @cache  = if @status
                  @success_cache
                else
                  @failure_cache
                end
    end

    @status
  end

  # Fetch rewards, if applicable
  #
  # @return [false, Array] Returns `false` if the action was not tried, or an array, otherwise
  def rewards
    if @status.nil?
      false
    else
      @cache
    end
  end

  # Retrieve a reward (and remove it) from the available rewards list
  #
  # @return [nil, Item, Money] Returns the removed reward from the rewards list, or `nil` if no rewards match
  def get_reward(index)
    if @cache.nil?
      nil
    else
      @cache.delete_at index
    end
  end
end

Instance Method Details

#get_reward(index) ⇒ nil, ...

Retrieve a reward (and remove it) from the available rewards list

Returns:

  • (nil, Item, Money)

    Returns the removed reward from the rewards list, or ‘nil` if no rewards match



74
75
76
77
78
79
80
# File 'lib/radventure/action.rb', line 74

def get_reward(index)
  if @cache.nil?
    nil
  else
    @cache.delete_at index
  end
end

#rewardsfalse, Array

Fetch rewards, if applicable

Returns:

  • (false, Array)

    Returns ‘false` if the action was not tried, or an array, otherwise



63
64
65
66
67
68
69
# File 'lib/radventure/action.rb', line 63

def rewards
  if @status.nil?
    false
  else
    @cache
  end
end

#try(roll) ⇒ Boolean

Try to roll for this action

Parameters:

  • roll (Integer)

    The result of the roll

Returns:

  • (Boolean)

    Whether the check succeeded or not



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/radventure/action.rb', line 47

def try(roll)
  if @status.nil?
    @status = roll.to_i >= @success
    @cache  = if @status
                @success_cache
              else
                @failure_cache
              end
  end

  @status
end