Compare commits

..

No commits in common. "3daa17cccfeab0f6e7bb8972fcfbbfc28a42ec9d" and "2d15914d1ad77eb918f4fde2d106a6892b103fc4" have entirely different histories.

3 changed files with 44 additions and 67 deletions

View file

@ -6,9 +6,7 @@ CREATE TABLE users (
account_id INTEGER references accounts(id), account_id INTEGER references accounts(id),
registered INTEGER NOT NULL, registered INTEGER NOT NULL,
-- idk why but we get weird errors if we use `COLLATE case_insensitive` username TEXT NOT NULL COLLATE case_insensitive,
-- maybe troubleshoot later, this works fine for now.
username TEXT NOT NULL, -- COLLATE case_insensitive,
stars INTEGER NOT NULL DEFAULT 0, stars INTEGER NOT NULL DEFAULT 0,
demons INTEGER NOT NULL DEFAULT 0, demons INTEGER NOT NULL DEFAULT 0,

View file

@ -34,6 +34,6 @@ _these features are implemented_
## todo ## todo
- get users multiple pages - ___fix the fucking get users for usernames!!!!___
- move authorization logic to (./src/helpers/accounts.rs)[./src/helpers/accounts.rs] - move authorization logic to (./src/helpers/accounts.rs)[./src/helpers/accounts.rs]
- make gjp2 authentication faster (bcrypt?) - make gjp2 authentication faster (bcrypt?)

View file

@ -3,6 +3,7 @@ use rocket::http::Status;
use rocket::response::status; use rocket::response::status;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::result::Error;
use crate::helpers; use crate::helpers;
use crate::db; use crate::db;
@ -13,7 +14,7 @@ pub struct FormGetUsers {
str: String str: String
} }
#[post("/getGJUsers20.php", data = "<input>")] #[post("/accounts/getGJUsers20.php", data = "<input>")]
pub fn get_users(input: Form<FormGetUsers>) -> status::Custom<&'static str> { pub fn get_users(input: Form<FormGetUsers>) -> status::Custom<&'static str> {
let connection = &mut db::establish_connection_pg(); let connection = &mut db::establish_connection_pg();
@ -21,75 +22,53 @@ pub fn get_users(input: Form<FormGetUsers>) -> status::Custom<&'static str> {
use crate::schema::users::dsl::*; use crate::schema::users::dsl::*;
use crate::models::User; use crate::models::User;
let mut query_users = users.into_boxed(); let mut query = users.into_boxed();
match input.str.parse::<i32>() { match input.str.parse::<i32>() {
Ok(id_value) => query_users = query_users.filter(id.eq(id_value)), Ok(id_value) => query = query.filter(id.eq(id_value)),
Err(_) => query_users = query_users.filter(username.ilike(input.str.to_owned() + "%")) Err(_) => query = query.filter(username.like(input.str.to_owned() + "%"))
}; };
let mut results: Vec<String> = vec![]; let results = query
.order(stars.desc())
.limit(10)
.offset(input.page * 10)
.get_result::<User, >(connection)
.expect("Fatal error loading users");
for result in { let response = helpers::format::format(hashmap! {
query_users 1 => results.username,
.order(stars.desc()) 2 => results.id.to_string(),
.offset(input.page * 10) 3 => results.stars.to_string(),
.limit(10) 4 => results.demons.to_string(),
.get_results::<User, >(connection) 8 => results.creator_points.to_string(),
.expect("Fatal error loading users") 9 => {
} { vec![
let formatted_result = helpers::format::format(hashmap! { results.cube,
1 => result.username, results.ship,
2 => result.id.to_string(), results.ball,
3 => result.stars.to_string(), results.ufo,
4 => result.demons.to_string(), results.wave,
8 => result.creator_points.to_string(), results.robot
9 => { ][results.icon_type as usize].to_string()
vec![ },
result.cube, 10 => results.color1.to_string(),
result.ship, 11 => results.color2.to_string(),
result.ball, 13 => results.coins.to_string(),
result.ufo, 14 => results.icon_type.to_string(),
result.wave, 15 => results.special.to_string(),
result.robot 16 => {
][result.icon_type as usize].to_string() match results.account_id {
}, Some(account_id_value) => account_id_value.to_string(),
10 => result.color1.to_string(), None => match results.udid {
11 => result.color2.to_string(), Some(udid_value) => udid_value.to_string(),
13 => result.coins.to_string(), None => panic!("user has no account_id or udid?!?!?")
14 => result.icon_type.to_string(),
15 => result.special.to_string(),
16 => {
match result.account_id {
Some(account_id_value) => account_id_value.to_string(),
None => match result.udid {
Some(udid_value) => udid_value.to_string(),
None => panic!("user has no account_id or udid?!?!?")
}
} }
} }
}); }
});
results.push(formatted_result) println!("{}", response);
};
let mut query_users_count = users.into_boxed(); return status::Custom(Status::Ok, "1")
match input.str.parse::<i32>() {
Ok(id_value) => query_users_count = query_users_count.filter(id.eq(id_value)),
Err(_) => query_users_count = query_users_count.filter(username.ilike(input.str.to_owned() + "%"))
};
let amount = query_users_count
.count()
.get_result::<i64>(connection)
.expect("Error querying user count");
let response = if amount < 1 {
String::from("-1")
} else {
vec![results.join("|"), format!("{}:#{}:10", amount, input.page * 10)].join("#")
};
return status::Custom(Status::Ok, Box::leak(response.into_boxed_str()))
} }