Skip to content

Commit

Permalink
Fix flo-client generated replay doesn't include all chats.
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxu committed Feb 13, 2024
1 parent ffa7ba2 commit 49fff89
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions binaries/flo-cli/src/observer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use flo_observer_archiver::{ArchiverOptions, Fetcher};
use flo_replay::{generate_replay, GenerateReplayOptions};
use flo_replay::{generate_replay, GenerateReplayOptions, ReplayChatPolicy};
use s2_grpc_utils::S2ProtoUnpack;
use structopt::StructOpt;

Expand Down Expand Up @@ -77,7 +77,7 @@ impl Command {
GenerateReplayOptions {
game,
archive,
include_chats: true,
chat_policy: ReplayChatPolicy::IncludeAllChats,
},
w,
)
Expand Down
9 changes: 8 additions & 1 deletion crates/client/src/lan/game/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,14 @@ impl<'a> GameHandler<'a> {
}
};
if let Some(the_file) = the_file {
match generate_replay_from_packets(game_info, packet_copy, true, the_file).await {
match generate_replay_from_packets(
game_info,
packet_copy,
flo_replay::ReplayChatPolicy::IncludeAllChats,
the_file,
)
.await
{
Ok(_) => {}
Err(err) => {
tracing::error!("Could not generate replay because: {}", err);
Expand Down
35 changes: 26 additions & 9 deletions crates/replay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const FLO_PLAYER_ID: u8 = index_to_player_id(FLO_OB_SLOT);
pub struct GenerateReplayOptions {
pub game: flo_types::observer::GameInfo,
pub archive: Bytes,
pub include_chats: bool,
pub chat_policy: ReplayChatPolicy,
}

fn regenerate_game_info(
Expand Down Expand Up @@ -292,7 +292,7 @@ fn initialize_replay(game: &flo_types::observer::GameInfo) -> Result<(Vec<Record

fn convert_packet_to_record(
p: Packet,
include_chats: bool,
chat_policy: ReplayChatPolicy,
) -> Result<(Option<Record>, Option<u8>)> {
let (record, dropped_player) = match p.type_id() {
W3GSPacketTypeId::PlayerLeft => {
Expand All @@ -316,9 +316,19 @@ fn convert_packet_to_record(
}
W3GSPacketTypeId::ChatFromHost => {
let mut record = None;
if include_chats {
let payload: flo_w3gs::protocol::chat::ChatFromHost = p.decode_simple()?;
if payload.0.to_players.contains(&FLO_PLAYER_ID) {
match chat_policy {
ReplayChatPolicy::NoChats => {}
ReplayChatPolicy::IncludeChatVisibleToObservers => {
let payload: flo_w3gs::protocol::chat::ChatFromHost = p.decode_simple()?;
if payload.0.to_players.contains(&FLO_PLAYER_ID) {
record = Some(Record::ChatMessage(PlayerChatMessage {
player_id: payload.from_player(),
message: payload.0.message,
}));
}
}
ReplayChatPolicy::IncludeAllChats => {
let payload: flo_w3gs::protocol::chat::ChatFromHost = p.decode_simple()?;
record = Some(Record::ChatMessage(PlayerChatMessage {
player_id: payload.from_player(),
message: payload.0.message,
Expand All @@ -341,18 +351,25 @@ fn convert_packet_to_record(
Ok((record, dropped_player))
}

#[derive(Debug, Clone, Copy)]
pub enum ReplayChatPolicy {
NoChats,
IncludeChatVisibleToObservers,
IncludeAllChats,
}

pub async fn generate_replay_from_packets<W>(
game: flo_types::observer::GameInfo,
packets: Vec<Packet>,
include_chats: bool,
chat_policy: ReplayChatPolicy,
w: W,
) -> Result<()>
where
W: Write + Seek,
{
let (mut records, mut active_player_ids) = initialize_replay(&game)?;
for packet in packets.into_iter() {
let (record, dropped_player_id) = convert_packet_to_record(packet, include_chats)?;
let (record, dropped_player_id) = convert_packet_to_record(packet, chat_policy)?;
if let Some(rec) = record {
records.push(rec);
}
Expand All @@ -371,7 +388,7 @@ pub async fn generate_replay<W>(
GenerateReplayOptions {
game,
archive,
include_chats,
chat_policy,
}: GenerateReplayOptions,
w: W,
) -> Result<()>
Expand All @@ -393,7 +410,7 @@ where
for r in archive_records {
match r {
GameRecordData::W3GS(p) => {
let (record, dropped_player_id) = convert_packet_to_record(p, include_chats)?;
let (record, dropped_player_id) = convert_packet_to_record(p, chat_policy)?;
if let Some(rec) = record {
records.push(rec);
}
Expand Down

0 comments on commit 49fff89

Please sign in to comment.