matthewtodd-settlers 0.1.1 → 0.1.2

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/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
4
  s.name = 'settlers'
5
- s.version = '0.1.1'
5
+ s.version = '0.1.2'
6
6
  s.summary = "Provides a simple command-line executable for playing Robb Thomas' JSettlers game."
7
7
  s.files = FileList['[A-Z]*', 'bin/*', 'lib/**/*.rb', 'resources/**/*']
8
8
  s.executables = ['settlers']
@@ -11,7 +11,7 @@ spec = Gem::Specification.new do |s|
11
11
  end
12
12
 
13
13
  desc 'Generate a gemspec file'
14
- task :gemspec do
14
+ task :default do
15
15
  File.open("#{spec.name}.gemspec", 'w') do |f|
16
16
  f.write spec.to_ruby
17
17
  end
@@ -1,3 +1,3 @@
1
+ require 'settlers/java_command'
1
2
  require 'settlers/jar'
2
- require 'settlers/runner'
3
3
  require 'settlers/game'
@@ -1,26 +1,34 @@
1
1
  module Settlers
2
2
  class Game
3
- def initialize(port=8880, server_delay=2)
4
- @host, @port, @server_delay, @runner = 'localhost', port, server_delay, Runner.new
3
+ DEFAULT_PORT = 8880
4
+ MAXIMUM_SERVER_CONNECTIONS = 4
5
+ SERVER_STARTUP_DELAY = 2
6
+ NO_PASSWORD = "''"
7
+
8
+ def initialize(port=DEFAULT_PORT)
9
+ @host, @port = 'localhost', port
5
10
  end
6
11
 
7
12
  def play
8
- @runner.background server.command('soc.server.SOCServer', @port, 4, 'root', ''); sleep @server_delay.to_i
9
- @runner.background server.command('soc.robot.SOCRobotClient', @host, @port, 'Leonardo', '')
10
- @runner.background server.command('soc.robot.SOCRobotClient', @host, @port, 'Humperdink', '')
11
- @runner.background server.command('soc.robot.SOCRobotClient', @host, @port, 'Elwood', '')
12
- @runner.foreground client.command('soc.client.SOCPlayerClient', @host, @port)
13
- @runner.clean_up
13
+ server.start(@port, MAXIMUM_SERVER_CONNECTIONS, 'root', NO_PASSWORD); sleep SERVER_STARTUP_DELAY
14
+ robot.start(@host, @port, 'Leonardo', NO_PASSWORD)
15
+ robot.start(@host, @port, 'Humperdink', NO_PASSWORD)
16
+ robot.start(@host, @port, 'Elwood', NO_PASSWORD)
17
+ human.run(@host, @port)
14
18
  end
15
19
 
16
20
  private
17
21
 
18
22
  def server
19
- Jar.new('JSettlersServer.jar')
23
+ Jar.new('JSettlersServer.jar').running('soc.server.SOCServer')
24
+ end
25
+
26
+ def robot
27
+ Jar.new('JSettlersServer.jar').running('soc.robot.SOCRobotClient')
20
28
  end
21
29
 
22
- def client
23
- Jar.new('JSettlers.jar')
30
+ def human
31
+ Jar.new('JSettlers.jar').running('soc.client.SOCPlayerClient')
24
32
  end
25
33
  end
26
34
  end
@@ -4,8 +4,8 @@ module Settlers
4
4
  @path = path
5
5
  end
6
6
 
7
- def command(*args)
8
- ['java', '-cp', full_path] + args
7
+ def running(class_name)
8
+ JavaCommand.new(full_path, class_name)
9
9
  end
10
10
 
11
11
  private
@@ -0,0 +1,22 @@
1
+ module Settlers
2
+ class JavaCommand
3
+ def initialize(class_path, class_name)
4
+ @class_path, @class_name = class_path, class_name
5
+ end
6
+
7
+ def run(*args)
8
+ system command(args)
9
+ end
10
+
11
+ def start(*args)
12
+ pid = fork { exec command(args) }
13
+ at_exit { Process.kill 'INT', pid }
14
+ end
15
+
16
+ private
17
+
18
+ def command(args)
19
+ "java -cp #{@class_path} #{@class_name} #{args.join(' ')}"
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matthewtodd-settlers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Todd
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-10 00:00:00 -07:00
12
+ date: 2008-10-12 00:00:00 -07:00
13
13
  default_executable: settlers
14
14
  dependencies: []
15
15
 
@@ -26,7 +26,7 @@ files:
26
26
  - bin/settlers
27
27
  - lib/settlers/game.rb
28
28
  - lib/settlers/jar.rb
29
- - lib/settlers/runner.rb
29
+ - lib/settlers/java_command.rb
30
30
  - lib/settlers.rb
31
31
  - resources/jsettlers-1.0.6
32
32
  - resources/jsettlers-1.0.6/COPYING.txt
@@ -1,25 +0,0 @@
1
- module Settlers
2
- class Runner
3
- def initialize
4
- @pids = []
5
- end
6
-
7
- def background(command)
8
- @pids.unshift fork { exec *ensure_strings(command) }
9
- end
10
-
11
- def foreground(command)
12
- system *ensure_strings(command)
13
- end
14
-
15
- def clean_up
16
- Process.kill 'INT', *@pids
17
- end
18
-
19
- private
20
-
21
- def ensure_strings(command)
22
- command.map { |arg| arg.to_s }
23
- end
24
- end
25
- end