1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-04 02:24:12 +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 client: u8,
pub raw: u8, pub raw: u8,
pub kind: ChatMessageKind, pub kind: ChatMessageKind,
pub from: String, pub from: Option<String>,
pub text: String, pub text: String,
} }
@ -157,7 +157,8 @@ impl BitRead<LittleEndian> for SayText2Message {
fn read(stream: &mut Stream) -> ReadResult<Self> { fn read(stream: &mut Stream) -> ReadResult<Self> {
let client = stream.read()?; let client = stream.read()?;
let raw = stream.read()?; let raw = stream.read()?;
let (kind, from, text): (ChatMessageKind, String, String) = if stream.read::<u8>()? == 1 { let (kind, from, text): (ChatMessageKind, Option<String>, String) =
if stream.read::<u8>()? == 1 {
let first = stream.read::<u8>()?; let first = stream.read::<u8>()?;
if first == 7 { if first == 7 {
let _color = stream.read_string(Some(6))?; let _color = stream.read_string(Some(6))?;
@ -170,14 +171,12 @@ impl BitRead<LittleEndian> for SayText2Message {
// grave talk is in the format '*DEAD* \u0003$from\u0001: $text'b // grave talk is in the format '*DEAD* \u0003$from\u0001: $text'b
let start = text.find(char::from(3)).unwrap_or(0); let start = text.find(char::from(3)).unwrap_or(0);
let end = text.find(char::from(1)).unwrap_or(0); let end = text.find(char::from(1)).unwrap_or(0);
let from: String = String::from_utf8( let from: String = text.chars().skip(start + 1).take(end - start - 1).collect();
text.bytes().skip(start + 1).take(end - start - 1).collect(), let text: String = text.chars().skip(end + 5).collect();
)?;
let text: String = String::from_utf8(text.bytes().skip(end + 5).collect())?;
let kind = ChatMessageKind::ChatAllDead; let kind = ChatMessageKind::ChatAllDead;
(kind, from, text) (kind, Some(from), text)
} else { } else {
(ChatMessageKind::ChatAll, "".to_owned(), text) (ChatMessageKind::ChatAll, None, text)
} }
} else { } else {
let _ = stream.set_pos(stream.pos() - 8)?; let _ = stream.set_pos(stream.pos() - 8)?;
@ -186,18 +185,17 @@ impl BitRead<LittleEndian> for SayText2Message {
let from = stream.read().or_else(handle_utf8_error)?; let from = stream.read().or_else(handle_utf8_error)?;
let text = stream.read().or_else(handle_utf8_error)?; let text = stream.read().or_else(handle_utf8_error)?;
let _ = stream.skip_bits(16)?; let _ = stream.skip_bits(16)?;
(kind, from, text) (kind, Some(from), text)
}; };
// cleanup color codes // cleanup color codes
let mut text = text.replace(char::from(1), "").replace(char::from(3), ""); let mut text = text.replace(char::from(1), "").replace(char::from(3), "");
while let Some(pos) = text.find(char::from(7)) { while let Some(pos) = text.find(char::from(7)) {
text = String::from_utf8( text = text
text.bytes() .chars()
.take(pos) .take(pos)
.chain(text.bytes().skip(pos + 7)) .chain(text.chars().skip(pos + 7))
.collect(), .collect();
)?;
} }
Ok(SayText2Message { Ok(SayText2Message {

View file

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