11const fs = require ( "fs" ) ;
22const path = require ( "path" ) ;
3+ const sha1 = require ( "sha1" ) ;
34const { createCanvas, loadImage } = require ( "canvas" ) ;
45const isLocal = typeof process . pkg === "undefined" ;
56const basePath = isLocal ? process . cwd ( ) : path . dirname ( process . execPath ) ;
67const buildDir = `${ basePath } /build` ;
78const layersDir = `${ basePath } /layers` ;
89const {
9- layersOrder,
1010 format,
1111 baseUri,
1212 description,
1313 background,
1414 uniqueDnaTorrance,
15- editionSize,
15+ layerConfigurations,
16+ rarityDelimiter,
1617} = require ( path . join ( basePath , "/src/config.js" ) ) ;
1718const console = require ( "console" ) ;
1819const canvas = createCanvas ( format . width , format . height ) ;
@@ -30,16 +31,23 @@ const buildSetup = () => {
3031
3132const getRarityWeight = ( _str ) => {
3233 let nameWithoutExtension = _str . slice ( 0 , - 4 ) ;
33- var nameWithoutWeight = Number ( nameWithoutExtension . split ( / [ * ] + / ) . pop ( ) ) ;
34+ var nameWithoutWeight = Number (
35+ nameWithoutExtension . split ( rarityDelimiter ) . pop ( )
36+ ) ;
3437 if ( isNaN ( nameWithoutWeight ) ) {
3538 nameWithoutWeight = 0 ;
3639 }
3740 return nameWithoutWeight ;
3841} ;
3942
43+ const cleanDna = ( _str ) => {
44+ var dna = Number ( _str . split ( ":" ) . shift ( ) ) ;
45+ return dna ;
46+ } ;
47+
4048const cleanName = ( _str ) => {
4149 let nameWithoutExtension = _str . slice ( 0 , - 4 ) ;
42- var nameWithoutWeight = nameWithoutExtension . split ( / [ * ] + / ) . shift ( ) ;
50+ var nameWithoutWeight = nameWithoutExtension . split ( rarityDelimiter ) . shift ( ) ;
4351 return nameWithoutWeight ;
4452} ;
4553
@@ -51,6 +59,7 @@ const getElements = (path) => {
5159 return {
5260 id : index ,
5361 name : cleanName ( i ) ,
62+ filename : i ,
5463 path : `${ path } ${ i } ` ,
5564 weight : getRarityWeight ( i ) ,
5665 } ;
@@ -87,7 +96,7 @@ const drawBackground = () => {
8796const addMetadata = ( _dna , _edition ) => {
8897 let dateTime = Date . now ( ) ;
8998 let tempMetadata = {
90- dna : _dna . join ( "" ) ,
99+ dna : sha1 ( _dna . join ( "" ) ) ,
91100 name : `#${ _edition } ` ,
92101 description : description ,
93102 image : `${ baseUri } /${ _edition } .png` ,
@@ -122,7 +131,9 @@ const drawElement = (_element) => {
122131
123132const constructLayerToDna = ( _dna = [ ] , _layers = [ ] ) => {
124133 let mappedDnaToLayers = _layers . map ( ( layer , index ) => {
125- let selectedElement = layer . elements [ _dna [ index ] ] ;
134+ let selectedElement = layer . elements . find (
135+ ( e ) => e . id == cleanDna ( _dna [ index ] )
136+ ) ;
126137 return {
127138 name : layer . name ,
128139 selectedElement : selectedElement ,
@@ -149,7 +160,9 @@ const createDna = (_layers) => {
149160 // subtract the current weight from the random weight until we reach a sub zero value.
150161 random -= layer . elements [ i ] . weight ;
151162 if ( random < 0 ) {
152- return randNum . push ( layer . elements [ i ] . id ) ;
163+ return randNum . push (
164+ `${ layer . elements [ i ] . id } :${ layer . elements [ i ] . filename } `
165+ ) ;
153166 }
154167 }
155168 } ) ;
@@ -168,46 +181,53 @@ const saveMetaDataSingleFile = (_editionCount) => {
168181} ;
169182
170183const startCreating = async ( ) => {
184+ let layerConfigIndex = 0 ;
171185 let editionCount = 1 ;
172186 let failedCount = 0 ;
173- const layers = layersSetup ( layersOrder ) ;
174- while ( editionCount <= editionSize ) {
175- let newDna = createDna ( layers ) ;
176- if ( isDnaUnique ( dnaList , newDna ) ) {
177- let results = constructLayerToDna ( newDna , layers ) ;
178- let loadedElements = [ ] ;
179-
180- results . forEach ( ( layer ) => {
181- loadedElements . push ( loadLayerImg ( layer ) ) ;
182- } ) ;
183-
184- await Promise . all ( loadedElements ) . then ( ( elementArray ) => {
185- ctx . clearRect ( 0 , 0 , format . width , format . height ) ;
186- if ( background . generate ) {
187- drawBackground ( ) ;
188- }
189- elementArray . forEach ( ( element ) => {
190- drawElement ( element ) ;
187+ layerConfigurations . forEach ( async ( layerConfig ) => {
188+ const layers = layersSetup ( layerConfig . layersOrder ) ;
189+ while ( editionCount <= layerConfig . layerEditionSize ) {
190+ let newDna = createDna ( layers ) ;
191+ if ( isDnaUnique ( dnaList , newDna ) ) {
192+ let results = constructLayerToDna ( newDna , layers ) ;
193+ let loadedElements = [ ] ;
194+
195+ results . forEach ( ( layer ) => {
196+ loadedElements . push ( loadLayerImg ( layer ) ) ;
191197 } ) ;
192- saveImage ( editionCount ) ;
193- addMetadata ( newDna , editionCount ) ;
194- saveMetaDataSingleFile ( editionCount ) ;
195- console . log ( `Created edition: ${ editionCount } , with DNA: ${ newDna } ` ) ;
196- } ) ;
197-
198- dnaList . push ( newDna ) ;
199- editionCount ++ ;
200- } else {
201- console . log ( "DNA exists!" ) ;
202- failedCount ++ ;
203- if ( failedCount >= uniqueDnaTorrance ) {
204- console . log (
205- `You need more layers or elements to generate ${ editionSize } artworks!`
206- ) ;
207- process . exit ( ) ;
198+
199+ await Promise . all ( loadedElements ) . then ( ( elementArray ) => {
200+ ctx . clearRect ( 0 , 0 , format . width , format . height ) ;
201+ if ( background . generate ) {
202+ drawBackground ( ) ;
203+ }
204+ elementArray . forEach ( ( element ) => {
205+ drawElement ( element ) ;
206+ } ) ;
207+ saveImage ( editionCount ) ;
208+ addMetadata ( newDna , editionCount ) ;
209+ saveMetaDataSingleFile ( editionCount ) ;
210+ console . log (
211+ `Created edition: ${ editionCount } , with DNA: ${ sha1 (
212+ newDna . join ( "" )
213+ ) } `
214+ ) ;
215+ } ) ;
216+
217+ dnaList . push ( newDna ) ;
218+ editionCount ++ ;
219+ } else {
220+ console . log ( "DNA exists!" ) ;
221+ failedCount ++ ;
222+ if ( failedCount >= uniqueDnaTorrance ) {
223+ console . log (
224+ `You need more layers or elements to generate ${ layerConfig . layerEditionSize } artworks!`
225+ ) ;
226+ process . exit ( ) ;
227+ }
208228 }
209229 }
210- }
230+ } ) ;
211231 writeMetaData ( JSON . stringify ( metadataList ) ) ;
212232} ;
213233
0 commit comments