-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
309 lines (261 loc) · 10 KB
/
index.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const express = require('express');
require("dotenv").config();
console.log(process.env)
run = ""
const app = express();
const fs = require('fs');
const {createCanvas, loadImage, registerFont} = require('canvas');
const resrictedPlayers = ['0'];
var old_timestamp = 10;
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
};
registerFont(`${__dirname}/Minecraft_Small_Caps.otf`, {family: 'Minecraft'});
async function generateImage({playerName, placement, potatoesCollected, playerUUID}) {
// Constants
const width = 128;
const height = 128;
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
const placementColors = ["#FAEE4D", "#FFFFFF", "#D87F33", "#A4A8B8"]
const placementShadowColors = ["#ba8524", "#545760", "#9f5224", "#4c4c4c"]
const placementLastIndex = placementColors.length-1;
// Format parameters
potatoesCollected = formatNumber(potatoesCollected)
placement = formatNumber(placement);
playerUUID = playerUUID === undefined ? '' : playerUUID;
playerName = playerName === undefined ? 'error' : playerName;
// Make rendering pixel-perfect
ctx.antialias = 'none'; // Text
ctx.quality = 'best';
ctx.textRendering = "optimizeLegibility";
ctx.imageSmoothingEnabled = false; // Images
// Register the font we are going to be using for the entire image
registerFont(`${__dirname}/Minecraft_Small_Caps.otf`, {family: 'Minecraft'});
// -- Image Rendering --
// Load the background image and draw it
const backgroundImage = await loadImage(__dirname + '/background.png');
ctx.drawImage(backgroundImage, 0, 0, width, height);
// Load the placement player head frame
const playerHeadFrame = await loadImage(__dirname + `/profile_frames/${placement >= 4 ? 4 : placement}.png`)
ctx.drawImage(playerHeadFrame, 0, 0, width, height)
// Draw player head
try {
const response = await fetch(`https://mc-heads.net/avatar/${playerUUID}`);
const buffer = await response.arrayBuffer();
const img = await loadImage(Buffer.from(buffer));
ctx.drawImage(img, width / 2 - 16, 28, 32, 32);
} catch (err) {
console.log("Error fetching image -- Background makes sure it's replaced with a checkerboard pattern.")
console.error(err);
}
// -- Text Rendering --
// Draw player name
// Measure player name pixel width with 18px size, if it does not fit scale down to 9px
ctx.font = '18px Minecraft'
if (ctx.measureText(playerName).width >= width - 4) ctx.font = '9px Minecraft'
drawShadowText(ctx, playerName, width / 2, 15, "center")
// Draw statistics
ctx.font = 'bold 18px Minecraft';
drawShadowText(ctx, potatoesCollected, width, 81, "right")
drawColoredShadowText(ctx, `#${placement}`,
placementColors[placement - 1] === undefined ? placementColors[placementLastIndex] : placementColors[placement - 1],
placementShadowColors[placement - 1] === undefined ? placementShadowColors[placementLastIndex] : placementShadowColors[placement - 1],
width, 102, "right");
// Draw timestamp
ctx.font = '9px Minecraft'
drawShadowText(ctx, getFormattedDateList()[0], 1, 120)
drawShadowText(ctx, getFormattedDateList()[1], 1, 126)
// Get image buffer and save to file
return canvas.toBuffer('image/png');
}
// Add commas to numbers (1000 -> 1,000)
function formatNumber(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function drawColoredShadowText(ctx, text, color, shadowColor, x, y, alignment) {
const textWidth = ctx.measureText(text).width;
// Calculate the x position based on the alignment
let posX;
if (alignment === 'right') {
posX = x - textWidth - 4;
} else if (alignment === 'center') {
posX = ctx.canvas.width / 2 - textWidth / 2;
} else {
// Default to left alignment
posX = x;
}
// Draw shadow
ctx.fillStyle = shadowColor;
ctx.fillText(text, posX + 1, y + 1);
// Draw text
ctx.fillStyle = color;
ctx.fillText(text, posX, y);
}
function drawShadowText(ctx, text, x, y, alignment) {
drawColoredShadowText(ctx, text, 'white', '#545760', x, y, alignment)
}
// Returns a list containing the formatted date at index 0 and the formatted hour at index 1 (UTC)
function getFormattedDateList() {
const now = new Date();
const month = String(now.getUTCMonth() + 1).padStart(2, '0');
const day = String(now.getUTCDate()).padStart(2, '0');
const hours = String(now.getUTCHours()).padStart(2, '0');
const minutes = String(now.getUTCMinutes()).padStart(2, '0');
return [`${day}/${month}`, `${hours}:${minutes}`];
}
function findPlayerKey(obj, str) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (obj[key][0].toLowerCase() === str.toLowerCase()) {
return key;
}
}
}
return null;
};
app.use(express.static(__dirname));
app.get('/', (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get('/hints', (req, res) => {
res.sendFile(__dirname + "/hints.html");
});
app.get('/faq', (req, res) => {
res.sendFile(__dirname + "/faq.html");
});
app.get('/blog', (req, res) => {
res.sendFile(__dirname + "/Potatolord_blog.html");
})
async function fetchName(uuid) {
return await fetch(`https://api.mojang.com/user/profile/${uuid}`)
.then((response) => response.json())
}
function getKeyPlacement(jsonObj, targetKey) {
const sortedKeys = Object.entries(jsonObj)
.map(([key, value]) => ({ key, length: value.length }))
.sort((a, b) => b.length - a.length || (b.key > a.key ? -1 : 1));
let rank = 1;
let lastLength = sortedKeys[0].length;
for (let i = 0; i < sortedKeys.length; i++) {
if (sortedKeys[i].length < lastLength) {
rank = i + 1;
}
if (sortedKeys[i].key === targetKey) {
return rank;
}
lastLength = sortedKeys[i].length;
}
return -1;
}
app.post('/post', express.json(), (req,res) => {
console.log(req.body);
try {
const P = 'P';
let timestamp = new Date().getTime() / 1000;
if (timestamp >= 1717164000 && timestamp < 1717498815) {
let plotID = req.headers['user-agent'].split(' (')[1].split(', ')[0];
fs.readFile(__dirname + "/data/plotdata.json", async function (err, data) {
fs.readFile(__dirname + "/data/id_name.json", async function (nameErr, nameData) {
const nameConvert = JSON.parse(nameData);
const plotName = nameConvert[plotID];
const json = JSON.parse(data);
if (typeof json[plotName] !== 'undefined' && typeof req.body['point'] === 'string' && typeof req.body['uuid'] === 'string' && req.body['key'] === process.env[P + plotID] && !resrictedPlayers.includes(req.body['uuid']) && typeof process.env[P + plotID] === 'string') {
const half_length = Math.floor(json[plotName].length / 2);
if (json[plotName].slice(0,half_length).includes(req.body['point'])) {
fs.readFile(__dirname + "/data/playerdata.json", async function (perr, pdata) {
var pjson = JSON.parse(pdata);
if (typeof pjson[req.body['uuid']] === 'undefined') {
console.log("New Player");
var pname = await fetchName(req.body['uuid']);
pname = pname["name"];
if (typeof pname !== 'undefined') {
var keyName = req.body['uuid']
pjson[keyName] = [pname, req.body['point']];
fs.writeFileSync(__dirname + "/data/playerdata.json", JSON.stringify(pjson));
};
}
else if (!pjson[req.body['uuid']].includes(req.body['point'])) {
console.log("Old Player");
var keyName = req.body['uuid'];
var pname = await fetchName(req.body[keyName])["name"];
pjson[keyName].push(req.body['point']);
fs.writeFileSync(__dirname + "/data/playerdata.json", JSON.stringify(pjson));
};
});
};
};
});
});
};
}
catch(err) {
console.log(err);
};
res.send();
});
app.get('/sync', async (req, res) => {
if (!typeof old_timestamp === 'number') {
old_timestamp = 10;
};
let timestamp = new Date().getTime();
res.sendFile(__dirname + "/index.html");
if ((timestamp - old_timestamp) >= 86400000) {
console.log(old_timestamp);
old_timestamp = new Date().getTime() + 86400000;
fs.readFile(__dirname + "/data/playerdata.json", async function (perr, pdata) {
const pjson = JSON.parse(pdata);
for (const [key, value] of Object.entries(pjson)) {
let newName = await fetchName(key);
if (typeof newName["name"] === 'string'){
pjson[key][0] = newName["name"];
await sleep(500);
};
};
fs.writeFileSync(__dirname + "/data/playerdata.json", JSON.stringify(pjson));
console.log("Successfully Synced!")
});
old_timestamp = new Date().getTime();
};
});
app.get('/player', (req, resp) => {
const uuid = req.query['uuid'];
if (typeof uuid === 'string') {
fs.readFile(__dirname + "/data/playerdata.json", async function (err, data) {
const playersjson = JSON.parse(data);
let keyID = '';
if (uuid.includes('-')) {
keyID = uuid;
} else {
keyID = findPlayerKey(playersjson, uuid);
}
if (keyID in playersjson) {
const placement = getKeyPlacement(playersjson, keyID);
const cardImage = await generateImage({
playerName: playersjson[keyID][0],
placement: placement,
potatoesCollected: playersjson[keyID].length - 1,
playerUUID: keyID,
});
resp.set('Content-Type', 'image/png');
resp.send(cardImage);
}
else {
const cardImage = await generateImage({
playerName: '?',
placement: 404,
potatoesCollected: '?',
playerUUID: 'ce08a3c5-b498-403c-8eca-513179f15a10',
});
resp.set('Content-Type', 'image/png');
resp.send(cardImage);
}
});
}
else {
resp.send();
}
});
app.listen(8080, () => {
console.log('Server started at ' + new Date().toUTCString());
});