Class: GamesParadise::ShakesAndFidgets::UserInput

Inherits:
Object
  • Object
show all
Includes:
Colours::E, Colours, Shared, ShakesAndFidgets::Colours
Defined in:
lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb

Constant Summary collapse

CREATE_HERO_ON_STARTUP =
#

CREATE_HERO_ON_STARTUP

#
true

Instance Method Summary collapse

Methods included from Shared

#calculate_cost_for_n_increase_of_attribute, #fetch_user_input, #load_yaml, #report_name, #save_character, #user_input

Methods included from Colours

#brown, #cfile, #convert_colour, #fancy, #normal, #pink, #red, #yel

Constructor Details

#initialize(run_already = true) ⇒ UserInput

#

initialize

#


40
41
42
43
44
45
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 40

def initialize(
    run_already = true
  )
  reset
  run if run_already
end

Instance Method Details

#act_on_user_input(i = @user_input) ⇒ Object

#

act_on_user_input

The input to this method is supposed to be a String.

#


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 279

def act_on_user_input(i = @user_input)
  _ = i.chomp
  _ = _.strip # Since March 2012 we strip the input.

  if _.include? ' '
    if _.include?('.') and _.include?('=') # Assume something like this: hero.name = joe
      splitted = _.split('=').map(&:strip)
      @character.send(splitted[0].split('.')[1]+'=', splitted[1])
    end
    splitted = _.split(' ')
    a = splitted[1..-1] # All arguments.
    _ = splitted[0]
    f = splitted[1] # First argument.
  else
    a = nil
  end

  case _ # case tag
  # ======================================================================= #
  # === help
  # ======================================================================= #
  when 'help','hel','he','h','?','menu'
    show_help
  # ======================================================================= #
  # === name?
  # ======================================================================= #
  when 'name?'
    char?.report_name
  # ======================================================================= #
  # === set_xp
  # ======================================================================= #
  when 'set_xp','setxp','xp'
    char?.set_xp f.to_i
    e 'Now setting the XP to '+simp(char?.xp?.to_s)+'.'
  when 'character?','hero?','?hero','feedback?',
       'show_abenteuerlust','char?','stats?','c?'
    feedback_character
  when 'set_class','setclass','class'
    set_class(f)
  when 'level?','levels?'
    show_levels
    show_current_level
  when 'zero'
    case f
    when 'abenteuerlust','1','ab'
      @character.abenteuerlust = 0
      e 'Setting Abenteuerlust back to 0.'
    else # Batch set to zero.
      char?.set_money 0
      char?.set_xp 0
    end
  when 'addgold'
    e 'Adding 2500 coins now (for debugging purposes).'
    @character.add_cash '2500'
  when 'cash?','money?','gold?'
    efancy @character.cash?
  when 'zauberladen','magic','5','05','zauber','zau'
    Zauberladen.new(char?)
  when 'ehre','fame','ehre?','fame?'
    @character.report_fame
  when 'hall_of_fame','HallofFame','hall?','halloffame'
    HallofFame.new(char?)
  when 'save','15','sav'
    save
  when 'Ehrenhalle','ehrenhalle'
    Ehrenhalle.new
  when 'tavern','tav','abenteuer','ab', # Start the adventure here.
       'go_adventure','adventure','1','taverne',
       'abent','ad'
    enter_tavern
  when '8','post_office','postoffice','post'
    e 'You enter the post office.'
    e 'You can input "help" next to get an overview over as the available'
    e 'options in the post office.'
    @post_office.menu
  when 'motivation?','abenteuerlust' # Let's find out the motivation of our hero.
    efancy @character.abenteuerlust?
  when '4','waffenladen','weapons','waffen','weapon',
       'waffe'
    Waffenladen.new(character?)
    show_help
  when 'stall'
    e 'Not yet implemented.'
  when 'all_adventures','abenteuer?','adventures?','a?'
    show_available_adventures
  when '3','stadtwache','work','worker','work?',
       'stadtw','wache'
    Stadtwache.new(char?)
    show_help
  when 'add_xp','addxp'
    add_xp(a)
  when 'cat'
    cat(a)
  when 'xp?'
    efancy @character.xp?
  when 'open','open_project_files'
    open_project_files
  when 'demotivate'
    @character.abenteuerlust = 0
  when 'url','url?'
    e 'http://s30.sfgame.de/'
  when 'hero','character','char','c','11','create'
    consider_making_character
    feedback_character
  when 'self'
    pp self
  when 'pwd'
    e Dir.pwd
  when 'mode?'
    e 'The current mode is: '+@mode
  when 'll'
    ShowDirectoryContent.do_show
  when 'test2'
    # pp @character.layers[:torso].attribute_boosts unless @character.layers[:torso].is_a?(String)
  when 'test','t','te','equip','equipment'
    e 'Testing:'
    make_character
    @character.try_to_equip # Defaults to a new armour without argument.
    @character.try_to_equip
    @character.try_to_equip
    @character.try_to_equip
    @character.try_to_equip
    @character.try_to_equip
    @character.try_to_equip
    # pp @charackter.all_armours # this fails.
  when 'toggle'
    @mode = 'debug'
  when 'debug','13','debug?'
    debug
  when 'load','14','restore','reloadc'
    load_character
  when 'reload'
    reload
  when 'q','qq'
    exit_program
  when ''
  else # Else tag.
    not_found(_)
  end
