Class: GamesParadise::ShakesAndFidgets::Adventure

Inherits:
Base
  • Object
show all
Defined in:
lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb

Constant Summary collapse

DEFAULT_ADVENTURE_DESCRIPTION =
#

DEFAULT_ADVENTURE_DESCRIPTION

#
'Die nackte Priesterin.'
FILE_ADVENTURE =
#

FILE_ADVENTURE

#
GamesParadise::PROJECT_BASE_DIRECTORY+'shakes_and_fidgets/yaml/adventure.yml'
CANCELLED =
#

CANCELLED

#
'[Cancelled]'

Constants inherited from Base

Base::CONTROL_C_CODE, Base::N

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#cat, #commandline_arguments?, #efancy, #eparse, #first_argument?, #forestgreen, #gold, #lightblue, #lightgreen, #mediumorchid, #mediumslateblue, #opnn, #peru, #register_sigint, #rev, #royalblue, #set_commandline_arguments, #sfile, #steelblue, #teal, #tomato, #yellow

Methods included from BaseModule

#cliner, #commandline_arguments?, #first_argument?, #infer_the_namespace, #namespace?, #rename_file, #reset_the_internal_hash, #return_pwd, #set_commandline_arguments

Constructor Details

#initialize(run_already = true) ⇒ Adventure

#

initialize

#


54
55
56
57
58
59
60
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 54

def initialize(
    run_already = true
  )
  reset
  run_already = false if run_already.to_s.include? 'dont'
  run if run_already
end

Class Method Details

.show_available_adventuresObject

#

GamesParadise::ShakesAndFidgets::Adventure.show_available_adventures

#


41
42
43
44
45
46
47
48
49
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 41

def self.show_available_adventures
  e 'These adventures are available:'
  _ = ShakesAndFidgets::Adventure.new(:dont_run_yet)
  _.reset
  _.dataset.each_with_index {|(key, *), index|
    e '  '+simp(((index+1).to_s).rjust(3))+' '+sfancy(key)
  }
  e 'Thus, we have a total of '+_.dataset.size.to_s+' entries available.'
end

Instance Method Details

#character?Boolean Also known as: character

#

character?

#

Returns:

  • (Boolean)


365
366
367
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 365

def character?
  @character
end

#count_down(i = @dauer) ⇒ Object

#

count_down

Use this method to count down.

#


410
411
412
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 410

def count_down(i = @dauer)
  AsciiParadise::AsciiCounter.new(i, PADDING_TO_USE)
end

#dataset?Boolean Also known as: dataset

#

dataset

#

Returns:

  • (Boolean)


112
113
114
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 112

def dataset?
  @adventure
end

#debugObject

#

debug

#


317
318
319
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 317

def debug
  pp self
end

#let_character_fight_adventure_monsterObject

#

let_character_fight_adventure_monster

#


324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 324

def let_character_fight_adventure_monster
  monster_is_dead = true
  monster_is_dead = false if rand(53) == 1 # hack for now.
  # fight @character, Opponent.new # Find a new opponent for him here.
  if monster_is_dead
    e N+sfancy(@character.name)+' hat das Adventure bestanden!'
    @character.add_xp @reward
  else
    e N+'Oh nein!'
    e 'Das Monster hat '+sfancy(@character.name)+normal+' besiegt. :('
    e 'Es gibt daher keine XP für diesen Kampf.'
  end
end

#load_adventure(i = FILE_ADVENTURE) ⇒ Object

#

load_adventure

This method will also assign to the @adventure variable.

#


343
344
345
346
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 343

def load_adventure(i = FILE_ADVENTURE)
  @adventure = YAML.load_file(i) # Keeps all Adventures.
  return @adventure
end

#money?Boolean Also known as: money

#

money?

#

Returns:

  • (Boolean)


358
359
360
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 358

def money?
  @money
end

#padded_display(title, value) ⇒ Object

#

padded_display

#


255
256
257
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 255

