AJKCAY3ODMTFBO6GXWXWQSWNFIH3H7JNBRFVW5H6ZSWA7SZGY3FAC
#[derive(Queryable, Insertable, SimpleObject)]
#[table_name = "team_stats"]
pub struct TeamStats {
pub team: i32,
pub zone_id: i32,
pub bosses_killed: i32,
pub raid_nights: String,
pub min_hours: f32,
pub max_hours: f32,
}
async fn team_stats(
&self,
ctx: &Context<'_>,
team: i32,
zone: i32,
) -> Result<Option<TeamStats>> {
use crate::schema::team_stats::dsl as ts;
let Data { conpool, .. } = ctx.data()?;
Ok(ts::team_stats
.filter(ts::team.eq(team).and(ts::zone_id.eq(zone)))
.select((
ts::team,
ts::zone_id,
ts::bosses_killed,
ts::raid_nights,
ts::min_hours,
ts::max_hours,
))
.first(&*conpool.get()?)
.optional()?)
}
for (encounter, difficulty) in new_encounters {
if let Some(difficulty) = difficulty {
generate_encounter_analysis(
&*con,
guild_id.id() as i32,
encounter as i32,
difficulty as i32,
)?
if !new_encounters.is_empty() {
for (encounter, difficulty) in new_encounters {
if let Some(difficulty) = difficulty {
generate_encounter_analysis(
&*con,
guild_id.id() as i32,
encounter as i32,
difficulty as i32,
)?
}
macro_rules! ex {
($t:path) => {
$t.eq(excluded($t))
};
}
pub fn team_analysis(con: &PgConnection, team_id: i32, zone_id: i32) -> Result<()> {
use crate::schema::encounter_stats::dsl as enc;
use crate::schema::logs::dsl as logs;
use crate::schema::team_stats::dsl as ts;
let kills = enc::encounter_stats
.inner_join(logs::logs.on(enc::kill_log.eq(logs::iid.nullable())))
.filter(enc::team.eq(team_id).and(logs::zone_id.eq(zone_id)))
.count()
.get_result::<i64>(con)? as i32;
let stat = TeamStats {
team: team_id,
zone_id,
bosses_killed: kills,
raid_nights: "".to_string(),
min_hours: 0.0,
max_hours: 0.0,
};
diesel::insert_into(ts::team_stats)
.values(&stat)
.on_conflict((ts::team, ts::zone_id))
.do_update()
.set((
ex!(ts::bosses_killed),
ex!(ts::raid_nights),
ex!(ts::min_hours),
ex!(ts::max_hours),
))
.execute(con)?;
Ok(())
}
-- Your SQL goes here
create table team_stats (
id serial primary key,
team integer not null references teams(id),
zone_id integer not null,
bosses_killed integer not null,
raid_nights text not null,
min_hours real not null,
max_hours real not null,
unique(team, zone_id)
);
-- This file should undo anything in `up.sql`