cryptopunks 2.0.0 → 3.0.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.
@@ -1,207 +0,0 @@
1
-
2
- module Cryptopunks
3
-
4
- class Metadata
5
- ### todo/fix:
6
- ## move into Punks::Metadata or such
7
- class Sprite
8
- attr_reader :id, :name, :type, :gender
9
-
10
-
11
- def initialize( id:,
12
- name:,
13
- type:,
14
- gender: )
15
- @id = id # zero-based index eg. 0,1,2,3, etc.
16
- @name = name
17
- @type = type
18
- @gender = gender
19
- end
20
-
21
- ## todo/check - find better names for type attribute/archetypes?
22
- ## use (alternate name/alias) base or face for archetypes? any others?
23
- def attribute?() @type.downcase.start_with?( 'attribute' ); end
24
- def archetype?() @type.downcase.start_with?( 'archetype' ); end
25
- end # class Metadata::Sprite
26
- end # class Metadata
27
-
28
-
29
-
30
-
31
- class Generator
32
-
33
- ######
34
- # static helpers - (turn into "true" static self.class methods - why? why not?)
35
- #
36
- def normalize_key( str )
37
- str.downcase.gsub(/[ ()°_-]/, '').strip
38
- end
39
-
40
- def normalize_gender( str )
41
- ## e.g. Female => f
42
- ## F => f
43
- ## always return f or m
44
- str.downcase[0]
45
- end
46
-
47
-
48
- def build_attributes_by_name( recs )
49
- h = {}
50
- recs.each_with_index do |rec|
51
- key = normalize_key( rec.name )
52
- key << "_(#{rec.gender})" if rec.attribute?
53
-
54
- if h[ key ]
55
- puts "!!! ERROR - attribute name is not unique:"
56
- pp rec
57
- puts "duplicate:"
58
- pp h[key]
59
- exit 1
60
- end
61
- h[ key ] = rec
62
- end
63
- ## pp h
64
- h
65
- end
66
-
67
-
68
- def build_recs( recs ) ## build and normalize (meta data) records
69
-
70
- ## sort by id
71
- recs = recs.sort do |l,r|
72
- l['id'].to_i( 10 ) <=> r['id'].to_i( 10 ) # use base10 (decimal)
73
- end
74
-
75
- ## assert all recs are in order by id (0 to size)
76
- recs.each_with_index do |rec, exp_id|
77
- id = rec['id'].to_i(10)
78
- if id != exp_id
79
- puts "!! ERROR - meta data record ids out-of-order - expected id #{exp_id}; got #{id}"
80
- exit 1
81
- end
82
- end
83
-
84
- ## convert to "wrapped / immutable" kind-of struct
85
- recs = recs.map do |rec|
86
- id = rec['id'].to_i( 10 )
87
- name = rec['name']
88
- gender = normalize_gender( rec['gender'] )
89
- type = rec['type']
90
-
91
- Metadata::Sprite.new(
92
- id: id,
93
- name: name,
94
- type: type,
95
- gender: gender)
96
- end
97
- recs
98
- end # method build_recs
99
-
100
-
101
-
102
-
103
- def initialize( image_path="./spritesheet.png",
104
- meta_path="./spritesheet.csv" )
105
- @sheet = Pixelart::ImageComposite.read( image_path )
106
- recs = CsvHash.read( meta_path )
107
-
108
- @recs = build_recs( recs )
109
-
110
- ## lookup by "case/space-insensitive" name / key
111
- @attributes_by_name = build_attributes_by_name( @recs )
112
- end
113
-
114
-
115
- def spritesheet() @sheet; end
116
- alias_method :sheet, :spritesheet
117
-
118
-
119
- def records() @recs; end
120
- alias_method :meta, :records
121
-
122
-
123
-
124
-
125
- def find_meta( q, gender: nil ) ## gender (m/f) required for attributes!!!
126
-
127
- key = normalize_key( q ) ## normalize q(uery) string/symbol
128
- key << "_(#{normalize_gender( gender )})" if gender
129
-
130
- rec = @attributes_by_name[ key ]
131
- if rec
132
- puts " lookup >#{key}< => #{rec.id}: #{rec.name} / #{rec.type} (#{rec.gender})"
133
- # pp rec
134
- else
135
- puts "!! WARN - no lookup found for key >#{key}<"
136
- end
137
- rec
138
- end
139
-
140
-
141
- def find( q, gender: nil ) ## gender (m/f) required for attributes!!!
142
- rec = find_meta( q, gender: gender )
143
-
144
- ## return image if record found
145
- rec ? @sheet[ rec.id ] : nil
146
- end
147
-
148
-
149
-
150
-
151
- def to_recs( *values )
152
- archetype_name = values[0]
153
-
154
- ### todo/fix: check for nil/not found!!!!
155
- archetype = find_meta( archetype_name )
156
- if archetype.nil?
157
- puts "!! ERROR - archetype >#{archetype}< not found; sorry"
158
- exit 1
159
- end
160
-
161
- recs = [archetype]
162
-
163
- attribute_names = values[1..-1]
164
- ## note: attribute lookup requires gender from archetype!!!!
165
- attribute_gender = archetype.gender
166
-
167
- attribute_names.each do |attribute_name|
168
- attribute = find_meta( attribute_name, gender: attribute_gender )
169
- if attribute.nil?
170
- puts "!! ERROR - attribute >#{attribute_name}< for (#{attribute_gender}) not found; sorry"
171
- exit 1
172
- end
173
- recs << attribute
174
- end
175
-
176
- recs
177
- end
178
-
179
-
180
-
181
-
182
- def generate_image( *values, background: nil )
183
-
184
- ids = if values[0].is_a?( Integer ) ## assume integer number (indexes)
185
- values
186
- else ## assume strings (names)
187
- to_recs( *values ).map { |rec| rec.id }
188
- end
189
-
190
-
191
- punk = Pixelart::Image.new( 24, 24 )
192
-
193
- if background ## for now assume background is (simply) color
194
- punk.compose!( Pixelart::Image.new( 24, 24, background ) )
195
- end
196
-
197
- ids.each do |id|
198
- punk.compose!( @sheet[ id ] )
199
- end
200
-
201
- punk
202
- end
203
- alias_method :generate, :generate_image
204
-
205
- end # class Generator
206
-
207
- end # module Cryptopunks