def padded_display(title, value)
  e blue+title.strip.ljust(15)+rev+(value.to_s.strip.rjust(21))
end

#report_adventure_datasetObject

#

report_adventure_dataset

Use the following method to get more information about an Adventure.

#


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 236

def report_adventure_dataset
  result = N+blue+'Titel des Adventures: '+rev
  if Object.const_defined? :Konsole
    result << Konsole.italic(@name)
  else
    result << @name
  end
  e result
  e red+N+@description # We use a newline here, makes it nicer to read IMO.
  e # A spacer.
  e blue+('Dauer des Adventure:').ljust(24)+rev+@dauer.to_s.rjust(3)+' Sekunden'
  padded_display('XP Reward: ',    @reward.to_s+' XP')
  padded_display('Money Reward: ', @money.to_s+' silver coins')
  padded_display('Motivation: ',   '-'+@motivation_cost.to_s)
end

#resetObject

#

reset

#


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 65

def reset
  # ======================================================================= #
  # === @adventures
  # ======================================================================= #
  @adventures = [] # This keeps our adventures.
  # ======================================================================= #
  # === @be_interactive
  # ======================================================================= #
  @be_interactive = false # if true we are in interactive mode.
  # ======================================================================= #
  # === @character
  # ======================================================================= #
  @character = nil
  load_adventure # This has to be done only once, actually.
end

#select_an_adventure(choose_n_adventures = 3, optional_motivation_of_the_hero = 100) ⇒ Object Also known as: start_interactively

#

select_an_adventure

This interactive method allows you to fetch user input and specifically assign an adventure. We must keep in mind that we only want to fetch unique adventures - if the adventure already exists in our hash, we won’t add it.

The first argument to this method tells us how many adventures we want to have available in our options array.

#


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 127

def select_an_adventure(
    choose_n_adventures             = 3,
    optional_motivation_of_the_hero = 100
  )
  report_name; e 'You enter the Tavern.'
  @adventures.clear # Empty the list of @adventures before we can continue.
  if optional_motivation_of_the_hero == 0
    e 'You feel too tired to adventure today.'
    return CANCELLED
  else
    loop {
      new_adventure = create_adventure(:random)
      # =================================================================== #
      # Fill up the list of available adventures next.
      # =================================================================== #
      @adventures << new_adventure unless @adventures.include? new_adventure
      # pp @adventures.include? Adventure 
      break if @adventures.size == choose_n_adventures
    }
    cliner
    @adventures.each_with_index {|adventure, index|
      e # A leading newline is good.
      i = index+1
      padding_to_use = '  ' # Whether we use padding or not.
      name_of_the_adventure = adventure.keys.first
      duration = adventure[name_of_the_adventure]['Dauer']
      # =================================================================== #
      # We get the description next.
      # =================================================================== #
      description = adventure[name_of_the_adventure]['Beschreibung']
      # =================================================================== #
      # Next display the header of this adventure:
      # =================================================================== #
      if Object.const_defined? :Konsole
        name_of_the_adventure = Konsole.italic(name_of_the_adventure)+Konsole.rev
      end
      # =================================================================== #
      # Report the name of the adventure finally.
      # =================================================================== #
      e padding_to_use+sfancy(i.to_s)+'  '+name_of_the_adventure+
        ' [Duration: '+duration.to_s+']'
      e # A newline after we showed the name of the adventure.
      # =================================================================== #
      # Show the description of the Adventure next:
      # =================================================================== #
      if description.encoding.to_s == 'UTF-8'
        description = description.force_encoding('iso-8859-1')
      end
      begin
        splitted = description.split("\n")
        splitted.map {|entry|
          '     '+entry
        }.each {|entry| e entry } # Pad it up a little here.
      rescue ArgumentError => error # We assume an Encoding problem has happened here.
        cliner {
          pp error
          pp description
          pp description.encoding.to_s
          pp splitted.each {|entry|
            p entry.class
          }
        }if splitted
      end
    }; e; cliner
    # Here the user will have to make a choice:
    e 'Please pick one of these now (or enter a '+
      sfancy('non-number')+' to cancel):'
    user_input = get_user_input # Fetch the original input here.
    if user_input =~ /^\d+$/ # Userinput is a valid number.
      @be_interactive = true
      # =================================================================== #
      # Ok, the user may input something such as '3'. This means
      # he picks the 3rd adventure.
      # =================================================================== #
      n_adventures_available = @adventures.size # How many are available.
      user_input = user_input.to_i # Now it is an Integer.
      user_input = 1 if user_input < 1 # We always default to 1 here if below 1.
      if user_input > n_adventures_available # Ok user picked too high
        user_input = n_adventures_available
      end
      # =================================================================== #
      # Finally we deduct 1 because ruby Arrays start to count at 0.
      # =================================================================== #
      user_input -= 1
      title = @adventures[user_input].keys.first
      data  = @adventures[user_input][title] # Get a pointer to the data.
      dauer = data['Dauer']
      # =================================================================== #
      # Data now holds the required dataset about the adventure.
      # =================================================================== #
      return create_adventure( # This method is defined in this file here.
        title, # We will set all the required attributes.
        dauer,
        data['Erfahrung'],
        data['Belohnung'],
        data['Adventurelust'],
        data['Beschreibung'] 
      )
    else # User wants to cancel here.
      return CANCELLED
    end
  end
