Module: LanguageCards::Controllers::Game

Extended by:
Helpers::GameHelper, Helpers::ViewHelper
Defined in:
lib/language_cards/controllers/game.rb

Class Method Summary collapse

Methods included from Helpers::GameHelper

calc_score

Methods included from Helpers::ViewHelper

clear, divider, draw, humanize, t

Class Method Details

.process(cards, mode) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/language_cards/controllers/game.rb', line 19

def process(cards, mode)
  ic = struct_data.new(cards, mode)
  ic.get_input
  {
    correct: ic.valid?,
    last: ic.valid? ? ic.correct_msg : ic.incorrect_msg
  }
end

.render(correct:, incorrect:, title:, timer:, last:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/language_cards/controllers/game.rb', line 8

def render(correct:, incorrect:, title:, timer:, last:)
  _score = t('Game.ScoreMenu.Score') + ": %0.2d%%" % calc_score(correct, incorrect)
  _timer = [((t('Timer.Timer') + ": " + timer.ha) if timer.time?), nil, timer.h]
  _mexit = t 'Menu.Exit'

  view = File.expand_path('../view/game.erb', __dir__).
    

.struct_dataObject



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
# File 'lib/language_cards/controllers/game.rb', line 28

def struct_data
  Struct.new(:collection, :mode) do
    def input
      @input 
    end

    def get_input
      @input ||= CLI.ask("#{I18n.t('Game.TypeThis')}: #{display}")
    end

    def card
      @card ||= collection.sample
    end

    def display
      "#{card}"
    end

    def expected
      case mode
      when :translate
        card.translation
      when :typing_practice
         "#{card}" 
      else
        raise "Invalid mode in Game Controller!"
      end
    end

    def correct_msg
      "#{I18n.t('Game.Correct')} #{input} = #{display}"
    end

    def incorrect_msg
      output = "#{I18n.t('Game.Incorrect')} #{input} != #{display}"
      output << " #{I18n.t('Game.Its')} #{expected}" if mode == :translate
      output
    end

    def valid?
      !!(expected == input)
    end
  end
end