added leaderboard
This commit is contained in:
parent
4e9fde0bb8
commit
b25eed4cec
11
src/main.rs
11
src/main.rs
@ -14,7 +14,8 @@ use rocket::{
|
|||||||
json::Json
|
json::Json
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
use crate::sql::Test;
|
use sql::LeaderBoardTest;
|
||||||
|
use crate::sql::*;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn test() -> String {
|
fn test() -> String {
|
||||||
@ -81,10 +82,16 @@ fn get_user_tests(user_id: u32) -> Json<Vec<Test>> {
|
|||||||
Json(tests)
|
Json(tests)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/leaderboard")]
|
||||||
|
fn leaderboard() -> Json<Vec<LeaderBoardTest>> {
|
||||||
|
let leaderboard = sql::get_leaderboard(0).expect("error finding user_id");
|
||||||
|
Json(leaderboard)
|
||||||
|
}
|
||||||
|
|
||||||
#[launch]
|
#[launch]
|
||||||
fn rocket() -> Rocket<Build> {
|
fn rocket() -> Rocket<Build> {
|
||||||
rocket::build()
|
rocket::build()
|
||||||
.mount("/test", routes![test]) // testing only, should return "Hello world"
|
.mount("/test", routes![test]) // testing only, should return "Hello world"
|
||||||
.mount("/api", routes![post_test, create_user, login, get_user_tests])
|
.mount("/api", routes![post_test, create_user, login, get_user_tests, leaderboard])
|
||||||
.mount("/typing", FileServer::from(relative!("website"))) // hosts the fileserver
|
.mount("/typing", FileServer::from(relative!("website"))) // hosts the fileserver
|
||||||
}
|
}
|
35
src/sql.rs
35
src/sql.rs
@ -188,3 +188,38 @@ pub fn get_user_tests(
|
|||||||
|
|
||||||
Ok(tests)
|
Ok(tests)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(crate = "rocket::serde")]
|
||||||
|
pub struct LeaderBoardTest {
|
||||||
|
username: String,
|
||||||
|
wpm: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_leaderboard(
|
||||||
|
_user_id: u32
|
||||||
|
) -> Result<Vec<LeaderBoardTest>>{
|
||||||
|
let connection = get_connection();
|
||||||
|
let mut statement = connection.prepare(
|
||||||
|
"SELECT users.username, MAX(tests.wpm)
|
||||||
|
FROM tests
|
||||||
|
INNER JOIN users ON users.user_id = tests.user_id
|
||||||
|
GROUP BY users.username
|
||||||
|
ORDER BY tests.wpm DESC",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let test_iter = statement
|
||||||
|
.query_map((), |row| {
|
||||||
|
Ok( LeaderBoardTest {
|
||||||
|
username: row.get(0)?,
|
||||||
|
wpm: row.get(1)?
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let mut tests: Vec<LeaderBoardTest> = vec![];
|
||||||
|
for test in test_iter {
|
||||||
|
tests.push(test.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(tests)
|
||||||
|
}
|
@ -185,7 +185,9 @@ class API {
|
|||||||
JSON.stringify(user)
|
JSON.stringify(user)
|
||||||
);
|
);
|
||||||
|
|
||||||
this.login(username, password);
|
xhr.onload = () => {
|
||||||
|
this.login(username,password);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
login(pUsername, pPassword) {
|
login(pUsername, pPassword) {
|
||||||
@ -227,4 +229,13 @@ class API {
|
|||||||
user.tests = JSON.parse(xhr.response);
|
user.tests = JSON.parse(xhr.response);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getLeaderBoard() {
|
||||||
|
let xhr = new XMLHttpRequest();
|
||||||
|
xhr.open('GET', `${this.url}leaderboard/`);
|
||||||
|
xhr.send();
|
||||||
|
xhr.onload = () => {
|
||||||
|
user.leaderboard = JSON.parse(xhr.response);
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
@ -4,5 +4,6 @@ class User {
|
|||||||
this.password = "there";
|
this.password = "there";
|
||||||
this.userId = 0;
|
this.userId = 0;
|
||||||
this.tests;
|
this.tests;
|
||||||
|
this.leaderboard;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -31,6 +31,7 @@
|
|||||||
<script src="./screens/signUpScreen.js"></script>
|
<script src="./screens/signUpScreen.js"></script>
|
||||||
<script src="./screens/loginscreen.js"></script>
|
<script src="./screens/loginscreen.js"></script>
|
||||||
<script src="./screens/profilescreen.js"></script>
|
<script src="./screens/profilescreen.js"></script>
|
||||||
|
<script src="./screens/leaderboardscreen.js"></script>
|
||||||
|
|
||||||
<!-- API Script Files -->
|
<!-- API Script Files -->
|
||||||
<script src="./api/api.js"></script>
|
<script src="./api/api.js"></script>
|
||||||
|
@ -5,7 +5,8 @@ class EndScreen {
|
|||||||
new Button(110,0,100,30,0,true,"#fff",false,"#000","#000","Login"),
|
new Button(110,0,100,30,0,true,"#fff",false,"#000","#000","Login"),
|
||||||
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
||||||
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
||||||
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test")
|
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
||||||
|
new Button(550,0,140,30,0,true,"#fff",false,"#000","#000","Leaderboard"),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,11 +16,10 @@ class EndScreen {
|
|||||||
textAlign(CENTER, CENTER);
|
textAlign(CENTER, CENTER);
|
||||||
fill(0);
|
fill(0);
|
||||||
text("Test Complete\nPress enter to start another test", 0, 0, windowWidth - 100, windowHeight - 100);
|
text("Test Complete\nPress enter to start another test", 0, 0, windowWidth - 100, windowHeight - 100);
|
||||||
this.buttons[0].draw();
|
|
||||||
this.buttons[1].draw();
|
for (let i = 0; i < this.buttons.length; i++) {
|
||||||
this.buttons[2].draw();
|
this.buttons[i].draw()
|
||||||
this.buttons[3].draw();
|
}
|
||||||
this.buttons[4].draw();
|
|
||||||
if (this.buttons[0].isPressed()) {
|
if (this.buttons[0].isPressed()) {
|
||||||
screenManager.setScreen(new SignUpScreen());
|
screenManager.setScreen(new SignUpScreen());
|
||||||
} else if (this.buttons[1].isPressed()) {
|
} else if (this.buttons[1].isPressed()) {
|
||||||
@ -30,10 +30,12 @@ class EndScreen {
|
|||||||
screenManager.setScreen(new ProfileScreen());
|
screenManager.setScreen(new ProfileScreen());
|
||||||
} else if (this.buttons[4].isPressed()) {
|
} else if (this.buttons[4].isPressed()) {
|
||||||
screenManager.setScreen(new TestScreen())
|
screenManager.setScreen(new TestScreen())
|
||||||
|
} else if (this.buttons[5].isPressed()) {
|
||||||
|
screenManager.setScreen(new LeaderboardScreen())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
letterTyped() {
|
letterTyped(key) {
|
||||||
screenManager.setScreen(new StartScreen());
|
if (key === "ENTER") screenManager.setScreen(new StartScreen());
|
||||||
}
|
}
|
||||||
}
|
}
|
48
website/screens/leaderboardscreen.js
Normal file
48
website/screens/leaderboardscreen.js
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
class LeaderboardScreen {
|
||||||
|
constructor() {
|
||||||
|
this.buttons = [
|
||||||
|
new Button(0,0,100,30,0,true,"#fff",false,"#000","#000","Sign Up"),
|
||||||
|
new Button(110,0,100,30,0,true,"#fff",false,"#000","#000","Login"),
|
||||||
|
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
||||||
|
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
||||||
|
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
||||||
|
new Button(550,0,140,30,0,true,"#fff",false,"#000","#000","Leaderboard"),
|
||||||
|
];
|
||||||
|
api.getLeaderBoard()
|
||||||
|
}
|
||||||
|
|
||||||
|
draw() {
|
||||||
|
background("#eeeee4");
|
||||||
|
textSize(100);
|
||||||
|
textAlign(CENTER, CENTER);
|
||||||
|
fill("#000");
|
||||||
|
text("Leaderboard", 0, 100, windowWidth, 120);
|
||||||
|
for (let i = 0; i < this.buttons.length; i++) {
|
||||||
|
this.buttons[i].draw()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.buttons[0].isPressed()) {
|
||||||
|
screenManager.setScreen(new SignUpScreen());
|
||||||
|
} else if (this.buttons[1].isPressed()) {
|
||||||
|
screenManager.setScreen(new LoginScreen());
|
||||||
|
} else if (this.buttons[2].isPressed()) {
|
||||||
|
api.logout();
|
||||||
|
} else if (this.buttons[3].isPressed()) {
|
||||||
|
screenManager.setScreen(new ProfileScreen());
|
||||||
|
} else if (this.buttons[4].isPressed()) {
|
||||||
|
screenManager.setScreen(new TestScreen())
|
||||||
|
} else if (this.buttons[5].isPressed()) {
|
||||||
|
screenManager.setScreen(new LeaderboardScreen())
|
||||||
|
}
|
||||||
|
|
||||||
|
textSize(20);
|
||||||
|
fill("#000");
|
||||||
|
if (user.leaderboard != undefined) {
|
||||||
|
for (let i = 0; i < user.leaderboard.length; i++) {
|
||||||
|
text(`#${i+1}: ${user.leaderboard[i].username} : ${user.leaderboard[i].wpm}wpm`, 0, i*30+300, windowWidth, 30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fill("#000");
|
||||||
|
text(`Logged in as ${user.username}`, windowWidth-100, 15);
|
||||||
|
}
|
||||||
|
}
|
@ -58,6 +58,7 @@ class LoginScreen {
|
|||||||
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
||||||
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
||||||
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
||||||
|
new Button(550,0,140,30,0,true,"#fff",false,"#000","#000","Leaderboard"),
|
||||||
]
|
]
|
||||||
|
|
||||||
this.activeTextBox = 0
|
this.activeTextBox = 0
|
||||||
@ -93,11 +94,6 @@ class LoginScreen {
|
|||||||
screenManager.setScreen(new StartScreen());
|
screenManager.setScreen(new StartScreen());
|
||||||
}
|
}
|
||||||
|
|
||||||
this.buttons[3].draw();
|
|
||||||
this.buttons[4].draw();
|
|
||||||
this.buttons[5].draw();
|
|
||||||
this.buttons[6].draw();
|
|
||||||
this.buttons[7].draw();
|
|
||||||
if (this.buttons[3].isPressed()) {
|
if (this.buttons[3].isPressed()) {
|
||||||
screenManager.setScreen(new SignUpScreen());
|
screenManager.setScreen(new SignUpScreen());
|
||||||
} else if (this.buttons[4].isPressed()) {
|
} else if (this.buttons[4].isPressed()) {
|
||||||
@ -108,6 +104,8 @@ class LoginScreen {
|
|||||||
screenManager.setScreen(new ProfileScreen());
|
screenManager.setScreen(new ProfileScreen());
|
||||||
} else if (this.buttons[7].isPressed()) {
|
} else if (this.buttons[7].isPressed()) {
|
||||||
screenManager.setScreen(new TestScreen())
|
screenManager.setScreen(new TestScreen())
|
||||||
|
} else if (this.buttons[8].isPressed()) {
|
||||||
|
screenManager.setScreen(new LeaderboardScreen())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ class ProfileScreen {
|
|||||||
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
||||||
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
||||||
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
||||||
|
new Button(550,0,140,30,0,true,"#fff",false,"#000","#000","Leaderboard"),
|
||||||
];
|
];
|
||||||
api.getUserTests();
|
api.getUserTests();
|
||||||
}
|
}
|
||||||
@ -16,11 +17,10 @@ class ProfileScreen {
|
|||||||
textAlign(CENTER, CENTER);
|
textAlign(CENTER, CENTER);
|
||||||
fill("#000");
|
fill("#000");
|
||||||
text("Profile", 0, 100, windowWidth, 120);
|
text("Profile", 0, 100, windowWidth, 120);
|
||||||
this.buttons[0].draw();
|
|
||||||
this.buttons[1].draw();
|
for (let i = 0; i < this.buttons.length; i++) {
|
||||||
this.buttons[2].draw();
|
this.buttons[i].draw()
|
||||||
this.buttons[3].draw();
|
}
|
||||||
this.buttons[4].draw();
|
|
||||||
if (this.buttons[0].isPressed()) {
|
if (this.buttons[0].isPressed()) {
|
||||||
screenManager.setScreen(new SignUpScreen());
|
screenManager.setScreen(new SignUpScreen());
|
||||||
} else if (this.buttons[1].isPressed()) {
|
} else if (this.buttons[1].isPressed()) {
|
||||||
@ -31,6 +31,8 @@ class ProfileScreen {
|
|||||||
screenManager.setScreen(new ProfileScreen());
|
screenManager.setScreen(new ProfileScreen());
|
||||||
} else if (this.buttons[4].isPressed()) {
|
} else if (this.buttons[4].isPressed()) {
|
||||||
screenManager.setScreen(new TestScreen())
|
screenManager.setScreen(new TestScreen())
|
||||||
|
} else if (this.buttons[5].isPressed()) {
|
||||||
|
screenManager.setScreen(new LeaderboardScreen())
|
||||||
}
|
}
|
||||||
|
|
||||||
textSize(20);
|
textSize(20);
|
||||||
|
@ -57,6 +57,7 @@ class SignUpScreen {
|
|||||||
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
||||||
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
||||||
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
||||||
|
new Button(550,0,140,30,0,true,"#fff",false,"#000","#000","Leaderboard"),
|
||||||
]
|
]
|
||||||
|
|
||||||
this.activeTextBox = 0
|
this.activeTextBox = 0
|
||||||
@ -91,14 +92,7 @@ class SignUpScreen {
|
|||||||
this.textboxes[1].getWords()
|
this.textboxes[1].getWords()
|
||||||
)
|
)
|
||||||
screenManager.setScreen(new StartScreen());
|
screenManager.setScreen(new StartScreen());
|
||||||
}
|
} else if (this.buttons[3].isPressed()) {
|
||||||
|
|
||||||
this.buttons[3].draw();
|
|
||||||
this.buttons[4].draw();
|
|
||||||
this.buttons[5].draw();
|
|
||||||
this.buttons[6].draw();
|
|
||||||
this.buttons[7].draw();
|
|
||||||
if (this.buttons[3].isPressed()) {
|
|
||||||
screenManager.setScreen(new SignUpScreen());
|
screenManager.setScreen(new SignUpScreen());
|
||||||
} else if (this.buttons[4].isPressed()) {
|
} else if (this.buttons[4].isPressed()) {
|
||||||
screenManager.setScreen(new LoginScreen());
|
screenManager.setScreen(new LoginScreen());
|
||||||
@ -108,6 +102,8 @@ class SignUpScreen {
|
|||||||
screenManager.setScreen(new ProfileScreen());
|
screenManager.setScreen(new ProfileScreen());
|
||||||
} else if (this.buttons[7].isPressed()) {
|
} else if (this.buttons[7].isPressed()) {
|
||||||
screenManager.setScreen(new TestScreen())
|
screenManager.setScreen(new TestScreen())
|
||||||
|
} else if (this.buttons[5].isPressed()) {
|
||||||
|
screenManager.setScreen(new LeaderboardScreen())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ class StartScreen {
|
|||||||
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
new Button(220,0,100,30,0,true,"#fff",false,"#000","#000","Logout"),
|
||||||
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
new Button(330,0,100,30,0,true,"#fff",false,"#000","#000","Profile"),
|
||||||
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
new Button(440,0,100,30,0,true,"#fff",false,"#000","#000","Test"),
|
||||||
|
new Button(550,0,140,30,0,true,"#fff",false,"#000","#000","Leaderboard"),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,11 +16,10 @@ class StartScreen {
|
|||||||
textAlign(CENTER, CENTER);
|
textAlign(CENTER, CENTER);
|
||||||
fill("#000");
|
fill("#000");
|
||||||
text("Press enter to start test", 0, 0, windowWidth - 100, windowHeight - 100);
|
text("Press enter to start test", 0, 0, windowWidth - 100, windowHeight - 100);
|
||||||
this.buttons[0].draw();
|
|
||||||
this.buttons[1].draw();
|
for (let i = 0; i < this.buttons.length; i++) {
|
||||||
this.buttons[2].draw();
|
this.buttons[i].draw()
|
||||||
this.buttons[3].draw();
|
}
|
||||||
this.buttons[4].draw();
|
|
||||||
if (this.buttons[0].isPressed()) {
|
if (this.buttons[0].isPressed()) {
|
||||||
screenManager.setScreen(new SignUpScreen());
|
screenManager.setScreen(new SignUpScreen());
|
||||||
} else if (this.buttons[1].isPressed()) {
|
} else if (this.buttons[1].isPressed()) {
|
||||||
@ -30,7 +30,10 @@ class StartScreen {
|
|||||||
screenManager.setScreen(new ProfileScreen());
|
screenManager.setScreen(new ProfileScreen());
|
||||||
} else if (this.buttons[4].isPressed()) {
|
} else if (this.buttons[4].isPressed()) {
|
||||||
screenManager.setScreen(new TestScreen());
|
screenManager.setScreen(new TestScreen());
|
||||||
|
} else if (this.buttons[5].isPressed()) {
|
||||||
|
screenManager.setScreen(new LeaderboardScreen())
|
||||||
}
|
}
|
||||||
|
|
||||||
fill("#000");
|
fill("#000");
|
||||||
text(`${user.username}`, windowWidth-100, 15);
|
text(`${user.username}`, windowWidth-100, 15);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user