sqlx update + clippy

This commit is contained in:
Robin Appelman 2024-02-10 18:52:54 +01:00
commit e1a4036b61
29 changed files with 1261 additions and 776 deletions

View file

@ -1,21 +1,16 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, sqlx::Type, Deserialize, Serialize, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, sqlx::Type, Deserialize, Serialize, Eq, PartialEq, Default)]
#[sqlx(type_name = "team")]
#[sqlx(rename_all = "lowercase")]
pub enum TeamId {
Blue,
Red,
#[serde(other)]
#[default]
Other,
}
impl Default for TeamId {
fn default() -> Self {
TeamId::Other
}
}
#[derive(Debug, Clone, Copy, sqlx::Type, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
#[sqlx(rename_all = "lowercase")]
@ -61,7 +56,7 @@ pub enum EventType {
Other,
}
#[derive(Debug, Clone, Copy, sqlx::Type, Deserialize, Serialize, Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, sqlx::Type, Deserialize, Serialize, Hash, Eq, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
#[sqlx(rename_all = "lowercase")]
pub enum Medigun {
@ -69,30 +64,20 @@ pub enum Medigun {
QuickFix,
Vaccinator,
#[serde(other)]
#[default]
Medigun,
}
impl Default for Medigun {
fn default() -> Self {
Medigun::Medigun
}
}
#[derive(Debug, Clone, Copy, sqlx::Type, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, sqlx::Type, Eq, PartialEq, Default)]
#[sqlx(rename_all = "lowercase")]
#[sqlx(type_name = "map_type")]
pub enum MapType {
Stopwatch,
Cp,
KOTH,
CTF,
Koth,
Ctf,
UltiDuo,
BBall,
#[default]
Other,
}
impl Default for MapType {
fn default() -> Self {
MapType::Other
}
}

View file

@ -23,7 +23,7 @@ pub async fn store_log(pool: &PgPool, id: i32, log: &NormalizedLog) -> Result<()
log.info.date() as DateTime<Utc>,
2
)
.execute(&mut tx)
.execute(&mut *tx)
.await?;
for (num, round) in log.rounds.iter().enumerate() {
@ -48,47 +48,49 @@ pub async fn store_log(pool: &PgPool, id: i32, log: &NormalizedLog) -> Result<()
round.team.red.charges as i32,
round.team.blue.charges as i32,
)
.fetch_one(&mut tx)
.fetch_one(&mut *tx)
.await?
.id;
for event in &round.events {
match event {
Event::PointCap { time, team, point } => {
if let Some(team) = team {
sqlx::query!(
"INSERT INTO events_point_cap(round_id, time, team, point)\
VALUES($1, $2, $3, $4)",
round_id,
*time as i32,
*team as TeamId,
*point as i32,
)
.execute(&mut tx)
.await?;
}
Event::PointCap {
time,
team: Some(team),
point,
} => {
sqlx::query!(
"INSERT INTO events_point_cap(round_id, time, team, point)\
VALUES($1, $2, $3, $4)",
round_id,
*time as i32,
*team as TeamId,
*point as i32,
)
.execute(&mut *tx)
.await?;
}
Event::RoundWin { time, team } => {
if let Some(team) = team {
sqlx::query!(
"INSERT INTO events_round_win(round_id, time, team)\
VALUES($1, $2, $3)",
round_id,
*time as i32,
team.unwrap_or_default() as TeamId,
)
.execute(&mut tx)
.await?;
}
Event::RoundWin {
time,
team: Some(team),
} => {
sqlx::query!(
"INSERT INTO events_round_win(round_id, time, team)\
VALUES($1, $2, $3)",
round_id,
*time as i32,
*team as TeamId,
)
.execute(&mut *tx)
.await?;
}
Event::MedicDeath {
time,
team,
team: Some(team),
steamid,
killer,
} => {
if let Some(team) = team {
sqlx::query!(
sqlx::query!(
"INSERT INTO events_medic_death(round_id, time, team, steam_id, killer)\
VALUES($1, $2, $3, $4, $5)",
round_id,
@ -97,47 +99,42 @@ pub async fn store_log(pool: &PgPool, id: i32, log: &NormalizedLog) -> Result<()
u64::from(*steamid) as i64,
u64::from(*killer) as i64,
)
.execute(&mut tx)
.await?;
}
.execute(&mut *tx)
.await?;
}
Event::Drop {
time,
steamid,
team,
team: Some(team),
} => {
if let Some(team) = team {
sqlx::query!(
"INSERT INTO events_drop(round_id, time, team, steam_id)\
sqlx::query!(
"INSERT INTO events_drop(round_id, time, team, steam_id)\
VALUES($1, $2, $3, $4)",
round_id,
*time as i32,
*team as TeamId,
u64::from(*steamid) as i64,
)
.execute(&mut tx)
.await?;
}
round_id,
*time as i32,
*team as TeamId,
u64::from(*steamid) as i64,
)
.execute(&mut *tx)
.await?;
}
Event::Charge {
medigun,
time,
steamid,
team,
team: Some(team),
} => {
if let Some(team) = team {
sqlx::query!(
"INSERT INTO events_charge(round_id, time, team, medigun, steam_id)\
sqlx::query!(
"INSERT INTO events_charge(round_id, time, team, medigun, steam_id)\
VALUES($1, $2, $3, $4, $5)",
round_id,
*time as i32,
*team as TeamId,
*medigun as Medigun,
u64::from(*steamid) as i64,
)
.execute(&mut tx)
.await?;
}
round_id,
*time as i32,
*team as TeamId,
*medigun as Medigun,
u64::from(*steamid) as i64,
)
.execute(&mut *tx)
.await?;
}
_ => {}
}
@ -234,7 +231,7 @@ pub async fn store_log(pool: &PgPool, id: i32, log: &NormalizedLog) -> Result<()
deaths.sniper as i32,
deaths.spy as i32,
)
.fetch_one(&mut tx)
.fetch_one(&mut *tx)
.await?
.id;
@ -252,7 +249,7 @@ pub async fn store_log(pool: &PgPool, id: i32, log: &NormalizedLog) -> Result<()
class.assists as i32,
class.dmg as i32,
)
.fetch_one(&mut tx)
.fetch_one(&mut *tx)
.await?
.id;
@ -267,7 +264,7 @@ pub async fn store_log(pool: &PgPool, id: i32, log: &NormalizedLog) -> Result<()
stats.hits as i32,
stats.dmg as i32,
)
.execute(&mut tx)
.execute(&mut *tx)
.await?;
}
}
@ -284,7 +281,7 @@ pub async fn store_log(pool: &PgPool, id: i32, log: &NormalizedLog) -> Result<()
kill_streak.time,
kill_streak.streak,
)
.execute(&mut tx)
.execute(&mut *tx)
.await?;
}
@ -313,13 +310,13 @@ pub async fn upgrade(
kill_streak.time,
kill_streak.streak,
)
.execute(&mut tx)
.execute(&mut *tx)
.await?;
}
}
sqlx::query!("UPDATE logs SET version = $1 WHERE id = $2", to, id)
.execute(&mut tx)
.execute(&mut *tx)
.await?;
tx.commit().await?;

