DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: f2de86d05f6458f905521a221c7470b86207f285 deck-builder/server/src/systems/event/message/mod.rs -rw-r--r-- 3.4 KiB
f2de86d0Jonni Liljamo feat: add id to CfgUser and AfterAuth 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
/*
 * 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 bevy_ecs::{event::EventReader, system::Res};
use laurelin_shared::{
    api::game::{
        all_forming, create, my_games, ResponseAllForming, ResponseCreateGame, ResponseMyGames,
    },
    server::{
        channels::DataRequestChannel,
        messages::{DataRequest, DataRequestResponse, DataRequestType},
    },
};
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>,
    global: Res<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 response = create(&config.api_address, &cookie);
                    let json = match response {
                        ResponseCreateGame::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle
                        ResponseCreateGame::Valid(result) => {
                            serde_json::to_string(&result).unwrap() // TODO: handle
                        }
                    };
                    server.send_message::<DataRequestChannel, DataRequestResponse>(
                        &user_key,
                        &DataRequestResponse::new(request.r#type, &json),
                    );
                }
                DataRequestType::GameAllForming => {
                    // TODO: handle
                    let cookie = global.user_to_session_map.get(&user_key).unwrap();
                    let response = all_forming(&config.api_address, &cookie);
                    let json = match response {
                        ResponseAllForming::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle
                        ResponseAllForming::Valid(result) => {
                            serde_json::to_string(&result).unwrap() // TODO: handle
                        }
                    };
                    server.send_message::<DataRequestChannel, DataRequestResponse>(
                        &user_key,
                        &DataRequestResponse::new(request.r#type, &json),
                    );
                }
                DataRequestType::GameMyGames => {
                    // TODO: handle
                    let cookie = global.user_to_session_map.get(&user_key).unwrap();
                    let response = my_games(&config.api_address, &cookie);
                    let json = match response {
                        ResponseMyGames::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle
                        ResponseMyGames::Valid(result) => {
                            serde_json::to_string(&result).unwrap() // TODO: handle
                        }
                    };
                    server.send_message::<DataRequestChannel, DataRequestResponse>(
                        &user_key,
                        &DataRequestResponse::new(request.r#type, &json),
                    );
                }
            }
        }
    }
}