end

#add_motivation(i) ⇒ Object

#

add_motivation

Delegate into the Character class here.

#


155
156
157
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 155

def add_motivation(i)
  @character.add_motivation(i)
end

#add_xp(i) ⇒ Object

#

add_xp

#


176
177
178
179
180
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 176

def add_xp(i)
  i = i.first if i.is_a? Array
  e 'Now adding '+sfancy(i)+' XP to your hero '+@character.name+'.'
  @character.add_xp(i)
end

#bold_yellow(i = '') ⇒ Object

#

bold_yellow

#


238
239
240
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 238

def bold_yellow(i = '')
  ShakesAndFidgets.bold_yellow(i)
end

#character?Boolean Also known as: char?

#

character?

#

Returns:

  • (Boolean)


479
480
481
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 479

def character?
  @character
end

#consider_creating_heroObject

#

consider_creating_hero

#


220
221
222
223
224
225
226
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 220

def consider_creating_hero
  if CREATE_HERO_ON_STARTUP
    e 'We will create a hero now because CREATE_HERO_ON_STARTUP was '+
      'set to true.'
    make_hero
  end
end

#continually_fetch_user_inputObject

#

continually_fetch_user_input

Use this method to fetch user input from the main menu.

#


425
426
427
428
429
430
431
432
433
434
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 425

def continually_fetch_user_input
  begin
    fetch_user_input # This is defined in shared.rb
  rescue Interrupt
    exit_program
  end
  @user_input.each {|entry|
    act_on_user_input(entry)
  }
end

#create_characterObject Also known as: make_character, make_hero, consider_making_character

#

create_character

#


439
440
441
442
443
444
445
446
447
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 439

def create_character
  if @character
    # pass through in this case, as the character
    # already exists.
  else
    e 'Creating a '+simp('Hero')+' next.'
    @character = Character.new
  end
end

#debugObject

#

debug

#


71
72
73
74
75
76
77
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 71

def debug # (debug tag)
  cliner {
    pp @character
    pp @character.name if @character
    # pp @abenteuer
  }
end

#deduct_n_pilze(i) ⇒ Object

#

deduct_n_pilze

#


169
170
171
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 169

def deduct_n_pilze(i)
  @character.pilze('-'+i.to_s)
end

#enter_tavernObject

#

enter_tavern

We enter a tavern here. The tavern is a “virtual room” for now, only available via this method call.

#


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
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 188

def enter_tavern
  # ======================================================================= #
  # Intercept weary heroes here - they can drink something.
  # ======================================================================= #
  if @character.abenteuerlust == 0
    e 'Du hast keine Lust mehr auf Abenteuer.'
    e 'Willst du etwas trinken? Kostet aber einen Pilz.'
    if user_input[0,1] =~ /[yj]/
      e 'Ok, du trinkst etwas.'
      add_motivation('+20')
      deduct_n_pilze(1)
      show_abenteuerlust
    else
      e 'Du entschliesst dich nichts zu trinken.'
    end
  else
    @abenteuer = ShakesAndFidgets::Abenteuer.new(false) # bl $SHAKES/abenteuer.rb
    create_character if @character.nil? # Safeguard. Create it if it does not exist yet.
    old_xp = @character.xp?
    _ = @abenteuer.select_an_adventure(3, @character.motivation) # Allow the user to select an adventure.
    if _ == '[Cancelled]'
      e _ unless @character.motivation == 0
    else
      @abenteuer.start_abenteuer(@character)
      save if @character.xp? > old_xp # Only save here when he gained XP.
    end
  end
end

#exit_programObject

#

exit_program

#


82
83
84
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 82

def exit_program # exit tag
  exit
end

#feedback_characterObject

#

feedback_character

Feedback information about the character.

#


488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 488

def feedback_character
  if @character
    e 'Feedbacking some information about the character.'
    e
    @character.report_name
    @character.report_xp
    @character.report_abenteuerlust
    @character.report_fame
    @character.report_level
    @character.report_stats
    @character.report_money
    @character.report_race
    @character.report_character_class
    e
  else
    e 'We can not feedback anything about the character'
    e 'because none was created so far.'
  end
end

#initialize_armoursObject

#

initialize_armours

#


146
147
148
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 146

def initialize_armours
  @armours = Armour.new
end

#initialize_variablesObject

#

initialize_variables

#


64
65
66
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 64

def initialize_variables
  @mode = 'normal' # Can be either normal or debug mode. Is called from within run()
end

#load_characterObject

#

load_character

Load the character here. Right now we only load the XP set.

#


128
129
130
131
132
133
134
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 128

