the_game 0.0.6 → 0.1.a

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.
data/README.rdoc CHANGED
@@ -18,6 +18,6 @@ Run the executable +the_game+.
18
18
  -l, --license Display license and exit
19
19
  -v, --version Display version and exit
20
20
 
21
- == Copyright
21
+ == License
22
22
  :include:LICENSE.txt
23
23
 
data/bin/the_game CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- #:include:the_game/bin/the_game
2
+
3
3
  require 'the_game'
4
4
  require 'the_game/cli'
5
5
 
data/lib/the_game/cli.rb CHANGED
@@ -1,11 +1,14 @@
1
1
  require 'optparse'
2
2
 
3
- # Command-line interface
3
+ # TheGame::CLI is the_game's command-line interface.
4
+ #
5
+ # The executable +the_game+ calls this method. Here is the source of +the_game+:
6
+ # :include:bin/the_game
4
7
  module TheGame::CLI
5
8
  class << self
6
9
  def start!(args = ARGV)
7
10
  @game = TheGame::Game.new
8
- @game_mode = :help
11
+ @game_mode = nil
9
12
 
10
13
  opts = OptionParser.new(args) do |opts|
11
14
  opts.banner = "Usage: the_game [options] [mode]"
@@ -27,7 +30,7 @@ module TheGame::CLI
27
30
  when :play then @game.start!
28
31
  when :license then puts TheGame::LICENSE
29
32
  when :version then puts "the_game v#{TheGame::Version::STRING}"
30
- else puts opts end
33
+ else puts opts end # Display help message
31
34
  end
32
35
  end
33
36
  end
@@ -17,13 +17,15 @@ class TheGame::Command
17
17
  end
18
18
  end
19
19
 
20
- def method_missing(m, g, a)
21
- "#{m.to_s.command_sanitize}: command not found"
20
+ def method_missing(m, g, a); "#{m.to_s.command_sanitize}: command not found"; end
21
+
22
+ def available_methods
23
+ methods(false).select{ |m| m.match(/^_/) }.map{ |m| m.to_s.command_sanitize }
22
24
  end
23
25
 
24
- def _args(g, a)
25
- a.args.inspect
26
- end
26
+ def _raise(g, a); raise FuntimeError, a; end
27
+
28
+ def _args(g, a); a.args.inspect; end
27
29
 
28
30
  def _echo(g, a); a; end
29
31
 
@@ -33,8 +35,7 @@ class TheGame::Command
33
35
  "Usage: set [key] [value]"
34
36
  else
35
37
  key = a.words.first.to_s.downcase
36
- r = g.set(key, value)
37
- r == true ? "#{key.capitalize} set to #{value.inspect}" : r
38
+ pretty_rescue("#{key.capitalize} set to #{value.inspect}") { g.set(key, value) }
38
39
  end
39
40
  end
40
41
 
@@ -55,30 +56,20 @@ class TheGame::Command
55
56
  end
56
57
  end
57
58
 
58
- def _help(g, a)
59
- commands = methods(false).select{|m|m.match(/^_/)}.map{|m|m.to_s.command_sanitize}
60
-
61
- "Commands available: [ #{commands.join(', ')} ]"
62
- end
59
+ def _help(g, a); "Commands available: [ #{available_methods.join(', ')} ]"; end
63
60
 
64
- def _version(g, a)
65
- TheGame::Version::STRING
66
- end
61
+ def _version(g, a); "the_game v#{TheGame::Version::STRING}"; end
67
62
 
68
- def _license(g, a)
69
- TheGame::LICENSE
70
- end
63
+ def _license(g, a); TheGame::LICENSE; end
71
64
 
72
- def _game(g, a)
73
- g.inspect
74
- end
65
+ def _game(g, a); g.inspect; end
75
66
 
76
- def _save(g, a)
77
- g.save
78
- end
67
+ def _save(g, a); pretty_rescue { g.save; "Game saved." }; end
79
68
 
80
- def _quit(g, a)
81
- exit
82
- end
69
+ def _read_savefile(g, a); pretty_rescue { g.read_savefile }; end
70
+
71
+ def _load(g, a); pretty_rescue { g.load; "Game loaded." }; end
72
+
73
+ def _quit(g, a); exit; end
83
74
  end
84
- end
75
+ end
@@ -0,0 +1 @@
1
+ class FuntimeError < StandardError; end
data/lib/the_game/game.rb CHANGED
@@ -8,6 +8,7 @@ class TheGame::Game
8
8
  def initialize
