1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

from is optional for chat message

This commit is contained in:
Robin Appelman 2019-08-24 02:55:15 +02:00
commit 2da874da18
2 changed files with 47 additions and 49 deletions

View file

@ -142,7 +142,7 @@ pub struct SayText2Message {
pub client: u8,
pub raw: u8,
pub kind: ChatMessageKind,
pub from: String,
pub from: Option<String>,
pub text: String,
}
@ -157,47 +157,45 @@ impl BitRead<LittleEndian> for SayText2Message {
fn read(stream: &mut Stream) -> ReadResult<Self> {
let client = stream.read()?;
let raw = stream.read()?;
let (kind, from, text): (ChatMessageKind, String, String) = if stream.read::<u8>()? == 1 {
let first = stream.read::<u8>()?;
if first == 7 {
let _color = stream.read_string(Some(6))?;
} else {
let _ = stream.skip_bits(8)?;
}
let (kind, from, text): (ChatMessageKind, Option<String>, String) =
if stream.read::<u8>()? == 1 {
let first = stream.read::<u8>()?;
if first == 7 {
let _color = stream.read_string(Some(6))?;
} else {
let _ = stream.skip_bits(8)?;
}
let text: String = stream.read().or_else(handle_utf8_error)?;
if text.starts_with("*DEAD*") {
// grave talk is in the format '*DEAD* \u0003$from\u0001: $text'b
let start = text.find(char::from(3)).unwrap_or(0);
let end = text.find(char::from(1)).unwrap_or(0);
let from: String = String::from_utf8(
text.bytes().skip(start + 1).take(end - start - 1).collect(),
)?;
let text: String = String::from_utf8(text.bytes().skip(end + 5).collect())?;
let kind = ChatMessageKind::ChatAllDead;
(kind, from, text)
let text: String = stream.read().or_else(handle_utf8_error)?;
if text.starts_with("*DEAD*") {
// grave talk is in the format '*DEAD* \u0003$from\u0001: $text'b
let start = text.find(char::from(3)).unwrap_or(0);
let end = text.find(char::from(1)).unwrap_or(0);
let from: String = text.chars().skip(start + 1).take(end - start - 1).collect();
let text: String = text.chars().skip(end + 5).collect();
let kind = ChatMessageKind::ChatAllDead;
(kind, Some(from), text)
} else {
(ChatMessageKind::ChatAll, None, text)
}
} else {
(ChatMessageKind::ChatAll, "".to_owned(), text)
}
} else {
let _ = stream.set_pos(stream.pos() - 8)?;
let _ = stream.set_pos(stream.pos() - 8)?;
let kind = stream.read()?;
let from = stream.read().or_else(handle_utf8_error)?;
let text = stream.read().or_else(handle_utf8_error)?;
let _ = stream.skip_bits(16)?;
(kind, from, text)
};
let kind = stream.read()?;
let from = stream.read().or_else(handle_utf8_error)?;
let text = stream.read().or_else(handle_utf8_error)?;
let _ = stream.skip_bits(16)?;
(kind, Some(from), text)
};
// cleanup color codes
let mut text = text.replace(char::from(1), "").replace(char::from(3), "");
while let Some(pos) = text.find(char::from(7)) {
text = String::from_utf8(
text.bytes()
.take(pos)
.chain(text.bytes().skip(pos + 7))
.collect(),
)?;
text = text
.chars()
.take(pos)
.chain(text.chars().skip(pos + 7))
.collect();
}
Ok(SayText2Message {

View file

@ -26,7 +26,7 @@ impl ChatMassage {
pub fn from_message(message: SayText2Message, tick: u32) -> Self {
ChatMassage {
kind: message.kind,
from: message.from,
from: message.from.unwrap_or_default(),
text: message.text,
tick,
}
@ -232,21 +232,21 @@ impl Analyser {
}
fn handle_user_message(&mut self, message: UserMessage, tick: u32) {
match message {
UserMessage::SayText2(message) => match message.kind {
ChatMessageKind::NameChange => self.change_name(message.from, message.text),
_ => self.chat.push(ChatMassage::from_message(message, tick)),
},
_ => {}
if let UserMessage::SayText2(text_message) = message {
if text_message.kind == ChatMessageKind::NameChange {
if let Some(from) = text_message.from {
self.change_name(from, text_message.text);
}
} else {
self.chat
.push(ChatMassage::from_message(text_message, tick));
}
}
}
fn change_name(&mut self, from: String, to: String) {
for (_, user) in self.users.iter_mut() {
if user.name == from {
user.name = to;
return;
}
if let Some(user) = self.users.values_mut().find(|user| user.name == from) {
user.name = to;
}
}
@ -316,9 +316,9 @@ impl UserState {
users
.into_iter()
.map(|(user_id, user)| {
.map(|(_, user)| {
(
user_id,
user.user_id,
UserState {
classes: classes.remove(&user.user_id).unwrap_or(HashMap::new()),
team: teams.remove(&user.user_id).unwrap_or(Team::Other),