language_cards 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63589a48bd2f0c2326af779dcf874f2ccef9f1a7
4
- data.tar.gz: 5cc7ad11580b6c00b5419479d140f04dd9ad9e46
3
+ metadata.gz: f22e96585dc1b1e68ac22cdf4c2f3a4952e0c89b
4
+ data.tar.gz: ae89a8be6614e9707d4ac400bd1a87535718ae63
5
5
  SHA512:
6
- metadata.gz: ecad707c3a93bca4f1185838436ee397d7118dc86d876d4ba539dc46c3074c1891c8a3442944c2f91d8eaa675120369b5f32896f6faaa73964acd4ffed258f86
7
- data.tar.gz: c847ce04ab37c5f9a5888288aa0526c845c7d8b76683bc9312ca54c53e5214fd8bef451e75ae3c2c5ca14806dfa7e013e990fae0fcb567a4ace0cf33e04b2468
6
+ metadata.gz: 8ff61fd64bf7e765c593e642799d2a869a47f40cc620fb3b71d27af34f722cbdc548b4861f24cf7b74c5cb7942c2fe374c2600f0a20a62c6f8cf47b17e9019b4
7
+ data.tar.gz: 8170a20139eeb403cb5b99b361e0edffb0a64e39788e90100a5f30f55fa6e709c08ed33dde30626e444a63e3b35d6541105acc7138d6ee243b229540197b42ee
@@ -0,0 +1,66 @@
1
+ module LanguageCards
2
+ module Controllers
3
+ module Game
4
+ class << self
5
+ include Helpers::ViewHelper
6
+ include Helpers::GameHelper
7
+
8
+ def render(correct:, incorrect:, title:, timer:, last:)
9
+ _score = t('Game.ScoreMenu.Score') + ": %0.2d%" % calc_score(correct, incorrect)
10
+ _timer = [((t('Timer.Timer') + ": " + timer.ha) if timer.time?), nil, timer.h]
11
+ _mexit = t 'Menu.Exit'
12
+
13
+ view = ERB.new(IO.read(File.expand_path('../view/game.erb', __dir__)))
14
+ view.result(binding)
15
+ end
16
+
17
+ def process(card_collection, mode)
18
+ ic = struct_data.new(card_collection, mode.peek)
19
+ ic.get_input
20
+ {
21
+ correct: ic.valid?,
22
+ last: ic.valid? ? ic.correct_msg : ic.incorrect_msg
23
+ }
24
+ end
25
+
26
+ def struct_data
27
+ Struct.new(:collection, :mode) do
28
+ def input
29
+ @input
30
+ end
31
+
32
+ def get_input
33
+ @input ||= CLI.ask("#{I18n.t('Game.TypeThis')} #{collection.mapped_as}: #{display}")
34
+ end
35
+
36
+ def comp_bitz
37
+ @comp_bitz ||= collection.rand
38
+ end
39
+
40
+ def display
41
+ comp_bitz.display
42
+ end
43
+
44
+ def expected
45
+ comp_bitz.expected
46
+ end
47
+
48
+ def correct_msg
49
+ "#{I18n.t('Game.Correct')} #{input} = #{display}"
50
+ end
51
+
52
+ def incorrect_msg
53
+ output = "#{I18n.t('Game.Incorrect')} #{input} != #{display}"
54
+ output << " #{I18n.t('Game.Its')} #{expected}" if mode == :translate
55
+ output
56
+ end
57
+
58
+ def valid?
59
+ collection.correct?(input, comp_bitz)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,23 @@
1
+ module LanguageCards
2
+ module Controllers
3
+ module MainMenu
4
+ class << self
5
+ include Helpers::ViewHelper
6
+
7
+ def render(courses:, mode:)
8
+ _title = t 'Menu.Title'
9
+ _select = t 'Menu.Choose'
10
+ _mode = case mode.peek
11
+ when :translate then t 'Menu.ModeTranslate'
12
+ when :typing then t 'Menu.ModeTyping'
13
+ end
14
+ _courses = courses.each.with_index.map {|item,index| "#{index + 1}: #{item}" }
15
+ _mexit = t 'Menu.Exit'
16
+
17
+ view = ERB.new(IO.read(File.expand_path('../view/main_menu.erb', __dir__)))
18
+ view.result(binding)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ module LanguageCards
2
+ module Helpers
3
+ module GameHelper
4
+ def calc_score c, i # correct, incorrect
5
+ (0.001+c.to_i)*100.0/(c.to_i+i.to_i+0.001)*1.0
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ module LanguageCards
2
+ module Helpers
3
+ module ViewHelper
4
+ def divider
5
+ '~' * SUBMENUWIDTH
6
+ end
7
+
8
+ def t str
9
+ I18n.t str
10
+ end
11
+
12
+ def draw left=nil, center=nil, right=nil
13
+ width = SUBMENUWIDTH
14
+ str = left.to_s
15
+ str = str + center.to_s.rjust(width/2 - str.length + center.to_s.length/2)
16
+ str + right.to_s.rjust(width - str.length)
17
+ end
18
+
19
+ def clear
20
+ printf ::LanguageCards::ESC::CLEAR
21
+ end
22
+ end
23
+ end
24
+ end
@@ -24,11 +24,12 @@ module LanguageCards
24
24
  end