def load_character
  e 'Loading from '+cfile+STORE_WHERE+normal
  dataset = YAML.load_file(STORE_WHERE)
  @character = character.new
  @character.set_xp(dataset)
  feedback_character
end

#not_found(i) ⇒ Object

#

not_found

Use this method when a command was not found.

#


269
270
271
272
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 269

def not_found(i)
  e 'Not found: `'+sfancy(i)+'`'
  e 'You can do "?" to show the help section.'
end

#open_in_editor(i) ⇒ Object

#

open_in_editor

#


118
119
120
121
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 118

def open_in_editor(i)
  _ = MAIN_EDITOR+' '+i
  esystem _
end

#open_project_filesObject

#

open_project_files (open tag)

Use this to open some project files. There will be a slight delay on purpose if we use bluefish.

#


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 92

def open_project_files
  base_dir = BASE_DIR
  array = %w(
    abenteuer.rb
    arena/arena.rb
    armour.rb
    character.rb
    menu.rb
    opponent.rb
    shared.rb
    user_input.rb
    stadtwache.rb
    TODO/TODO_FOR_SHAKES_AND_FIDGETS.md
  )
  # ======================================================================= #
  # === Open all these files next.
  # ======================================================================= #
  array.each {|file|
    open_in_editor(base_dir+file)
    sleep 0.6 if MAIN_EDITOR == 'bluefish'
  }
end

#reloadObject

#

reload

#


139
140
141
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 139

def reload # reload tag
  initialize_armours
end

#resetObject

#

reset

#


50
51
52
53
54
55
56
57
58
59
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 50

def reset
  # ======================================================================= #
  # === @character
  # ======================================================================= #
  @character = nil
  # ======================================================================= #
  # === @post_office
  # ======================================================================= #
  @post_office = ShakesAndFidgets::PostOffice.new
end

#revObject

#

rev

#


511
512
513
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 511

def rev
  ShakesAndFidgets.rev
end

#runObject

#

run (run tag)

General run method.

#


254
255
256
257
258
259
260
261
262
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 254

def run
  initialize_variables
  display_help # Show help options on startup.
  initialize_armours # Easier helper.
  consider_creating_hero
  loop {
    continually_fetch_user_input
  }
end

#saveObject

#

save (save tag)

#


245
246
247
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 245

def save # Save method. Save the character dataset. (save tag)
  save_character(@character) # This method is defined in shared.rb
end

#set_character_class(i = '') ⇒ Object Also known as: set_class

#

set_character_class

#


471
472
473
474
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 471

def set_character_class(i = '')
  e 'Now setting to class '+sfancy(i.to_s)+'.' 
  @character.set_character_class(i) # Delegate to class Character.
end

#show_abenteuerlustObject

#

show_abenteuerlust

#


162
163
164
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 162

def show_abenteuerlust
  eparse 'Die Abenteuerlust ist nun: '+@character.abenteuerlust?.to_s
end

#show_available_adventuresObject

#

show_available_adventures

#


231
232
233
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 231

def show_available_adventures
  Abenteuer.show_available_adventures
end

#show_current_levelObject

#

show_current_level

#


463
464
465
466
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 463

def show_current_level
  e 'The character currently has '+simp(char?.xp?.to_s)+' XP.'
  @character.report_level
end

#show_helpObject Also known as: display_help

#

show_help (help tag)

#


518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 518

def show_help
  # ======================================================================= #
  # === Instantiate a new Menu next
  # ======================================================================= #
  menu = ShakesAndFidgets::Menu.new
  e; report_name; e 'The following options are available for user input:'+N
  e
  menu_entries = menu.entries
  n_menu_entries = menu_entries.size
  menu_entries.each_with_index {|entry, index|
    index += 1
    option = '%-12s' % entry[0] 
    description = entry[1]
    index = index.to_s.rjust(2)
    _ = rev+'# ('+rev+bold_yellow(index)+') '+description
    e '  - '+option+' '+brown(_)
  }
  e '  - Hero         '+brown('# ')+rev+'('+pink(n_menu_entries+1)+rev+
    ') Create your hero (or feedback the current hero).'
  e '  - Debug        '+brown('# ')+rev+'('+pink(n_menu_entries+2)+rev+
    ') Feedback debug information.'
  e '  - Load         '+brown('# ')+rev+'('+pink(n_menu_entries+3)+rev+
    ') load the character dataset'
  e '  - Save         '+brown('# ')+rev+'('+pink(n_menu_entries+4)+rev+
    ') to save your character to file.'
  e '  - Test         '+brown('# ')+rev+'('+pink(n_menu_entries+5)+rev+
    ') test your scripts'+N
  e
end

#show_levelsObject Also known as: show_xp_levels

#

show_levels

Show XP levels through this method here.

#


456
457
458
# File 'lib/games_paradise/shakes_and_fidgets/user_input/user_input.rb', line 456

def show_levels
  e XP_LEVELS_AS_ARRAY.map {|entry| '  '+entry }.join
end