Class: RubyWarrior::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_warrior/game.rb

Instance Method Summary collapse

Instance Method Details

#current_levelObject

levels



166
167
168
# File 'lib/ruby_warrior/game.rb', line 166

def current_level
  @current_level ||= profile.current_level
end

#final_reportObject



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ruby_warrior/game.rb', line 174

def final_report
  if profile.calculate_average_grade && !Config.practice_level
    report = ""
    report << R18n.t.game.average_grade_for_tower(Level.grade_letter(profile.calculate_average_grade))
    profile.current_epic_grades.keys.sort.each do |level|
      report << "  #{R18n.t.level.number(level)} #{Level.grade_letter(profile.current_epic_grades[level])}\n"
    end
    report << R18n.t.level.to_practice
    report
  end
end

#go_back_to_normal_modeObject



123
124
125
126
127
128
# File 'lib/ruby_warrior/game.rb', line 123

def go_back_to_normal_mode
  profile.enable_normal_mode
  prepare_next_level
  UI.puts R18n.t.mode.back_to_normal
  UI.puts R18n.t.readme.see_updated(profile.directory_name)
end

#make_game_directoryObject



28
29
30
31
32
33
34
35
# File 'lib/ruby_warrior/game.rb', line 28

def make_game_directory
  if UI.ask(R18n.t.directory.not_found)
    Dir.mkdir(Config.path_prefix + '/rubywarrior')
  else
    UI.puts R18n.t.directory.cannot_continue
    exit
  end
end

#new_profileObject



145
146
147
148
149
150
# File 'lib/ruby_warrior/game.rb', line 145

def new_profile
  profile = Profile.new
  profile.tower_path = UI.choose(R18n.t.game.tower, towers).path
  profile.warrior_name = UI.request(R18n.t.warrior.enter_name)
  profile
end

#next_levelObject



170
171
172
# File 'lib/ruby_warrior/game.rb', line 170

def next_level
  @next_level ||= profile.next_level
end

#play_current_levelObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby_warrior/game.rb', line 69

def play_current_level
  continue = true
  current_level.load_player
  UI.puts R18n.t.level.starting(current_level.number)
  current_level.play
  if current_level.passed?
    if next_level.exists?
      UI.puts R18n.t.stairs.found
    else
      UI.puts R18n.t.end_of_game
      continue = false
    end
    current_level.tally_points
    if profile.epic?
      UI.puts final_report if final_report && !continue
    else
      request_next_level
    end
  else
    continue = false
    UI.puts R18n.t.level.failed(current_level.number)
    if !Config.skip_input? && current_level.clue && UI.ask(R18n.t.level.clues)
      UI.puts current_level.clue.hard_wrap
    end
  end
  continue
end

#play_epic_modeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_warrior/game.rb', line 37

def play_epic_mode
  Config.delay /= 2 if Config.delay # speed up UI since we're going to be doing a lot here
  profile.current_epic_score = 0
  profile.current_epic_grades = {}
  if Config.practice_level
    @current_level = @next_level = nil
    profile.level_number = Config.practice_level
    play_current_level
  else
    playing = true
    while playing
      @current_level = @next_level = nil
      profile.level_number += 1
      playing = play_current_level
    end
    profile.save # saves the score for epic mode
  end
end

#play_normal_modeObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_warrior/game.rb', line 56

def play_normal_mode
  if Config.practice_level
    UI.puts R18n.t.practice_level.unable_not_in_epic_mode
  else
    if current_level.number.zero?
      prepare_next_level
      UI.puts R18n.t.first_level_generated(profile.directory_name)
    else
      play_current_level
    end
  end
end

#prepare_epic_modeObject



117
118
119
120
121
# File 'lib/ruby_warrior/game.rb', line 117

def prepare_epic_mode
  profile.enable_epic_mode
  profile.level_number = 0
  profile.save # this saves score too
end

#prepare_next_levelObject



111
112
113
114
115
# File 'lib/ruby_warrior/game.rb', line 111

def prepare_next_level
  next_level.generate_player_files
  profile.level_number += 1
  profile.save # this saves score and new abilities too
end

#profileObject



141
142
143
# File 'lib/ruby_warrior/game.rb', line 141

def profile
  @profile ||= choose_profile
end

#profile_pathsObject



137
138
139
# File 'lib/ruby_warrior/game.rb', line 137

def profile_paths
  Dir[Config.path_prefix + '/rubywarrior/**/.profile']
end

#profilesObject

profiles



133
134
135
# File 'lib/ruby_warrior/game.rb', line 133

def profiles
  profile_paths.map { |profile| Profile.load(profile) }
end

#request_next_levelObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby_warrior/game.rb', line 97

def request_next_level
  if !Config.skip_input? && (next_level.exists? ? UI.ask(R18n.t.level.continue_next) : UI.ask(R18n.t.level.continue_epic_mode))
    if next_level.exists?
      prepare_next_level
      UI.puts R18n.t.readme.see_updated(profile.directory_name)
    else
      prepare_epic_mode
      UI.puts R18n.t.to_play_epic_mode
    end
  else
    UI.puts R18n.t.level.staying_on_current
  end
end

#startObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_warrior/game.rb', line 5

def start
  UI.puts R18n.t.welcome
  
  if File.exist?(Config.path_prefix + '/.profile')
    @profile = Profile.load(Config.path_prefix + '/.profile')
  else
    if File.exist?(Config.path_prefix + "/ruby-warrior")
      FileUtils.mv(Config.path_prefix + '/ruby-warrior', Config.path_prefix + '/rubywarrior')
    end
    make_game_directory unless File.exist?(Config.path_prefix + '/rubywarrior')
  end
  
  if profile.epic?
    if profile.level_after_epic?
      go_back_to_normal_mode
    else
      play_epic_mode
    end
  else
    play_normal_mode
  end
end

#tower_pathsObject



159
160
161
# File 'lib/ruby_warrior/game.rb', line 159

def tower_paths
  Dir[File.expand_path('../../../towers/*', __FILE__)]
end

#towersObject

towers



155
156
157
# File 'lib/ruby_warrior/game.rb', line 155

def towers
  tower_paths.map { |path| Tower.new(path) }
end