Class: Deck

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

Class Method Summary collapse

Class Method Details

.card_typesObject



63
64
65
66
# File 'lib/manasimu/data.rb', line 63

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
# File 'lib/manasimu/data.rb', line 3

def self.create(lines)
  items = Deck.input_to_card_hash(lines)
  Deck.get_card_details(items)
end

.find_card_types(lines) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
# File 'lib/manasimu/data.rb', line 68

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


  [-1, 4].each do |j|

    distinct_types.sort! do |a,b| 
      if j < 0
        a.name <=> b.name 
      elsif a.names[j] and b.names[j]
        a.names[j] <=> b.names[j]
      elsif not a.names[j] and not b.names[j]
        0
      elsif a.names[j]
        -1
      else
        1
      end
    end

    lines.each do |line|
      line.chomp!
      search_type = distinct_types.bsearch do |type|
        if j < 0
          name = type.name.split(' // ')[0]
        elsif type.names[j]
          name = type.names[j].split(' // ')[0]
        end

        if name
          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
        else
          false
        end
      end
      if search_type
        if j < 0
          a = search_type.name.split(' // ')[0]
        else
          a = search_type.names[j].split(' // ')[0]
        end
        if line =~ /^#{a}.*$/ and a != 'X'
          ret << search_type
        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



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
# File 'lib/manasimu/data.rb', line 145

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/manasimu/data.rb', line 8

def self.input_to_card_hash(lines)
  result = []
  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
    deck_item[:set] = $3.strip
    deck_item[:setnum] = $4.strip
    result << deck_item
  end
  result
end