ai_games-parser 0.2.1 → 0.3.0
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 +4 -4
- data/.codeclimate.yml +16 -0
- data/.rubocop.yml +0 -3
- data/.travis.yml +2 -2
- data/Gemfile +2 -0
- data/LICENSE.txt +1 -1
- data/README.md +26 -3
- data/ai_games-parser.gemspec +1 -1
- data/lib/ai_games/parser.rb +80 -4
- metadata +4 -5
- data/lib/ai_games/parser/abstract_parser.rb +0 -104
- data/lib/ai_games/parser/version.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 104e313efe3bb5652763cac019387c01c317ea19
|
4
|
+
data.tar.gz: 45d018cea9e91843c1a01d79cf61d19276de1e7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 093861ab9761b09a16508d6000df2cab157ebce6d2320e4f3f09fa3d86e2eba09ecbc1499687a08887a462006a2a3dd9704ffb07620721b886d2011f74165a4e
|
7
|
+
data.tar.gz: ac727de7853114168711c4422872ae6f20175de53d842b4dcc2daa4f74784d89bed2a3b552787be303382c2b13f832a82291eb8c17a62dc82a667452687b14df
|
data/.codeclimate.yml
ADDED
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# AIGames::Parser
|
2
2
|
|
3
|
+
[](https://travis-ci.org/jdno/ai_games-parser)
|
4
|
+
[](https://codeclimate.com/github/jdno/ai_games-parser)
|
5
|
+
[](https://codeclimate.com/github/jdno/ai_games-parser/coverage)
|
6
|
+
|
3
7
|
This gem provides a parser that can be used to communicate with the game engine
|
4
8
|
in The AI Games' competitions. By extending it, the parser can be customized to
|
5
9
|
work in any competition.
|
@@ -22,7 +26,9 @@ Or install it yourself as:
|
|
22
26
|
|
23
27
|
## Usage
|
24
28
|
|
25
|
-
|
29
|
+
The gem provides an abstract parser in `AIGames::Parser`. Extend this class, and
|
30
|
+
overwrite the method `parse(command_array)` to customize your parser for the
|
31
|
+
specific challenge.
|
26
32
|
|
27
33
|
## Development
|
28
34
|
|
@@ -39,5 +45,22 @@ the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
|
39
45
|
|
40
46
|
## License
|
41
47
|
|
42
|
-
|
43
|
-
|
48
|
+
Copyright (c) 2016 [Jan David Nose](https://github.com)
|
49
|
+
|
50
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
51
|
+
of this software and associated documentation files (the "Software"), to deal
|
52
|
+
in the Software without restriction, including without limitation the rights
|
53
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
54
|
+
copies of the Software, and to permit persons to whom the Software is
|
55
|
+
furnished to do so, subject to the following conditions:
|
56
|
+
|
57
|
+
The above copyright notice and this permission notice shall be included in
|
58
|
+
all copies or substantial portions of the Software.
|
59
|
+
|
60
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
61
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
62
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
63
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
64
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
65
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
66
|
+
THE SOFTWARE.
|
data/ai_games-parser.gemspec
CHANGED
data/lib/ai_games/parser.rb
CHANGED
@@ -1,7 +1,83 @@
|
|
1
|
-
require 'ai_games/parser/version'
|
2
|
-
|
3
1
|
module AIGames
|
4
|
-
|
5
|
-
|
2
|
+
# This class provides an abstract parser that can be extended to implement
|
3
|
+
# the logic that is required for a specific competition. It provides methods
|
4
|
+
# to communicate with the game engine, but does NOT contain any logic how to
|
5
|
+
# parse the engine's commands. This is totally up to the implementations of
|
6
|
+
# this class.
|
7
|
+
class Parser
|
8
|
+
VERSION = '0.3.0'
|
9
|
+
|
10
|
+
# Initializes the parser. Pass in options using a hash structure.
|
11
|
+
def initialize(options = nil)
|
12
|
+
initialize_streams options if options
|
13
|
+
end
|
14
|
+
|
15
|
+
# Starts the main loop. This loop runs until the game engine closes the
|
16
|
+
# console line. During the loop, the parser reads from the game engine,
|
17
|
+
# sanitizes the input a little bit, and then passes it to a method that
|
18
|
+
# needs to be overwritten by parsers extending this interface.
|
19
|
+
def run
|
20
|
+
AIGames::Logger.info 'Parser.run : Starting loop'
|
21
|
+
|
22
|
+
loop do
|
23
|
+
command = read_from_engine
|
24
|
+
break if command.nil?
|
25
|
+
|
26
|
+
command.strip!
|
27
|
+
next if command.length == 0
|
28
|
+
|
29
|
+
response = parse split_line command
|
30
|
+
write_to_engine response unless response.nil? || response.length < 1
|
31
|
+
end
|
32
|
+
|
33
|
+
AIGames::Logger.info 'Parser.run : Stopping loop'
|
34
|
+
end
|
35
|
+
|
36
|
+
# Parses the given command array. This method MUST return a valid response
|
37
|
+
# string that can be send to the engine.
|
38
|
+
# rubocop:disable Lint/UnusedMethodArgument
|
39
|
+
def parse(command_array)
|
40
|
+
fail 'Method must be overwritten'
|
41
|
+
end
|
42
|
+
# rubocop:enable Lint/UnusedMethodArgument
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
# Initializes the input and output streams that the parser uses. The
|
47
|
+
# expected format for the options is a hash with the keys `:input` and
|
48
|
+
# `:output`. Use this to mock `STDIN` and `STDOUT` for tests.
|
49
|
+
def initialize_streams(options)
|
50
|
+
@input = options[:input] if options.key? :input
|
51
|
+
@output = options[:output] if options.key? :output
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns the input stream. If no stream has been configured, $stdin is
|
55
|
+
# returned.
|
56
|
+
def input
|
57
|
+
@input ||= $stdin
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns the output stream. If no stream has been configured, $stdout is
|
61
|
+
# returned.
|
62
|
+
def output
|
63
|
+
@output ||= $stdout
|
64
|
+
end
|
65
|
+
|
66
|
+
# Reads from the input stream.
|
67
|
+
def read_from_engine
|
68
|
+
input.gets
|
69
|
+
end
|
70
|
+
|
71
|
+
# Writes to the output stream.
|
72
|
+
def write_to_engine(string)
|
73
|
+
output.puts string
|
74
|
+
output.flush
|
75
|
+
end
|
76
|
+
|
77
|
+
# Formats a command line for further processing. The input is split at the
|
78
|
+
# whitespace character, and all its parts are converted to downcase.
|
79
|
+
def split_line(line)
|
80
|
+
line.split(' ').map(&:downcase)
|
81
|
+
end
|
6
82
|
end
|
7
83
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ai_games-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan David Nose
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ai_games-logger
|
@@ -90,6 +90,7 @@ executables: []
|
|
90
90
|
extensions: []
|
91
91
|
extra_rdoc_files: []
|
92
92
|
files:
|
93
|
+
- ".codeclimate.yml"
|
93
94
|
- ".gitignore"
|
94
95
|
- ".rubocop.yml"
|
95
96
|
- ".travis.yml"
|
@@ -102,8 +103,6 @@ files:
|
|
102
103
|
- bin/console
|
103
104
|
- bin/setup
|
104
105
|
- lib/ai_games/parser.rb
|
105
|
-
- lib/ai_games/parser/abstract_parser.rb
|
106
|
-
- lib/ai_games/parser/version.rb
|
107
106
|
homepage: https://github.com/jdno/ai_games-parser
|
108
107
|
licenses:
|
109
108
|
- MIT
|
@@ -124,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
123
|
version: '0'
|
125
124
|
requirements: []
|
126
125
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.
|
126
|
+
rubygems_version: 2.5.1
|
128
127
|
signing_key:
|
129
128
|
specification_version: 4
|
130
129
|
summary: Skeleton for a parser for The AI Games' competitions
|
@@ -1,104 +0,0 @@
|
|
1
|
-
# Copyright (c) 2015 Jan David Nose
|
2
|
-
#
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
8
|
-
# furnished to do so, subject to the following conditions:
|
9
|
-
#
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
11
|
-
# all copies or substantial portions of the Software.
|
12
|
-
#
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
# THE SOFTWARE.
|
20
|
-
require 'ai_games/logger'
|
21
|
-
|
22
|
-
module AIGames
|
23
|
-
module Parser
|
24
|
-
# This class provides an abstract parser that can be extended to implement
|
25
|
-
# the logic that is required for a specific competition. It provides methods
|
26
|
-
# to communicate with the game engine, but does NOT contain any logic how to
|
27
|
-
# parse the engine's commands. This is totally up to the implementations of
|
28
|
-
# this class.
|
29
|
-
class AbstractParser
|
30
|
-
# Initializes the parser. Pass in options using a hash structure.
|
31
|
-
def initialize(options = nil)
|
32
|
-
initialize_streams options if options
|
33
|
-
end
|
34
|
-
|
35
|
-
# Starts the main loop. This loop runs until the game engine closes the
|
36
|
-
# console line. During the loop, the parser reads from the game engine,
|
37
|
-
# sanitizes the input a little bit, and then passes it to a method that
|
38
|
-
# needs to be overwritten by parsers extending this interface.
|
39
|
-
def run
|
40
|
-
AIGames::Logger.info 'Parser.run : Starting loop'
|
41
|
-
|
42
|
-
loop do
|
43
|
-
command = read_from_engine
|
44
|
-
break if command.nil?
|
45
|
-
|
46
|
-
command.strip!
|
47
|
-
next if command.length == 0
|
48
|
-
|
49
|
-
response = parse split_line command
|
50
|
-
write_to_engine response unless response.nil? || response.length < 1
|
51
|
-
end
|
52
|
-
|
53
|
-
AIGames::Logger.info 'Parser.run : Stopping loop'
|
54
|
-
end
|
55
|
-
|
56
|
-
# Parses the given command array. This method MUST return a valid response
|
57
|
-
# string that can be send to the engine.
|
58
|
-
# rubocop:disable Lint/UnusedMethodArgument
|
59
|
-
def parse(command_array)
|
60
|
-
fail 'Method must be overwritten'
|
61
|
-
end
|
62
|
-
# rubocop:enable Lint/UnusedMethodArgument
|
63
|
-
|
64
|
-
private
|
65
|
-
|
66
|
-
# Initializes the input and output streams that the parser uses. The
|
67
|
-
# expected format for the options is a hash with the keys `:input` and
|
68
|
-
# `:output`. Use this to mock `STDIN` and `STDOUT` for tests.
|
69
|
-
def initialize_streams(options)
|
70
|
-
@input = options[:input] if options.key? :input
|
71
|
-
@output = options[:output] if options.key? :output
|
72
|
-
end
|
73
|
-
|
74
|
-
# Returns the input stream. If no stream has been configured, $stdin is
|
75
|
-
# returned.
|
76
|
-
def input
|
77
|
-
@input ||= $stdin
|
78
|
-
end
|
79
|
-
|
80
|
-
# Returns the output stream. If no stream has been configured, $stdout is
|
81
|
-
# returned.
|
82
|
-
def output
|
83
|
-
@output ||= $stdout
|
84
|
-
end
|
85
|
-
|
86
|
-
# Reads from the input stream.
|
87
|
-
def read_from_engine
|
88
|
-
input.gets
|
89
|
-
end
|
90
|
-
|
91
|
-
# Writes to the output stream.
|
92
|
-
def write_to_engine(string)
|
93
|
-
output.puts string
|
94
|
-
output.flush
|
95
|
-
end
|
96
|
-
|
97
|
-
# Formats a command line for further processing. The input is split at the
|
98
|
-
# whitespace character, and all its parts are converted to downcase.
|
99
|
-
def split_line(line)
|
100
|
-
line.split(' ').map(&:downcase)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|