DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: b487c450a27c0e35fc05ecc7e2736b77527b8c5e deck-builder/server/src/systems/event/message/mod.rs -rw-r--r-- 6.2 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
146
147
148
149
150
/*
 * This file is part of laurelin/server
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use std::vec;

use bevy_ecs::{
    event::EventReader,
    system::{Res, ResMut},
};
use laurelin_shared::{
    api::{
        game::{
            all_forming, create, my_games, ResponseAllForming, ResponseCreateGame, ResponseMyGames,
        },
        user::{self, ResponseInfo},
    },
    server::{
        channels::{CookieRefreshChannel, DataRequestChannel},
        messages::{CookieRefresh, DataRequest, DataRequestResponse, DataRequestType},
    },
    types::game::GamePub,
};
use naia_bevy_server::{events::MessageEvents, Server};

use crate::{Config, Global};

pub(crate) fn message_events(
    mut ev: EventReader<MessageEvents>,
    mut server: Server,
    config: Res<Config>,
    mut global: ResMut<Global>,
) {
    for events in ev.iter() {
        for (user_key, request) in events.read::<DataRequestChannel, DataRequest>() {
            match DataRequestType::from_u8(&request.r#type) {
                DataRequestType::GameCreate => {
                    // TODO: handle
                    let cookie = global.user_to_session_map.get(&user_key).unwrap();
                    let wrapped = create(&config.api_address, cookie);
                    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, None, Some(games_pub)),
                    );

                    // update cookie
                    global
                        .user_to_session_map
                        .insert(user_key, wrapped.cookie.clone());
                    server.send_message::<CookieRefreshChannel, CookieRefresh>(
                        &user_key,
                        &CookieRefresh::new(&wrapped.cookie),
                    );
                }
                DataRequestType::GameAllForming => {
                    // TODO: handle
                    let cookie = global.user_to_session_map.get(&user_key).unwrap();
                    let wrapped = all_forming(&config.api_address, cookie);
                    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, None, Some(games_pub)),
                    );

                    // update cookie
                    global
                        .user_to_session_map
                        .insert(user_key, wrapped.cookie.clone());
                    server.send_message::<CookieRefreshChannel, CookieRefresh>(
                        &user_key,
                        &CookieRefresh::new(&wrapped.cookie),
                    );
                }
                DataRequestType::GameMyGames => {
                    // TODO: handle
                    let cookie = global.user_to_session_map.get(&user_key).unwrap();
                    let wrapped = my_games(&config.api_address, cookie);
                    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, None, Some(games_pub)),
                    );

                    // update cookie
                    global
                        .user_to_session_map
                        .insert(user_key, wrapped.cookie.clone());
                    server.send_message::<CookieRefreshChannel, CookieRefresh>(
                        &user_key,
                        &CookieRefresh::new(&wrapped.cookie),
                    );
                }
                DataRequestType::PubUserDetails => {
                    // TODO: handle
                    let cookie = global.user_to_session_map.get(&user_key).unwrap();
                    let wrapped = user::info(
                        &config.api_address,
                        &request.data.unwrap_or("".to_string()),
                        cookie,
                    );
                    let user_details = match wrapped.response {
                        ResponseInfo::Error(_err) => {
                            panic!("I can't be bothered to handle this right now.")
                        }
                        ResponseInfo::Ok(result) => result,
                    };
                    server.send_message::<DataRequestChannel, DataRequestResponse>(
                        &user_key,
                        &DataRequestResponse::new(request.r#type, Some(vec![user_details]), None),
                    );

                    // update cookie
                    global
                        .user_to_session_map
                        .insert(user_key, wrapped.cookie.clone());
                    server.send_message::<CookieRefreshChannel, CookieRefresh>(
                        &user_key,
                        &CookieRefresh::new(&wrapped.cookie),
                    );
                }
            }
        }
    }
}