end

#set_character(i = nil) ⇒ Object Also known as: set_charakter

#

set_character

This method assigns to the @character variable.

#


264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 264

def set_character(i = nil)
  if i
    if i.is_a? Character
      @character = i
    else
      raise 'Illegal object passed. Please use '+
          'only class Character here.'
    end
  else
    @character = nil
  end
end

#set_dauer(i) ⇒ Object Also known as: set_delay

#

set_dauer

#


372
373
374
375
376
377
378
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 372

def set_dauer(i)
  if i.to_s.include? '-' # Assume a Range in this case then.
    splitted = i.to_s.strip.split('-').map(&:to_i)
    i = rand(splitted.first..splitted.last) # Grab a random entry here.
  end
  @dauer = i.to_i # n Sekunden.
end

#set_description(i) ⇒ Object

#

set_description

#


91
92
93
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 91

def set_description(i)
  @description = i
end

#set_money_reward(i) ⇒ Object

#

set_money_reward

#


351
352
353
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 351

def set_money_reward(i)
  @money = i # In silver coins.
end

#set_motivation_cost(i = 10) ⇒ Object

#

set_motivation_cost

#


98
99
100
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 98

def set_motivation_cost(i = 10)
  @motivation_cost = i
end

#set_name(i) ⇒ Object

#

set_name

#


84
85
86
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 84

def set_name(i)
  @name = i
end

#set_xp_reward(i) ⇒ Object

#

set_xp_reward

#


105
106
107
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 105

def set_xp_reward(i) # in Xp
  @reward = i
end

#start_adventure(character = @character) ⇒ Object Also known as: start, run

#

start_adventure (run tag)

This starts the Adventure. You must pass the character doing the adventure as argument.

#


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/games_paradise/shakes_and_fidgets/adventure/adventure.rb', line 386

def start_adventure(
    character = @character
  )
  create_adventure(:random) unless @be_interactive
  set_character(character)
  e 'Das '+sfancy('Abenteuer ')+'"'+simp(@name.to_s)+'" startet nun.'
  report_adventure_dataset
  e # A newline, since as of February 2016.
  count_down(@dauer) # Before doing anything, we count down.
  # Ok, delay finished at this point. Now we find out whether the character
  # has passed the adventure or not, by having him fight a monster. If
  # that monster is defeated, the character is granted XP. The motivation
  # is deducated no matter what though.
  @character.deduct_motivation @motivation_cost
  @character.add_money(money?)
  let_character_fight_adventure_monster
end