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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use std::net::SocketAddr;

use bevy::prelude::*;
use de_core::schedule::PreMovement;
use de_messages::{FromGame, FromServer, GameOpenError, JoinError, Readiness, ToGame, ToServer};
use de_net::Reliability;
use de_types::player::Player;

use crate::{
    config::ConnectionType,
    lifecycle::{FatalErrorEvent, NetGameConfRes},
    messages::{
        FromGameServerEvent, FromMainServerEvent, MessagesSet, Ports, ToGameServerEvent,
        ToMainServerEvent,
    },
    netstate::NetState,
};

pub(crate) struct GamePlugin;

impl Plugin for GamePlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<GameOpenedEvent>()
            .add_event::<GameJoinedEvent>()
            .add_event::<PeerJoinedEvent>()
            .add_event::<PeerLeftEvent>()
            .add_event::<GameReadinessEvent>()
            .add_event::<SetReadinessEvent>()
            .add_systems(OnEnter(NetState::Connected), open_or_join)
            .add_systems(
                PreMovement,
                (
                    process_from_server
                        .run_if(on_event::<FromMainServerEvent>())
                        .after(MessagesSet::RecvMessages),
                    process_from_game
                        .run_if(on_event::<FromGameServerEvent>())
                        .after(MessagesSet::RecvMessages),
                ),
            )
            .add_systems(
                PostUpdate,
                set_readiness
                    .run_if(in_state(NetState::Joined))
                    .run_if(on_event::<SetReadinessEvent>())
                    .before(MessagesSet::SendMessages),
            )
            .add_systems(OnEnter(NetState::ShuttingDown), leave);
    }
}

/// A new game on the given socket address was just opened.
#[derive(Event)]
pub struct GameOpenedEvent(pub SocketAddr);

/// A game was just joined.
#[derive(Event)]
pub struct GameJoinedEvent {
    player: Player,
}

impl GameJoinedEvent {
    fn new(player: Player) -> Self {
        Self { player }
    }

    pub fn player(&self) -> Player {
        self.player
    }
}

#[derive(Event)]
pub struct PeerJoinedEvent(Player);

impl PeerJoinedEvent {
    pub fn id(&self) -> Player {
        self.0
    }
}

#[derive(Event)]
pub struct PeerLeftEvent(Player);

impl PeerLeftEvent {
    pub fn id(&self) -> Player {
        self.0
    }
}

/// This event is sent when game readiness stage of the joined game changes.
#[derive(Event, Deref)]
pub struct GameReadinessEvent(Readiness);

/// Send this event to change player readiness stage.
#[derive(Event)]
pub struct SetReadinessEvent(Readiness);

impl From<Readiness> for SetReadinessEvent {
    fn from(readiness: Readiness) -> Self {
        Self(readiness)
    }
}

fn open_or_join(
    conf: Res<NetGameConfRes>,
    mut main_server: EventWriter<ToMainServerEvent>,
    mut game_server: EventWriter<ToGameServerEvent>,
) {
    match conf.connection_type() {
        ConnectionType::CreateGame { max_players, .. } => {
            info!("Sending a open-game request.");
            main_server.send(ToServer::OpenGame { max_players }.into());
        }
        ConnectionType::JoinGame(_) => {
            info!("Sending a join-game request.");
            game_server.send(ToGameServerEvent::new(
                Reliability::SemiOrdered,
                ToGame::Join,
            ));
        }
    }
}

fn process_from_server(
    conf: Res<NetGameConfRes>,
    mut ports: ResMut<Ports>,
    mut events: EventReader<FromMainServerEvent>,
    mut outputs: EventWriter<ToGameServerEvent>,
    mut opened: EventWriter<GameOpenedEvent>,
    mut fatals: EventWriter<FatalErrorEvent>,
) {
    for event in events.read() {
        match event.message() {
            FromServer::Pong(id) => {
                info!("Pong {} received from server.", *id);
            }
            FromServer::GameOpened { port } => match ports.init_game_port(*port) {
                Ok(_) => {
                    info!("Game on port {} opened.", *port);
                    // Send something to open NAT.
                    outputs.send(ToGameServerEvent::new(
                        Reliability::Unordered,
                        ToGame::Ping(u32::MAX),
                    ));
                    opened.send(GameOpenedEvent(SocketAddr::new(conf.server_host(), *port)));
                }
                Err(err) => {
                    fatals.send(FatalErrorEvent::new(format!("Invalid GameOpened: {err:?}")));
                }
            },
            FromServer::GameOpenError(err) => match err {
                GameOpenError::DifferentGame => {
                    fatals.send(FatalErrorEvent::new(
                        "Cannot open game, the player already joined a game.",
                    ));
                }
            },
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn process_from_game(
    mut inputs: EventReader<FromGameServerEvent>,
    mut fatals: EventWriter<FatalErrorEvent>,
    state: Res<State<NetState>>,
    mut joined_events: EventWriter<GameJoinedEvent>,
    mut peer_joined_events: EventWriter<PeerJoinedEvent>,
    mut peer_left_events: EventWriter<PeerLeftEvent>,
    mut readiness_events: EventWriter<GameReadinessEvent>,
    mut next_state: ResMut<NextState<NetState>>,
) {
    for event in inputs.read() {
        match event.message() {
            FromGame::Pong(id) => {
                trace!("Received Pong({id}).");
            }
            FromGame::NotJoined => {
                fatals.send(FatalErrorEvent::new(
                    "Player is no longer part of the game.",
                ));
            }
            FromGame::Joined(player) => {
                info!("Joined game as {player}.");
                next_state.set(NetState::Joined);
                joined_events.send(GameJoinedEvent::new(*player));
            }
            FromGame::JoinError(error) => match error {
                JoinError::GameFull => {
                    fatals.send(FatalErrorEvent::new("Game is full, cannot join."));
                }
                JoinError::GameNotOpened => {
                    fatals.send(FatalErrorEvent::new(
                        "Game is no longer opened, cannot join.",
                    ));
                }
                JoinError::AlreadyJoined => {
                    fatals.send(FatalErrorEvent::new(
                        "Already joined the game, cannot re-join.",
                    ));
                }
                JoinError::DifferentGame => {
                    fatals.send(FatalErrorEvent::new(
                        "Player already joined a different game.",
                    ));
                }
            },
            FromGame::Left => {
                if state.get() < &NetState::ShuttingDown {
                    fatals.send(FatalErrorEvent::new("Player was kicked from the game."));
                }
            }
            FromGame::PeerJoined(id) => {
                info!("Peer {id} joined.");
                peer_joined_events.send(PeerJoinedEvent(*id));
            }
            FromGame::PeerLeft(id) => {
                info!("Peer {id} left.");
                peer_left_events.send(PeerLeftEvent(*id));
            }
            FromGame::GameReadiness(readiness) => {
                info!("Game readiness changed to: {readiness:?}");
                readiness_events.send(GameReadinessEvent(*readiness));
            }
        }
    }
}

fn set_readiness(
    mut readiness_events: EventReader<SetReadinessEvent>,
    mut message_events: EventWriter<ToGameServerEvent>,
) {
    let Some(readiness) = readiness_events.read().last() else {
        return;
    };

    message_events.send(ToGameServerEvent::new(
        Reliability::SemiOrdered,
        ToGame::Readiness(readiness.0),
    ));
}

fn leave(mut server: EventWriter<ToGameServerEvent>) {
    info!("Sending leave game message.");
    // Send this even if not yet joined because the join / open-game request
    // might already be processed.
    server.send(ToGameServerEvent::new(
        Reliability::SemiOrdered,
        ToGame::Leave,
    ));
}