team04_server/messages/
end_fight.rs

1use crate::lobby::state::{clients::PlayerId, players::PlayerInMatchup};
2
3use super::*;
4
5pub struct EndFight {
6    pub matchup: [PlayerId; 2],
7    pub winner: Option<PlayerId>,
8}
9
10impl EndFight {
11    pub fn new(p1: PlayerId, p2: PlayerId, winner: Option<PlayerInMatchup>) -> Self {
12        Self {
13            matchup: [p1, p2],
14            winner: winner.map(|w| match w {
15                PlayerInMatchup::Player1 => p1,
16                PlayerInMatchup::Player2 => p2,
17            }),
18        }
19    }
20}
21
22impl MessageTx for EndFight {
23    fn serialize(&self) -> String {
24        #[derive(Serialize)]
25        #[serde(rename_all = "camelCase")]
26        struct EndFight {
27            #[serde(rename = "messageType")]
28            message_type: &'static str,
29            #[serde(rename = "match")]
30            pub matchup: [PlayerId; 2],
31            pub winner: Option<PlayerId>,
32        }
33
34        let message = EndFight {
35            message_type: "END_FIGHT",
36            matchup: self.matchup,
37            winner: self.winner,
38        };
39
40        serialize(&message)
41    }
42}