9
9
  TheGame.initialize!
10
10
  @created_at = Time.now
11
+ @saved_at = Time.now
11
12
  @name = "anonymous"
12
13
  @level = 1
13
14
  end
@@ -15,36 +16,34 @@ class TheGame::Game
15
16
  def yaml_dump
16
17
  YAML.dump({
17
18
  'created_at' => @created_at,
19
+ 'saved_at' => @saved_at,
18
20
  'name' => @name,
19
- 'level' => @level,
20
- 'config' => {},
21
- 'the_game' => {
22
- 'version' => TheGame::Version::STRING
23
- }
21
+ 'level' => @level
24
22
  })
25
23
  end
24
+
25
+ def load(savefile = TheGame::SAVEFILE_PATH)
26
+ yaml = YAML.load(read_savefile(savefile))
27
+ @created_at = yaml['created_at']
28
+ @saved_at = yaml['saved_at']
29
+ @name = yaml['name']
30
+ @level = yaml['level']
31
+ end
26
32
 
27
33
  def save(savefile = TheGame::SAVEFILE_PATH)
28
- r = false
29
- begin
30
- Zlib::GzipWriter.open(savefile, Zlib::BEST_COMPRESSION) { |gz| gz << yaml_dump }
31
- rescue => e
32
- r = e
33
- end
34
- r || "Game saved."
34
+ @saved_at = Time.now
35
+ Zlib::GzipWriter.open(savefile, Zlib::BEST_COMPRESSION) { |gz| gz << yaml_dump }
35
36
  end
36
-
37
- # Try set name [NAME] and set level [VALUE]
37
+
38
+ def read_savefile(savefile = TheGame::SAVEFILE_PATH)
39
+ Zlib::GzipReader.open(savefile) { |gz| gz.read }
40
+ end
41
+
38
42
  def set(key, value)
39
- begin
40
- case key.downcase.to_sym
41
- when :name then @name = value
42
- when :level then @level = value
43
- else raise ArgumentError, "Cannot set #{key}!" end
44
- rescue Exception => e
45
- return e
46
- end
47
- true
43
+ case key.downcase.to_sym
44
+ when :name then @name = value
45
+ when :level then @level = value
46
+ else raise ArgumentError, "Cannot set #{key}!" end
48
47
  end
49
48
 
50
49
  # Names are green,
@@ -55,14 +54,18 @@ class TheGame::Game
55
54
  end
56
55
 
57
56
  def start!
58
- #TheGame.opts.parse!
59
57
  puts "~ the_game.rb v#{TheGame::Version::STRING} ~\nuse `help` for available commands"
60
58
 
61
59
  trap(:INT) { puts; exit }
62
60
 
63
61
  loop do
64
- output = TheGame::Command.send(self, ::Readline.readline(prompt, true))
62
+ input = ::Readline.readline(prompt, true)
63
+ output = begin
64
+ TheGame::Command.send(self, input)
65
+ rescue => e
66
+ e.pp
67
+ end
65
68
  puts output unless output.blank?
66
69
  end
67
70
  end
68
- end
71
+ end
@@ -0,0 +1,5 @@
1
+ module TheGame::Helpers::ExceptionHelpers
2
+ def pp
3
+ "\e[31mError: #{inspect}\e[0m"
4
+ end
5
+ end
@@ -1,4 +1,9 @@
1
1
  require 'the_game/helpers'
2
+ require 'the_game/helpers/string'
3
+ require 'the_game/helpers/object'
4
+ require 'the_game/helpers/exception'
5
+
6
+ include TheGame::Helpers
2
7
 
3
8
  class String #:nodoc:
4
9
  include TheGame::Helpers::StringHelpers
@@ -6,4 +11,8 @@ end
6
11
 
7
12
  class Object #:nodoc:
8
13
  include TheGame::Helpers::ObjectHelpers
9
- end
14
+ end
15
+
16
+ class Exception #:nodoc:
17
+ include TheGame::Helpers::ExceptionHelpers
18
+ end
@@ -2,7 +2,18 @@
2
2
  # require 'the_game/helpers/import'
3
3
  # will bring include these helpers within their respective classes.
4
4
 
