jkf 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d818940b6bb6a5173cdff2ccadedeffa608204a7
4
- data.tar.gz: 68032a91c6c67e182783093b6a30e7a5f2b8cf22
3
+ metadata.gz: 5c91b3b16debff343f298891452919ea2ec20527
4
+ data.tar.gz: b7c82914f613103e23087dd55f824da707846ca8
5
5
  SHA512:
6
- metadata.gz: 16d4a5fc1a4d4ce5e200451716c239d18c07e8225e5ba7ddfe719d31863d30f7d57f34f1d9ba8fc21556a20aa974fb51c0cd277357bc3fd10774d21a0f2eae72
7
- data.tar.gz: b49d9d3c088278ba553d4e06b53f079368877b6ddc5a3da22b407849f77d91fae04a8632d27e586d8d1d99f3064f79f73b92c5b9c95fb77f0772b5124fac8697
6
+ metadata.gz: c38021483c7d0457332083545ae03081f45de7bd8c43378d6c66f656cc0436f308422a253822a3545152c597a56621ffeccdb5e45e722faa0f1c6fc0ec8208ae
7
+ data.tar.gz: 41dae1d0deaed3ed33d21955429ab23f7f70f0e316b77dd0637b64b4412a36058a150be2cccd547cdb31774caeabe853c8cd14ce8e7bc00a710c033dcae24ccc
@@ -5,5 +5,6 @@ module Jkf
5
5
  end
6
6
 
7
7
  require 'json'
8
- require 'jkf/converter/ki2'
9
8
  require 'jkf/converter/kif'
