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:
parent
9ea7a0d9a7
commit
2da874da18
2 changed files with 47 additions and 49 deletions
|
|
@ -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,7 +157,8 @@ 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 (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))?;
|
||||
|
|
@ -170,14 +171,12 @@ impl BitRead<LittleEndian> for SayText2Message {
|
|||
// 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 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, from, text)
|
||||
(kind, Some(from), text)
|
||||
} else {
|
||||
(ChatMessageKind::ChatAll, "".to_owned(), text)
|
||||
(ChatMessageKind::ChatAll, None, text)
|
||||
}
|
||||
} else {
|
||||
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 text = stream.read().or_else(handle_utf8_error)?;
|
||||
let _ = stream.skip_bits(16)?;
|
||||
(kind, from, text)
|
||||
(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()
|
||||
text = text
|
||||
.chars()
|
||||
.take(pos)
|
||||
.chain(text.bytes().skip(pos + 7))
|
||||
.collect(),
|
||||
)?;
|
||||
.chain(text.chars().skip(pos + 7))
|
||||
.collect();
|
||||
}
|
||||
|
||||
Ok(SayText2Message {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
if let Some(user) = self.users.values_mut().find(|user| user.name == from) {
|
||||
user.name = to;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue