Class: Deck

Inherits:
Object
  • Object
show all
Defined in:
lib/manasimu/data.rb

Class Method Summary collapse

Class Method Details

.card_typesObject



88
89
90
91
# File 'lib/manasimu/data.rb', line 88

def self.card_types
  path = File.expand_path( '../../../db/card_type_aggregate', __FILE__ )
  @@card_types ||= Marshal.load(File.open(path, 'r'))
end

.create(lines) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/manasimu/data.rb', line 3

def self.create(lines)
  items, only_names = Deck.input_to_card_hash(lines)

  # 名前だけの行を考慮する
  names = only_names.map do |hash| hash[:name] end
  types = find_card_types(names)

  if types.length == names.length
    only_names.each_with_index do |a, i|
      name = types[i].name
      if types[i].language == "ja"
        name = types[i].names[0]
      end
      deck_item = {}
      deck_item[:amount] = only_names[i][:amount]
      deck_item[:name] = name
      deck_item[:set] = types[i].set_code
      deck_item[:setnum] = types[i].number
      items << deck_item
    end
  end

  Deck.get_card_details(items)
end

.find_card_types(lines) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/manasimu/data.rb', line 93

def self.find_card_types(lines)
  ret = []

  distinct_types = []
  card_types.each do |type|
    next if distinct_types[-1] and distinct_types[-1].name == type.name
    distinct_types << type
  end

  en = -1
  ja = 0

  [en, ja].each do |language|

    distinct_types.sort! do |a,b| 
      if language == en
        a.name <=> b.name 
      elsif language == ja
        d = a.name_ja_split.length <=> b.name_ja_split.length
        if d == 0
          a.name_ja_split <=> b.name_ja_split
        else
          d
        end
      else
        # none
      end
    end

    lines.each do |line|
      line.chomp!
      line.chomp.strip!
      line.chomp.lstrip!
      line.gsub!(/ \d+$/, '')

      if line =~ /^[\/,\|\d-]*$/ or line =~ /^x\d+$/
        next 
      end

      search_type = 
        if language == en
          # binary search
          distinct_types.bsearch do |type|
            name = type.name.split(' // ')[0]
            flag = true
            name.chars.each_with_index do |nc,i|
              if line.length > i
                lc = line.chars[i]
                if nc > lc
                  flag = true
                  break
                elsif nc < lc
                  flag = false
                  break
                else
                  # continue
                end
              else
                flag = true
                break
              end
            end
            flag
          end
        else language == ja
          # Levenshtein distance
          
          line.gsub!(/[①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬●〇▼▽▲△]+/, '')
          line.chomp!
          line.strip!
          line.lstrip!

          len = line.length

          s_index = distinct_types.bsearch_index do |type|
            name = type.name_ja_split
            name.length >= len - 2
          end

          e_index = distinct_types.bsearch_index do |type|
            name = type.name_ja_split
            name.length >= len + 2
          end

          next if not s_index

          min = Float::MAX
          min_type = nil

          distinct_types[s_index..e_index].each do |type|
            name = type.name_ja_split

            check_indexies = []
            if name.length == 1
              check_indexies[0] = 0
            elsif name.length == 2
              check_indexies[0] = 0
              check_indexies[1] = 1
            elsif name.length > 0
              check_indexies[0] = 0
              check_indexies[1] = (name.length / 2).to_i
              check_indexies[2] = name.length - 1
            end

            next if check_indexies.length == 0

            include_some = false
            check_indexies.each do |idx|
              if not name[idx].empty? and line.index(name[idx])
                include_some = true
                break
              end
            end

            next if not include_some

            d = levenshtein(line, name)
            base =  [name.length, line.length].max
            diff_rate =  d.to_f / base
            if diff_rate < min
              min = diff_rate
              min_type = type
            end
            if d == 0
              break
            end
          end

          result = nil

          if min_type and min <= 0.3
            result = min_type
          end

          result
        end

      if search_type
        if language == en
          a = search_type.name.split(' // ')[0]
          if line =~ /^#{a}.*$/ and a != 'X'
            search_type.language = 'en'
            ret << search_type
          end
        elsif language == ja
          search_type.language = 'ja'
          ret << search_type
        else
          # none
        end
      end
    end
  end

  ret.sort! do |a,b| a.converted_mana_cost <=> b.converted_mana_cost end
  ret.uniq!
  ret
