DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

face8bc68a55ac41b5d9c3d44cfbfeef182af8c6 — Jonni Liljamo 1 year, 9 months ago 5409a8d
feat(client, server, shared): vittumainen bandage päälle, olkoot stna.
M client/src/plugins/menu/ui/play/mod.rs => client/src/plugins/menu/ui/play/mod.rs +4 -4
@@ 12,7 12,7 @@ use bevy::{
};

mod ui;
use laurelin_shared::types::game::Game;
use laurelin_shared::types::game::GamePub;
pub use ui::*;

#[derive(Default, Resource, Reflect)]


@@ 26,11 26,11 @@ pub struct PlayScreenData {
    pub waiting_for_my_games: bool,

    #[reflect(ignore)]
    pub cur_game: Option<Game>,
    pub cur_game: Option<GamePub>,
    #[reflect(ignore)]
    pub all_forming: Vec<Game>,
    pub all_forming: Vec<GamePub>,
    #[reflect(ignore)]
    pub my_games: Vec<Game>,
    pub my_games: Vec<GamePub>,
}

#[derive(Default, PartialEq, Clone, Reflect)]

M client/src/plugins/menu/ui/play/ui.rs => client/src/plugins/menu/ui/play/ui.rs +2 -1
@@ 173,7 173,8 @@ fn browse_forming(ui: &mut egui::Ui, data: &mut PlayScreenData, cfg_user: &CfgUs
                            ui.with_layout(
                                egui::Layout::right_to_left(egui::Align::Center),
                                |ui| {
                                    if game.guest_id.unwrap_or_default().to_string() == cfg_user.id
                                    if game.guest_id.clone().unwrap_or_default().to_string()
                                        == cfg_user.id
                                    {
                                        ui.add_enabled(false, egui::Button::new("Joined"));
                                        if ui.button("Inspect").clicked() {

M client/src/plugins/networking/systems/events/receive/mod.rs => client/src/plugins/networking/systems/events/receive/mod.rs +3 -3
@@ 86,17 86,17 @@ pub fn message_events(
                DataRequestType::GameCreate => {
                    // TODO: handle possible error (unwrap,
                    //       and if the response data is an error)
                    play_data.cur_game = serde_json::from_str(&response.data).unwrap();
                    play_data.cur_game = response.games.unwrap().get(0).cloned();
                    play_data.state = PlayScreenState::InLobbyHost;
                }
                DataRequestType::GameAllForming => {
                    // TODO: handle possible error
                    play_data.all_forming = serde_json::from_str(&response.data).unwrap();
                    play_data.all_forming = response.games.unwrap();
                    play_data.waiting_for_all_forming = false;
                }
                DataRequestType::GameMyGames => {
                    // TODO: handle possible error
                    play_data.my_games = serde_json::from_str(&response.data).unwrap();
                    play_data.my_games = response.games.unwrap();
                    play_data.waiting_for_my_games = false;
                }
            }

M server/src/systems/event/message/mod.rs => server/src/systems/event/message/mod.rs +27 -18
@@ 6,6 6,8 @@
 * See LICENSE for licensing information.
 */

use std::vec;

use bevy_ecs::{
    event::EventReader,
    system::{Res, ResMut},


@@ 18,6 20,7 @@ use laurelin_shared::{
        channels::{CookieRefreshChannel, DataRequestChannel},
        messages::{CookieRefresh, DataRequest, DataRequestResponse, DataRequestType},
    },
    types::game::GamePub,
};
use naia_bevy_server::{events::MessageEvents, Server};



@@ 36,15 39,17 @@ pub(crate) fn message_events(
                    // TODO: handle
                    let cookie = global.user_to_session_map.get(&user_key).unwrap();
                    let wrapped = create(&config.api_address, &cookie);
                    let json = match wrapped.response {
                        ResponseCreateGame::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle
                        ResponseCreateGame::Valid(result) => {
                            serde_json::to_string(&result).unwrap() // TODO: handle
                        }
                    let game = match wrapped.response {
                        ResponseCreateGame::Error(_err) => None, // TODO: handle
                        ResponseCreateGame::Valid(result) => Some(result),
                    };
                    let mut games_pub = vec![];
                    if let Some(game) = game {
                        games_pub.push(GamePub::from_game(&game));
                    }
                    server.send_message::<DataRequestChannel, DataRequestResponse>(
                        &user_key,
                        &DataRequestResponse::new(request.r#type, &json),
                        &DataRequestResponse::new(request.r#type, None, Some(games_pub)),
                    );

                    // update cookie


@@ 60,15 65,17 @@ pub(crate) fn message_events(
                    // TODO: handle
                    let cookie = global.user_to_session_map.get(&user_key).unwrap();
                    let wrapped = all_forming(&config.api_address, &cookie);
                    let json = match wrapped.response {
                        ResponseAllForming::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle
                        ResponseAllForming::Valid(result) => {
                            serde_json::to_string(&result).unwrap() // TODO: handle
                        }
                    let games = match wrapped.response {
                        ResponseAllForming::Error(_err) => vec![], // TODO: handle
                        ResponseAllForming::Valid(result) => result,
                    };
                    let mut games_pub = vec![];
                    for game in games {
                        games_pub.push(GamePub::from_game(&game));
                    }
                    server.send_message::<DataRequestChannel, DataRequestResponse>(
                        &user_key,
                        &DataRequestResponse::new(request.r#type, &json),
                        &DataRequestResponse::new(request.r#type, None, Some(games_pub)),
                    );

                    // update cookie


@@ 84,15 91,17 @@ pub(crate) fn message_events(
                    // TODO: handle
                    let cookie = global.user_to_session_map.get(&user_key).unwrap();
                    let wrapped = my_games(&config.api_address, &cookie);
                    let json = match wrapped.response {
                        ResponseMyGames::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle
                        ResponseMyGames::Valid(result) => {
                            serde_json::to_string(&result).unwrap() // TODO: handle
                        }
                    let games = match wrapped.response {
                        ResponseMyGames::Error(_err) => vec![], // TODO: handle
                        ResponseMyGames::Valid(result) => result,
                    };
                    let mut games_pub = vec![];
                    for game in games {
                        games_pub.push(GamePub::from_game(&game));
                    }
                    server.send_message::<DataRequestChannel, DataRequestResponse>(
                        &user_key,
                        &DataRequestResponse::new(request.r#type, &json),
                        &DataRequestResponse::new(request.r#type, None, Some(games_pub)),
                    );

                    // update cookie

M shared/src/server/messages/datarequest.rs => shared/src/server/messages/datarequest.rs +7 -3
@@ 8,6 8,8 @@

use naia_bevy_shared::Message;

use crate::types::{game::GamePub, user::UserPub};

#[derive(Message)]
pub struct DataRequest {
    pub r#type: u8,


@@ 22,14 24,16 @@ impl DataRequest {
#[derive(Message)]
pub struct DataRequestResponse {
    pub r#type: u8,
    pub data: String,
    pub users: Option<Vec<UserPub>>,
    pub games: Option<Vec<GamePub>>,
}

impl DataRequestResponse {
    pub fn new(r#type: u8, data: &str) -> Self {
    pub fn new(r#type: u8, users: Option<Vec<UserPub>>, games: Option<Vec<GamePub>>) -> Self {
        Self {
            r#type,
            data: data.to_string(),
            users,
            games,
        }
    }
}

M shared/src/types/game.rs => shared/src/types/game.rs +40 -0
@@ 8,6 8,7 @@

use chrono::NaiveDateTime;
use diesel::{Insertable, Queryable};
use naia_bevy_shared::Serde;
use serde::{Deserialize, Serialize};
use uuid::Uuid;



@@ 33,6 34,45 @@ pub struct Game {
    pub game_data_id: Uuid,
}

#[derive(Serialize, Deserialize, Clone, Serde, PartialEq)]
pub struct GamePub {
    pub id: String,
    pub created_at: String,
    pub updated_at: String,
    pub host_id: String,
    pub guest_id: Option<String>,
    pub state: i16,
    pub ended_at: Option<String>,
    pub game_data_id: String,
}

impl GamePub {
    pub fn from_game(game: &Game) -> Self {
        Self {
            id: game.id.to_string(),
            created_at: game.created_at.to_string(),
            updated_at: game.updated_at.to_string(),
            host_id: game.host_id.to_string(),
            guest_id: {
                if let Some(guest_id) = game.guest_id {
                    Some(guest_id.to_string())
                } else {
                    None
                }
            },
            state: game.state,
            ended_at: {
                if let Some(ended_at) = game.ended_at {
                    Some(ended_at.to_string())
                } else {
                    None
                }
            },
            game_data_id: game.game_data_id.to_string(),
        }
    }
}

#[derive(Deserialize, Insertable)]
#[diesel(table_name=games)]
pub struct InsertableGame {

M shared/src/types/user.rs => shared/src/types/user.rs +14 -5
@@ 8,6 8,7 @@

use chrono::NaiveDateTime;
use diesel::{Insertable, Queryable};
use naia_bevy_shared::Serde;
use serde::{Deserialize, Serialize};
use uuid::Uuid;



@@ 24,13 25,21 @@ pub struct User {
    pub password: String,
}

#[derive(Deserialize)]
#[derive(Serialize, Deserialize, Clone, Serde, PartialEq)]
pub struct UserPub {
    pub id: Uuid,
    pub created_at: NaiveDateTime,
    pub updated_at: NaiveDateTime,
    pub id: String,
    pub created_at: String,
    pub username: String,
    pub email: String,
}

impl UserPub {
    pub fn from_user(user: &User) -> Self {
        Self {
            id: user.id.to_string(),
            created_at: user.created_at.to_string(),
            username: user.username.to_string(),
        }
    }
}

#[derive(Deserialize, Insertable)]