~starkingdoms/starkingdoms

ref: cd7053cdc98554b3e38dba6927a0e00d411b49ad starkingdoms/server/src/player/request_save.rs -rw-r--r-- 1.5 KiB
cd7053cd — ghostly_zsh codegen and also functioning strings 1 year, 4 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
use std::net::SocketAddr;

use bevy::prelude::*;
use bevy_rapier2d::prelude::Velocity;
use starkingdoms_common::{pack_savefile, unpack_savefile, SaveData};

use crate::{
    module::component::{Attach, CanAttach, LooseAttach, PartFlags, PartType},
    planet::PlanetType,
    ws::WsEvent,
    AppKeys, Packet,
};

use super::component::Player;

pub fn request_save(
    attached_query: &Query<
        (
            Entity,
            &PartType,
            &mut Transform,
            &mut Attach,
            &Velocity,
            Option<&CanAttach>,
            Option<&LooseAttach>,
            &mut PartFlags,
        ),
        (Without<PlanetType>, Without<Player>),
    >,
    old_save: Option<String>,
    app_keys: AppKeys,
    attach: Attach,
    event_queue: &mut Vec<WsEvent>,
    from: &SocketAddr,
) {
    let unused_modules = if let Some(ref old_save) = old_save {
        if let Ok(old_savedata) = unpack_savefile(&app_keys.app_key, old_save.to_string()) {
            old_savedata.unused_modules
        } else {
            Vec::new()
        }
    } else {
        Vec::new()
    };
    let save = SaveData {
        children: crate::module::save::construct_save_data(attach.clone(), attached_query),
        unused_modules,
    };
    let save_string = pack_savefile(&app_keys.app_key, save);
    let packet = Packet::SaveData {
        payload: save_string,
    };

    event_queue.push(WsEvent::Send {
        to: *from,
        message: packet.into(),
    });
}