This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icon-gen.js
111 lines (102 loc) · 3.29 KB
/
icon-gen.js
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
const PImage = require('pureimage');
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const { stage_id_info, char_id_info } = require('./web/infos.json');
const example_input = {
output_path : 'web/out.png',
ingame_time : '04:08.66',
stage_name : 'Pokémon Stadium',
stage_id : 3,
p0_char : 19,
p1_char : 19,
p0_color : 3,
p1_color : 1,
p1_stock : 0,
p0_stock : 1
};
function tsToPaths(ts) {
// take a timestamp => return icon_paths = [stage, char0, char1]
const img_basedir = 'web/icon';
const nice = ts.nice; // chg to info.meta.nice
console.log(stage_id_info);
const stage_img = stage_id_info[nice.stage_id].icon;
let char0_img, char1_img;
if (Number.isInteger(nice.p0_color) && Number.isInteger(nice.p1_color)) {
char0_img = char_id_info[nice.p0_char].skins[nice.p0_color];
char1_img = char_id_info[nice.p1_char].skins[nice.p1_color];
} else {
char0_img = char_id_info[nice.p0_char].icon;
char1_img = char_id_info[nice.p1_char].icon;
}
return [ stage_img, char0_img, char1_img ].map((filename) => {
return path.join(img_basedir, filename);
});
}
function makeImg(image_paths, output_path, nice) {
// image paths: [stage, char0, char1...]
Promise.all(
image_paths.map((img_path) => {
return PImage.decodePNGFromStream(fs.createReadStream(img_path));
})
).then((images) => {
const cw = 640;
const ch = 480;
let background = PImage.make(cw, ch);
let ctx = background.getContext('2d');
const icon_scale = 4;
let img = images[0];
//prettier-ignore
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, 640, 480);
img = images[1]; // char0
//prettier-ignore
ctx.drawImage(img, 0, 0, img.width, img.height, 200, 20, img.width * icon_scale, img.height * icon_scale);
img = images[2]; // char1
//prettier-ignore
ctx.drawImage(img, 0, 0, img.width, img.height, 380, 20, img.width * icon_scale, img.height * icon_scale);
// time bckgrnd
ctx.fillStyle = '#000000';
ctx.fillRect(250, 400, 400, 100);
// time text
ctx.fillStyle = '#ffffff';
ctx.font = "100pt 'Arial Narrow'";
ctx.fillText(nice.ingame_time, 300, 480);
PImage.encodeJPEGToStream(
background,
fs.createWriteStream(output_path),
50
).then(() => {
console.log(`wrote ${output_path}`);
});
});
}
function storeThumbnail(timestamp, out_path) {
const src_paths = tsToPaths(timestamp);
makeImg(src_paths, out_path, timestamp.nice);
}
function setAllThumbnail(force_write = false) {
const { getAllTimestampsArr } = require('./api.js');
const all_timestamps = getAllTimestampsArr();
let to_write = [];
for (const ts of all_timestamps) {
const out_path = path.join('web', 'thumbnail', ts.uid + '.png');
if (!fs.existsSync(out_path) || force_write) {
to_write.push([ ts, out_path ]);
}
}
if (to_write.length > 0) {
var fnt = PImage.registerFont('web/arial_narrow_7.ttf', 'Arial Narrow');
fnt.load(() => {
to_write.map((el) => {
storeThumbnail(el[0], el[1]);
});
});
}
}
// for (const id of _.values(stage_id_info)) {
// if (id.icon) {
// let img = path.join('web', 'icon', id.icon);
// console.log(fs.existsSync(img));
// }
// }
module.exports = { setAllThumbnail };