Class: GamesAndRpgParadise::ShakesAndFidgets::Adventure

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

Constant Summary collapse

DEFAULT_ADVENTURE_DESCRIPTION =
#

DEFAULT_ADVENTURE_DESCRIPTION

#
'Die nackte Priesterin.'
FILE_ADVENTURE =
#

FILE_ADVENTURE

#
GamesAndRpgParadise.project_base_directory?+
'games/shakes_and_fidgets/yaml/adventure.yml'
CANCELLED =
#

CANCELLED

#
'[Cancelled]'

Constants included from Base::Extensions::Colours

Base::Extensions::Colours::ARRAY_AVAILABLE_KONSOLE_COLOURS

Constants included from CommonExtensions

CommonExtensions::CONTROL_C_CODE, CommonExtensions::N

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::Extensions::Colours

ecomment, #efancy, #eparse, #forestgreen, #gold, #grey, #lightblue, #mediumseagreen, #mediumslateblue, #peru, #rev, sdir, sfancy, #sfile, simp, #teal, #yellow

Methods included from Base::Extensions::CommandlineArguments

#commandline_arguments?, #filter_away_commandline_arguments, #first_argument?, #first_non_hyphened_argument?, #set_commandline_arguments

Methods included from CommonExtensions

#cat, #cd, #cliner, #copy_file, #delete, #dirname_but_retains_the_trailing_slash, #disable_colours, #ensure_that_the_log_directory_exists, #esystem, #get_user_input, #infer_the_namespace, #is_on_roebe?, #log_dir?, #mkdir, #mkdir_then_cd_into_it, #mv, #namespace?, #opne, #opnn, #project_base_directory?, #project_image_directory?, #project_yaml_directory?, #rds, #register_sigint, #remove_this_directory, #rename_file, #reset_the_internal_hash, #return_pwd, #return_today, #touch_file, #wrap, #write_what_into

Constructor Details

#initialize(run_already = true) ⇒ Adventure

#

initialize

#


55
56
57
58
59
60
61
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 55

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

#

GamesAndRpgParadise::ShakesAndFidgets::Adventure.show_available_adventures

#


42
43
44
45
46
47
48
49
50
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 42

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)


366
367
368
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 366

def character?
  @character
end

#count_down(i = @dauer) ⇒ Object

#

count_down

Use this method to count down.

#


411
412
413
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 411

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

#dataset?Boolean Also known as: dataset

#

dataset

#

Returns:

  • (Boolean)


113
114
115
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 113

def dataset?
  @adventure
end

#debugObject

#

debug

#


318
319
320
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 318

def debug
  pp self
end

#let_character_fight_adventure_monsterObject

#

let_character_fight_adventure_monster

#


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

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.

#


344
345
346
347
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 344

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)


359
360
361
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 359

def money?
  @money
end

#padded_display(title, value) ⇒ Object

#

padded_display

#


256
257
258
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 256

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.

#


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

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

#


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

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.

#


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
230
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 128

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.

#


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

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

#


373
374
375
376
377
378
379
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 373

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

#


92
93
94
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 92

def set_description(i)
  @description = i
end

#set_money_reward(i) ⇒ Object

#

set_money_reward

#


352
353
354
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 352

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

#set_motivation_cost(i = 10) ⇒ Object

#

set_motivation_cost

#


99
100
101
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 99

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

#set_name(i) ⇒ Object

#

set_name

#


85
86
87
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 85

def set_name(i)
  @name = i
end

#set_xp_reward(i) ⇒ Object

#

set_xp_reward

#


106
107
108
# File 'lib/games_and_rpg_paradise/games/shakes_and_fidgets/adventure/adventure.rb', line 106

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.

#


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

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