DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

8db41b3a1e2ad2024db3d194fa35b4c231b2f3b5 — Jonni Liljamo 1 year, 9 months ago ef761fb
feat(client): revamp user details event, send it when needed
M client/src/plugins/networking/systems/events/receive/mod.rs => client/src/plugins/networking/systems/events/receive/mod.rs +26 -2
@@ 28,6 28,8 @@ use crate::{
    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() {


@@ 58,6 60,7 @@ pub fn disconnect_events(mut ev: EventReader<DisconnectEvent>) {
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>,


@@ 93,12 96,33 @@ pub fn message_events(
                }
                DataRequestType::GameAllForming => {
                    // TODO: handle possible error
                    play_data.all_forming = response.games.unwrap();
                    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
                    play_data.my_games = response.games.unwrap();
                    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 => {

M client/src/plugins/networking/systems/events/send/user.rs => client/src/plugins/networking/systems/events/send/user.rs +48 -11
@@ 6,21 6,58 @@
 * See LICENSE for licensing information.
 */

use bevy::prelude::EventReader;
use laurelin_shared::server::{
    channels::DataRequestChannel,
    messages::{DataRequest, DataRequestType},
use bevy::prelude::{EventReader, ResMut};
use laurelin_shared::{
    server::{
        channels::DataRequestChannel,
        messages::{DataRequest, DataRequestType},
    },
    types::user::UserPub,
};
use naia_bevy_client::Client;

use super::data_request;
use crate::Global;

pub struct PubUserDetailsEvent {
    pub id: String,
}
data_request!(
    pub_user_details_event,
    PubUserDetailsEvent,
    DataRequestType::PubUserDetails,
    |ev| Some(ev.id.clone())
);

pub fn pub_user_details_event(
    mut ev: EventReader<PubUserDetailsEvent>,
    mut client: Client,
    mut global: ResMut<Global>,
) {
    for ev in ev.iter() {
        // check if already in cache OR in cache queue
        if !global
            .users_cache
            .iter()
            .filter(|&u| u.id == ev.id)
            .cloned()
            .collect::<Vec<UserPub>>()
            .is_empty()
        {
            return;
        }

        if !global
            .users_cache_queue
            .iter()
            .filter(|&id| id == &ev.id)
            .cloned()
            .collect::<Vec<String>>()
            .is_empty()
        {
            return;
        }

        // add to cache queue
        global.users_cache_queue.push(ev.id.clone());

        // send request
        client.send_message::<DataRequestChannel, DataRequest>(&DataRequest::new(
            DataRequestType::PubUserDetails as u8,
            Some(ev.id.clone()),
        ));
    }
}