25
25
 
26
26
  end
27
+ # Recursive Builder
27
28
  @CARDS = CardCollection.new @CARDS
28
29
  end
29
30
 
30
31
  def start
31
- UserInterface.new.start(@CARDS)
32
+ UserInterface.new(@CARDS).start
32
33
  end
33
34
  end
34
35
  end
@@ -1,55 +1,21 @@
1
1
  require_relative 'timer'
2
+ require_relative 'helpers/view_helper'
3
+ require_relative 'helpers/game_helper'
4
+ require_relative 'controllers/main_menu'
5
+ require_relative 'controllers/game'
6
+ require 'erb'
2
7
 
3
8
  module LanguageCards
4
9
  class UserInterface
5
- def initialize
6
- @last = nil
10
+ include Helpers::ViewHelper
11
+ include Controllers
12
+ def initialize cards
13
+ @cards = cards
14
+ @courses = cards.classes
7
15
  @mode = [:translate, :typing].cycle
8
- @title = ""
9
- end
10
-
11
- def main_menu(courses:)
12
- title = I18n.t 'Menu.Title'
13
- select = I18n.t 'Menu.Choose'
14
- mode = case @mode.peek
15
- when :translate then I18n.t('Menu.ModeTranslate')
16
- when :typing then I18n.t('Menu.ModeTyping')
17
- end
18
- mexit = I18n.t 'Menu.Exit'
19
-
20
- <<-MAINMENU
21
- #{'~' * SUBMENUWIDTH}
22
- #{title}#{('v' + VERSION).rjust(SUBMENUWIDTH - title.length)}
23
- #{'~' * SUBMENUWIDTH}
24
- #{select}#{(I18n.t('Menu.GameMode') + mode).rjust(SUBMENUWIDTH - select.length)}
25
-
26
- #{ courses.each.with_index.map {|item,index| "#{index + 1}: #{item}\n" }.join.chop }
27
-
28
- #{mexit}#{("m: " + I18n.t('Menu.ToggleGameMode')).rjust(SUBMENUWIDTH - mexit.length)}
29
- #{'~' * SUBMENUWIDTH}
30
- MAINMENU
31
- end
32
-
33
- def score_menu(correct:, incorrect:)
34
- score = "#{I18n.t 'Game.ScoreMenu.Score'}: #{correct.to_i} #{I18n.t 'Game.ScoreMenu.OutOf'} #{correct.to_i + incorrect.to_i}"
35
- timer = @timer.time? ? (I18n.t('Timer.Timer') + ": " + @timer.ha) : ""
36
- timer = timer + @timer.h.rjust(SUBMENUWIDTH - timer.length)
37
- title = @title.to_s
38
- <<-SCOREMENU
39
- #{'~' * SUBMENUWIDTH}
40
- #{title.rjust(SUBMENUWIDTH/2+title.length/2)}
41
- #{'~' * SUBMENUWIDTH}
42
- #{timer}
43
- #{'~' * SUBMENUWIDTH}
44
- #{score + I18n.t('Menu.Exit').rjust(SUBMENUWIDTH - score.length)}
45
-
46
- #{@last}
47
- #{'~' * SUBMENUWIDTH}
48
- SCOREMENU
49
16
  end
50
17
 
51
- def start(cards)
52
- @courses = cards.classes
18
+ def start
53
19
  clear
54
20
 
55
21
  CLI.say SPLASH_SCREEN
@@ -59,24 +25,29 @@ SCOREMENU
59
25
  loop do
60
26
  clear
61
27
 
62
- CLI.say main_menu(courses: courses)
28
+ CLI.say MainMenu.render courses: courses, mode: mode
63
29
 
64
30
  value = CLI.ask("")
65
31
 
66
- next @mode.next if value =~ /\Am\z/i
32
+ next mode.next if value =~ /\Am\z/i
67
33
  value = value.to_i - 1 rescue next
68
34
 
69
- @last = nil
35
+ last = nil
70
36
  if (0..courses.length-1).include? value
71
37
  collection = cards.select_collection(courses(value))
72
- @title = collection.name
73
- @timer = Timer.new
38
+ timer = Timer.new
74
39
  begin
75
40
  loop do
76
41
  clear