5
- module TheGame::Helpers; end
6
-
7
- require 'the_game/helpers/string'
8
- require 'the_game/helpers/object'
5
+ module TheGame::Helpers
6
+ def pretty_rescue(default_value = false, &b)
7
+ return_value = default_value
8
+ begin
9
+ return_value = yield if block_given?
10
+ rescue => error
11
+ return_value = error.pp
12
+ end
13
+ if default_value
14
+ return_value || default_value
15
+ else
16
+ return_value
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ class TheGame::Item
2
+ attr_accessor :name
3
+
4
+ def initialize(name = "Item")
5
+ @name = name
6
+ end
7
+ end
@@ -1,11 +1,9 @@
1
1
  module TheGame
2
2
  LICENSE = "the_game Copyright (C) 2012 Shigeki Kitajima
3
-
4
3
  <http://shigeki.99k.org/the_game/>
5
4
 
6
5
  This program is open source: you can redistribute it and/or modify it under the
7
6
  terms of the MIT License. This program is distributed in the hope that it will
8
7
  be useful, but WITHOUT ANY WARRANTY.
9
-
10
8
  <http://opensource.org/licenses/MIT/>"
11
9
  end
@@ -1,11 +1,14 @@
1
- raise "Sorry, only works with ruby >= 1.9 right now." if RUBY_VERSION < "1.9"
1
+ if RUBY_VERSION < "1.9"
2
+ puts "Sorry, the_game only works with ruby >= 1.9 right now."
3
+ exit
4
+ end
2
5
 
3
6
  module TheGame
4
7
  module Version
5
- STRING = "0.0.6"
8
+ STRING = "0.1.a"
6
9
  ARRAY = STRING.split('.')
7
10
  MAJOR = ARRAY[0]
8
11
  MINOR = ARRAY[1]
9
12
  BUILD = ARRAY[2]
10
13
  end
11
- end
14
+ end
data/lib/the_game.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'the_game/version'
2
2
  require 'the_game/license'
3
+ require 'the_game/exception'
3
4
  require 'the_game/helpers/import'
5
+ require 'the_game/item'
4
6
  require 'the_game/game'
5
7
  require 'the_game/command'
6
8
 
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: the_game
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
5
- prerelease:
4
+ version: 0.1.a
5
+ prerelease: 4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Shigeki Kitajima
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-07 00:00:00.000000000 Z
12
+ date: 2012-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shoulda
16
- requirement: &17844180 !ruby/object:Gem::Requirement
16
+ requirement: &15848100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *17844180
24
+ version_requirements: *15848100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &17843140 !ruby/object:Gem::Requirement
27
+ requirement: &15847620 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *17843140
35
+ version_requirements: *15847620
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &17842440 !ruby/object:Gem::Requirement
38
+ requirement: &15847140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.6.4
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *17842440
46
+ version_requirements: *15847140
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rcov
49
- requirement: &17841660 !ruby/object:Gem::Requirement
49
+ requirement: &15846660 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *17841660
57
+ version_requirements: *15846660
58
58
  description: the_game is a text-based game written in Ruby, but it doesn't do much
59
59
  right now.
60
60
  email: shigeki.kitajima@gmail.com
@@ -71,11 +71,14 @@ files:
71
71
  - lib/the_game.rb
72
72
  - lib/the_game/cli.rb
73
73
  - lib/the_game/command.rb
74
+ - lib/the_game/exception.rb
74
75
  - lib/the_game/game.rb
75
76
  - lib/the_game/helpers.rb
77
+ - lib/the_game/helpers/exception.rb
76
78
  - lib/the_game/helpers/import.rb
77
79
  - lib/the_game/helpers/object.rb
78
80
  - lib/the_game/helpers/string.rb
81
+ - lib/the_game/item.rb
79
82
  - lib/the_game/license.rb
80
83
  - lib/the_game/version.rb
81
84
  homepage: http://shigeki.99k.org/the_game/
@@ -93,13 +96,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
96
  version: '0'
94
97
  segments:
95
98
  - 0
96
- hash: -3503081812645725962
99
+ hash: 901862298871238973
97
100
  required_rubygems_version: !ruby/object:Gem::Requirement
98
101
  none: false
99
102
  requirements:
100
- - - ! '>='
103
+ - - ! '>'
101
104
  - !ruby/object:Gem::Version
102
- version: '0'
105
+ version: 1.3.1
103
106
  requirements: []
104
107
  rubyforge_project:
105
108
  rubygems_version: 1.8.13