Skip to content

Commit

Permalink
show server send messages as chat
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Mar 29, 2024
1 parent d9ff9d4 commit 217211f
Show file tree
Hide file tree
Showing 12 changed files with 785 additions and 45 deletions.
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ tempfile = { version = "3", optional = true }
lazy_static = { version = "1", optional = true }
prettyplease = { version = "0.1", optional = true }

[patch.crates-io]
schemars = { path = '../../rust/schemars/schemars' }
schemars_derive = { path = '../../rust/schemars/schemars_derive' }

[features]
schema = ["schemars", "bitbuffer/schemars"]
trace = ["tracing", "tracing-subscriber"]
Expand Down
72 changes: 41 additions & 31 deletions src/demo/message/usermessage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,39 +247,43 @@ pub struct SayText2Message {
pub text: MaybeUtf8String,
}

impl SayText2Message {
pub fn plain_text(&self) -> String {
// 1: normal, 2: old colors, 3: team, 4: location, 5 achievement, 6 custom
let mut text = self.text.to_string().replace(|c| c <= char::from(6), "");
// 7: 6-char hex
while let Some(pos) = text.chars().enumerate().find_map(|(index, c)| {
if c == char::from(7) {
Some(index)
} else {
None
}
}) {
text = text
.chars()
.take(pos)
.chain(text.chars().skip(pos + 7))
.collect();
fn to_plain_text(text: &str) -> String {
// 1: normal, 2: old colors, 3: team, 4: location, 5 achievement, 6 custom
let mut text = text.replace(|c| c <= char::from(6), "");
// 7: 6-char hex
while let Some(pos) = text.chars().enumerate().find_map(|(index, c)| {
if c == char::from(7) {
Some(index)
} else {
None
}
// 9: 8-char hex
while let Some(pos) = text.chars().enumerate().find_map(|(index, c)| {
if c == char::from(9) {
Some(index)
} else {
None
}
}) {
text = text
.chars()
.take(pos)
.chain(text.chars().skip(pos + 9))
.collect();
}) {
text = text
.chars()
.take(pos)
.chain(text.chars().skip(pos + 7))
.collect();
}
// 9: 8-char hex
while let Some(pos) = text.chars().enumerate().find_map(|(index, c)| {
if c == char::from(9) {
Some(index)
} else {
None
}
text
}) {
text = text
.chars()
.take(pos)
.chain(text.chars().skip(pos + 9))
.collect();
}
text
}

impl SayText2Message {
pub fn plain_text(&self) -> String {
to_plain_text(self.text.as_ref())
}
}

Expand Down Expand Up @@ -364,6 +368,12 @@ pub struct TextMessage {
pub substitute: [MaybeUtf8String; 4],
}

impl TextMessage {
pub fn plain_text(&self) -> String {
to_plain_text(self.text.as_ref())
}
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(BitRead, BitWrite, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ResetHudMessage {
Expand Down
39 changes: 30 additions & 9 deletions src/demo/parser/analyser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::demo::gameevent_gen::{
GameEvent, PlayerDeathEvent, PlayerSpawnEvent, TeamPlayRoundWinEvent,
};
use crate::demo::message::packetentities::EntityId;
use crate::demo::message::usermessage::{ChatMessageKind, SayText2Message, UserMessage};
use crate::demo::message::usermessage::{
ChatMessageKind, HudTextLocation, SayText2Message, TextMessage, UserMessage,
};
use crate::demo::message::{Message, MessageType};
use crate::demo::packet::stringtable::StringTableEntry;
use crate::demo::parser::handler::{BorrowMessageHandler, MessageHandler};
Expand Down Expand Up @@ -39,6 +41,15 @@ impl ChatMessage {
tick,
}
}

pub fn from_text(message: &TextMessage, tick: DemoTick) -> Self {
ChatMessage {
kind: ChatMessageKind::Empty,
from: String::new(),
text: message.plain_text(),
tick,
}
}
}

#[derive(
Expand Down Expand Up @@ -457,16 +468,26 @@ impl Analyser {
}

fn handle_user_message(&mut self, message: &UserMessage, tick: DemoTick) {
if let UserMessage::SayText2(text_message) = message {
if text_message.kind == ChatMessageKind::NameChange {
if let Some(from) = text_message.from.clone() {
self.change_name(from.into(), text_message.plain_text());
match message {
UserMessage::SayText2(text_message) => {
if text_message.kind == ChatMessageKind::NameChange {
if let Some(from) = text_message.from.clone() {
self.change_name(from.into(), text_message.plain_text());
}
} else {
self.state
.chat
.push(ChatMessage::from_message(text_message, tick));
}
} else {
self.state
.chat
.push(ChatMessage::from_message(text_message, tick));
}
UserMessage::Text(text_message) => {
if text_message.location == HudTextLocation::PrintTalk {
self.state
.chat
.push(ChatMessage::from_text(text_message, tick));
}
}
_ => {}
}
}

Expand Down
102 changes: 102 additions & 0 deletions tests/snapshots/tests__comp.dem_minimal.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,90 @@ expression: state
---
{
"chat": [
{
"kind": "Empty",
"from": "",
"text": "[demos.tf]: Demo recording completed",
"tick": 0
},
{
"kind": "TF_Chat_All",
"from": "frying pan",
"text": "gamers assembly",
"tick": 16
},
{
"kind": "Empty",
"from": "",
"text": "[demos.tf]: Uploading demo auto-20181103-2046-pl_upward.dem",
"tick": 204
},
{
"kind": "Empty",
"from": "",
"text": "[demos.tf]: Demos must be at least 5 minutes long",
"tick": 2638
},
{
"kind": "TF_Chat_All",
"from": "Sian",
"text": "thanks @demos.tf",
"tick": 3284
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] RED lost their uber advantage (39 seconds)",
"tick": 4376
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] BLU had uber for 48 seconds before using it",
"tick": 12508
},
{
"kind": "TF_Chat_AllDead",
"from": "frying pan",
"text": ":(",
"tick": 20640
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] BLU spent 16.6 seconds after spawning before healing",
"tick": 23676
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] RED lost their uber advantage (11 seconds)",
"tick": 27124
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] RED's medic died with 100% uber",
"tick": 27540
},
{
"kind": "TF_Chat_AllDead",
"from": "Chochy",
"text": "epic map",
"tick": 29596
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] BLU had uber for 80 seconds before using it",
"tick": 32424
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] RED's medic died with 100% uber",
"tick": 38084
},
{
"kind": "TF_Chat_AllDead",
"from": "Mystt",
Expand All @@ -46,6 +106,12 @@ expression: state
"text": "why are you up there",
"tick": 46256
},
{
"kind": "Empty",
"from": "",
"text": "#TF_TeamsSwitched",
"tick": 48464
},
{
"kind": "TF_Chat_All",
"from": "frying pan",
Expand All @@ -58,6 +124,18 @@ expression: state
"text": "!log",
"tick": 49060
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] BLU lost their uber advantage (11 seconds)",
"tick": 52764
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] BLU had uber for 72 seconds before using it",
"tick": 57374
},
{
"kind": "TF_Chat_AllDead",
"from": "Chochy",
Expand All @@ -70,6 +148,24 @@ expression: state
"text": "t",
"tick": 60618
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] BLU lost their uber advantage (19 seconds)",
"tick": 81376
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] BLU's medic died with 100% uber",
"tick": 82206
},
{
"kind": "Empty",
"from": "",
"text": "[STV Stats] BLU spent 9.1 seconds after spawning before healing",
"tick": 83404
},
{
"kind": "TF_Chat_AllDead",
"from": "Chochy",
Expand All @@ -88,6 +184,12 @@ expression: state
"text": "gg",
"tick": 85732
},
{
"kind": "Empty",
"from": "",
"text": "[SOAP] Plugins reloaded.",
"tick": 85900
},
{
"kind": "TF_Chat_All",
"from": "",
Expand Down
24 changes: 24 additions & 0 deletions tests/snapshots/tests__decal.dem_minimal.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ expression: state
"text": "gr",
"tick": 47043
},
{
"kind": "Empty",
"from": "",
"text": "#TF_TeamsSwitched",
"tick": 47283
},
{
"kind": "TF_Chat_All",
"from": "",
Expand Down Expand Up @@ -394,6 +400,12 @@ expression: state
"text": "[LogsTF] Uploading logs...",
"tick": 85836
},
{
"kind": "Empty",
"from": "",
"text": "[demos.tf]: Demo recording completed",
"tick": 85843
},
{
"kind": "TF_Chat_Team_Dead",
"from": "huge obese guy",
Expand All @@ -406,6 +418,12 @@ expression: state
"text": "!log",
"tick": 86033
},
{
"kind": "Empty",
"from": "",
"text": "[demos.tf]: Uploading demo 20190819-2009-pl_upward.dem",
"tick": 86033
},
{
"kind": "TF_Chat_All",
"from": "",
Expand All @@ -418,6 +436,12 @@ expression: state
"text": "[LogsTF] To see the stats, type: !log",
"tick": 86133
},
{
"kind": "Empty",
"from": "",
"text": "[demos.tf]: json_decode error: Syntax error",
"tick": 86218
},
{
"kind": "TF_Chat_All",
"from": "^D The Godfather | KING",
Expand Down
Loading

0 comments on commit 217211f

Please sign in to comment.