77
- @timer.mark
78
- CLI.say score_menu(correct: @correct, incorrect: @incorrect)
79
- game_logic(collection)
42
+ timer.mark
43
+ CLI.say Game.render correct: @correct,
44
+ incorrect: @incorrect,
45
+ title: collection.name,
46
+ timer: timer,
47
+ last: last
48
+ result = Game.process(collection, mode)
49
+ result[:correct] ? correct : incorrect
50
+ last = result[:last]
80
51
  end
81
52
  rescue SystemExit, Interrupt
82
53
  end
@@ -88,64 +59,17 @@ SCOREMENU
88
59
  end
89
60
 
90
61
  private
91
- def clear
92
- printf ::LanguageCards::ESC::CLEAR
93
- end
94
-
95
- IC=Struct.new(:collection, :mode) do
96
- def input
97
- @input
98
- end
99
-
100
- def get_input
101
- @input ||= CLI.ask("#{I18n.t('Game.TypeThis')} #{collection.mapped_as}: #{display}")
102
- end
103
-
104
- def comp_bitz
105
- @comp_bitz ||= collection.rand
106
- end
107
-
108
- def display
109
- comp_bitz.display
110
- end
111
-
112
- def expected
113
- comp_bitz.expected
114
- end
115
-
116
- def correct_msg
117
- "#{I18n.t('Game.Correct')} #{input} = #{display}"
118
- end
119
-
120
- def incorrect_msg
121
- output = "#{I18n.t('Game.Incorrect')} #{input} != #{display}"
122
- output << " #{I18n.t('Game.Its')} #{expected}" if mode == :translate
123
- output
124
- end
125
-
126
- def valid?
127
- collection.correct?(input, comp_bitz)
128
- end
129
- end
130
-
131
- def game_logic(c)
132
- ic = IC.new(c, @mode.peek)
133
- ic.get_input
134
- ic.valid? ? last_was_correct(ic) : last_was_incorrect(ic)
135
- end
136
-
137
- def last_was_correct(ic)
62
+ attr_reader :mode, :cards
63
+ def correct
138
64
  @correct = @correct.to_i + 1
139
- @last = ic.correct_msg
140
65
  end
141
66
 
142
- def last_was_incorrect(ic)
67
+ def incorrect
143
68
  @incorrect = @incorrect.to_i + 1
144
- @last = ic.incorrect_msg
145
69
  end
146
70
 
147
71
  def courses(value = nil)
148
- courses = @courses.select {|c| detect_course_mode(c) == @mode.peek }
72
+ courses = @courses.select {|c| detect_course_mode(c) == mode.peek }
149
73
  value ? courses[value] : courses
150
74
  end
151
75
 
@@ -1,3 +1,3 @@
1
1
  module LanguageCards
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,9 @@
1
+ <%= divider %>
2
+ <%= draw nil, title %>
3
+ <%= divider %>
4
+ <%= draw(*_timer) %>
5
+ <%= divider %>
6
+ <%= draw (if correct || incorrect then _score end), nil, _mexit %>
7
+
8
+ <%= last %>
9
+ <%= divider %>
@@ -0,0 +1,9 @@
1
+ <%= divider %>
2
+ <%= draw(_title, nil, 'v' + VERSION) %>
3
+ <%= divider %>
4
+ <%= draw(_select, nil, I18n.t('Menu.GameMode') + _mode) %>
5
+ <% _courses.each do |course| %>
6
+ <%= course.chomp %><% end %>
7
+
8
+ <%= draw(_mexit, nil, ("m: " + I18n.t('Menu.ToggleGameMode'))) %>
9
+ <%= divider %>
data/locales/en.yml CHANGED
@@ -14,7 +14,6 @@ en:
14
14
  Its: it's
15
15
  ScoreMenu:
16
16
  Score: Your score is
17
- OutOf: out of
18
17
  Timer:
19
18
  Timer: Timer
20
19
  AverageSeconds: second average
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: language_cards
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark
@@ -106,11 +106,17 @@ files:
106
106
  - lib/language_cards/card_collection.rb
107
107
  - lib/language_cards/comp_bitz.rb
108
108
  - lib/language_cards/comparator.rb
109
+ - lib/language_cards/controllers/game.rb
110
+ - lib/language_cards/controllers/main_menu.rb
111
+ - lib/language_cards/helpers/game_helper.rb
112
+ - lib/language_cards/helpers/view_helper.rb
109
113
  - lib/language_cards/language_cards.rb
110
114
  - lib/language_cards/mappings.rb
111
115
  - lib/language_cards/timer.rb
112
116
  - lib/language_cards/user_interface.rb
113
117
  - lib/language_cards/version.rb
118
+ - lib/language_cards/view/game.erb
119
+ - lib/language_cards/view/main_menu.erb
114
120
  - locales/en.yml
115
121
  homepage: http://github.com/danielpclark/language_cards
116
122
  licenses: