Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add chat messages as game event #190

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/parser/src/second_pass/game_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,40 @@ impl<'a> SecondPassParser<'a> {
}
None
}
pub fn create_custom_event_chat_message(&mut self, msg_bytes: &[u8]) -> Result<(), DemoParserError> {
use csgoproto::usermessages::CUserMessageSayText2;
self.game_events_counter.insert("chat_message".to_string());
if !self.wanted_events.contains(&"chat_message".to_string()) && self.wanted_events.first() != Some(&"all".to_string()) {
return Ok(());
}
let chat_msg: CUserMessageSayText2 = match Message::parse_from_bytes(&msg_bytes) {
Ok(msg) => msg,
Err(_) => return Err(DemoParserError::MalformedMessage),
};

let mut fields = vec![];
let controller_id = chat_msg.entityindex();
let res = self.find_user_by_controller_id(controller_id);
let entity_id = match res {
Some(metadata) => metadata.player_entity_id.unwrap_or(i32::MAX),
_ => i32::MAX,
};
fields.push(self.create_player_name_field(entity_id, "user"));
fields.push(self.create_player_steamid_field(entity_id, "user"));
fields.extend(self.find_extra_props_events(entity_id, "user"));
fields.push(EventField {
data: Some(Variant::String(chat_msg.param2().to_owned())),
name: "chat_message".to_string(),
});
let ge = GameEvent {
name: "chat_message".to_string(),
fields: fields,
tick: self.tick,
};
self.game_events.push(ge);
Ok(())
}