end

.get_card_details(deck_items) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/manasimu/data.rb', line 271

def self.get_card_details(deck_items)
  cards = []
  card_id = 0
  clone_card_types = []
  deck_items.each do |deck_item|
    card_type = card_types.find(deck_item[:set], deck_item[:setnum])
    clone = CardType.create(card_type, deck_item[:name])
    clone_card_types << clone
    if clone.is_land?
      if clone.name =~ /.*Pathway$/
        card = PathwayCard.new(clone)
      elsif clone.contents[0].text =~ /enters the battlefield tapped\./
        card = TapLandCard.new(clone)
      elsif clone.contents[0].text =~ /enters the battlefield tapped unless you control two or more other lands./
        card = SlowLandCard.new(clone)
      elsif clone.text =~ /earch your library for a basic land card, put it onto the battlefield tapped, then shuffle/
        card = FetchLandCard.new(clone)
        card.configure
      elsif clone.set_code == 'SNC' and 
        clone.contents[0].text =~ /enters the battlefield, sacrifice it/
        card = SncFetchLandCard.new(clone)
        card.configure
      elsif clone.type[0] =~ /^Basic Land — .*$/
        card = BasicLandCard.new(clone)
      else
        card = Card.new(clone)
      end
    else
      card = Card.new(clone)
    end
    deck_item[:amount].to_i.times do 
      card_clone = card.dup
      card_clone.id = card_id
      card_id += 1
      cards << card_clone
    end
  end
  [cards, clone_card_types]
end

.input_to_card_hash(lines) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/manasimu/data.rb', line 28

def self.input_to_card_hash(lines)
  result = []
  only_names = []
  looking_for_deck_line = false
  for line in lines do
    trimmed = line.chomp
    trimmed_lower = trimmed.downcase

    # Ignore reserved words
    if trimmed_lower == "deck"
      looking_for_deck_line = false
      next
    end

    if trimmed_lower == "commander" 
      looking_for_deck_line = true
      next
    end
    if trimmed_lower == "companion" 
      looking_for_deck_line = true
      next
    end
    #Assumes sideboard comes after deck
    if trimmed_lower == "sideboard"
      break
    end
    if trimmed_lower == "maybeboard"
      # Assumes maybeboard comes after deck
      break
    end
    # Ignore line comments
    if trimmed.start_with? ('#')
      next
    end
    if looking_for_deck_line
      next
    end
    # An empty line divides the main board cards from the side board cards
    if trimmed.empty?
      break
    end

    if !(trimmed =~ /\s*(\d+)\s+([^\(#\n\r]+)(?:\s*\((\w+)\)\s+(\d+))?\s*/)
      next
    end

    deck_item = {}
    deck_item[:amount] = $1.strip
    deck_item[:name] = $2.strip
    if $3
      deck_item[:set] = $3.strip
      deck_item[:setnum] = $4.strip
      result << deck_item
    else
      only_names << deck_item
    end
  end
  [result, only_names]
end

.levenshtein(first, second) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/manasimu/data.rb', line 311

def self.levenshtein(first, second)
  matrix = [(0..first.length).to_a]
  (1..second.length).each do |j|
    matrix << [j] + [0] * (first.length)
  end

  (1..second.length).each do |i|
    (1..first.length).each do |j|
      if first[j-1] == second[i-1]
        matrix[i][j] = matrix[i-1][j-1]
      else
        matrix[i][j] = [
          matrix[i-1][j],
          matrix[i][j-1],
          matrix[i-1][j-1],
        ].min + 1
      end
    end
  end
  return matrix.last.last
end

.tsearch(arr, line, low, high) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/manasimu/data.rb', line 252

def self.tsearch(arr, line, low, high)
  if high - low < 1000
    return [low, high]
  end
  c1 = ((low * 2 + high ) / 3).to_i - 1
  c2 = ((low + high * 2 ) / 3).to_i + 1

  n1 = arr[c1].name_ja_split
  n2 = arr[c2].name_ja_split
  
  y1 = levenshtein(line, n1)
  y2 = levenshtein(line, n2)
  if y1 > y2
    tsearch(arr, line, c1, high)
  else
    tsearch(arr, line, low, c2)
  end
end