logo
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
use super::{ffi, Value, ValueConverter, ValueConverterTrait, WorldData};

/// Represents the unique multiplayer id of a player.
pub type PlayerId = ffi::PlayerId;

/// Represents a set of player ids used for filtering visual and audible things.
#[must_use]
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct PlayerIdSet {
    set: WorldData,
}

impl PlayerIdSet {
    /// Creates a `PlayerIdSet` from a set of player ids
    /// `exclusive` set to `true` means that this set means all other players than the ones in
    /// `players`.
    pub fn new(players: &[PlayerId], exclusive: bool) -> Self {
        let flags = if exclusive {
            ffi::PlayerIdSetFlags::EXCLUSIVE
        } else {
            ffi::PlayerIdSetFlags::empty()
        };

        Self {
            set: WorldData::create_struct(
                ffi::CreateDataType::PlayerIdSet,
                &ffi::PlayerIdSet {
                    player_ids_ptr: players.as_ptr() as u32,
                    num_player_ids: players.len() as u32,
                    flags,
                    reserved: [0; 4],
                },
            ),
        }
    }

    /// Creates a `PlayerIdSet` from a set of player ids.
    /// This set will mean all players in the list.
    pub fn including(players: &[PlayerId]) -> Self {
        Self::new(players, false)
    }

    /// Creates a `PlayerIdSet` from a set of player ids
    /// This set will mean all players not in the list.
    pub fn excluding(players: &[PlayerId]) -> Self {
        Self::new(players, true)
    }
}

impl ValueConverterTrait<PlayerIdSet> for ValueConverter {
    fn into_value(v: PlayerIdSet) -> Value {
        // Partial move of mesh (WorldData) here, will not invoke drop
        <Self as ValueConverterTrait<WorldData>>::into_value(v.set)
    }
    fn from_value(v: &Value) -> PlayerIdSet {
        PlayerIdSet {
            set: <Self as ValueConverterTrait<WorldData>>::from_value(v),
        }
    }
}