DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: b487c450a27c0e35fc05ecc7e2736b77527b8c5e deck-builder/client/src/plugins/networking/systems/events/receive/mod.rs -rw-r--r-- 4.8 KiB
b487c450Jonni Liljamo feat(api, shared): patch game endpoint and request, cleanup unused apis 1 year, 9 months ago
                                                                                
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
/*
 * This file is part of laurelin/client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use bevy::prelude::*;
use laurelin_shared::server::{
    channels::{AfterAuthChannel, CookieRefreshChannel, DataRequestChannel},
    messages::{AfterAuth, CookieRefresh, DataRequestResponse, DataRequestType},
};
use naia_bevy_client::{
    events::{ClientTickEvent, ConnectEvent, DisconnectEvent, MessageEvents, RejectEvent},
    Client,
};

use crate::{
    cfg::CfgUser,
    plugins::menu::{
        ui::{
            connect::{ConnectScreenData, ConnectState},
            play::{PlayScreenData, PlayScreenState},
        },
        MenuState,
    },
    Global,
};

use super::send::user::PubUserDetailsEvent;

pub fn connect_events(mut ev: EventReader<ConnectEvent>, client: Client) {
    for _ in ev.iter() {
        if let Ok(server_address) = client.server_address() {
            info!("Laurelin client connected to: '{}'", server_address);
        }
    }
}

pub fn reject_events(mut ev: EventReader<RejectEvent>, mut data: ResMut<ConnectScreenData>) {
    for _ in ev.iter() {
        warn!("Laurelin client rejected from connecting to server!");

        data.error = "rejected from connecting".to_string();
        data.state = if data.state == ConnectState::LoggingIn {
            ConnectState::Login
        } else {
            ConnectState::Register
        }
    }
}

pub fn disconnect_events(mut ev: EventReader<DisconnectEvent>) {
    for _ in ev.iter() {
        info!("Laurelin client disconnected from server.");
    }
}

pub fn message_events(
    mut commands: Commands,
    mut ev: EventReader<MessageEvents>,
    mut pud_ev_w: EventWriter<PubUserDetailsEvent>,
    mut global: ResMut<Global>,
    mut cfg_user: ResMut<CfgUser>,
    mut connect_data: ResMut<ConnectScreenData>,
    mut play_data: ResMut<PlayScreenData>,
) {
    for events in ev.iter() {
        for aa_message in events.read::<AfterAuthChannel, AfterAuth>() {
            // TODO: move handling to a separate function?

            // save details from AfterAuth
            cfg_user.id = aa_message.id;
            cfg_user.username = aa_message.username;
            cfg_user.cookie = aa_message.cookie;

            // reset the connection screen to login
            connect_data.state = ConnectState::Login;

            // take us to the main menu
            commands.insert_resource(NextState(Some(MenuState::Menu)));
        }

        for cr_message in events.read::<CookieRefreshChannel, CookieRefresh>() {
            cfg_user.cookie = cr_message.cookie;
        }

        for response in events.read::<DataRequestChannel, DataRequestResponse>() {
            match DataRequestType::from_u8(&response.r#type) {
                DataRequestType::GameCreate => {
                    // TODO: handle possible error (unwrap,
                    //       and if the response data is an error)
                    play_data.cur_game = response.games.unwrap().get(0).cloned();
                    play_data.state = PlayScreenState::InLobbyHost;
                }
                DataRequestType::GameAllForming => {
                    // TODO: handle possible error
                    let all_forming = response.games.unwrap();

                    for game in &all_forming {
                        pud_ev_w.send(PubUserDetailsEvent {
                            id: game.host_id.clone(),
                        });
                    }

                    play_data.all_forming = all_forming;
                    play_data.waiting_for_all_forming = false;
                }
                DataRequestType::GameMyGames => {
                    // TODO: handle possible error
                    let my_games = response.games.unwrap();

                    for game in &my_games {
                        pud_ev_w.send(PubUserDetailsEvent {
                            id: game.host_id.clone(),
                        });
                        if let Some(guest_id) = &game.guest_id {
                            pud_ev_w.send(PubUserDetailsEvent {
                                id: guest_id.clone(),
                            });
                        }
                    }

                    play_data.my_games = my_games;
                    play_data.waiting_for_my_games = false;
                }
                DataRequestType::PubUserDetails => {
                    // TODO: handle possible error
                    let user = response.users.unwrap().get(0).unwrap().clone();
                    global.users_cache_queue.retain(|id| id != &user.id);
                    global.users_cache.push(user);
                }
            }
        }
    }
}

pub fn tick_events(mut ev: EventReader<ClientTickEvent>, client: Client) {
    if !client.is_connected() {
        return;
    }

    for ClientTickEvent(_tick) in ev.iter() {}
}