1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

use std::fmt;
use std::sync::Arc;

use crate::Team;
use crate::Error;
use crate::Connector;

use crate::net::Packet;
use crate::net::BinaryReader;

use crate::message::any_chat_message::prelude::*;


pub struct TeamCastChatMessage {
    data:   ChatMessageData,
    to:     Arc<Team>,
    message:String,
}

impl TeamCastChatMessage {
    pub fn from_packet(connector: &Arc<Connector>, packet: &Packet, reader: &mut BinaryReader) -> Result<TeamCastChatMessage, Error> {
        Ok(TeamCastChatMessage {
            data:   ChatMessageData::from_packet(connector, packet, reader)?,
            to:     {
                let player = connector.player().upgrade().ok_or(Error::PlayerNotAvailable)?;
                let group = player.universe_group().upgrade().ok_or(Error::PlayerNotInUniverseGroup)?;
                group.team(reader.read_unsigned_byte()?)?
            },
            message:reader.read_string()?,
        })
    }

    pub fn to(&self) -> &Arc<Team> {
        &self.to
    }

    pub fn message(&self) -> &str {
        &self.message
    }
}

// TODO replace with delegation directive
// once standardized: https://github.com/rust-lang/rfcs/pull/1406
impl Message for TeamCastChatMessage {
    fn timestamp(&self) -> &DateTime {
        self.data.timestamp()
    }
}

// TODO replace with delegation directive
// once standardized: https://github.com/rust-lang/rfcs/pull/1406
impl ChatMessage for TeamCastChatMessage {
    fn from(&self) -> &Arc<Player> {
        self.data.from()
    }
}

impl fmt::Display for TeamCastChatMessage {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{}] <T: {}> {}", self.timestamp(), self.to().name(), self.message())
    }
}