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:
parent
9ea7a0d9a7
commit
2da874da18
2 changed files with 47 additions and 49 deletions
|
|
@ -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,47 +157,45 @@ 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) =
|
||||||
let first = stream.read::<u8>()?;
|
if stream.read::<u8>()? == 1 {
|
||||||
if first == 7 {
|
let first = stream.read::<u8>()?;
|
||||||
let _color = stream.read_string(Some(6))?;
|
if first == 7 {
|
||||||
} else {
|
let _color = stream.read_string(Some(6))?;
|
||||||
let _ = stream.skip_bits(8)?;
|
} else {
|
||||||
}
|
let _ = stream.skip_bits(8)?;
|
||||||
|
}
|
||||||
|
|
||||||
let text: String = stream.read().or_else(handle_utf8_error)?;
|
let text: String = stream.read().or_else(handle_utf8_error)?;
|
||||||
if text.starts_with("*DEAD*") {
|
if text.starts_with("*DEAD*") {
|
||||||
// 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 kind = ChatMessageKind::ChatAllDead;
|
||||||
let text: String = String::from_utf8(text.bytes().skip(end + 5).collect())?;
|
(kind, Some(from), text)
|
||||||
let kind = ChatMessageKind::ChatAllDead;
|
} else {
|
||||||
(kind, from, text)
|
(ChatMessageKind::ChatAll, None, text)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
(ChatMessageKind::ChatAll, "".to_owned(), text)
|
let _ = stream.set_pos(stream.pos() - 8)?;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let _ = stream.set_pos(stream.pos() - 8)?;
|
|
||||||
|
|
||||||
let kind = stream.read()?;
|
let kind = stream.read()?;
|
||||||
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 {
|
||||||
|
|
|
||||||
|
|
@ -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),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue