-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules_weeklyReviewRenderer.js.html
468 lines (398 loc) · 26.6 KB
/
modules_weeklyReviewRenderer.js.html
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Source: modules/weeklyReviewRenderer.js | Source: modules/weeklyReviewRenderer.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-jsdoc.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/tui-doc.css">
</head>
<body>
<nav class="lnb" id="lnb">
<div class="logo" style="width: 100px; height: 100px">
<a href="https://github.com/Project-Skill-Tree/Skill-Tree-Discord-Bot" rel="noopener noreferrer" target="_blank">
<img src="https://cdn.discordapp.com/attachments/939508059758288939/979289803239608360/FinalLogoAlt2.png" width="100%" height="100%">
</a>
</div>
<div class="title">
<h1><a href="index.html" class="link">Source: modules/weeklyReviewRenderer.js</a></h1>
</div>
<div class="search-container" id="search-container">
<input type="text" placeholder="Search">
<ul></ul>
</div>
<div class="lnb-api hidden"><h3>Modules</h3><ul><li><a href="module-APIHelper.html">APIHelper</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:APIHelper_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-APIHelper.html#.cancel">cancel</a></li><li><a href="module-APIHelper.html#.deleteTask">deleteTask</a></li><li><a href="module-APIHelper.html#.getAllInList">getAllInList</a></li><li><a href="module-APIHelper.html#.getAvailable">getAvailable</a></li><li><a href="module-APIHelper.html#.getCurrentTasks">getCurrentTasks</a></li><li><a href="module-APIHelper.html#.getInProgress">getInProgress</a></li><li><a href="module-APIHelper.html#.getRecentTasks">getRecentTasks</a></li><li><a href="module-APIHelper.html#.revert">revert</a></li><li><a href="module-APIHelper.html#.skip">skip</a></li><li><a href="module-APIHelper.html#.start">start</a></li><li><a href="module-APIHelper.html#.updateTask">updateTask</a></li><li><a href="module-APIHelper.html#~getAPIKey">getAPIKey</a></li></ul></div></li><li><a href="module-badge.html">badge</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:badge_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-badge.html#.drawBadge">drawBadge</a></li><li><a href="module-badge.html#.getBadgeIcon">getBadgeIcon</a></li></ul></div></li><li><a href="module-createSwipePanel.html">createSwipePanel</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:createSwipePanel_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-createSwipePanel.html#.createLargeMultiActionSwipePanel">createLargeMultiActionSwipePanel</a></li><li><a href="module-createSwipePanel.html#.createLargeSwipePanel">createLargeSwipePanel</a></li><li><a href="module-createSwipePanel.html#.createYesNoPanel">createYesNoPanel</a></li></ul></div></li><li><a href="module-frequencyFormatter.html">frequencyFormatter</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:frequencyFormatter_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-frequencyFormatter.html#.dayToDate">dayToDate</a></li><li><a href="module-frequencyFormatter.html#.formatFrequency">formatFrequency</a></li><li><a href="module-frequencyFormatter.html#.getAbsDate">getAbsDate</a></li><li><a href="module-frequencyFormatter.html#.getDaysBetweenDates">getDaysBetweenDates</a></li></ul></div></li><li><a href="module-ProfileRenderer.html">ProfileRenderer</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:ProfileRenderer_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-ProfileRenderer.html#.displayLevelUp">displayLevelUp</a></li><li><a href="module-ProfileRenderer.html#.displayProfile">displayProfile</a></li><li><a href="module-ProfileRenderer.html#.getProfileImage">getProfileImage</a></li><li><a href="module-ProfileRenderer.html#~drawProfile">drawProfile</a></li><li><a href="module-ProfileRenderer.html#~drawProfileInfo">drawProfileInfo</a></li><li><a href="module-ProfileRenderer.html#~drawXP">drawXP</a></li><li><a href="module-ProfileRenderer.html#~getLevelUpProfileImage">getLevelUpProfileImage</a></li></ul></div></li><li><a href="module-romanNumeralHelper.html">romanNumeralHelper</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:romanNumeralHelper_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-romanNumeralHelper.html#.romanise">romanise</a></li></ul></div></li><li><a href="module-UIHelper.html">UIHelper</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:UIHelper_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-UIHelper.html#.addGlow">addGlow</a></li><li><a href="module-UIHelper.html#.codeBlock">codeBlock</a></li><li><a href="module-UIHelper.html#.fillRoundRect">fillRoundRect</a></li><li><a href="module-UIHelper.html#.imageToBuffer">imageToBuffer</a></li><li><a href="module-UIHelper.html#.splitToN">splitToN</a></li><li><a href="module-UIHelper.html#.strokeRoundRect">strokeRoundRect</a></li><li><a href="module-UIHelper.html#.tint">tint</a></li></ul></div></li><li><a href="module-WeeklyReviewRenderer.html">WeeklyReviewRenderer</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:WeeklyReviewRenderer_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-WeeklyReviewRenderer.html#.displayReview">displayReview</a></li><li><a href="module-WeeklyReviewRenderer.html#~drawHeaderFooter">drawHeaderFooter</a></li><li><a href="module-WeeklyReviewRenderer.html#~drawTasks">drawTasks</a></li><li><a href="module-WeeklyReviewRenderer.html#~drawXP">drawXP</a></li><li><a href="module-WeeklyReviewRenderer.html#~getWeeklyReview">getWeeklyReview</a></li></ul></div></li><li><a href="module-XPHandler.html">XPHandler</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:XPHandler_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-XPHandler.html#.calcLevelFromXP">calcLevelFromXP</a></li><li><a href="module-XPHandler.html#.calcXPFromLevel">calcXPFromLevel</a></li><li><a href="module-XPHandler.html#.calcXPToLevelUp">calcXPToLevelUp</a></li><li><a href="module-XPHandler.html#.getCharacter">getCharacter</a></li><li><a href="module-XPHandler.html#.getColor">getColor</a></li><li><a href="module-XPHandler.html#.getRank">getRank</a></li></ul></div></li></ul></div><div class="lnb-api hidden"><h3>Classes</h3><ul><li><a href="Challenge.html">Challenge</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Challenge_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="Challenge.html#.create">create</a></li><li><a href="Challenge.html#update">update</a></li></ul></div></li><li><a href="Item.html">Item</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Item_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="Item.html#.create">create</a></li><li><a href="Item.html#toLine">toLine</a></li><li><a href="Item.html#toString">toString</a></li><li><a href="Item.html#update">update</a></li></ul></div></li><li><a href="ListPage.html">ListPage</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="ListPage_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="ListPage.html#update">update</a></li></ul></div></li><li><a href="Page.html">Page</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Page_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="Page.html#update">update</a></li></ul></div></li><li><a href="Rank.html">Rank</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Rank_sub"></div></li><li><a href="Setting.html">Setting</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Setting_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="Setting.html#start">start</a></li></ul></div></li><li><a href="Skill.html">Skill</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Skill_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="Skill.html#.create">create</a></li><li><a href="Skill.html#update">update</a></li></ul></div></li><li><a href="Task.html">Task</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Task_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="Task.html#.create">create</a></li><li><a href="Task.html#isChecked">isChecked</a></li><li><a href="Task.html#percentChecked">percentChecked</a></li><li><a href="Task.html#setChecked">setChecked</a></li></ul></div></li><li><a href="Unlocked.html">Unlocked</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Unlocked_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="Unlocked.html#update">update</a></li></ul></div></li><li><a href="User.html">User</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="User_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="User.html#.create">create</a></li></ul></div></li></ul></div><div class="lnb-api hidden"><h3>Interfaces</h3><ul><li><a href="Swipeable.html">Swipeable</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Swipeable_sub"></div></li></ul></div><div class="lnb-api hidden"><h3>Global</h3><ul><li><a href="global.html#addXP">addXP</a></li><li><a href="global.html#authUser">authUser</a></li><li><a href="global.html#createUser">createUser</a></li><li><a href="global.html#deleteUser">deleteUser</a></li><li><a href="global.html#getAPIKey">getAPIKey</a></li><li><a href="global.html#getSettings">getSettings</a></li><li><a href="global.html#getUser">getUser</a></li><li><a href="global.html#getUsers">getUsers</a></li><li><a href="global.html#getUsersInTimezone">getUsersInTimezone</a></li><li><a href="global.html#permlevel">permlevel</a></li><li><a href="global.html#saveProfile">saveProfile</a></li><li><a href="global.html#toProperCase">toProperCase</a></li><li><a href="global.html#updateTimezone">updateTimezone</a></li><li><a href="global.html#updateUser">updateUser</a></li><li><a href="global.html#updateXPHistory">updateXPHistory</a></li></ul></div>
</nav>
<div id="resizer"></div>
<div class="main" id="main">
<section>
<article>
<pre class="prettyprint source linenums"><code>const {MessageAttachment} = require("discord.js");
const Canvas = require("canvas");
const XPHelper = require("./XPHelper");
const {drawBadge} = require("../objects/badge");
const Skill = require("../objects/skill");
const Challenge = require("../objects/challenge");
const {getDaysBetweenDates} = require("./dateHelper");
/** @module WeeklyReviewRenderer */
/**
* Sends an embedded weekly review, including xp gained, new items, skills completed
* skill consistency and more
* @param user - Skill tree user object
* @param channel - the channel to send the message to
* @param tasks - tasks to display
*/
exports.displayReview = async (user, channel, tasks) => {
const reviewImage = new MessageAttachment(await getWeeklyReview(user, tasks), `${user.name}_review.png`);
return channel.send({content: `<@${user.discordid}> your weekly report has been published`, files: [reviewImage]});
};
/**
* Generate the weekly review for this user
* @param user - Skill tree user object
* @param tasks - Tasks to display
* @return {Promise<Buffer>} buffer -
*/
const getWeeklyReview = async (user, tasks) => {
const canvas = Canvas.createCanvas(400, 800);
const context = canvas.getContext("2d");
//Canvas settings
context.antialias = "default";
context.quality = "nearest";
context.imageSmoothingEnabled = false;
context.font = "30px \"Akira\"";
//background
const background = await Canvas.loadImage("./assets/backgrounds/weekly_report.png");
context.drawImage(background, 0, 0, background.width, background.height);
await drawHeaderFooter(canvas, user);
await drawXP(canvas, user,20, 150, 360, 30);
await drawTasks(canvas, user, tasks,0,190,400);
//return final buffer
return canvas.toBuffer();
}
/**
* Draw header banner and title, as well as footer banner and skill tree logo
* @param canvas
* @return {Promise<void>}
*/
const drawHeaderFooter = async(canvas, user) => {
const context = canvas.getContext("2d");
context.font = "30px \"Akira\"";
const header = await Canvas.loadImage("./assets/backgrounds/bg2.png");
context.drawImage(header, 0, -80, header.width, header.height);
const footer = await Canvas.loadImage("./assets/backgrounds/bg2.png");
context.drawImage(footer, 0, 800 - 50, footer.width, footer.height);
context.shadowColor = "rgba(255,198,7,1.0)";
context.shadowBlur = 10;
context.fillStyle = "rgba(255,255,255,1)";
const title = "WEEKLY REPORT";
const headerWidth = context.measureText(title).width;
context.fillText(title, 200 - headerWidth*0.5, 70);
context.shadowBlur = 20;
context.fillRect(0,120,400,3);
context.font = "20px \"Akira\"";
const metric = context.measureText(user.username);
const width = Math.min(metric.width, 300);
context.fillText(user.username, 200 - width*0.5, 100, 300);
context.font = "30px \"Akira\"";
context.shadowBlur = 20;
context.fillRect(0,800 - 50,400,3);
context.shadowBlur = 0;
context.font = "15px \"Akira\"";
context.shadowBlur = 5;
const footerText = "SKILL TREE";
const footerWidth = context.measureText(footerText).width;
context.fillText(footerText, 200 - footerWidth*0.5, 785);
context.shadowBlur = 0;
const logo = await Canvas.loadImage("./assets/skilltree/logo_gold.png");
const iconSizeRatio = Math.min(32 / logo.width, 32 / logo.height);
context.drawImage(logo,
200 - footerWidth*0.5 - 32 - 10,
800 - 25 - 16,
logo.width*iconSizeRatio,
logo.height*iconSizeRatio);
}
/**
* Draw xp changed since last review
* @param canvas
* @param user
* @param x
* @param y
* @param w
* @param h
* @return {Promise<void>}
*/
const drawXP = async (canvas, user, x, y, w, h) => {
const context = canvas.getContext("2d");
const level = XPHelper.calcLevelFromXP(user.xp);
const excessXP = user.xp - XPHelper.calcXPFromLevel(level);
const maxXP = XPHelper.calcXPToLevelUp(level);
//Add extra text to show increase
context.font = "16px \"Akira\"";
const prevXP = user.getPrevXP();
const prevLevel = XPHelper.calcLevelFromXP(prevXP);
const xpIncrease = user.xp - prevXP;
const increaseText = `+ ${xpIncrease} XP`;
const increaseTextWidth = context.measureText(increaseText).width;
const XPwidth = w - increaseTextWidth - 10;
context.fillStyle = "rgb(255,255,255)";
context.fillText(increaseText, x + XPwidth + 10, y + h - 10, increaseTextWidth);
// Fill with lighter gradient for newXP
const color = XPHelper.getColor(level);
let [red, green, blue] = color.substring(color.indexOf("(") + 1, color.lastIndexOf(")")).split(/,\s*/);
red = Math.floor(Math.min(red * 2, 254));
green = Math.floor(Math.min(green * 2, 254));
blue = Math.floor(Math.min(blue * 2, 254));
context.fillStyle = `rgba(${red}, ${green}, ${blue}, 1.0)`;
context.fillRect(x,y,XPwidth*Math.min(excessXP, maxXP)/maxXP,h);
//Fill to show last level XP
if (prevLevel === level) {
context.fillStyle = XPHelper.getColor(level);
context.fillRect(x,y,XPwidth*Math.min(prevXP, maxXP)/maxXP,h);
}
//Draw XP bar outline
context.strokeStyle = "rgba(255, 255, 255, 0.7)";
context.lineWidth = 3;
context.strokeRect(x,y,XPwidth,h);
context.lineWidth = 1;
context.strokeStyle = "rgba(255, 255, 255, 0)";
//Draw XP text
context.font = "15px \"Akira\"";
const xpText = `${excessXP}XP / ${maxXP}XP`;
const textWidth = context.measureText(xpText).width;
//Draw XP text outline
context.lineWidth = 3;
context.strokeStyle = "rgb(0,0,0)";
context.strokeText(xpText, x + XPwidth*0.5 - textWidth*0.5, y + h - 10);
context.lineWidth = 1;
//Fill XP text
context.fillStyle = "rgb(255,255,255)";
context.fillText(xpText, x + XPwidth*0.5 - textWidth*0.5, y + h - 10);
}
/**
* Draw set of tasks that the user is currently participating in, or have recently ended
* @param canvas
* @param user
* @param tasks
* @param x
* @param y
* @param w
* @return {Promise<void>}
*/
const drawTasks = async (canvas, user, tasks, x, y, w) => {
const context = canvas.getContext("2d");
const pad = 10;
//Sort tasks in descending order of level and then XP
let taskList = tasks.sort((a, b) => {
//sort challenges by xp instead of level
if (a.child.level === undefined || b.child.level === undefined) {
return b.child.xp - a.child.xp;
}
if (b.child.level !== a.child.level) { //sort by level
return (b.child.level - a.child.level);
} else { // if that fails: sort by xp
return b.child.xp - a.child.xp;
}
});
const map = {};
taskList.forEach(task => {
const k = task.child.getName();
if (!map[k] || task.data.length > map[k]?.data.length) {
map[k] = task;
}
});
taskList = taskList.filter(task => map[task.child.getName()] === task).slice(0,6);
//Width of one task
const tHeight = 90;
const tWidth = (w - pad*2 - tHeight - 10) / 7;
const size = Math.min(tHeight, tWidth) - 4;
if (taskList.length === 0) {
//draw challenge icon
const challenge = await Canvas.loadImage("./assets/characters/character6.png");
const challengeSizeRatio = Math.min(300 / challenge.width, 300 / challenge.height);
context.shadowColor = "#000000";
context.shadowBlur = 30;
context.drawImage(challenge,
x + pad + w*0.5 - challenge.width * challengeSizeRatio*0.5,
y + pad + 150 - challenge.height * challengeSizeRatio*0.5,
challenge.width * challengeSizeRatio,
challenge.height * challengeSizeRatio);
context.shadowBlur = 0;
const text = "Things don't always \n" +
"go our way, son \n\n" +
"But what's important is \n" +
"continuing the journey.\n\n" +
"Use the start command to \n" +
"continue levelling up";
context.font = "18px \"Akira\"";
context.strokeStyle = "rgba(0,0,0,1.0)";
context.lineWidth = 5;
context.strokeText(text,
x + pad + 10,
y + pad + 350,
w - pad*2 - 10);
context.lineWidth = 1;
context.fillStyle = "rgba(255, 255, 255, 1.0)";
context.fillText(text,
x + pad + 10,
y + pad + 350,
w - pad*2 - 10);
return;
}
//draw tasks
await taskList.map(async (task, index) => {
if (task.completed) {
task.data = task.data.slice(0,task.data.lastIndexOf(true)+1);
}
//SKILL
if (task.child instanceof Skill) {
//Draw skill badge
drawBadge(canvas, x + pad + tHeight * 0.5,
y + pad + tHeight * index + tHeight * 0.5,
tHeight, task.child.getIconName(), task.child.level);
//Draw day completion chart
context.shadowBlur = 0;
context.strokeStyle = "rgba(0,0,0,0)";
context.fillStyle = "rgba(20, 20, 20, 1.0)";
//x (startX) + tHeight (size of icon) + pad (padding) + 5
context.fillRect(
x + tHeight + pad + 5,
y + pad + index*tHeight + tHeight*0.5 - size*0.5 - 5,
w - pad*2 - tHeight,
size + 15);
const dateIndex = new Date(new Date().getTime() + user.timezone*3600000);
let numChecked = 0;
for (let i = 0; i < 7; i++) {
if (getDaysBetweenDates(dateIndex, task.startDate, user.timezone) > 0) context.fillStyle = "rgba(20, 20, 20, 1.0)";
else if (task.completed && getDaysBetweenDates(
new Date(task.startDate.getTime() + (task.data.length-1)*86400000),
dateIndex,
user.timezone) > 0 ) context.fillStyle = "rgba(20, 20, 20, 1.0)";
else if (task.isChecked(dateIndex, user.timezone)) {
context.fillStyle = "rgba(108, 199, 78,0.8)";
numChecked += 1;
}
else context.fillStyle = "rgba(102, 102, 102, 0.6)";
context.fillRect(
x + w - pad - tWidth*(i+1) + 2,
y + pad + index*tHeight + tHeight*0.5 - size*0.5 + 2,
size,
size);
if (!task.completed) {
task.setChecked(task.isChecked(dateIndex, user.timezone), dateIndex, user.timezone);
}
dateIndex.setUTCDate(dateIndex.getUTCDate() - 1);
}
//Draw completion percentage
context.font = "25px \"Akira\"";
let percent = Math.floor(100 * numChecked / Math.min(task.data.length, 7));
percent = isNaN(percent) ? 0 : percent;
const text = `${Math.max(percent, 0)}%`;
const percentMetric = context.measureText(text);
const textHeight = percentMetric.actualBoundingBoxAscent + percentMetric.actualBoundingBoxDescent;
const textWidth = percentMetric.width;
context.strokeStyle = "rgba(0,0,0,1.0)";
context.lineWidth = 3;
context.strokeText(text,
x + w * 0.5 + tHeight*0.5 - textWidth*0.5,
y + pad + tHeight*index + tHeight*0.5 + textHeight*0.5);
context.lineWidth = 1;
context.fillStyle = "rgba(255, 255, 255, 1.0)";
context.fillText(text,
x + w * 0.5 + tHeight*0.5 - textWidth*0.5,
y + pad + tHeight*index + tHeight*0.5 + textHeight*0.5);
//CHALLENGE
} else if (task.child instanceof Challenge) {
//draw challenge icon
const challenge = await Canvas.loadImage("./assets/icons/challenge.png");
const challengeSizeRatio = Math.min(tHeight / challenge.width, tHeight / challenge.height);
context.drawImage(challenge,
x + pad,
y + pad + tHeight * index,
challenge.width * challengeSizeRatio,
challenge.height * challengeSizeRatio);
//Draw day completion chart
context.shadowBlur = 0;
context.strokeStyle = "rgba(0,0,0,0)";
context.fillStyle = "rgba(20, 20, 20, 1.0)";
//x (startX) + tHeight (size of icon) + pad (padding) + 5
context.fillRect(
x + tHeight + pad + 5,
y + pad + index*tHeight + tHeight*0.5 - size*0.5 - 5,
w - pad*2 - tHeight,
size + 15);
const goalSize = (w - pad*2 - tHeight) / task.child.goals.length - 5;
for (let i = 0; i < task.child.goals.length; i++) {
if (i < task.data.filter(Boolean).length) context.fillStyle = "rgba(108, 199, 78,0.8)";
else context.fillStyle = "rgba(102, 102, 102, 0.6)";
context.fillRect(
x + tHeight + pad + i*(goalSize+5) + 2,
y + pad + index*tHeight + tHeight*0.5 - size*0.5 + 2,
goalSize,
size);
}
//Draw completion percentage
context.font = "25px \"Akira\"";
let percent = Math.floor(100 * task.data.filter(Boolean).length / task.child.goals.length);
percent = isNaN(percent) ? 0 : percent;
const text = `${Math.max(percent, 0)}%`;
const percentMetric = context.measureText(text);
const textHeight = percentMetric.actualBoundingBoxAscent + percentMetric.actualBoundingBoxDescent;
const textWidth = percentMetric.width;
context.strokeStyle = "rgba(0,0,0,1.0)";
context.lineWidth = 3;
context.strokeText(text,
x + w * 0.5 + tHeight*0.5 - textWidth*0.5,
y + pad + tHeight*index + tHeight*0.5 + textHeight*0.5);
context.lineWidth = 1;
context.fillStyle = "rgba(255, 255, 255, 1.0)";
context.fillText(text,
x + w * 0.5 + tHeight*0.5 - textWidth*0.5,
y + pad + tHeight*index + tHeight*0.5 + textHeight*0.5);
}
//Draw title
const title = task.child.getName();
const titleMetric = context.measureText(title);
const titleHeight = titleMetric.actualBoundingBoxAscent + titleMetric.actualBoundingBoxDescent;
const titleWidth = Math.min(titleMetric.width, w - pad*2 - tHeight);
context.strokeStyle = "rgba(0,0,0,1.0)";
context.lineWidth = 3;
context.strokeText(title,
x + w * 0.5 + tHeight*0.5 - titleWidth*0.5,
y + pad + tHeight*index + titleHeight*0.5,
w - pad*2 - tHeight);
context.lineWidth = 1;
context.fillStyle = "rgba(255, 255, 255, 1.0)";
context.fillText(title,
x + w * 0.5 + tHeight*0.5 - titleWidth*0.5,
y + pad + tHeight*index + titleHeight*0.5,
w - pad*2 - tHeight);
});
}
</code></pre>
</article>
</section>
</div>
<footer>
<img class="logo" src="https://cdn.discordapp.com/attachments/939508059758288939/979289803239608360/FinalLogoAlt2.png" style="width: 100px; height: 100px">
<div class="footer-text">NHN Entertainment. Frontend Development Lab</div>
</footer>
<script>prettyPrint();</script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/tui-doc.js"></script>
<script src="scripts/linenumber.js"></script>
<script>
var id = '_sub'.replace(/"/g, '_');
var selectedApi = document.getElementById(id); // do not use jquery selector
var $selectedApi = $(selectedApi);
$selectedApi.removeClass('hidden');
$selectedApi.parent().find('.glyphicon').removeClass('glyphicon-plus').addClass('glyphicon-minus');
showLnbApi();
</script>
</body>
</html>