From 388ed83bc15216d9b63712cf70482727c0e1a445 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Fri, 24 Mar 2023 12:32:07 +0200 Subject: [PATCH] chore(client, server, api, shared): fix clippy lints --- api/src/actions/game/info.rs | 2 +- api/src/actions/game/join.rs | 2 +- api/src/actions/user/create.rs | 4 +- api/src/handlers/game/mod.rs | 34 ++-- api/src/handlers/user/create.rs | 6 +- api/src/handlers/user/info.rs | 4 +- client/src/main.rs | 2 + client/src/plugins/menu/ui/play/ui.rs | 223 ++++++++++++++---------- client/src/util/sl.rs | 10 +- server/src/systems/event/message/mod.rs | 6 +- server/src/systems/event/mod.rs | 4 +- shared/src/api/mod.rs | 2 +- shared/src/types/game.rs | 16 +- 13 files changed, 175 insertions(+), 140 deletions(-) diff --git a/api/src/actions/game/info.rs b/api/src/actions/game/info.rs index 6c3e0be..7d2fd33 100644 --- a/api/src/actions/game/info.rs +++ b/api/src/actions/game/info.rs @@ -9,6 +9,6 @@ use diesel::PgConnection; use laurelin_shared::error::api::APIError; -pub(crate) fn info(conn: &mut PgConnection) -> Result<(), APIError> { +pub(crate) fn info(_conn: &mut PgConnection) -> Result<(), APIError> { Err(APIError::Undefined) } diff --git a/api/src/actions/game/join.rs b/api/src/actions/game/join.rs index c05a7b4..92511fd 100644 --- a/api/src/actions/game/join.rs +++ b/api/src/actions/game/join.rs @@ -9,6 +9,6 @@ use diesel::PgConnection; use laurelin_shared::error::api::APIError; -pub(crate) fn join(conn: &mut PgConnection) -> Result<(), APIError> { +pub(crate) fn join(_conn: &mut PgConnection) -> Result<(), APIError> { Err(APIError::Undefined) } diff --git a/api/src/actions/user/create.rs b/api/src/actions/user/create.rs index 4e7d081..967401a 100644 --- a/api/src/actions/user/create.rs +++ b/api/src/actions/user/create.rs @@ -50,8 +50,8 @@ pub(crate) fn create(conn: &mut PgConnection, user: &InsertableUser) -> Result { // TODO: we certainly could handle Diesel errors here... - return Err(APIError::UserCreationFailed); + Err(APIError::UserCreationFailed) } - Ok(u) => return Ok(u), + Ok(u) => Ok(u), } } diff --git a/api/src/handlers/game/mod.rs b/api/src/handlers/game/mod.rs index b90f07d..4628d3c 100644 --- a/api/src/handlers/game/mod.rs +++ b/api/src/handlers/game/mod.rs @@ -28,9 +28,7 @@ async fn create(session: Session, pool: web::Data) -> impl Responder { }) .await; match game_create { - Err(_err) => { - return HttpResponse::InternalServerError().json(APIError::Undefined); - } + Err(_err) => HttpResponse::InternalServerError().json(APIError::Undefined), Ok(game_res) => match game_res { Err(err) => HttpResponse::InternalServerError().body(err.to_string()), Ok(game) => HttpResponse::Ok().json(game), @@ -41,27 +39,35 @@ async fn create(session: Session, pool: web::Data) -> impl Responder { } #[get("/api/game/{id}")] -async fn info(session: Session, pool: web::Data, id: web::Path) -> impl Responder { +async fn info( + session: Session, + _pool: web::Data, + _id: web::Path, +) -> impl Responder { let session_validation = session::validate_session(&session); match session_validation { Err(err) => err, - Ok(user_id) => { + Ok(_user_id) => { // - return HttpResponse::Ok().body("INFO"); + HttpResponse::Ok().body("INFO") } } } #[post("/api/game/{id}/join")] -async fn join(session: Session, pool: web::Data, id: web::Path) -> impl Responder { +async fn join( + session: Session, + _pool: web::Data, + _id: web::Path, +) -> impl Responder { let session_validation = session::validate_session(&session); match session_validation { Err(err) => err, - Ok(user_id) => { + Ok(_user_id) => { // - return HttpResponse::Ok().body("JOIN"); + HttpResponse::Ok().body("JOIN") } } } @@ -72,7 +78,7 @@ async fn all_forming(session: Session, pool: web::Data) -> impl Responde match session_validation { Err(err) => err, - Ok(user_id) => { + Ok(_user_id) => { let all_forming = web::block(move || { let mut conn = match pool.get() { Err(_) => return Err(APIError::DatabasePoolGetFailed), @@ -82,9 +88,7 @@ async fn all_forming(session: Session, pool: web::Data) -> impl Responde }) .await; match all_forming { - Err(_err) => { - return HttpResponse::InternalServerError().json(APIError::Undefined); - } + Err(_err) => HttpResponse::InternalServerError().json(APIError::Undefined), Ok(games_res) => match games_res { Err(err) => HttpResponse::InternalServerError().body(err.to_string()), Ok(games) => HttpResponse::Ok().json(games), @@ -110,9 +114,7 @@ async fn my_games(session: Session, pool: web::Data) -> impl Responder { }) .await; match my_games { - Err(_err) => { - return HttpResponse::InternalServerError().json(APIError::Undefined); - } + Err(_err) => HttpResponse::InternalServerError().json(APIError::Undefined), Ok(games_res) => match games_res { Err(err) => HttpResponse::InternalServerError().body(err.to_string()), Ok(games) => HttpResponse::Ok().json(games), diff --git a/api/src/handlers/user/create.rs b/api/src/handlers/user/create.rs index 8531084..59eb31a 100644 --- a/api/src/handlers/user/create.rs +++ b/api/src/handlers/user/create.rs @@ -44,12 +44,10 @@ pub(crate) async fn create( match user_create { Err(_err) => { // TODO: handle? - return HttpResponse::InternalServerError().json(APIError::Undefined); + HttpResponse::InternalServerError().json(APIError::Undefined) } Ok(user_res) => match user_res { - Err(err) => { - return HttpResponse::InternalServerError().json(err); - } + Err(err) => HttpResponse::InternalServerError().json(err), Ok(user) => match session.insert("user_id", user.id) { Err(err) => HttpResponse::InternalServerError().body(err.to_string()), Ok(_) => HttpResponse::Ok().json(user), diff --git a/api/src/handlers/user/info.rs b/api/src/handlers/user/info.rs index 38a236c..9be7723 100644 --- a/api/src/handlers/user/info.rs +++ b/api/src/handlers/user/info.rs @@ -32,9 +32,7 @@ pub(crate) async fn info( }) .await; match user_details { - Err(_err) => { - return HttpResponse::InternalServerError().json(APIError::Undefined); - } + Err(_err) => HttpResponse::InternalServerError().json(APIError::Undefined), Ok(user_details_res) => match user_details_res { Err(err) => HttpResponse::InternalServerError().body(err.to_string()), Ok(user_details) => HttpResponse::Ok().json(user_details), diff --git a/client/src/main.rs b/client/src/main.rs index 89ae73b..7c818b4 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -6,6 +6,8 @@ * See LICENSE for licensing information. */ +#![allow(clippy::too_many_arguments)] + use bevy::{ app::AppExit, prelude::*, diff --git a/client/src/plugins/menu/ui/play/ui.rs b/client/src/plugins/menu/ui/play/ui.rs index 2b074b6..0aacdba 100644 --- a/client/src/plugins/menu/ui/play/ui.rs +++ b/client/src/plugins/menu/ui/play/ui.rs @@ -8,7 +8,10 @@ use bevy::prelude::*; use bevy_egui::{egui, EguiContexts}; -use laurelin_shared::types::game::{GAMESTATE_FINISHED, GAMESTATE_INPROGRESS}; +use laurelin_shared::types::{ + game::{GAMESTATE_FINISHED, GAMESTATE_INPROGRESS}, + user::UserPub, +}; use crate::{ cfg::CfgUser, @@ -17,6 +20,7 @@ use crate::{ networking::send::game::{GameAllFormingEvent, GameCreateEvent, GameMyGamesEvent}, }, util::egui::menuwindow, + Global, }; use super::{PlayScreenBrowseState, PlayScreenData, PlayScreenState}; @@ -26,6 +30,7 @@ pub fn ui( mut egui_contexts: EguiContexts, mut data: ResMut, cfg_user: Res, + global: Res, mut creategame_ev_w: EventWriter, mut allforming_ev_w: EventWriter, mut mygames_ev_w: EventWriter, @@ -100,15 +105,15 @@ pub fn ui( }); ui.vertical_centered(|ui| { - egui::ScrollArea::vertical().show(ui, |mut ui| match data.browse_state { + egui::ScrollArea::vertical().show(ui, |ui| match data.browse_state { PlayScreenBrowseState::Forming => { - browse_forming(&mut ui, &mut data, &cfg_user); + browse_forming(ui, &mut data, &cfg_user, &global); } PlayScreenBrowseState::InProgress => { - browse_inprogress(&mut ui, &mut data); + browse_inprogress(ui, &mut data, &global); } PlayScreenBrowseState::Finished => { - browse_finished(&mut ui, &mut data); + browse_finished(ui, &mut data, &global); } }); }); @@ -150,123 +155,163 @@ pub fn ui( ); } -fn browse_forming(ui: &mut egui::Ui, data: &mut PlayScreenData, cfg_user: &CfgUser) { +fn browse_forming( + ui: &mut egui::Ui, + data: &mut PlayScreenData, + cfg_user: &CfgUser, + global: &Global, +) { if data.waiting_for_all_forming { ui.horizontal(|ui| { ui.spinner(); ui.label("loading..."); }); + return; + } + + if data.all_forming.is_empty() { + ui.label("No forming games found."); } else { - if data.all_forming.is_empty() { - ui.label("No forming games found."); - } else { - for game in data.all_forming.clone() { - egui::Frame::none() - .fill(egui::Color32::BLACK) - .rounding(4.) - .outer_margin(4.) - .inner_margin(4.) - .show(ui, |ui| { - ui.horizontal(|ui| { - //ui.label(format!("Host: {}", game.host.username)); - ui.label("Host: host-username-here"); - ui.with_layout( - egui::Layout::right_to_left(egui::Align::Center), - |ui| { - if game.guest_id.clone().unwrap_or_default().to_string() - == cfg_user.id - { - ui.add_enabled(false, egui::Button::new("Joined")); - if ui.button("Inspect").clicked() { - data.cur_game = Some(game.clone()); - data.state = PlayScreenState::InLobbyGuest; - } - } else if game.host_id.to_string() == cfg_user.id { - ui.add_enabled(false, egui::Button::new("Host")); - if ui.button("Inspect").clicked() { - data.cur_game = Some(game.clone()); - data.state = PlayScreenState::InLobbyHost; - } - } else { - if ui.button("Join").clicked() { - //data.cur_game = Some(game.clone()); - //joingame_ev_w.send(JoinGameEvent); - } - } - }, - ); + for game in data.all_forming.clone() { + egui::Frame::none() + .fill(egui::Color32::BLACK) + .rounding(4.) + .outer_margin(4.) + .inner_margin(4.) + .show(ui, |ui| { + ui.horizontal(|ui| { + ui.label(format!( + "Host: {}", + global + .users_cache + .iter() + .filter(|u| u.id == game.host_id) + .cloned() + .collect::>() + .first() + .unwrap_or(&UserPub { + id: "".to_string(), + created_at: "".to_string(), + username: "".to_string() + }) + .username + )); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if game.guest_id.clone().unwrap_or_default() == cfg_user.id { + ui.add_enabled(false, egui::Button::new("Joined")); + if ui.button("Inspect").clicked() { + data.cur_game = Some(game.clone()); + data.state = PlayScreenState::InLobbyGuest; + } + } else if game.host_id == cfg_user.id { + ui.add_enabled(false, egui::Button::new("Host")); + if ui.button("Inspect").clicked() { + data.cur_game = Some(game.clone()); + data.state = PlayScreenState::InLobbyHost; + } + } else if ui.button("Join").clicked() { + //data.cur_game = Some(game.clone()); + //joingame_ev_w.send(JoinGameEvent); + } }); }); - } + }); } } } -fn browse_inprogress(ui: &mut egui::Ui, data: &mut PlayScreenData) { +fn browse_inprogress(ui: &mut egui::Ui, data: &mut PlayScreenData, global: &Global) { if data.waiting_for_my_games { ui.horizontal(|ui| { ui.spinner(); ui.label("loading..."); }); + return; + } + + if data.my_games.is_empty() { + ui.label("No games found."); } else { - if data.my_games.is_empty() { - ui.label("No games found."); - } else { - let mut games = data.my_games.clone(); - games.retain(|g| g.state == GAMESTATE_INPROGRESS); + let mut games = data.my_games.clone(); + games.retain(|g| g.state == GAMESTATE_INPROGRESS); - for game in games { - egui::Frame::none() - .fill(egui::Color32::BLACK) - .rounding(4.) - .outer_margin(4.) - .inner_margin(4.) - .show(ui, |ui| { - ui.horizontal(|ui| { - //ui.label(format!("Host: {}", game.host.username)); - ui.label("Host: host-username-here"); - ui.with_layout( - egui::Layout::right_to_left(egui::Align::Center), - |ui| { - if ui.button("Resume").clicked() { - data.cur_game = Some(game.clone()); - //resumegame_ev_w.send(ResumeGameEvent); - } - }, - ); + for game in games { + egui::Frame::none() + .fill(egui::Color32::BLACK) + .rounding(4.) + .outer_margin(4.) + .inner_margin(4.) + .show(ui, |ui| { + ui.horizontal(|ui| { + ui.label(format!( + "Host: {}", + global + .users_cache + .iter() + .filter(|u| u.id == game.host_id) + .cloned() + .collect::>() + .first() + .unwrap_or(&UserPub { + id: "".to_string(), + created_at: "".to_string(), + username: "".to_string() + }) + .username + )); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.button("Resume").clicked() { + data.cur_game = Some(game.clone()); + //resumegame_ev_w.send(ResumeGameEvent); + } }); }); - } + }); } } } -fn browse_finished(ui: &mut egui::Ui, data: &mut PlayScreenData) { +fn browse_finished(ui: &mut egui::Ui, data: &mut PlayScreenData, global: &Global) { if data.waiting_for_my_games { ui.horizontal(|ui| { ui.spinner(); ui.label("loading..."); }); + return; + } + + if data.my_games.is_empty() { + ui.label("No games found."); } else { - if data.my_games.is_empty() { - ui.label("No games found."); - } else { - let mut games = data.my_games.clone(); - games.retain(|g| g.state == GAMESTATE_FINISHED); + let mut games = data.my_games.clone(); + games.retain(|g| g.state == GAMESTATE_FINISHED); - for game in games { - egui::Frame::none() - .fill(egui::Color32::BLACK) - .rounding(4.) - .outer_margin(4.) - .inner_margin(4.) - .show(ui, |ui| { - ui.horizontal(|ui| { - ui.label("Host: host-username-here"); - //ui.label(format!("Host: {}", game.host.username)); - }); + for game in games { + egui::Frame::none() + .fill(egui::Color32::BLACK) + .rounding(4.) + .outer_margin(4.) + .inner_margin(4.) + .show(ui, |ui| { + ui.horizontal(|ui| { + ui.label(format!( + "Host: {}", + global + .users_cache + .iter() + .filter(|u| u.id == game.host_id) + .cloned() + .collect::>() + .first() + .unwrap_or(&UserPub { + id: "".to_string(), + created_at: "".to_string(), + username: "".to_string() + }) + .username + )); }); - } + }); } } } diff --git a/client/src/util/sl.rs b/client/src/util/sl.rs index 9888dc3..30d7bcf 100644 --- a/client/src/util/sl.rs +++ b/client/src/util/sl.rs @@ -17,10 +17,12 @@ pub fn load serde::Deserialize<'a>>(target_dir: &str, file_ true => { info!("sl::load found '{}'", file_path); let json: Vec = std::fs::read(file_path.clone()).unwrap(); - serde_json::from_str(std::str::from_utf8(&json).unwrap()).expect(&format!( - "sl::load couldn't deserialize the config at '{}'", - file_path - )) + serde_json::from_str(std::str::from_utf8(&json).unwrap()).unwrap_or_else(|_| { + panic!( + "sl::load couldn't deserialize the config at '{}'", + file_path + ) + }) } false => { warn!("sl::load couldn't find '{}', using defaults", file_path); diff --git a/server/src/systems/event/message/mod.rs b/server/src/systems/event/message/mod.rs index 3377e7e..6ce8709 100644 --- a/server/src/systems/event/message/mod.rs +++ b/server/src/systems/event/message/mod.rs @@ -41,7 +41,7 @@ pub(crate) fn message_events( DataRequestType::GameCreate => { // TODO: handle let cookie = global.user_to_session_map.get(&user_key).unwrap(); - let wrapped = create(&config.api_address, &cookie); + let wrapped = create(&config.api_address, cookie); let game = match wrapped.response { ResponseCreateGame::Error(_err) => None, // TODO: handle ResponseCreateGame::Valid(result) => Some(result), @@ -67,7 +67,7 @@ pub(crate) fn message_events( DataRequestType::GameAllForming => { // TODO: handle let cookie = global.user_to_session_map.get(&user_key).unwrap(); - let wrapped = all_forming(&config.api_address, &cookie); + let wrapped = all_forming(&config.api_address, cookie); let games = match wrapped.response { ResponseAllForming::Error(_err) => vec![], // TODO: handle ResponseAllForming::Valid(result) => result, @@ -93,7 +93,7 @@ pub(crate) fn message_events( DataRequestType::GameMyGames => { // TODO: handle let cookie = global.user_to_session_map.get(&user_key).unwrap(); - let wrapped = my_games(&config.api_address, &cookie); + let wrapped = my_games(&config.api_address, cookie); let games = match wrapped.response { ResponseMyGames::Error(_err) => vec![], // TODO: handle ResponseMyGames::Valid(result) => result, diff --git a/server/src/systems/event/mod.rs b/server/src/systems/event/mod.rs index 1aff9a6..c4c6cf1 100644 --- a/server/src/systems/event/mod.rs +++ b/server/src/systems/event/mod.rs @@ -84,7 +84,7 @@ pub(crate) fn connect_events( // TODO: handle let aa_details = temp.afterauth_details.get(user_key).unwrap(); server.send_message::( - &user_key, + user_key, &AfterAuth { id: aa_details.0.id.to_string().clone(), username: aa_details.0.username.clone(), @@ -96,7 +96,7 @@ pub(crate) fn connect_events( .user_to_session_map .insert(*user_key, aa_details.1.clone()); - temp.afterauth_details.remove(&user_key); + temp.afterauth_details.remove(user_key); } } diff --git a/shared/src/api/mod.rs b/shared/src/api/mod.rs index 8f68620..9a183b8 100644 --- a/shared/src/api/mod.rs +++ b/shared/src/api/mod.rs @@ -23,7 +23,7 @@ pub struct APIInfo { pub fn info(api_address: String) -> Result { let client = reqwest::blocking::Client::new(); - let resp = client.get(&format!("{}/info", api_address)).send(); + let resp = client.get(format!("{}/info", api_address)).send(); match resp { Ok(r) => Ok(r.json().unwrap()), Err(_) => Err("Could not reach API".to_string()), diff --git a/shared/src/types/game.rs b/shared/src/types/game.rs index 50486fe..8b04a66 100644 --- a/shared/src/types/game.rs +++ b/shared/src/types/game.rs @@ -53,21 +53,9 @@ impl GamePub { created_at: game.created_at.to_string(), updated_at: game.updated_at.to_string(), host_id: game.host_id.to_string(), - guest_id: { - if let Some(guest_id) = game.guest_id { - Some(guest_id.to_string()) - } else { - None - } - }, + guest_id: { game.guest_id.map(|guest_id| guest_id.to_string()) }, state: game.state, - ended_at: { - if let Some(ended_at) = game.ended_at { - Some(ended_at.to_string()) - } else { - None - } - }, + ended_at: { game.ended_at.map(|ended_at| ended_at.to_string()) }, game_data_id: game.game_data_id.to_string(), } } -- 2.44.1