View file

@ -136,7 +136,7 @@ fn is_valid(value: &serde_json::Value) -> bool {
if value.get("success").is_none() {
return false;
}
if value.get("success").unwrap().as_bool().unwrap_or_default() == false {
if !value.get("success").unwrap().as_bool().unwrap_or_default() {
return false;
}

View file

@ -6,6 +6,7 @@ pub use crate::raw::{
};
use chrono::{DateTime, NaiveDateTime, Utc};
use serde::Deserialize;
use std::cmp::Ordering;
use std::collections::HashMap;
use steamid_ng::SteamID;
@ -75,9 +76,9 @@ impl Info {
} else if self.map.starts_with("cp") {
MapType::Cp
} else if self.map.starts_with("koth") {
MapType::KOTH
MapType::Koth
} else if self.map.starts_with("ctf") {
MapType::CTF
MapType::Ctf
} else if self.map.starts_with("ultiduo") {
MapType::UltiDuo
} else if self.map.starts_with("bball") {
@ -88,7 +89,9 @@ impl Info {
}
pub fn date(&self) -> DateTime<Utc> {
DateTime::from_utc(NaiveDateTime::from_timestamp(self.date as i64, 0), Utc)
NaiveDateTime::from_timestamp_opt(self.date as i64, 0)
.unwrap()
.and_utc()
}
}
@ -133,7 +136,7 @@ impl From<RawLog> for NormalizedLog {
.or(raw.info.rounds)
.unwrap_or_default()
.into_iter()
.map(|raw| Round::from(raw))
.map(Round::from)
.collect();
let teams = raw.teams.or(raw.info.teams).unwrap_or_default();
@ -188,33 +191,18 @@ impl From<crate::raw::Round> for Round {
}
pub fn map_is_stopwatch(map: &str) -> bool {
if map.starts_with("pl_") {
true
} else if map.starts_with("cp_steel") {
true
} else if map.starts_with("cp_gravelpit") {
true
} else if map.starts_with("cp_dustbowl") {
true
} else if map.starts_with("cp_egypt") {
true
} else if map.starts_with("cp_degrootkeep") {
true
} else if map.starts_with("cp_gorge") {
true
} else if map.starts_with("cp_junction") {
true
} else if map.starts_with("cp_mossrock") {
true
} else if map.starts_with("cp_manor") {
true
} else if map.starts_with("cp_snowplow") {
true
} else if map.starts_with("cp_alloy") {
true
} else {
false
}
map.starts_with("pl_")
|| map.starts_with("cp_steel")
|| map.starts_with("cp_gravelpit")
|| map.starts_with("cp_dustbowl")
|| map.starts_with("cp_egypt")
|| map.starts_with("cp_degrootkeep")
|| map.starts_with("cp_gorge")
|| map.starts_with("cp_junction")
|| map.starts_with("cp_mossrock")
|| map.starts_with("cp_manor")
|| map.starts_with("cp_snowplow")
|| map.starts_with("cp_alloy")
}
/// Add missing round wins for 2nd round blue win
@ -273,7 +261,7 @@ fn get_first_event_time(round: &Round) -> u32 {
round
.events
.iter()
.filter_map(|event| Some(event.time()))
.map(|event| event.time())
.last()
.unwrap_or_default()
}
@ -327,24 +315,28 @@ fn normalize_stopwatch_score(log: &mut NormalizedLog) {
let second_half_capped = get_round_point_capped(&log.rounds[1]);
// "blue" is the team that attacked first
if first_half_capped > second_half_capped {
log.teams.blue.score = 1;
log.teams.red.score = 0;
} else if second_half_capped > first_half_capped {
log.teams.blue.score = 0;
log.teams.red.score = 1;
} else {
let first_half_cap_time = get_last_cap_time(&log.rounds[0]);
let second_half_cap_time = get_last_cap_time(&log.rounds[1])
.saturating_sub(get_round_end_time(&log.rounds[0]));
if first_half_cap_time < second_half_cap_time {
match first_half_capped.cmp(&second_half_capped) {
Ordering::Greater => {
log.teams.blue.score = 1;
log.teams.red.score = 0;
} else {
}
Ordering::Less => {
log.teams.blue.score = 0;
log.teams.red.score = 1;
}
Ordering::Equal => {
let first_half_cap_time = get_last_cap_time(&log.rounds[0]);
let second_half_cap_time = get_last_cap_time(&log.rounds[1])
.saturating_sub(get_round_end_time(&log.rounds[0]));
if first_half_cap_time < second_half_cap_time {
log.teams.blue.score = 1;
log.teams.red.score = 0;
} else {
log.teams.blue.score = 0;
log.teams.red.score = 1;
}
}
}
}
}

View file

@ -268,7 +268,7 @@ pub enum Event {
},
RoundWin {
time: u32,
team: Option<Option<TeamId>>,
team: Option<TeamId>,
},
Drop {
time: u32,