Skip to content

Commit f4c2dad

Browse files
committed
rs format
1 parent 286e918 commit f4c2dad

File tree

13 files changed

+409
-1099
lines changed

13 files changed

+409
-1099
lines changed

src/analyze.rs

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,9 @@ fn season_2023(data: &web::Json<db_main::MainInsert>) -> Result<AnalysisResults,
4141
let analyzer = vader_sentiment::SentimentIntensityAnalyzer::new();
4242
let game_data = data.game.split(",").collect::<Vec<_>>();
4343
let analysis_results: Vec<f64> = vec![
44-
analyzer
45-
.polarity_scores(data.defend.as_str())
46-
.get("compound")
47-
.unwrap()
48-
.clone(),
49-
analyzer
50-
.polarity_scores(data.driving.as_str())
51-
.get("compound")
52-
.unwrap()
53-
.clone(),
54-
analyzer
55-
.polarity_scores(data.overall.as_str())
56-
.get("compound")
57-
.unwrap()
58-
.clone(),
44+
analyzer.polarity_scores(data.defend.as_str()).get("compound").unwrap().clone(),
45+
analyzer.polarity_scores(data.driving.as_str()).get("compound").unwrap().clone(),
46+
analyzer.polarity_scores(data.overall.as_str()).get("compound").unwrap().clone(),
5947
];
6048

6149
let mut score: f64 = 0.0;
@@ -208,10 +196,7 @@ fn season_2023(data: &web::Json<db_main::MainInsert>) -> Result<AnalysisResults,
208196

209197
let string_mps_scores: Vec<String> = mps_scores.iter().map(|float| float.to_string()).collect();
210198

211-
let string_analysis_results: Vec<String> = analysis_results
212-
.iter()
213-
.map(|float| float.to_string())
214-
.collect();
199+
let string_analysis_results: Vec<String> = analysis_results.iter().map(|float| float.to_string()).collect();
215200

216201
Ok(AnalysisResults {
217202
weight: string_mps_scores.join(","),
@@ -229,25 +214,12 @@ pub struct MatchTime2024 {
229214

230215
fn season_2024(data: &web::Json<db_main::MainInsert>) -> Result<AnalysisResults, Error> {
231216
let analyzer = vader_sentiment::SentimentIntensityAnalyzer::new();
232-
let game_data: Vec<MatchTime2024> =
233-
serde_json::from_str(data.game.as_str()).expect("failed to convert");
217+
let game_data: Vec<MatchTime2024> = serde_json::from_str(data.game.as_str()).expect("failed to convert");
234218

235219
let analysis_results: Vec<f64> = vec![
236-
analyzer
237-
.polarity_scores(data.defend.as_str())
238-
.get("compound")
239-
.unwrap()
240-
.clone(),
241-
analyzer
242-
.polarity_scores(data.driving.as_str())
243-
.get("compound")
244-
.unwrap()
245-
.clone(),
246-
analyzer
247-
.polarity_scores(data.overall.as_str())
248-
.get("compound")
249-
.unwrap()
250-
.clone(),
220+
analyzer.polarity_scores(data.defend.as_str()).get("compound").unwrap().clone(),
221+
analyzer.polarity_scores(data.driving.as_str()).get("compound").unwrap().clone(),
222+
analyzer.polarity_scores(data.overall.as_str()).get("compound").unwrap().clone(),
251223
];
252224

253225
let mut score: f64 = 0.0;
@@ -294,9 +266,11 @@ fn season_2024(data: &web::Json<db_main::MainInsert>) -> Result<AnalysisResults,
294266
8 => auto_scores = time.intake as i64,
295267
_ => {}
296268
}
297-
intake_time += time.intake;
298-
travel_time += time.travel;
299-
outtake_time += time.outtake;
269+
if time.score_type == 0 || time.score_type == 1 || time.score_type == 9 {
270+
intake_time += time.intake;
271+
travel_time += time.travel;
272+
outtake_time += time.outtake;
273+
}
300274
}
301275

302276
score += auto_scores as f64 * 18.0;
@@ -316,8 +290,7 @@ fn season_2024(data: &web::Json<db_main::MainInsert>) -> Result<AnalysisResults,
316290
fast_intake = score * (100.0 / (intake_time / (game_data.len() - 3) as f64));
317291
fast_travel = score * (100.0 / (travel_time / (game_data.len() - 3) as f64));
318292
fast_shoot = score * (100.0 / (outtake_time / (game_data.len() - 3) as f64));
319-
fast_cycle = score
320-
* (100.0 / (intake_time + travel_time + outtake_time / (game_data.len() - 3) as f64));
293+
fast_cycle = score * (100.0 / (intake_time + travel_time + outtake_time / (game_data.len() - 3) as f64));
321294
} else {
322295
fast_intake = 0.0;
323296
fast_travel = 0.0;
@@ -345,8 +318,7 @@ fn season_2024(data: &web::Json<db_main::MainInsert>) -> Result<AnalysisResults,
345318

346319
let string_mps_scores: Vec<String> = mps_scores.iter().map(|float| float.to_string()).collect();
347320

348-
let string_analysis_results: Vec<String> =
349-
analysis.iter().map(|float| float.to_string()).collect();
321+
let string_analysis_results: Vec<String> = analysis.iter().map(|float| float.to_string()).collect();
350322

351323
Ok(AnalysisResults {
352324
weight: string_mps_scores.join(","),

src/auth.rs

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,19 @@ pub struct CreateForm {
2727
password: String,
2828
}
2929

30-
pub async fn create_account(
31-
pool: &db_auth::Pool,
32-
create_form: web::Json<CreateForm>,
33-
) -> impl Responder {
30+
pub async fn create_account(pool: &db_auth::Pool, create_form: web::Json<CreateForm>) -> impl Responder {
3431
// check password length is between 8 and 32, inclusive
3532
if create_form.password.len() >= 8 && create_form.password.len() <= 64 {
3633
// check if user is a sketchy motherfucker
3734
let regex = Regex::new(r"^[a-z0-9A-Z- ~!@#$%^&*()=+/\_[_]{}|?.,]{3,64}$").unwrap();
38-
if !regex.is_match(&create_form.username)
39-
|| !regex.is_match(&create_form.password)
40-
|| !regex.is_match(&create_form.full_name)
41-
{
35+
if !regex.is_match(&create_form.username) || !regex.is_match(&create_form.password) || !regex.is_match(&create_form.full_name) {
4236
return HttpResponse::BadRequest()
4337
.status(StatusCode::from_u16(400).unwrap())
4438
.insert_header(("Cache-Control", "no-cache"))
4539
.body("{\"status\": \"you_sketchy_motherfucker\"}");
4640
}
4741
// check if username is taken
48-
let target_user_temp: Result<db_auth::User, actix_web::Error> =
49-
db_auth::get_user_username(pool, create_form.username.clone()).await;
42+
let target_user_temp: Result<db_auth::User, actix_web::Error> = db_auth::get_user_username(pool, create_form.username.clone()).await;
5043
if target_user_temp.is_ok() {
5144
return HttpResponse::BadRequest()
5245
.status(StatusCode::from_u16(409).unwrap())
@@ -57,12 +50,7 @@ pub async fn create_account(
5750
// check access key validity
5851
if create_form.access != "00000" {
5952
let access_key_temp: Result<Vec<db_auth::AccessKey>, actix_web::Error> =
60-
db_auth::get_access_key(
61-
pool,
62-
create_form.access.clone(),
63-
db_auth::AccessKeyQuery::ById,
64-
)
65-
.await;
53+
db_auth::get_access_key(pool, create_form.access.clone(), db_auth::AccessKeyQuery::ById).await;
6654
if access_key_temp.is_err() {
6755
return HttpResponse::BadRequest()
6856
.status(StatusCode::from_u16(403).unwrap())
@@ -72,15 +60,14 @@ pub async fn create_account(
7260
// insert into database
7361
let access_key = access_key_temp.unwrap().first().cloned();
7462
if let Some(valid_key) = access_key {
75-
let user_temp: Result<db_auth::User, actix_web::Error> =
76-
db_auth::create_user(
77-
pool,
78-
valid_key.team,
79-
html_escape::encode_text(&create_form.full_name).to_string(),
80-
html_escape::encode_text(&create_form.username).to_string(),
81-
html_escape::encode_text(&create_form.password).to_string(),
82-
)
83-
.await;
63+
let user_temp: Result<db_auth::User, actix_web::Error> = db_auth::create_user(
64+
pool,
65+
valid_key.team,
66+
html_escape::encode_text(&create_form.full_name).to_string(),
67+
html_escape::encode_text(&create_form.username).to_string(),
68+
html_escape::encode_text(&create_form.password).to_string(),
69+
)
70+
.await;
8471
// send final success/failure for creation
8572
if user_temp.is_err() {
8673
return HttpResponse::BadRequest()
@@ -139,8 +126,7 @@ pub async fn login(
139126
login_form: web::Json<LoginForm>,
140127
) -> impl Responder {
141128
// try to get target user from database
142-
let target_user_temp: Result<db_auth::User, actix_web::Error> =
143-
db_auth::get_user_username(pool, login_form.username.clone()).await;
129+
let target_user_temp: Result<db_auth::User, actix_web::Error> = db_auth::get_user_username(pool, login_form.username.clone()).await;
144130
if target_user_temp.is_err() {
145131
// query error, send failure response
146132
return HttpResponse::BadRequest()
@@ -171,10 +157,11 @@ pub async fn login(
171157
// save the username to the identity
172158
identity.remember(login_form.username.clone());
173159
// write the user object to the session
174-
session.write().unwrap().user_map.insert(
175-
target_user.clone().username.to_string(),
176-
target_user.clone(),
177-
);
160+
session
161+
.write()
162+
.unwrap()
163+
.user_map
164+
.insert(target_user.clone().username.to_string(), target_user.clone());
178165
// if admin, send admin success response
179166
if target_user.admin == "true" {
180167
// user is admin, send admin response for client cookie
@@ -214,12 +201,8 @@ pub async fn login(
214201
}
215202
}
216203

217-
pub async fn delete_account(
218-
pool: &db_auth::Pool,
219-
login_form: web::Json<LoginForm>,
220-
) -> Result<HttpResponse, actix_web::Error> {
221-
let target_user_temp: Result<db_auth::User, actix_web::Error> =
222-
db_auth::get_user_username(pool, login_form.username.clone()).await;
204+
pub async fn delete_account(pool: &db_auth::Pool, login_form: web::Json<LoginForm>) -> Result<HttpResponse, actix_web::Error> {
205+
let target_user_temp: Result<db_auth::User, actix_web::Error> = db_auth::get_user_username(pool, login_form.username.clone()).await;
223206
if target_user_temp.is_err() {
224207
return Ok(HttpResponse::BadRequest()
225208
.status(StatusCode::from_u16(400).unwrap())
@@ -240,12 +223,7 @@ pub async fn delete_account(
240223
.is_ok()
241224
{
242225
Ok(HttpResponse::Ok().json(
243-
db_auth::execute_manage_user(
244-
&pool,
245-
db_auth::UserManageAction::DeleteUser,
246-
[target_user.id.to_string(), "".to_string()],
247-
)
248-
.await?,
226+
db_auth::execute_manage_user(&pool, db_auth::UserManageAction::DeleteUser, [target_user.id.to_string(), "".to_string()]).await?,
249227
))
250228
} else {
251229
Ok(HttpResponse::BadRequest()
@@ -261,10 +239,7 @@ pub async fn delete_account(
261239
}
262240
}
263241

264-
pub async fn logout(
265-
session: web::Data<RwLock<crate::Sessions>>,
266-
identity: Identity,
267-
) -> HttpResponse {
242+
pub async fn logout(session: web::Data<RwLock<crate::Sessions>>, identity: Identity) -> HttpResponse {
268243
// if session exists, proceed
269244
if let Some(id) = identity.identity() {
270245
// forget identity

0 commit comments

Comments
 (0)