Fix heatmap issue and implement per round analysis
This commit is contained in:
@@ -3,6 +3,22 @@ use std::path::PathBuf;
|
||||
use diesel::prelude::*;
|
||||
use diesel_async::RunQueryDsl;
|
||||
|
||||
pub mod perround;
|
||||
pub mod base;
|
||||
pub mod heatmap;
|
||||
|
||||
pub trait Analysis {
|
||||
fn analyse(&self, input: AnalysisInput) -> Result<Box<dyn FnOnce(&mut diesel_async::pg::AsyncPgConnection) -> core::pin::Pin<Box<(dyn core::future::Future<Output = Result<(), diesel::result::Error>> + Send + '_)>> + Send>, ()>;
|
||||
}
|
||||
|
||||
pub static ANALYSIS_METHODS: std::sync::LazyLock<[std::sync::Arc<dyn Analysis + Send + Sync>; 3]> = std::sync::LazyLock::new(|| {
|
||||
[
|
||||
std::sync::Arc::new(base::BaseAnalysis::new()),
|
||||
std::sync::Arc::new(heatmap::HeatmapAnalysis::new()),
|
||||
std::sync::Arc::new(perround::PerRoundAnalysis::new()),
|
||||
]
|
||||
});
|
||||
|
||||
pub async fn poll_next_task(
|
||||
upload_folder: &std::path::Path,
|
||||
db_con: &mut diesel_async::pg::AsyncPgConnection,
|
||||
@@ -17,7 +33,7 @@ pub async fn poll_next_task(
|
||||
loop {
|
||||
let result = db_con
|
||||
.build_transaction()
|
||||
.run::<'_, _, diesel::result::Error, _>(|conn| {
|
||||
.run::<_, diesel::result::Error, _>(|conn| {
|
||||
Box::pin(async move {
|
||||
let mut results: Vec<crate::models::AnalysisTask> = query.load(conn).await?;
|
||||
let final_result = match results.pop() {
|
||||
@@ -92,65 +108,3 @@ pub struct BasePlayerStats {
|
||||
pub damage: usize,
|
||||
pub assists: usize,
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(input))]
|
||||
pub fn analyse_base(input: AnalysisInput) -> BaseInfo {
|
||||
tracing::info!("Performing Base analysis");
|
||||
|
||||
let file = std::fs::File::open(&input.path).unwrap();
|
||||
let mmap = unsafe { memmap2::MmapOptions::new().map(&file).unwrap() };
|
||||
|
||||
let result = analysis::endofgame::parse(&mmap).unwrap();
|
||||
|
||||
BaseInfo {
|
||||
map: result.map,
|
||||
players: result.players.into_iter().map(|(info, stats)| {
|
||||
(BasePlayerInfo {
|
||||
name: info.name,
|
||||
steam_id: info.steam_id,
|
||||
team: info.team,
|
||||
ingame_id: info.ingame_id,
|
||||
color: info.color,
|
||||
}, BasePlayerStats {
|
||||
kills: stats.kills,
|
||||
assists: stats.assists,
|
||||
damage: stats.damage,
|
||||
deaths: stats.deaths,
|
||||
})
|
||||
}).collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(input))]
|
||||
pub fn analyse_heatmap(input: AnalysisInput) -> std::collections::HashMap<String, analysis::heatmap::HeatMap> {
|
||||
tracing::info!("Generating HEATMAPs");
|
||||
|
||||
let file = std::fs::File::open(&input.path).unwrap();
|
||||
let mmap = unsafe { memmap2::MmapOptions::new().map(&file).unwrap() };
|
||||
|
||||
let config = analysis::heatmap::Config {
|
||||
cell_size: 5.0,
|
||||
};
|
||||
let result = analysis::heatmap::parse(&config, &mmap).unwrap();
|
||||
|
||||
tracing::info!("Got {} Entity-Heatmaps", result.player_heatmaps.len());
|
||||
result.player_heatmaps.into_iter().filter_map(|(entity_id, heatmap)| {
|
||||
let userid = match result.entity_to_player.get(&entity_id) {
|
||||
Some(u) => u,
|
||||
None => {
|
||||
tracing::warn!("Could not find User for Entity: {:?}", entity_id);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
let player = match result.player_info.get(userid) {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
tracing::warn!("Could not find player: {:?}", userid);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
Some((player.xuid.to_string(), heatmap))
|
||||
}).collect()
|
||||
}
|
||||
|
||||
114
backend/src/analysis/base.rs
Normal file
114
backend/src/analysis/base.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
use super::*;
|
||||
|
||||
pub struct BaseAnalysis {}
|
||||
|
||||
impl BaseAnalysis {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Analysis for BaseAnalysis {
|
||||
#[tracing::instrument(name = "Base", skip(self, input))]
|
||||
fn analyse(&self, input: AnalysisInput) -> Result<Box<dyn FnOnce(&mut diesel_async::pg::AsyncPgConnection) -> core::pin::Pin<Box<(dyn core::future::Future<Output = Result<(), diesel::result::Error>> + Send + '_)>> + Send>, ()> { tracing::info!("Performing Base analysis");
|
||||
|
||||
let file = std::fs::File::open(&input.path).unwrap();
|
||||
let mmap = unsafe { memmap2::MmapOptions::new().map(&file).unwrap() };
|
||||
|
||||
let result = analysis::endofgame::parse(&mmap).unwrap();
|
||||
|
||||
let base_result = BaseInfo {
|
||||
map: result.map,
|
||||
players: result.players.into_iter().map(|(info, stats)| {
|
||||
(BasePlayerInfo {
|
||||
name: info.name,
|
||||
steam_id: info.steam_id,
|
||||
team: info.team,
|
||||
ingame_id: info.ingame_id,
|
||||
color: info.color,
|
||||
}, BasePlayerStats {
|
||||
kills: stats.kills,
|
||||
assists: stats.assists,
|
||||
damage: stats.damage,
|
||||
deaths: stats.deaths,
|
||||
})
|
||||
}).collect()
|
||||
};
|
||||
|
||||
let (player_info, player_stats): (Vec<_>, Vec<_>) = base_result
|
||||
.players
|
||||
.into_iter()
|
||||
.map(|(info, stats)| {
|
||||
(
|
||||
crate::models::DemoPlayer {
|
||||
demo_id: input.demoid,
|
||||
name: info.name,
|
||||
steam_id: info.steam_id.clone(),
|
||||
team: info.team as i16,
|
||||
color: info.color as i16,
|
||||
},
|
||||
crate::models::DemoPlayerStats {
|
||||
demo_id: input.demoid,
|
||||
steam_id: info.steam_id,
|
||||
deaths: stats.deaths as i16,
|
||||
kills: stats.kills as i16,
|
||||
damage: stats.damage as i16,
|
||||
assists: stats.assists as i16,
|
||||
},
|
||||
)
|
||||
})
|
||||
.unzip();
|
||||
|
||||
let demo_info = crate::models::DemoInfo {
|
||||
demo_id: input.demoid,
|
||||
map: base_result.map,
|
||||
};
|
||||
|
||||
Ok(Box::new(move |connection| {
|
||||
let store_demo_info_query =
|
||||
diesel::dsl::insert_into(crate::schema::demo_info::dsl::demo_info)
|
||||
.values(demo_info)
|
||||
.on_conflict(crate::schema::demo_info::dsl::demo_id)
|
||||
.do_update()
|
||||
.set(
|
||||
crate::schema::demo_info::dsl::map
|
||||
.eq(diesel::upsert::excluded(crate::schema::demo_info::dsl::map)),
|
||||
);
|
||||
let store_demo_players_query =
|
||||
diesel::dsl::insert_into(crate::schema::demo_players::dsl::demo_players)
|
||||
.values(player_info)
|
||||
.on_conflict_do_nothing();
|
||||
|
||||
let store_demo_player_stats_query =
|
||||
diesel::dsl::insert_into(crate::schema::demo_player_stats::dsl::demo_player_stats)
|
||||
.values(player_stats)
|
||||
.on_conflict((
|
||||
crate::schema::demo_player_stats::dsl::demo_id,
|
||||
crate::schema::demo_player_stats::dsl::steam_id,
|
||||
))
|
||||
.do_update()
|
||||
.set((
|
||||
crate::schema::demo_player_stats::dsl::deaths.eq(diesel::upsert::excluded(
|
||||
crate::schema::demo_player_stats::dsl::deaths,
|
||||
)),
|
||||
crate::schema::demo_player_stats::dsl::kills.eq(diesel::upsert::excluded(
|
||||
crate::schema::demo_player_stats::dsl::kills,
|
||||
)),
|
||||
crate::schema::demo_player_stats::dsl::assists.eq(diesel::upsert::excluded(
|
||||
crate::schema::demo_player_stats::dsl::assists,
|
||||
)),
|
||||
crate::schema::demo_player_stats::dsl::damage.eq(diesel::upsert::excluded(
|
||||
crate::schema::demo_player_stats::dsl::damage,
|
||||
)),
|
||||
));
|
||||
|
||||
Box::pin(async move {
|
||||
store_demo_info_query.execute(connection).await?;
|
||||
store_demo_players_query.execute(connection).await?;
|
||||
store_demo_player_stats_query.execute(connection).await?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}))
|
||||
}
|
||||
}
|
||||
61
backend/src/analysis/heatmap.rs
Normal file
61
backend/src/analysis/heatmap.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use super::*;
|
||||
|
||||
pub struct HeatmapAnalysis {}
|
||||
|
||||
impl HeatmapAnalysis {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Analysis for HeatmapAnalysis {
|
||||
#[tracing::instrument(name = "Heatmap", skip(self, input))]
|
||||
fn analyse(&self, input: AnalysisInput) -> Result<Box<dyn FnOnce(&mut diesel_async::pg::AsyncPgConnection) -> core::pin::Pin<Box<(dyn core::future::Future<Output = Result<(), diesel::result::Error>> + Send + '_)>> + Send>, ()> {
|
||||
tracing::info!("Generating HEATMAPs");
|
||||
|
||||
let file = std::fs::File::open(&input.path).unwrap();
|
||||
let mmap = unsafe { memmap2::MmapOptions::new().map(&file).unwrap() };
|
||||
|
||||
let config = analysis::heatmap::Config {
|
||||
cell_size: 5.0,
|
||||
};
|
||||
let result = analysis::heatmap::parse(&config, &mmap).unwrap();
|
||||
|
||||
tracing::info!("Got {} Entity-Heatmaps", result.player_heatmaps.len());
|
||||
let heatmap_result: Vec<_> = result.player_heatmaps.into_iter().filter_map(|(userid, heatmap)| {
|
||||
let player = match result.player_info.get(&userid) {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
tracing::warn!("Could not find player: {:?}", userid);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
Some((player.xuid.to_string(), heatmap))
|
||||
}).collect();
|
||||
|
||||
let player_heatmaps: Vec<_> = heatmap_result.into_iter().map(|(player, heatmap)| {
|
||||
tracing::trace!("HeatMap for Player: {:?}", player);
|
||||
|
||||
crate::models::DemoPlayerHeatmap {
|
||||
demo_id: input.demoid,
|
||||
steam_id: player,
|
||||
data: serde_json::to_string(&heatmap).unwrap(),
|
||||
}
|
||||
}).collect();
|
||||
|
||||
Ok(Box::new(move |connection| {
|
||||
let store_demo_player_heatmaps_query = diesel::dsl::insert_into(crate::schema::demo_heatmaps::dsl::demo_heatmaps)
|
||||
.values(player_heatmaps)
|
||||
.on_conflict((crate::schema::demo_heatmaps::dsl::demo_id, crate::schema::demo_heatmaps::dsl::steam_id))
|
||||
.do_update()
|
||||
.set(crate::schema::demo_heatmaps::dsl::data.eq(diesel::upsert::excluded(crate::schema::demo_heatmaps::dsl::data)));
|
||||
|
||||
Box::pin(async move {
|
||||
store_demo_player_heatmaps_query.execute(connection).await?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}))
|
||||
}
|
||||
}
|
||||
42
backend/src/analysis/perround.rs
Normal file
42
backend/src/analysis/perround.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use super::*;
|
||||
|
||||
pub struct PerRoundAnalysis {}
|
||||
|
||||
impl PerRoundAnalysis {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Analysis for PerRoundAnalysis {
|
||||
#[tracing::instrument(name = "PerRoundAnalysis", skip(self, input))]
|
||||
fn analyse(&self, input: AnalysisInput) -> Result<Box<dyn FnOnce(&mut diesel_async::pg::AsyncPgConnection) -> core::pin::Pin<Box<(dyn core::future::Future<Output = Result<(), diesel::result::Error>> + Send + '_)>> + Send>, ()> {
|
||||
let file = std::fs::File::open(&input.path).unwrap();
|
||||
let mmap = unsafe { memmap2::MmapOptions::new().map(&file).unwrap() };
|
||||
|
||||
let result = analysis::perround::parse(&mmap).unwrap();
|
||||
|
||||
let values: Vec<crate::models::DemoRound> = result.rounds.into_iter().enumerate().map(|(i, r)| {
|
||||
crate::models::DemoRound {
|
||||
demo_id: input.demoid,
|
||||
round_number: i as i16,
|
||||
start_tick: r.start as i64,
|
||||
end_tick: r.end as i64,
|
||||
win_reason: serde_json::to_string(&r.winreason).unwrap(),
|
||||
events: serde_json::to_value(&r.events).unwrap(),
|
||||
}
|
||||
}).collect();
|
||||
|
||||
Ok(Box::new(move |connection| {
|
||||
Box::pin(async move {
|
||||
let query = diesel::dsl::insert_into(crate::schema::demo_round::dsl::demo_round)
|
||||
.values(&values)
|
||||
.on_conflict_do_nothing();
|
||||
|
||||
query.execute(connection).await?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,9 @@ where
|
||||
.layer(axum::extract::DefaultBodyLimit::max(500 * 1024 * 1024)),
|
||||
)
|
||||
.route("/:id/info", axum::routing::get(info))
|
||||
.route("/:id/analysis/scoreboard", axum::routing::get(scoreboard))
|
||||
.route("/:id/reanalyse", axum::routing::get(analyise))
|
||||
.route("/:id/analysis/scoreboard", axum::routing::get(scoreboard))
|
||||
.route("/:id/analysis/perround", axum::routing::get(perround))
|
||||
.route("/:id/analysis/heatmap", axum::routing::get(heatmap))
|
||||
.with_state(Arc::new(DemoState {
|
||||
upload_folder: upload_folder.into(),
|
||||
@@ -305,19 +306,117 @@ async fn heatmap(
|
||||
Ok(axum::Json(data))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(session))]
|
||||
async fn perround(
|
||||
session: UserSession,
|
||||
Path(demo_id): Path<i64>,
|
||||
) -> Result<axum::response::Json<Vec<common::demo_analysis::DemoRound>>, axum::http::StatusCode> {
|
||||
let rounds_query = crate::schema::demo_round::dsl::demo_round.filter(crate::schema::demo_round::dsl::demo_id.eq(demo_id));
|
||||
let round_players_query = crate::schema::demo_players::dsl::demo_players.filter(crate::schema::demo_players::dsl::demo_id.eq(demo_id));
|
||||
|
||||
let mut db_con = crate::db_connection().await;
|
||||
|
||||
let raw_rounds: Vec<crate::models::DemoRound> = rounds_query.load(&mut db_con).await.unwrap();
|
||||
let players: Vec<crate::models::DemoPlayer> = round_players_query.load(&mut db_con).await.unwrap();
|
||||
|
||||
let mut result = Vec::with_capacity(raw_rounds.len());
|
||||
for raw_round in raw_rounds.into_iter() {
|
||||
let reason = match serde_json::from_str(&raw_round.win_reason) {
|
||||
Ok(analysis::perround::WinReason::StillInProgress) => common::demo_analysis::RoundWinReason::StillInProgress,
|
||||
Ok(analysis::perround::WinReason::TKilled) => common::demo_analysis::RoundWinReason::TKilled,
|
||||
Ok(analysis::perround::WinReason::CTKilled) => common::demo_analysis::RoundWinReason::CTKilled,
|
||||
Ok(analysis::perround::WinReason::BombDefused) => common::demo_analysis::RoundWinReason::BombDefused,
|
||||
Ok(analysis::perround::WinReason::BombExploded) => common::demo_analysis::RoundWinReason::BombExploded,
|
||||
Ok(analysis::perround::WinReason::TimeRanOut) => common::demo_analysis::RoundWinReason::TimeRanOut,
|
||||
Ok(other) => {
|
||||
tracing::error!("Unknown Mapping {:?}", other);
|
||||
return Err(axum::http::StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Deserializing Win Reason: {:?}", e);
|
||||
return Err(axum::http::StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
};
|
||||
|
||||
let parsed_events: Vec<analysis::perround::RoundEvent> = serde_json::from_value(raw_round.events).unwrap();
|
||||
let events: Vec<_> = parsed_events.into_iter().map(|event| {
|
||||
match event {
|
||||
analysis::perround::RoundEvent::BombPlanted => common::demo_analysis::RoundEvent::BombPlanted,
|
||||
analysis::perround::RoundEvent::BombDefused => common::demo_analysis::RoundEvent::BombDefused,
|
||||
analysis::perround::RoundEvent::Kill { attacker, died } => {
|
||||
let attacker_name = players.iter().find(|p| p.steam_id == attacker.to_string()).map(|p| p.name.clone()).unwrap();
|
||||
let died_name = players.iter().find(|p| p.steam_id == died.to_string()).map(|p| p.name.clone()).unwrap();
|
||||
|
||||
common::demo_analysis::RoundEvent::Killed {
|
||||
attacker: attacker_name,
|
||||
died: died_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
}).collect();
|
||||
|
||||
result.push(common::demo_analysis::DemoRound {
|
||||
reason,
|
||||
events,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(axum::Json(result))
|
||||
}
|
||||
|
||||
// The corresponding values for each map can be found using the Source2 Viewer and opening the
|
||||
// files in 'game/csgo/pak01_dir.vpk' and then 'resource/overviews/{map}.txt'
|
||||
static MINIMAP_COORDINATES: phf::Map<&str, MiniMapDefinition> = phf::phf_map! {
|
||||
"de_inferno" => MiniMapDefinition {
|
||||
pos_x: -2087.0,
|
||||
pos_y: 3870.0,
|
||||
scale: 4.9,
|
||||
"cs_italy" => MiniMapDefinition {
|
||||
pos_x: -2647.0,
|
||||
pos_y: 2592.0,
|
||||
scale: 4.6
|
||||
},
|
||||
"cs_office" => MiniMapDefinition {
|
||||
pos_x: -1838.0,
|
||||
pos_y: 1858.0,
|
||||
scale: 4.1,
|
||||
},
|
||||
"de_ancient" => MiniMapDefinition {
|
||||
pos_x: -2953.0,
|
||||
pos_y: 2164.0,
|
||||
scale: 5.0,
|
||||
},
|
||||
"de_anubis" => MiniMapDefinition {
|
||||
pos_x: -2796.0,
|
||||
pos_y: 3328.0,
|
||||
scale: 5.22,
|
||||
},
|
||||
"de_dust2" => MiniMapDefinition {
|
||||
pos_x: -2476.0,
|
||||
pos_y: 3239.0,
|
||||
scale: 4.4
|
||||
},
|
||||
"de_inferno" => MiniMapDefinition {
|
||||
pos_x: -2087.0,
|
||||
pos_y: 3870.0,
|
||||
scale: 4.9,
|
||||
},
|
||||
"de_mirage" => MiniMapDefinition {
|
||||
pos_x: -3230.0,
|
||||
pos_y: 1713.0,
|
||||
scale: 5.0,
|
||||
},
|
||||
"de_nuke" => MiniMapDefinition {
|
||||
pos_x: -3453.0,
|
||||
pos_y: 2887.0,
|
||||
scale: 7.0,
|
||||
},
|
||||
"de_overpass" => MiniMapDefinition {
|
||||
pos_x: -4831.0,
|
||||
pos_y: 1781.0,
|
||||
scale: 5.2,
|
||||
},
|
||||
"de_vertigo" => MiniMapDefinition {
|
||||
pos_x: -3168.0,
|
||||
pos_y: 1762.0,
|
||||
scale: 4.0,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
||||
@@ -101,96 +101,26 @@ pub async fn run_analysis(upload_folder: impl Into<std::path::PathBuf>) {
|
||||
|
||||
let demo_id = input.demoid;
|
||||
|
||||
let base_input = input.clone();
|
||||
let base_result = tokio::task::spawn_blocking(move || crate::analysis::analyse_base(base_input))
|
||||
.await
|
||||
.unwrap();
|
||||
let mut store_result_fns = Vec::new();
|
||||
for analysis in analysis::ANALYSIS_METHODS.iter().map(|a| a.clone()) {
|
||||
let input = input.clone();
|
||||
let store_result = match tokio::task::spawn_blocking(move || analysis.analyse(input)).await {
|
||||
Ok(Ok(r)) => r,
|
||||
Ok(Err(e)) => {
|
||||
tracing::error!("Analysis failed: {:?}", e);
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Joining Task: {:?}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let heatmap_result = tokio::task::spawn_blocking(move || crate::analysis::analyse_heatmap(input))
|
||||
.await
|
||||
.unwrap();
|
||||
store_result_fns.push(store_result);
|
||||
}
|
||||
|
||||
let mut db_con = crate::db_connection().await;
|
||||
|
||||
let (player_info, player_stats): (Vec<_>, Vec<_>) = base_result
|
||||
.players
|
||||
.into_iter()
|
||||
.map(|(info, stats)| {
|
||||
(
|
||||
crate::models::DemoPlayer {
|
||||
demo_id,
|
||||
name: info.name,
|
||||
steam_id: info.steam_id.clone(),
|
||||
team: info.team as i16,
|
||||
color: info.color as i16,
|
||||
},
|
||||
crate::models::DemoPlayerStats {
|
||||
demo_id,
|
||||
steam_id: info.steam_id,
|
||||
deaths: stats.deaths as i16,
|
||||
kills: stats.kills as i16,
|
||||
damage: stats.damage as i16,
|
||||
assists: stats.assists as i16,
|
||||
},
|
||||
)
|
||||
})
|
||||
.unzip();
|
||||
|
||||
let player_heatmaps: Vec<_> = heatmap_result.into_iter().map(|(player, heatmap)| {
|
||||
tracing::trace!("HeatMap for Player: {:?}", player);
|
||||
|
||||
crate::models::DemoPlayerHeatmap {
|
||||
demo_id,
|
||||
steam_id: player,
|
||||
data: serde_json::to_string(&heatmap).unwrap(),
|
||||
}
|
||||
}).collect();
|
||||
|
||||
let demo_info = crate::models::DemoInfo {
|
||||
demo_id,
|
||||
map: base_result.map,
|
||||
};
|
||||
|
||||
let store_demo_info_query =
|
||||
diesel::dsl::insert_into(crate::schema::demo_info::dsl::demo_info)
|
||||
.values(&demo_info)
|
||||
.on_conflict(crate::schema::demo_info::dsl::demo_id)
|
||||
.do_update()
|
||||
.set(
|
||||
crate::schema::demo_info::dsl::map
|
||||
.eq(diesel::upsert::excluded(crate::schema::demo_info::dsl::map)),
|
||||
);
|
||||
let store_demo_players_query =
|
||||
diesel::dsl::insert_into(crate::schema::demo_players::dsl::demo_players)
|
||||
.values(player_info)
|
||||
.on_conflict_do_nothing();
|
||||
let store_demo_player_stats_query =
|
||||
diesel::dsl::insert_into(crate::schema::demo_player_stats::dsl::demo_player_stats)
|
||||
.values(player_stats)
|
||||
.on_conflict((
|
||||
crate::schema::demo_player_stats::dsl::demo_id,
|
||||
crate::schema::demo_player_stats::dsl::steam_id,
|
||||
))
|
||||
.do_update()
|
||||
.set((
|
||||
crate::schema::demo_player_stats::dsl::deaths.eq(diesel::upsert::excluded(
|
||||
crate::schema::demo_player_stats::dsl::deaths,
|
||||
)),
|
||||
crate::schema::demo_player_stats::dsl::kills.eq(diesel::upsert::excluded(
|
||||
crate::schema::demo_player_stats::dsl::kills,
|
||||
)),
|
||||
crate::schema::demo_player_stats::dsl::assists.eq(diesel::upsert::excluded(
|
||||
crate::schema::demo_player_stats::dsl::assists,
|
||||
)),
|
||||
crate::schema::demo_player_stats::dsl::damage.eq(diesel::upsert::excluded(
|
||||
crate::schema::demo_player_stats::dsl::damage,
|
||||
)),
|
||||
));
|
||||
let store_demo_player_heatmaps_query = diesel::dsl::insert_into(crate::schema::demo_heatmaps::dsl::demo_heatmaps)
|
||||
.values(player_heatmaps)
|
||||
.on_conflict((crate::schema::demo_heatmaps::dsl::demo_id, crate::schema::demo_heatmaps::dsl::steam_id))
|
||||
.do_update()
|
||||
.set((crate::schema::demo_heatmaps::dsl::data.eq(diesel::upsert::excluded(crate::schema::demo_heatmaps::dsl::data))));
|
||||
let update_process_info =
|
||||
diesel::dsl::update(crate::schema::processing_status::dsl::processing_status)
|
||||
.set(crate::schema::processing_status::dsl::info.eq(1))
|
||||
@@ -199,11 +129,11 @@ pub async fn run_analysis(upload_folder: impl Into<std::path::PathBuf>) {
|
||||
db_con
|
||||
.transaction::<'_, '_, '_, _, diesel::result::Error, _>(|conn| {
|
||||
Box::pin(async move {
|
||||
store_demo_info_query.execute(conn).await?;
|
||||
store_demo_players_query.execute(conn).await?;
|
||||
store_demo_player_stats_query.execute(conn).await?;
|
||||
store_demo_player_heatmaps_query.execute(conn).await?;
|
||||
for store_fn in store_result_fns {
|
||||
store_fn(conn).await?;
|
||||
}
|
||||
update_process_info.execute(conn).await?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -88,3 +88,15 @@ pub struct DemoPlayerHeatmap {
|
||||
pub steam_id: String,
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
#[derive(Queryable, Selectable, Insertable, Debug)]
|
||||
#[diesel(table_name = crate::schema::demo_round)]
|
||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||
pub struct DemoRound {
|
||||
pub demo_id: i64,
|
||||
pub round_number: i16,
|
||||
pub start_tick: i64,
|
||||
pub end_tick: i64,
|
||||
pub win_reason: String,
|
||||
pub events: serde_json::Value,
|
||||
}
|
||||
|
||||
@@ -44,6 +44,17 @@ diesel::table! {
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
demo_round (demo_id, round_number) {
|
||||
demo_id -> Int8,
|
||||
round_number -> Int2,
|
||||
start_tick -> Int8,
|
||||
end_tick -> Int8,
|
||||
win_reason -> Text,
|
||||
events -> Json,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
demos (demo_id) {
|
||||
steam_id -> Text,
|
||||
@@ -78,6 +89,7 @@ diesel::joinable!(demo_heatmaps -> demo_info (demo_id));
|
||||
diesel::joinable!(demo_info -> demos (demo_id));
|
||||
diesel::joinable!(demo_player_stats -> demo_info (demo_id));
|
||||
diesel::joinable!(demo_players -> demo_info (demo_id));
|
||||
diesel::joinable!(demo_round -> demo_info (demo_id));
|
||||
diesel::joinable!(processing_status -> demos (demo_id));
|
||||
|
||||
diesel::allow_tables_to_appear_in_same_query!(
|
||||
@@ -86,6 +98,7 @@ diesel::allow_tables_to_appear_in_same_query!(
|
||||
demo_info,
|
||||
demo_player_stats,
|
||||
demo_players,
|
||||
demo_round,
|
||||
demos,
|
||||
processing_status,
|
||||
sessions,
|
||||
|
||||
Reference in New Issue
Block a user