Class: Adventure::Challenge

Inherits:
Object
  • Object
show all
Defined in:
lib/adventure/challenge.rb

Overview

Represents an ingame challenge and/or check.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dc, skill, **opts) ⇒ Challenge

Create a new instance of Challenge

Raises:

  • (StandardError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/adventure/challenge.rb', line 19

def initialize(dc, skill, **opts)
  @name    = opts.key?(:name) ? opts[:name].strip : UUID.new.generate
  @dc      = dc.to_i
  @skill   = skill.strip
  @success = nil
  raise StandardError, 'All minimal texts must be set.' unless opts.key?(:challenge_text) && opts.key?(:challenge_inline) && opts.key?(:challenge_button) && opts.key?(:success_text) && opts.key?(:success_inline) && opts.key?(:failure_text) && opts.key?(:failure_inline)

  @challenge_text   = opts[:challenge_text].strip
  @challenge_inline = opts[:challenge_inline].strip
  @challenge_button = opts[:challenge_button].strip
  @success_text     = opts[:success_text].strip
  @success_inline   = opts[:success_inline].strip
  @failure_text     = opts[:failure_text].strip
  @failure_inline   = opts[:failure_inline].strip
end

Instance Attribute Details

#dcObject (readonly)

Returns the value of attribute dc.



13
14
15
# File 'lib/adventure/challenge.rb', line 13

def dc
  @dc
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/adventure/challenge.rb', line 10

def name
  @name
end

#skillObject (readonly)

Returns the value of attribute skill.



16
17
18
# File 'lib/adventure/challenge.rb', line 16

def skill
  @skill
end

Instance Method Details

#buttonString

The query to show as an action / option to the player.

Returns:

  • (String)

    The query / action text to show to the player.



62
63
64
# File 'lib/adventure/challenge.rb', line 62

def button
  @challenge_button
end

#roll(roll) ⇒ Boolean

Roll for the Adventure::Challenge and save success or failure to the object.

Parameters:

  • roll (Integer)

    The result of the player's roll.

Returns:

  • (Boolean)

    true if success, false otherwise.



91
92
93
94
# File 'lib/adventure/challenge.rb', line 91

def roll(roll)
  @success = roll >= @dc
  @success
end

#success?Boolean?

Whether or not the Adventure::Challenge was succeeded, or nil if untried.

Returns:

  • (Boolean, nil)

    nil if unrolled, otherwise true if succeeded or false if not.



76
77
78
# File 'lib/adventure/challenge.rb', line 76

def success?
  @success
end

#text(inline: false) ⇒ String

Either the Adventure::Challenge's challenge text if unrolled, or result text if rolled.

Parameters:

  • inline (Boolean) (defaults to: false)

    Whether to return the inline text or the full text. Defaults to full text (i.e. false)

Returns:

  • (String)

    A String of either the challenge or the result.



49
50
51
52
53
54
55
56
57
# File 'lib/adventure/challenge.rb', line 49

def text(inline: false)
  if @success.nil?
    inline ? @challenge_inline : @challenge_text
  elsif @success
    inline ? @success_inline : @success_text
  else
    inline ? @failure_inline : @failure_text
  end
end

#to_iInteger

The Adventure::Challenge's Difficulty Class (DC).

Returns:

  • (Integer)

    The chcallenges DC - even if rolled already.



69
70
71
# File 'lib/adventure/challenge.rb', line 69

def to_i
  @dc
end

#to_sString

Either the Adventure::Challenge's challenge inline text if unrolled, or inline result text if rolled.

An alias for #text with true as parameter.

Returns:

  • (String)

    A String of either the inline challenge text or the inline result text.

See Also:



41
42
43
# File 'lib/adventure/challenge.rb', line 41

def to_s
  text inline: true
end

#unrolled?Boolean

Whether or not the Adventure::Challenge is unrolled.

Returns:

  • (Boolean)

    true if not rolled for, false otherwise.



83
84
85
# File 'lib/adventure/challenge.rb', line 83

def unrolled?
  @success.nil?
end