pub fn create_custom_event_round_start(&mut self, _events: &[GameEventInfo]) -> Result<(), DemoParserError> {
self.game_events_counter.insert("round_start".to_string());
if !self.wanted_events.contains(&"round_start".to_string()) && self.wanted_events.first() != Some(&"all".to_string()) {
Expand Down
12 changes: 1 addition & 11 deletions src/parser/src/second_pass/other_netmessages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,7 @@ impl<'a> SecondPassParser<'a> {
Ok(())
}
pub fn parse_chat_messages(&mut self, bytes: &[u8]) -> Result<(), DemoParserError> {
let chat_msg: CUserMessageSayText2 = match Message::parse_from_bytes(&bytes) {
Ok(msg) => msg,
Err(_) => return Err(DemoParserError::MalformedMessage),
};
self.chat_messages.push(ChatMessageRecord {
entity_idx: chat_msg.entityindex,
param1: chat_msg.param1,
param2: chat_msg.param2,
param3: chat_msg.param3,
param4: chat_msg.param4,
});
self.create_custom_event_chat_message(bytes)?;
Ok(())
}
pub fn parse_convars(&mut self, bytes: &[u8]) -> Result<(), DemoParserError> {
Expand Down
2 changes: 0 additions & 2 deletions src/python/demoparser2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ from typing import Dict, Sequence, Optional, List, Tuple
class DemoParser:
def __init__(self, path: str) -> None: ...
def parse_header(self) -> Dict[str, str]: ...
def parse_convars(self) -> Dict[str, str]: ...
def list_game_events(self) -> List[str]: ...
def parse_grenades(self) -> pd.DataFrame: ...
def parse_chat_messages(self) -> pd.DataFrame: ...
def parse_player_info(self) -> pd.DataFrame: ...
def parse_item_drops(self) -> pd.DataFrame: ...
def parse_skins(self) -> pd.DataFrame: ...
Expand Down
93 changes: 0 additions & 93 deletions src/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,6 @@ impl DemoParser {
.unwrap_or_else(AHashMap::default)
.to_object(py))
}
/// Returns a dictionary with console vars set. This includes data
/// like this: "mp_roundtime": "1.92", "mp_buytime": "20" ...
pub fn parse_convars(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let settings = ParserInputs {
real_name_to_og_name: AHashMap::default(),
wanted_players: vec![],
wanted_player_props: vec![],
wanted_other_props: vec![],
wanted_events: vec![],
parse_ents: false,
wanted_ticks: vec![],
parse_projectiles: false,
only_header: true,
count_props: false,
only_convars: false,
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
};
Ok(output.convars.to_object(py))
}
/// Returns the names of game events present in the demo
pub fn list_game_events(&self, _py: Python<'_>) -> PyResult<Py<PyAny>> {
let settings = ParserInputs {
Expand Down Expand Up @@ -210,74 +185,6 @@ impl DemoParser {
Ok(pandas_df.to_object(py))
})
}

/// returns a DF with chat messages
///
/// Example output:
/// entid name message param3 param4
/// 0 8 person1 asdfa
/// 1 8 person2 asdf TSpawn
pub fn parse_chat_messages(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let settings = ParserInputs {
real_name_to_og_name: AHashMap::default(),
wanted_players: vec![],
wanted_player_props: vec![],
wanted_other_props: vec![],
wanted_events: vec![],
parse_ents: false,
wanted_ticks: vec![],
parse_projectiles: false,
only_header: true,
count_props: false,
only_convars: false,
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
};
let entids: Vec<Option<i32>> = output.chat_messages.iter().map(|x| x.entity_idx).collect();
let param1: Vec<Option<String>> = output
.chat_messages
.iter()
.map(|x| x.param1.clone())
.collect();
let param2: Vec<Option<String>> = output
.chat_messages
.iter()
.map(|x| x.param2.clone())
.collect();
let param3: Vec<Option<String>> = output
.chat_messages
.iter()
.map(|x| x.param3.clone())
.collect();
let param4: Vec<Option<String>> = output
.chat_messages
.iter()
.map(|x| x.param4.clone())
.collect();
let entids = arr_to_py(Box::new(Int32Array::from(entids)))?;
let param1 = rust_series_to_py_series(&Series::new("param1", param1))?;
let param2 = rust_series_to_py_series(&Series::new("param2", param2))?;
let param3 = rust_series_to_py_series(&Series::new("param3", param3))?;
let param4 = rust_series_to_py_series(&Series::new("param4", param4))?;

let polars = py.import_bound("polars")?;
let all_series_py = [entids, param1, param2, param3, param4].to_object(py);
Python::with_gil(|py| {
let df = polars.call_method1("DataFrame", (all_series_py,))?;
// Set column names
let column_names = ["entid", "name", "message", "param3", "param4"];
df.setattr("columns", column_names.to_object(py))?;
// Call to_pandas with use_pyarrow_extension_array = true
let kwargs = vec![("use_pyarrow_extension_array", true)].into_py_dict_bound(py);
let pandas_df = df.call_method("to_pandas", (), Some(&kwargs))?;
Ok(pandas_df.to_object(py))
})
}
pub fn parse_player_info(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let settings = ParserInputs {
real_name_to_og_name: AHashMap::default(),
Expand Down
13 changes: 0 additions & 13 deletions src/python/tests/signature_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ def test_parse_header_signature(self):
self.assertIsInstance(key, str)
self.assertIsInstance(value, str)

def test_parse_convars_signature(self):
parser = DemoParser(demo_path)
convars = parser.parse_convars()
self.assertIsInstance(convars, dict)
for key, value in convars.items():
self.assertIsInstance(key, str)
self.assertIsInstance(value, str)

def test_list_game_events_signature(self):
parser = DemoParser(demo_path)
game_events = parser.list_game_events()
Expand All @@ -35,11 +27,6 @@ def test_parse_grenades_signature(self):
grenades = parser.parse_grenades()
self.assertIsInstance(grenades, pd.DataFrame)

def test_parse_chat_messages_signature(self):
parser = DemoParser(demo_path)
chat_messages = parser.parse_chat_messages()
self.assertIsInstance(chat_messages, pd.DataFrame)

def test_parse_player_info_signature(self):
parser = DemoParser(demo_path)
player_info = parser.parse_player_info()
Expand Down
Loading