matthewtodd-settlers 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -1
- data/bin/settlers +1 -1
- data/lib/settlers.rb +1 -1
- data/lib/settlers/application.rb +129 -0
- metadata +25 -6
- data/lib/settlers/game.rb +0 -34
data/Rakefile
CHANGED
@@ -2,12 +2,15 @@ require 'rubygems'
|
|
2
2
|
|
3
3
|
spec = Gem::Specification.new do |s|
|
4
4
|
s.name = 'settlers'
|
5
|
-
s.version = '0.
|
5
|
+
s.version = '0.2.0'
|
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']
|
9
9
|
s.author = 'Matthew Todd'
|
10
10
|
s.email = 'matthew.todd@gmail.com'
|
11
|
+
|
12
|
+
s.add_dependency 'dnssd'
|
13
|
+
s.add_dependency 'highline'
|
11
14
|
end
|
12
15
|
|
13
16
|
desc 'Generate a gemspec file'
|
data/bin/settlers
CHANGED
data/lib/settlers.rb
CHANGED
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'set'
|
3
|
+
require 'shellwords'
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'dnssd'
|
7
|
+
require 'highline/import'
|
8
|
+
|
9
|
+
module Settlers
|
10
|
+
class Application
|
11
|
+
attr_accessor :server, :client, :style
|
12
|
+
|
13
|
+
def initialize(*args)
|
14
|
+
self.server = Server.new
|
15
|
+
self.client = Client.new
|
16
|
+
self.style = StandaloneGame
|
17
|
+
parse_options(args)
|
18
|
+
end
|
19
|
+
|
20
|
+
def run
|
21
|
+
style.new(self).run
|
22
|
+
end
|
23
|
+
|
24
|
+
def start_server
|
25
|
+
server.start
|
26
|
+
end
|
27
|
+
|
28
|
+
def start_client(server)
|
29
|
+
client.start(server)
|
30
|
+
end
|
31
|
+
|
32
|
+
def find_server
|
33
|
+
choose(*available_servers)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def parse_options(args)
|
39
|
+
# TODO it would be nice to provide a quick tabtab definition here.
|
40
|
+
OptionParser.new do |opts|
|
41
|
+
opts.on('-c', '--client') { self.style = BonjourClientGame }
|
42
|
+
opts.parse!(args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class StandaloneGame < Struct.new(:app)
|
47
|
+
def run
|
48
|
+
app.start_server
|
49
|
+
app.start_client(app.server)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class BonjourClientGame < Struct.new(:app)
|
54
|
+
def run
|
55
|
+
app.start_client(app.find_server)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def available_servers(timeout = 3)
|
60
|
+
servers = Set.new
|
61
|
+
|
62
|
+
dns = DNSSD.browse('_settlers._tcp') do |reply|
|
63
|
+
DNSSD.resolve(reply.name, reply.type, reply.domain) do |resolve_reply|
|
64
|
+
servers << RemoteServer.new(reply.name, resolve_reply.target, resolve_reply.port)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
puts 'Looking for settlers servers nearby...'
|
69
|
+
sleep timeout
|
70
|
+
dns.stop
|
71
|
+
|
72
|
+
return servers
|
73
|
+
end
|
74
|
+
|
75
|
+
class Server
|
76
|
+
# Maybe we can change these to attr_writer as we'd like to make them command-line configurable.
|
77
|
+
attr_reader :name, :host, :port, :startup_delay, :maximum_connections, :username, :password, :robot_names
|
78
|
+
|
79
|
+
def initialize
|
80
|
+
@name = "#{`hostname -s`.chomp}-#{`whoami`.chomp}"
|
81
|
+
@host = 'localhost'
|
82
|
+
@port = 8880
|
83
|
+
@startup_delay = 4
|
84
|
+
@maximum_connections = 12
|
85
|
+
@username = 'root'
|
86
|
+
@password = ''
|
87
|
+
@robot_names = %w(Leonardo Humperdink Elwood)
|
88
|
+
end
|
89
|
+
|
90
|
+
def start
|
91
|
+
announce
|
92
|
+
serve
|
93
|
+
sleep(startup_delay)
|
94
|
+
start_robots
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def announce
|
100
|
+
# Maybe it would be nice to include a TextRecord here providing something of a description?
|
101
|
+
DNSSD.register(name, '_settlers._tcp', 'local', port) do |rr|
|
102
|
+
puts "Announcing settlers server available on port #{port}."
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def serve
|
107
|
+
Jar.new('JSettlersServer.jar').running('soc.server.SOCServer').start(port, maximum_connections, username, Shellwords.escape(password))
|
108
|
+
end
|
109
|
+
|
110
|
+
def start_robots
|
111
|
+
robot_names.each do |name|
|
112
|
+
Jar.new('JSettlersServer.jar').running('soc.robot.SOCRobotClient').start(host, port, name, Shellwords.escape(password))
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class RemoteServer < Struct.new(:name, :host, :port)
|
118
|
+
def to_s
|
119
|
+
"#{name} (#{host}:#{port})"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class Client
|
124
|
+
def start(server)
|
125
|
+
Jar.new('JSettlers.jar').running('soc.client.SOCPlayerClient').run(server.host, server.port)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Todd
|
@@ -9,10 +9,29 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-05-02 00:00:00 -07:00
|
13
13
|
default_executable: settlers
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: dnssd
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: highline
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
16
35
|
description:
|
17
36
|
email: matthew.todd@gmail.com
|
18
37
|
executables:
|
@@ -24,7 +43,7 @@ extra_rdoc_files: []
|
|
24
43
|
files:
|
25
44
|
- Rakefile
|
26
45
|
- bin/settlers
|
27
|
-
- lib/settlers/
|
46
|
+
- lib/settlers/application.rb
|
28
47
|
- lib/settlers/jar.rb
|
29
48
|
- lib/settlers/java_command.rb
|
30
49
|
- lib/settlers.rb
|
@@ -58,7 +77,7 @@ requirements: []
|
|
58
77
|
rubyforge_project:
|
59
78
|
rubygems_version: 1.2.0
|
60
79
|
signing_key:
|
61
|
-
specification_version:
|
80
|
+
specification_version: 3
|
62
81
|
summary: Provides a simple command-line executable for playing Robb Thomas' JSettlers game.
|
63
82
|
test_files: []
|
64
83
|
|
data/lib/settlers/game.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
module Settlers
|
2
|
-
class Game
|
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
|
10
|
-
end
|
11
|
-
|
12
|
-
def play
|
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)
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def server
|
23
|
-
Jar.new('JSettlersServer.jar').running('soc.server.SOCServer')
|
24
|
-
end
|
25
|
-
|
26
|
-
def robot
|
27
|
-
Jar.new('JSettlersServer.jar').running('soc.robot.SOCRobotClient')
|
28
|
-
end
|
29
|
-
|
30
|
-
def human
|
31
|
-
Jar.new('JSettlers.jar').running('soc.client.SOCPlayerClient')
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|