9
+ require 'jkf/converter/ki2'
10
+ require 'jkf/converter/csa'
@@ -0,0 +1,160 @@
1
+ module Jkf::Converter
2
+ class Csa
3
+ VERSION = '2.2'
4
+
5
+ def convert(jkf)
6
+ hash = if jkf.is_a?(Hash)
7
+ jkf
8
+ else
9
+ JSON.parse(jkf)
10
+ end
11
+
12
+ result = version
13
+ result += convert_information(hash['header']) if hash['header']
14
+ result += convert_initial(hash['initial']) if hash['initial']
15
+ result += convert_moves(hash['moves']) if hash['moves']
16
+ result
17
+ end
18
+
19
+ def convert_information(header)
20
+ result = ''
21
+ result += 'N+' + (header.delete('先手') || header.delete('下手') || '') + "\n" if header['先手'] || header['下手']
22
+ result += 'N-' + (header.delete('後手') || header.delete('上手') || '') + "\n" if header['後手'] || header['上手']
23
+ header.each { |(k,v)| result += "$#{csa_header_key(k)}:#{v}\n" }
24
+ result
25
+ end
26
+
27
+ def convert_initial(initial)
28
+ result = ''
29
+ data = initial['data']
30
+ if initial['preset'] == 'OTHER'
31
+ 9.times { |y|
32
+ line = "P#{y+1}"
33
+ 9.times { |x|
34
+ piece = data['board'][8-x][y]
35
+ line += if piece == {}
36
+ " * "
37
+ else
38
+ csa_color(piece['color']) + piece['kind']
39
+ end
40
+ }
41
+ result += line + "\n"
42
+ }
43
+ else
44
+ result += 'PI'
45
+ case initial['preset']
46
+ when 'HIRATE'
47
+ when 'KY' # 香落ち
48
+ result += '11KY'
49
+ when 'KY_R' # 右香落ち
50
+ result += '91KY'
51
+ when 'KA' # 角落ち
52
+ result += '22KA'
53
+ when 'HI' # 飛車落ち
54
+ result += '82HI'
55
+ when 'HIKY' # 飛香落ち
56
+ result += '22HI11KY91KY'
57
+ when '2' # 二枚落ち
58
+ result += '82HI22KA'
59
+ when '3' # 三枚落ち
60
+ result += '82HI22KA91KY'
61
+ when '4' # 四枚落ち
62
+ result += '82HI22KA11KY91KY'
63
+ when '5' # 五枚落ち
64
+ result += '82HI22KA81KE11KY91KY'
65
+ when '5_L' # 左五枚落ち
66
+ result += '82HI22KA21KE11KY91KY'
67
+ when '6' # 六枚落ち
68
+ result += '82HI22KA21KE81KE11KY91KY'
69
+ when '8' # 八枚落ち
70
+ result += '82HI22KA31GI71GI21KE81KE11KY91KY'
71
+ when '10' # 十枚落ち
72
+ result += '82HI22KA41KI61KI31GI71GI21KE81KE11KY91KY'
73
+ end
74
+ end
75
+ # 持駒
76
+ if data['hands']
77
+ sum = 0
78
+ data['hands'][0].each_value { |n| sum += n }
79
+ if sum > 0
80
+ result += 'P+'
81
+ data['hands'][0].to_a.reverse.each { |(k, v)| v.times { result += "00#{k}" } }
82
+ result += "\n"
83
+ end
84
+ sum = 0
85
+ data['hands'][1].each_value { |n| sum += n }
86
+ if sum > 0
87
+ result += 'P-'
88
+ data['hands'][1].to_a.reverse.each { |(k, v)| v.times { result += "00#{k}" } }
89
+ result += "\n"
90
+ end
91
+ end
92
+ result += csa_color(data['color']) + "\n" if data['color']
93
+ result
94
+ end
95
+
96
+ def convert_moves(moves)
97
+ result = ''
98
+ moves.each do |move|
99
+ next if move == {}
100
+ result += convert_move(move['move']) if move['move']
101
+ result += convert_special(move['special'], move['color']) if move['special']
102
+ if move['time']
103
+ result += "," + convert_time(move['time'])
104
+ elsif move['move'] || move['special']
105
+ result += "\n"
106
+ end
107
+ result += convert_comments(move['comments']) if move['comments']
108
+ end
109
+ result
110
+ end
111
+
112
+ def convert_move(move)
113
+ result = csa_color(move['color'])
114
+ result += if move['from']
115
+ "#{move['from']['x']}#{move['from']['y']}"
116
+ else
117
+ "00"
118
+ end
119
+ result += "#{move['to']['x']}#{move['to']['y']}"
120
+ result += move['piece']
121
+ result
122
+ end
123
+
124
+ def convert_special(special, color=nil)
125
+ result = "%"
126
+ result += csa_color(color) if color
127
+ result += special
128
+ result
129
+ end
130
+
131
+ def convert_time(time)
132
+ sec = time['now']['m'] * 60 + time['now']['s']
133
+ "T#{sec}\n"
134
+ end
135
+
136
+ def convert_comments(comments)
137
+ comments.map { |comment| "'#{comment}" }.join("\n") + "\n"
138
+ end
139
+
140
+ protected
141
+
142
+ def csa_color(color)
143
+ color == 0 ? '+' : '-'
144
+ end
145
+
146
+ def version
147
+ "V#{VERSION}\n"
148
+ end
149
+
150
+ def csa_header_key(key)
151
+ {
152
+ "棋戦" => "EVENT",
153
+ "場所" => "SITE",
154
+ "開始日時" => "START_TIME",
155
+ "終了日時" => "END_TIME",
156
+ "持ち時間" => "TIME_LIMIT",
157
+ }[key] || key
158
+ end
159
+ end
160
+ end
@@ -710,11 +710,11 @@ module Jkf::Parser
710
710
  s5 = parse_nl
711
711
  if s5 != :failed
712
712
  @reported_pos = s0
713
- s0 = s1 = -> (from, to, piece) {
714
- ret = { "to" => to, "piece" => piece }
713
+ s0 = s1 = -> (color, from, to, piece) {
714
+ ret = { "color" => color, "to" => to, "piece" => piece }
715
715
  ret["from"] = from if from["x"] != 0
716
716
  ret
717
- }.call(s2, s3, s4)
717
+ }.call(s1, s2, s3, s4)
718
718
  else
719
719
  @current_pos = s0
720
720
  s0 = :failed
@@ -1,3 +1,3 @@
1
1
  module Jkf
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jkf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - iyuuya
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-21 00:00:00.000000000 Z
11
+ date: 2016-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,6 +86,7 @@ files:
86
86
  - jkf.gemspec
87
87
  - lib/jkf.rb
88
88
  - lib/jkf/converter.rb
89
+ - lib/jkf/converter/csa.rb
89
90
  - lib/jkf/converter/ki2.rb
90
91
  - lib/jkf/converter/kif.rb
91
92
  - lib/jkf/parser.rb