CS-Coursework/websites/Typing/screens/profilescreen.js

112 lines
4.0 KiB
JavaScript
Raw Normal View History

2022-11-28 11:04:49 +00:00
/**
* @file This file provides the user with their profilescreen, where they can see their own tests
* @author Arlo Filley
*
* TODO:
* - change button name
* - provide filters for tests
* - implement a way to scroll through tests
* - create a way to have personal bests and track them
* - store tests in localstorage.
* - show user tests even if they are not logged in
*/
/**
* This class displays all of the test data for a given user
*/
2022-11-17 11:48:12 +00:00
class ProfileScreen {
constructor() {
2022-11-18 14:51:05 +00:00
this.menu = new Menu();
2022-11-17 11:48:12 +00:00
api.getUserTests();
2022-11-29 12:21:38 +00:00
this.testButtons;
2023-09-05 08:52:28 +01:00
// this.buttons = [
// new Button(950, 270, 240, 120, "up"),
// new Button(950, 390, 240, 120, "down"),
// ]
2022-11-29 12:21:38 +00:00
this.offset = 0;
2023-09-05 08:52:28 +01:00
this.scroll_bar_button = new Button(1200, 200, 20, 20, "")
2022-11-17 11:48:12 +00:00
}
draw() {
textSize(100);
textAlign(CENTER, CENTER);
2022-12-01 14:41:14 +00:00
fill(user.colorScheme.text);
2022-11-17 12:10:54 +00:00
text("Profile", 0, 100, windowWidth, 120);
2022-11-18 11:33:25 +00:00
2022-11-18 14:51:05 +00:00
this.menu.draw();
2022-11-17 11:48:12 +00:00
textSize(20);
2022-12-01 14:41:14 +00:00
fill(user.colorScheme.text);
2022-11-17 11:48:12 +00:00
if (user.tests != undefined) {
2022-11-29 12:21:38 +00:00
if (this.testButtons === undefined) {
this.createTestButtons();
2022-11-17 11:48:12 +00:00
}
}
2022-11-29 12:21:38 +00:00
2022-12-01 14:41:14 +00:00
if (this.testButtons !== undefined && this.testButtons.length > 1) {
2022-11-29 12:21:38 +00:00
for (let i = 0; i < this.testButtons.length; i++) {
this.testButtons[i][0].draw()
this.testButtons[i][1].draw()
this.testButtons[i][2].draw()
this.testButtons[i][3].draw()
}
2022-12-01 14:41:14 +00:00
} else {
fill(user.colorScheme.text);
text("Looks Like You Don't have any tests :(", windowWidth / 2, 300);
2022-11-29 12:21:38 +00:00
}
2022-12-01 14:41:14 +00:00
fill(user.colorScheme.text);
2022-11-29 12:21:38 +00:00
text(`Logged in as ${user.username}`, windowWidth-150, 15);
2023-09-05 08:52:28 +01:00
if (user.tests === undefined) {
return;
}
fill(user.colorScheme.testBad);
rect(1200, 270, 20, 420);
fill(user.colorScheme.testGood);
rect(1200, 270, 20, 420 / user.tests.length * (this.offset + 1));
this.scroll_bar_button.height = (user.tests.length)
if (this.scroll_bar_button.isPressed()) {
this.scroll_bar_button.y = mouseY - this.scroll_bar_button.height / 2;
}
// the furthest up the scrollbar can go is the top
if (this.scroll_bar_button.y < 270) {
this.scroll_bar_button.y = 270;
}
if (this.scroll_bar_button.y > 690 - this.scroll_bar_button.height) {
this.scroll_bar_button.y = 690 - this.scroll_bar_button.height;
}
this.scroll_bar_button.draw();
this.offset = Number((
// number of pixels from top of screen / total range of options, put to a whole integer to produce the correct offset
(this.scroll_bar_button.y - 270) / ((420 - this.scroll_bar_button.height) / (user.tests.length - 13))
).toFixed(0));
this.createTestButtons(this.offset);
2022-11-29 12:21:38 +00:00
}
createTestButtons(offset = 0) {
this.testButtons = [[
2023-09-05 08:52:28 +01:00
new Button(600, 270, 100, 30, "test #"), // test # button
new Button(700, 270, 100, 30, "wpm"), // wpm button
new Button(800, 270, 100, 30, "accuracy"), // accuracy button
new Button(900, 270, 240, 30, "characters typed")
2022-11-29 12:21:38 +00:00
]];
let j = 300;
2022-12-01 14:41:14 +00:00
for (let i = user.tests.length-1-offset; i >= user.tests.length-13-offset && i >= 0; i--) {
2022-11-29 12:21:38 +00:00
this.testButtons.push([
2023-09-05 08:52:28 +01:00
new Button(600, j, 100, 30, `${i+1}`, true, true , "#000", "#000", "#fff"), // test # button
new Button(700, j, 100, 30, `${user.tests[i].wpm}`, true, true , "#000", "#000", "#fff"), // accuracy button
new Button(800, j, 100, 30, `${user.tests[i].accuracy}`, true, true , "#000", "#000", "#fff"), // wpm button
new Button(900, j, 240, 30, `${user.tests[i].test_length}`, true, true , "#000", "#000", "#fff")
2022-11-29 12:21:38 +00:00
])
j+=30;
}
2022-11-17 11:48:12 +00:00
}
}