Skip to content

Commit

Permalink
Always save replays on game handler exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxu committed Nov 13, 2023
1 parent 53c998e commit cb8098d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
56 changes: 31 additions & 25 deletions crates/client/src/lan/game/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,6 @@ impl<'a> GameHandler<'a> {
self.handle_game_packet(pkt).await?;
} else {
tracing::info!("game stream closed");
if self.save_replay {
let game_info = flo_types::observer::GameInfo::from((&*self.info.game, self.game_version_string.clone()));
let packet_copy = self.saved_packets.clone();
let mut user_replay_path = self.user_replay_path.clone();
tokio::spawn(async move {
let now = chrono::Utc::now();
let now_timestamp_str = format!("w3c-{}.w3g", now.format("%Y%m%d%H%M%S"));
user_replay_path.push_str(&now_timestamp_str);
let the_file = match std::fs::File::create(&user_replay_path) {
Ok(file) => Some(file),
Err(err) => {
tracing::error!("Could not open file: {}", err);
None
//return;
}
};
if let Some(the_file) = the_file {
match generate_replay_from_packets(game_info, packet_copy, true, the_file).await {
Ok(_) => {},
Err(err) => { tracing::error!("Could not generate replay because: {}", err); }
};
}
Ok::<(), Error>(())
});
}
return Ok(GameResult::Disconnected)
}
}
Expand Down Expand Up @@ -210,6 +185,37 @@ impl<'a> GameHandler<'a> {
}
}

pub fn start_save_replay(&self) {
if self.save_replay {
let game_info =
flo_types::observer::GameInfo::from((&*self.info.game, self.game_version_string.clone()));
let packet_copy = self.saved_packets.clone();
let mut user_replay_path = self.user_replay_path.clone();
tokio::task::spawn(async move {
let now = chrono::Utc::now();
let now_timestamp_str = format!("w3c-{}.w3g", now.format("%Y%m%d%H%M%S"));
user_replay_path.push_str(&now_timestamp_str);
let the_file = match std::fs::File::create(&user_replay_path) {
Ok(file) => Some(file),
Err(err) => {
tracing::error!("Could not open file: {}", err);
None
//return;
}
};
if let Some(the_file) = the_file {
match generate_replay_from_packets(game_info, packet_copy, true, the_file).await {
Ok(_) => {}
Err(err) => {
tracing::error!("Could not generate replay because: {}", err);
}
};
}
Ok::<(), Error>(())
});
}
}

#[inline]
async fn handle_incoming_w3gs(&mut self, pkt: Packet) -> Result<()> {
match pkt.type_id() {
Expand Down
2 changes: 2 additions & 0 deletions crates/client/src/lan/game/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ impl State {
guard.replace(GameEndReason::Unknown);
}
}
game_handler.start_save_replay();
stream.flush().await.ok();

Ok(())
}

Expand Down

0 comments on commit cb8098d

Please sign in to comment.