Class: LanguageCards::Controllers::Game

Inherits:
ApplicationController show all
Includes:
Helpers::GameHelper
Defined in:
lib/language_cards/controllers/game.rb

Instance Method Summary collapse

Methods included from Helpers::GameHelper

#calc_score

Methods inherited from ApplicationController

#initialize

Methods included from Helpers::ViewHelper

#clear, #divider, #draw, #humanize, #snake, #t, #wordwrap

Constructor Details

This class inherits a constructor from LanguageCards::Controllers::ApplicationController

Instance Method Details

#process(cards, mode) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/language_cards/controllers/game.rb', line 16

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
# 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'

  super(binding)
end

#struct_dataObject



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

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

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

    def card
      @card ||= card_set.sample.current
    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.join(', ')}" if mode == :translate
      output
    end

    def valid?
      card_set.match? input
    end
  end
end