-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrc.php
259 lines (225 loc) · 6.54 KB
/
src.php
1
2
3
4
5
6
7
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
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
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
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
251
252
253
254
255
256
257
258
259
<?
require_once "db/connection.php";
ini_set('max_execution_time', '3600'); //3600 seconds - 1 hour
$CONST = (object)[
'editionSize' => 324,
'rootPath' => str_replace(array("\\"), "/", __DIR__),
'dnaDelimiter' => '-',
'rarityDelimiter' => '#',
'collectionName' => 'Beer Mages',
'basePath' => 'https://storage.googleapis.com/nfkingdom.appspot.com',
'imgW' => 512,
'imgH' => 512,
'collectionLayersFolder' => 'mages',
'collectionBasePrice' => 1000,
];
$LAYERS = [
'Background',
'Weapon',
'Body',
'Face',
'Hood',
'Beard',
'Power',
];
// $LAYERS = [
// "Background",
// "Eyeball",
// "Eye color",
// "Iris",
// "Shine",
// "Bottom lid",
// "Top lid",
// ];
$DNA_LIST = [];
$IMG_W = $CONST->imgW;
$IMG_H = $CONST->imgH;
$IMG_BASE = imagecreatetruecolor($IMG_W, $IMG_H);
function array_find($xs, $f) {
foreach ($xs as $x) {
if ($f($x) === true)
return $x;
}
return null;
}
function isDnaUnique($dna) {
global $DNA_LIST;
$isUnique = !in_array($dna, $DNA_LIST);
if ($isUnique) {
array_push($DNA_LIST, $dna);
}
return $isUnique;
}
function cleanName($name) {
global $CONST;
$prettyName = substr($name, 0, -4);
$prettyName = explode($CONST->rarityDelimiter, $prettyName)[0];
return $prettyName;
}
function getRarityWeight($picname) {
global $CONST;
$nameWithoutExt = substr($picname, 0, -4);
$rarity = explode($CONST->rarityDelimiter, $nameWithoutExt)[1];
$rarity = is_null($rarity) ? 100 : $rarity;
var_dump($rarity);
return $rarity;
}
function getLayerPictures($layers_dir_path) {
$unfiltered = scandir($layers_dir_path, SCANDIR_SORT_NONE);
$images = [];
foreach ($unfiltered as $k => $v) {
if (substr($v, -4) === ".png") {
array_push($images, $v);
}
}
$res = array_map(function($pic, $i) use ($layers_dir_path) {
return [
'id' => $i,
'pic_name' => cleanName($pic),
'filename' => $pic,
'path' => "$layers_dir_path/$pic",
'weight' => getRarityWeight($pic),
];
}, $images, array_keys($images));
// print_r($res);
return $res;
}
function setupLayers() {
global $LAYERS, $CONST;
$layers = array_map(function($l, $i) use ($CONST) {
return [
"order" => $i,
"pictures" => getLayerPictures("$CONST->rootPath/$CONST->collectionLayersFolder/$l"),
"name" => $l,
];
}, $LAYERS, array_keys($LAYERS));
// print_r($layers);
return $layers;
}
function generateDna($layers) {
global $CONST;
$dnaNodes = [];
$rarityRates = [];
$collectionTotalWeight = 0;
foreach ($layers as $k => $v) {
$totalWeight = 0;
$layer_pics = $v['pictures'];
$layer_size = count($layer_pics);
// echo "<b>Layer {$v['name']}</b><br>";
// summarize weights of all pictures of a layers
foreach ($layer_pics as $k => $pic) {
$totalWeight += (int)$pic['weight'];
// echo "layer_fragment rarity: {$pic['weight']}<br>";
}
$accumulator = rand(0, $totalWeight);
for ($i=0; $i < count($layer_pics); $i++) {
$accumulator -= $layer_pics[$i]['weight'];
if ($accumulator <= 0) {
$pic = $layer_pics[$i];
$pic_id = $pic['id'];
$pic_filename = $pic['filename'];
if ($layer_size > 1) {
$rarity = $pic['weight'] / $totalWeight * 100;
array_push($rarityRates, $rarity);
$collectionTotalWeight += $totalWeight;
}
array_push($dnaNodes, "$pic_id:$pic_filename");
break;
}
}
// if ($layer_size > 1) {
// $r = $pic['weight'] / $totalWeight * 100;
// echo "item rarity: $r<br>";
// } else {
// echo "one item layer<br>";
// }
}
$dna = implode($CONST->dnaDelimiter, $dnaNodes);
$t = array_sum($rarityRates);
$rarity = array_sum($rarityRates) / count($rarityRates);
// echo "<b>Collection total weight:</b> $collectionTotalWeight, item total weight: $t, <b>Rarity:</b> $rarity<br><br>";
return ["dna" => $dna, "rarity" => $rarity];
}
function getDnaNodeId($dna_node) {
return explode(':', $dna_node)[0];
}
function getDnaLayers($dna, $layers) {
global $CONST;
$mapping = array_map(function($l, $i) use ($CONST, $dna) {
$pics = $l['pictures'];
// get dna node associated with layer
$dna_node = explode($CONST->dnaDelimiter, $dna)[$i];
$dna_node_id = getDnaNodeId($dna_node);
$pic = array_find($pics, function($p) use ($dna_node_id) {
// echo $p['id']." - ".$dna_node_id."<br>";
return $p['id'] == $dna_node_id;
});
return [
"layer_name" => $l['name'],
"layer_pic" => $pic,
];
}, $layers, array_keys($layers));
return $mapping;
}
function create() {
global $CONST, $IMG_BASE;
// configurate all layers and pictures
$layers = setupLayers();
//open connection
$c = connect();
$type = "creatures";
$qCreateCollection = "insert into collections (name, type) values ('$CONST->collectionName', '$type')";
$qGetId = "select LAST_INSERT_ID()";
$new_collection = $c->query($qCreateCollection);
$new_collection_id = $c->query($qGetId)->fetch_assoc()['LAST_INSERT_ID()'];
// var_dump($new_collection);
// var_dump($new_collection_id);
if (!$new_collection) {
disconnect($c);
return;
}
// create unique nft picture & write to database
$i = 1;
while ($i <= $CONST->editionSize) {
// get unique nft dna
$gen = generateDna($layers);
$dna = $gen['dna'];
$rarity = $gen['rarity'];
$price = 200 + $CONST->collectionBasePrice * (1 - $rarity / 100) * rand(99, 101) / 100;
// if dna exists, repeat iteration
if (!isDnaUnique($dna)) {
continue;
}
$layersToDraw = getDnaLayers($dna, $layers);
// clear 'canvas' by setting empty image
$output_image = $IMG_BASE;
foreach ($layersToDraw as $k => $l) {
$pic = $l['layer_pic'];
$src = $pic['path'];
$img = imagecreatefrompng($src);
// header('Content-type: image/png');
imagecopy($output_image, $img, 0, 0, 0, 0, $CONST->imgW, $CONST->imgH);
}
$filename = "$i.png";
// format collection name
$col_name_formatted = implode("-", explode(" ", $CONST->collectionName));
$image_url = "$CONST->basePath/$col_name_formatted/$filename";
// echo "$rarity, ";
// echo $new_collection_id."<br>";
$qInsertItem = "insert into items (collection_id, rarity, price, image_url) values ('$new_collection_id', '$rarity', '$price', '$image_url')";
$res = $c->query($qInsertItem);
if (!$res) {
var_dump('ERROR: Insertion token data in database.');
disconnect($c);
return;
}
imagepng($output_image, "$CONST->rootPath/results/$CONST->collectionName/$filename");
// imagedestroy($output_image);
$i += 1;
}
// disconnect($c);
}
create();
echo "All done";
print_r($DNA_LIST)
?>