DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: f2de86d05f6458f905521a221c7470b86207f285 deck-builder/server/src/systems/event/mod.rs -rw-r--r-- 4.3 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
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
/*
 * 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, ResMut},
};
use bevy_log::info;
use laurelin_shared::{
    api::user::{login, register, ResponseLogin, ResponseRegister},
    server::{
        channels::AfterAuthChannel,
        messages::{AfterAuth, Auth},
    },
};
use naia_bevy_server::{
    events::{AuthEvents, ConnectEvent, DisconnectEvent, ErrorEvent, TickEvent},
    Server,
};

use crate::{Config, Global, RuntimeTemp};

mod message;
pub(crate) use message::message_events;

pub(crate) fn auth_events(
    mut ev: EventReader<AuthEvents>,
    mut server: Server,
    config: Res<Config>,
    mut temp: ResMut<RuntimeTemp>,
) {
    for events in ev.iter() {
        for (user_key, auth) in events.read::<Auth>() {
            // TODO: accept a third type of authentication: existing session
            //       the client passes a cookie that it had stored,
            //       and we verify it with the api, returning the very same aa details.

            match auth.username {
                Some(username) => {
                    // register
                    let wrapped =
                        register(&config.api_address, &username, &auth.email, &auth.password);
                    match wrapped.response {
                        ResponseRegister::Error(_) => server.reject_connection(&user_key),
                        ResponseRegister::Ok(user) => {
                            server.accept_connection(&user_key);
                            temp.afterauth_details
                                .insert(user_key, (user, wrapped.cookie));
                        }
                    }
                }
                None => {
                    // login
                    let wrapped = login(&config.api_address, &auth.email, &auth.password);
                    match wrapped.response {
                        ResponseLogin::Error(_) => server.reject_connection(&user_key),
                        ResponseLogin::Ok(user) => {
                            server.accept_connection(&user_key);
                            temp.afterauth_details
                                .insert(user_key, (user, wrapped.cookie));
                        }
                    }
                }
            }
        }
    }
}

pub(crate) fn connect_events(
    mut ev: EventReader<ConnectEvent>,
    mut server: Server,
    mut temp: ResMut<RuntimeTemp>,
    mut global: ResMut<Global>,
) {
    for ConnectEvent(user_key) in ev.iter() {
        let address = server.user(user_key).address();
        info!("Naia Server connected to: {:?}", address);

        // TODO: handle
        let aa_details = temp.afterauth_details.get(user_key).unwrap();
        server.send_message::<AfterAuthChannel, AfterAuth>(
            &user_key,
            &AfterAuth {
                id: aa_details.0.id.to_string().clone(),
                username: aa_details.0.username.clone(),
                cookie: aa_details.1.clone(),
            },
        );

        global
            .user_to_session_map
            .insert(*user_key, aa_details.1.clone());

        temp.afterauth_details.remove(&user_key);
    }
}

pub(crate) fn disconnect_events(mut ev: EventReader<DisconnectEvent>, mut global: ResMut<Global>) {
    for DisconnectEvent(user_key, user) in ev.iter() {
        info!("Naia Server disconnected from: {:?}", user.address);

        global.user_to_session_map.remove(user_key);
    }
}

pub(crate) fn error_events(mut ev: EventReader<ErrorEvent>) {
    for ErrorEvent(error) in ev.iter() {
        info!("Naia Server Error: {:?}", error);
    }
}

pub(crate) fn tick_events(mut ev: EventReader<TickEvent>, mut server: Server) {
    let mut has_ticked = false;

    for TickEvent(server_tick) in ev.iter() {
        has_ticked = true;

        // Handle messages coming from clients
        let mut _messages = server.receive_tick_buffer_messages(server_tick);
    }

    if has_ticked {
        // Update scopes of entities
        // Not sure if this will come into play,
        // but see naia/demos/bevy/server/src/systems/events.rs::tick_events
        //for (_, user_key, entity) in server.scope_checks() {
        // ...
        //}
    }
}