forked from solstice23/osu-stats-signature
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
179 lines (152 loc) · 6.79 KB
/
render.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
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
import fs from 'fs';
import path from 'path';
import cheerio from 'cheerio';
import TextToSVG from 'text-to-svg';
import Color from 'color';
import * as libs from './libs.js';
const __dirname = path.resolve();
const textToSVGRegular = TextToSVG.loadSync(path.join(__dirname, '/assets/fonts/Comfortaa/Comfortaa-Regular.ttf'));
const textToSVGBold = TextToSVG.loadSync(path.join(__dirname, '/assets/fonts/Comfortaa/Comfortaa-Bold.ttf'));
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
export const getSVGTemplete = () => {
return fs.readFileSync(path.join(__dirname, '/assets/template.svg'), 'utf8');
}
export const getSVGContent = (x) => {
return fs.readFileSync(path.join(__dirname, x), 'utf8');
}
const getTransformedX = (x, w) => {
return x + w / 2 - 550 / 2;
}
export const getFlagSVG = (countryCode, x, y, h) => {
let svg = libs.getFlagSVGByCountryCode(countryCode);
let $ = cheerio.load(svg);
$('svg').attr('x', getTransformedX(x, h * 0.72));
$('svg').attr('y', y);
$('svg').attr('height', h);
return $.html('svg');
}
export const getPlaymodeSVG = (playmode, x, y, h) => {
let svg = libs.getPlaymodeSVG(playmode);
let $ = cheerio.load(svg);
$('svg').attr('x', getTransformedX(x, h));
$('svg').attr('y', y);
$('svg').attr('height', h);
return $.html('svg');
}
export const getSupporterSVG = (x, y, h) => {
let svg = fs.readFileSync(path.join(__dirname, '/assets/icons/supporter.svg'), 'utf8');
let $ = cheerio.load(svg);
$('svg').attr('x', getTransformedX(x, h * 0.8));
$('svg').attr('y', y);
$('svg').attr('height', h);
return $.html('svg');
}
export const getTextSVGPath = (TextToSVGObj, text, x, y, size, anchor = 'left top') => {
let path = TextToSVGObj.getPath(text, {
x: x,
y: y,
fontSize: size,
anchor: anchor,
fontFamily: 'Comfortaa',
attributes: {
fill: '#fff'
}
});
return path;
}
export const getTextSVGMetrics = (TextToSVGObj, text, x, y, size, anchor = 'left top') => {
let metrics = TextToSVGObj.getMetrics(text, {
x: x,
y: y,
fontSize: size,
anchor: anchor,
fontFamily: 'Comfortaa',
attributes: {
fill: '#fff'
}
});
return metrics;
}
const replaceCalcedColors = (data, svg) => {
let baseHue = data.options.color_hue;
svg = svg.replace('{{hsl-b5}}', new Color(`hsl(${baseHue}, 10%, 15%)`).hex());
svg = svg.replace('{{hsl-b4}}', new Color(`hsl(${baseHue}, 10%, 20%)`).hex());
svg = svg.replace('{{hsl-h1}}', new Color(`hsl(${baseHue}, 100%, 70%)`).hex());
return svg;
}
export const getRenderedSVG = (data, avatarBase64, userCoverImageBase64) => {
let templete = getSVGTemplete();
//尺寸
templete = templete.replace('{{width}}', data.options.size.width);
templete = templete.replace('{{height}}', data.options.size.height);
//动画
templete = templete.replace('{{fg-extra-class}}', data.options.animation ? "animation-enabled" : "");
//颜色
templete = replaceCalcedColors(data, templete);
//名字
templete = templete.replace('{{name}}', getTextSVGPath(textToSVGBold, data.username, 130, 20, 28));
let nameWidth = getTextSVGMetrics(textToSVGBold, data.username, 130, 20, 28).width;
//Support Tag
if (data.is_supporter){
templete = templete.replace('{{supporter-tag}}', getSupporterSVG(130 + nameWidth + 15, 24, 22));
}else{
templete = templete.replace('{{supporter-tag}}', '');
}
//头像和封面
templete = templete.replace('{{avatar-base64}}', avatarBase64);
templete = templete.replace('{{user-cover-base64}}', userCoverImageBase64);
//国旗和国家名
templete = templete.replace('{{flag}}', getFlagSVG(data.country_code, 135, 56, 20));
templete = templete.replace('{{country}}', getTextSVGPath(textToSVGRegular, data.country.name, 161, 59.5, 14));
//模式
templete = templete.replace('{{playmode-icon}}', getPlaymodeSVG(data.current_mode, 130, 88, 15));
templete = templete.replace('{{playmode}}', getTextSVGPath(textToSVGRegular, libs.getPlaymodeFullName(data.current_mode), 150, 89, 12));
//等级
templete = templete.replace('{{level}}', getTextSVGPath(textToSVGBold, data.statistics.level.current.toString(), 290, 143, 12, 'center middle'));
templete = templete.replace('{{level-percent}}', getTextSVGPath(textToSVGRegular, data.statistics.level.progress + "%", 259.5, 145, 9, 'right top'));
templete = templete.replace('{{level-bar-fg}}', `<path class="cls-10" d="M20,135a2.5,2.5,0,0,0,2.5,2.5H${clamp(Math.round(data.statistics.level.progress / 100 * (256 - 21) + 21), 21, 256)}.833a2.5,2.5,0,0,0,0-5H22.5A2.5,2.5,0,0,0,20,135Z" transform="translate(0 2)" />`);
//成绩计数
const gradesName = ["ssh", "ss", "sh", "s", "a"];
let gradeTextX = 360.7;
for (let grade of gradesName) {
templete = templete.replace(`{{${grade}-count}}`, getTextSVGPath(textToSVGRegular, data.statistics.grade_counts[grade].toString(), gradeTextX, 153, 9, 'center middle'));
gradeTextX += 38.62;
}
//pp
templete = templete.replace('{{pp}}', getTextSVGPath(textToSVGRegular, libs.formatNumber(Math.round(data.statistics.pp)), 20, 202, 13));
//奖章
templete = templete.replace('{{medals}}', getTextSVGPath(textToSVGRegular, libs.formatNumber(data.user_achievements.length), 82, 202, 13));
//游戏时间
templete = templete.replace('{{playtime}}', getTextSVGPath(textToSVGRegular, libs.formatPlaytime(data.statistics.play_time), 126, 202, 13));
//全球排名/区内排名
let globalRanking = libs.formatNumber(data.statistics.global_rank, '#');
templete = templete.replace('{{global-ranking}}', getTextSVGPath(textToSVGRegular, globalRanking, 268, 211, globalRanking.length < 10 ? 27 : 25));
templete = templete.replace('{{country-ranking}}', getTextSVGPath(textToSVGRegular, libs.formatNumber(data.statistics.country_rank, '#'), 269, 277, 17));
//其他统计信息
const statsName = ["ranked_score", "play_count", "total_score", "total_hits", "replays_watched_by_others"];
let statsTextY = 227;
for (let stat of statsName) {
templete = templete.replace(`{{${stat.replace(/_/g, '-')}}}`, getTextSVGPath(textToSVGRegular, libs.formatNumber(data.statistics[stat]), 218, statsTextY, 10, 'right top'));
statsTextY += 16;
}
//acc
templete = templete.replace('{{acc}}', getTextSVGPath(textToSVGRegular, data.statistics.hit_accuracy.toFixed(2).toString() + "%", 424, 202, 13));
//最大连击
templete = templete.replace('{{max-combo}}', getTextSVGPath(textToSVGRegular, libs.formatNumber(data.statistics.maximum_combo) + "x", 483, 202, 13));
//bp
templete = templete.replace('{{bp}}', getTextSVGPath(textToSVGRegular, libs.formatNumber(Math.round(data.extra_data?.scoresBest[0]?.pp ?? 0)) + "pp", 424, 249, 13));
//第一名
templete = templete.replace('{{first-place}}', getTextSVGPath(textToSVGRegular, libs.formatNumber(data.scores_first_count), 483, 249, 13));
return templete;
}
export const getErrorSVG = (err) => {
return textToSVGRegular.getSVG(err, {
x: 0,
y: 0,
fontSize: 30,
anchor: 'left top',
attributes: {
fill: '#ff66ab'
}
});
}