lin 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3bd1cf781f555ff5aa81115a5f6413e79c9494c3
4
+ data.tar.gz: 538d6106ce7375d47cb716ae805b5a919919d2dc
5
+ SHA512:
6
+ metadata.gz: b2ec67d3b532a24b5c3aa367c9fc44a8a4973c617b7f6f3a53d8d256d8bd6ea7169715daf71651799dd728cf28f919848b11755dabefe70f26cbadbe12904989
7
+ data.tar.gz: 1f91b622b33b6c5421646ff27c3da603ac8ff72c4e6a0bb5eecc3749a4b2439f1398594ecd2ef7c3dc4f30e180cbc4a197cf338d99a4fd492d040aa48e04a8e9
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ notifications:
3
+ disabled: true
4
+ rvm:
5
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Wojciech Wnętrzak
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ # Lin
2
+
3
+ Parser for contract bridge board "lin" notation, that is used on BBO.
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ parser = Lin::Parser.new("lin-content-here")
9
+ parser.bids
10
+ #=> ["1H", "PASS", "PASS", "PASS"]
11
+ ```
12
+
13
+ ## TODO
14
+
15
+ * Retrieve alerts
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rake/testtask"
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << "lib" << "test"
6
+ test.pattern = "test/**/*_test.rb"
7
+ test.verbose = true
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,5 @@
1
+ require "lin/version"
2
+ require "lin/parser"
3
+
4
+ module Lin
5
+ end
@@ -0,0 +1,164 @@
1
+ require "strscan"
2
+
3
+ module Lin
4
+ class Parser
5
+ attr_reader :source
6
+
7
+ def initialize(source)
8
+ @source = source
9
+ end
10
+
11
+ # Returns cards for S hand
12
+ #
13
+ # @return [Array<String>]
14
+ def s
15
+ @s ||= parse_hand(parsed["md"][0].split(",")[0][1..-1])
16
+ end
17
+
18
+ # Returns cards for W hand
19
+ #
20
+ # @return [Array<String>]
21
+ def w
22
+ @w ||= parse_hand(parsed["md"][0].split(",")[1])
23
+ end
24
+
25
+ # Returns cards for N hand
26
+ #
27
+ # @return [Array<String>]
28
+ def n
29
+ @n ||= parse_hand(parsed["md"][0].split(",")[2])
30
+ end
31
+
32
+ # Returns cards for E hand
33
+ #
34
+ # @return [Array<String>]
35
+ def e
36
+ @e ||= deck - s - w - n
37
+ end
38
+
39
+ # Returns S player name
40
+ #
41
+ # @return [String]
42
+ def s_name
43
+ @s_name ||= players[0]
44
+ end
45
+
46
+ # Returns W player name
47
+ #
48
+ # @return [String]
49
+ def w_name
50
+ @w_name ||= players[1]
51
+ end
52
+
53
+ # Returns N player name
54
+ #
55
+ # @return [String]
56
+ def n_name
57
+ @n_name ||= players[2]
58
+ end
59
+
60
+ # Returns E player name
61
+ #
62
+ # @return [String]
63
+ def e_name
64
+ @e_name ||= players[3]
65
+ end
66
+
67
+ # Returns board name
68
+ #
69
+ # @return [String]
70
+ def board_name
71
+ @board_name ||= parsed["ah"][0]
72
+ end
73
+
74
+ # Returns dealer
75
+ #
76
+ # @return ["N", "E", "S", "W"]
77
+ def dealer
78
+ @dealer ||= case parsed["md"][0][0]
79
+ when "1" then "S"
80
+ when "2" then "W"
81
+ when "3" then "N"
82
+ when "4" then "E"
83
+ end
84
+ end
85
+
86
+ # Returns bids
87
+ #
88
+ # @return [Array<String>]
89
+ def bids
90
+ @bids ||= parsed["mb"].map do |bid|
91
+ case bid.upcase
92
+ when "P" then "PASS"
93
+ when "D" then "X"
94
+ when "R" then "XX"
95
+ when /\dN/ then bid[0] + "NT"
96
+ else
97
+ bid.upcase
98
+ end
99
+ end
100
+ end
101
+
102
+ # Returns played cards
103
+ #
104
+ # @return [Array<String>]
105
+ def cards
106
+ @cards ||= parsed["pc"]
107
+ end
108
+
109
+ # Returns claimed number of tricks
110
+ #
111
+ # @return [Integer, nil]
112
+ def claim
113
+ @claim ||= parsed["mc"][0] && parsed["mc"][0].to_i
114
+ end
115
+
116
+ # Returns vulnerable
117
+ #
118
+ # @return ["NONE" "NS", "EW", "BOTH"]
119
+ def vulnerable
120
+ @vulnerable ||= case parsed["sv"][0]
121
+ when "n" then "NS"
122
+ when "e" then "EW"
123
+ when "b" then "BOTH"
124
+ else
125
+ "NONE"
126
+ end
127
+ end
128
+
129
+ private
130
+
131
+ def players
132
+ @players ||= parsed["pn"][0].split(",")
133
+ end
134
+
135
+ def parsed
136
+ return @parsed if defined?(@parsed)
137
+ scanner = StringScanner.new(source)
138
+ @parsed = Hash.new { |hash, key| hash[key] = [] }
139
+ until scanner.eos?
140
+ scanner.scan_until(/[\w]{2}\|[^\|]*\|/)
141
+ key, value = retrieve_pair(scanner.matched)
142
+ @parsed[key] << value
143
+ end
144
+ @parsed
145
+ end
146
+
147
+ # "md|some value here|", "md||"
148
+ def retrieve_pair(source)
149
+ result = source.split("|")
150
+ [result[0], result[1]]
151
+ end
152
+
153
+ def parse_hand(hand)
154
+ (hand.match(/S(.*?)H/)[1].split("").map { |value| "S" << value.upcase } <<
155
+ hand.match(/H(.*?)D/)[1].split("").map { |value| "H" << value.upcase } <<
156
+ hand.match(/D(.*?)C/)[1].split("").map { |value| "D" << value.upcase } <<
157
+ hand.match(/C(.*?)$/)[1].split("").map { |value| "C" << value.upcase }).flatten
158
+ end
159
+
160
+ def deck
161
+ ["SA", "SK", "SQ", "SJ", "ST", "S9", "S8", "S7", "S6", "S5", "S4", "S3", "S2", "HA", "HK", "HQ", "HJ", "HT", "H9", "H8", "H7", "H6", "H5", "H4", "H3", "H2", "DA", "DK", "DQ", "DJ", "DT", "D9", "D8", "D7", "D6", "D5", "D4", "D3", "D2", "CA", "CK", "CQ", "CJ", "CT", "C9", "C8", "C7", "C6", "C5", "C4", "C3", "C2"]
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,3 @@
1
+ module Lin
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "lin/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lin"
8
+ spec.version = Lin::VERSION
9
+ spec.authors = ["Wojciech Wnętrzak"]
10
+ spec.email = ["w.wnetrzak@gmail.com"]
11
+ spec.description = %q{Parser for contract bridge board lin notation}
12
+ spec.summary = %q{lin bridge boards notation parser}
13
+ spec.homepage = "https://github.com/morgoth/lin"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest", ">= 5.0.0"
24
+ end
@@ -0,0 +1,3 @@
1
+ require "minitest/autorun"
2
+
3
+ require "lin"
@@ -0,0 +1,78 @@
1
+ require "helper"
2
+
3
+ describe Lin::Parser do
4
+ def source
5
+ "pn|skrzat,morgoth85,piotr59,253|st||md|3S2579H38AD458QC26,S4H24569TJQKD9CJK,S36TQH7D23TKC379Q,|rh||ah|Board 13|sv|b|mb|p|mb|1C|mb|p|mb|2H|mb|p|mb|3C|mb|p|mb|3H|mb|p|mb|3S|mb|p|mb|4C|mb|p|mb|4D|mb|p|mb|4H|mb|p|mb|4S|mb|p|mb|4N|mb|p|mb|5D|mb|d|mb|6H|mb|p|mb|p|mb|p|pc|DK|pc|DA|pc|D4|pc|D9|pc|D6|pc|D5|pc|H9|pc|D3|pc|HK|pc|H7|pc|C4|pc|HA|pc|H8|mc|12|"
6
+ end
7
+
8
+ def alerted_auction_source
9
+ "pn|stanwest,morgoth85,bronek2,253|st||md|3S59H345QKD248C579,S24QH7JAD7TJKACQA,S68TJKH6D9QC236TJ,|rh||ah|Board 13|sv|b|mb|2D|an|wilk|mb|p|mb|2H|mb|d|mb|2S|mb|3H|mb|p|mb|4H|mb|p|mb|p|mb|p|pg||pc|S5|pc|S2|pc|ST|pc|S7|pg||pc|S6|pc|S3|pc|S9|pc|SQ|pg||pc|HA|pc|H6|pc|H2|pc|H3|pg||pc|HJ|pc|C2|pc|H8|pc|HQ|pg||pc|C7|pc|CQ|pc|C3|pc|C4|pg||pc|S4|pc|SJ|pc|SA|pc|H4|pg||pc|D4|pc|DK|pc|D9|pc|D5|pg||pc|CA|pc|C6|pc|C8|pc|C5|pg||pc|H7|pc|S8|pc|H9|pc|HK|pg||pc|D2|pc|DT|pc|DQ|pc|D3|pg||pc|SK|pc|HT|pc|D8|pc|D7|pg||pc|CK|pc|C9|pc|DJ|pc|CT|pg||pc|D6|pc|H5|pc|DA|pc|CJ|pg||"
10
+ end
11
+
12
+ it "returns s player" do
13
+ assert_equal "skrzat", Lin::Parser.new(source).s_name
14
+ end
15
+
16
+ it "returns w player" do
17
+ assert_equal "morgoth85", Lin::Parser.new(source).w_name
18
+ end
19
+
20
+ it "returns n player" do
21
+ assert_equal "piotr59", Lin::Parser.new(source).n_name
22
+ end
23
+
24
+ it "returns e player" do
25
+ assert_equal "253", Lin::Parser.new(source).e_name
26
+ end
27
+
28
+ it "returns board name" do
29
+ assert_equal "Board 13", Lin::Parser.new(source).board_name
30
+ end
31
+
32
+ it "returns bids" do
33
+ expected = ["PASS", "1C", "PASS", "2H", "PASS", "3C", "PASS", "3H", "PASS", "3S", "PASS", "4C", "PASS", "4D", "PASS", "4H", "PASS", "4S", "PASS", "4NT", "PASS", "5D", "X", "6H", "PASS", "PASS", "PASS"]
34
+ assert_equal expected, Lin::Parser.new(source).bids
35
+ end
36
+
37
+ it "returns alerted bids" do
38
+ expected = ["2D", "PASS", "2H", "X", "2S", "3H", "PASS", "4H", "PASS", "PASS", "PASS"]
39
+ assert_equal expected, Lin::Parser.new(alerted_auction_source).bids
40
+ end
41
+
42
+ it "returns cards" do
43
+ expected = ["DK", "DA", "D4", "D9", "D6", "D5", "H9", "D3", "HK", "H7", "C4", "HA", "H8"]
44
+ assert_equal expected, Lin::Parser.new(source).cards
45
+ end
46
+
47
+ it "returns claim" do
48
+ assert_equal 12, Lin::Parser.new(source).claim
49
+ end
50
+
51
+ it "returns no claim" do
52
+ assert_nil Lin::Parser.new(alerted_auction_source).claim
53
+ end
54
+
55
+ it "returns vulnerable" do
56
+ assert_equal "BOTH", Lin::Parser.new(source).vulnerable
57
+ end
58
+
59
+ it "returns s hand" do
60
+ assert_equal ["S2", "S5", "S7", "S9", "H3", "H8", "HA", "D4", "D5", "D8", "DQ", "C2", "C6"], Lin::Parser.new(source).s
61
+ end
62
+
63
+ it "returns w hand" do
64
+ assert_equal ["S4", "H2", "H4", "H5", "H6", "H9", "HT", "HJ", "HQ", "HK", "D9", "CJ", "CK"], Lin::Parser.new(source).w
65
+ end
66
+
67
+ it "returns n hand" do
68
+ assert_equal ["S3", "S6", "ST", "SQ", "H7", "D2", "D3", "DT", "DK", "C3", "C7", "C9", "CQ"], Lin::Parser.new(source).n
69
+ end
70
+
71
+ it "returns e hand" do
72
+ assert_equal ["SA", "SK", "SJ", "S8", "DA", "DJ", "D7", "D6", "CA", "CT", "C8", "C5", "C4"], Lin::Parser.new(source).e
73
+ end
74
+
75
+ it "returns dealer" do
76
+ assert_equal "N", Lin::Parser.new(source).dealer
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Wojciech Wnętrzak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 5.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 5.0.0
55
+ description: Parser for contract bridge board lin notation
56
+ email:
57
+ - w.wnetrzak@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/lin.rb
69
+ - lib/lin/parser.rb
70
+ - lib/lin/version.rb
71
+ - lin.gemspec
72
+ - test/helper.rb
73
+ - test/parser_test.rb
74
+ homepage: https://github.com/morgoth/lin
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.4
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: lin bridge boards notation parser
98
+ test_files:
99
+ - test/helper.rb
100
+ - test/parser_test.rb