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
use std::{
    hash::Hash,
    sync::{Arc, RwLock},
};

use crate::{
    bigmap::BigMapKey,
    world::{
        entity::{
            entity_handle::EntityHandle, error::EntityDoesNotExistError, net_entity::OwnedNetEntity,
        },
        host::mut_channel::MutChannelType,
    },
    ComponentKind, GlobalDiffHandler,
};

pub trait GlobalWorldManagerType<E: Copy + Eq + Hash>: EntityHandleConverter<E> {
    fn component_kinds(&self, entity: &E) -> Option<Vec<ComponentKind>>;
    fn to_handle_converter(&self) -> &dyn EntityHandleConverter<E>;
    fn new_mut_channel(&self, diff_mask_length: u8) -> Arc<RwLock<dyn MutChannelType>>;
    fn diff_handler(&self) -> Arc<RwLock<GlobalDiffHandler<E>>>;
    fn despawn(&mut self, entity: &E);
}

pub trait EntityHandleConverter<E: Copy + Eq + Hash> {
    fn handle_to_entity(&self, entity_handle: &EntityHandle) -> Result<E, EntityDoesNotExistError>;
    fn entity_to_handle(&self, entity: &E) -> Result<EntityHandle, EntityDoesNotExistError>;
}

pub trait NetEntityHandleConverter {
    fn handle_to_net_entity(
        &self,
        entity_handle: &EntityHandle,
    ) -> Result<OwnedNetEntity, EntityDoesNotExistError>;
    fn net_entity_to_handle(
        &self,
        net_entity: &OwnedNetEntity,
    ) -> Result<EntityHandle, EntityDoesNotExistError>;
}

pub trait NetEntityConverter<E: Copy + Eq + Hash> {
    fn entity_to_net_entity(&self, entity: &E) -> Result<OwnedNetEntity, EntityDoesNotExistError>;
    fn net_entity_to_entity(
        &self,
        net_entity: &OwnedNetEntity,
    ) -> Result<E, EntityDoesNotExistError>;
}

pub struct FakeEntityConverter;

impl NetEntityHandleConverter for FakeEntityConverter {
    fn handle_to_net_entity(
        &self,
        _: &EntityHandle,
    ) -> Result<OwnedNetEntity, EntityDoesNotExistError> {
        Ok(OwnedNetEntity::Host(0))
    }

    fn net_entity_to_handle(
        &self,
        _: &OwnedNetEntity,
    ) -> Result<EntityHandle, EntityDoesNotExistError> {
        Ok(EntityHandle::from_u64(0))
    }
}

pub struct EntityConverter<'a, 'b, E: Eq + Copy + Hash> {
    handle_converter: &'a dyn EntityHandleConverter<E>,
    net_entity_converter: &'b dyn NetEntityConverter<E>,
}

impl<'a, 'b, E: Eq + Copy + Hash> EntityConverter<'a, 'b, E> {
    pub fn new(
        handle_converter: &'a dyn EntityHandleConverter<E>,
        net_entity_converter: &'b dyn NetEntityConverter<E>,
    ) -> Self {
        Self {
            handle_converter,
            net_entity_converter,
        }
    }
}

impl<'a, 'b, E: Copy + Eq + Hash> NetEntityHandleConverter for EntityConverter<'a, 'b, E> {
    fn handle_to_net_entity(
        &self,
        entity_handle: &EntityHandle,
    ) -> Result<OwnedNetEntity, EntityDoesNotExistError> {
        if let Ok(entity) = self.handle_converter.handle_to_entity(entity_handle) {
            return self.net_entity_converter.entity_to_net_entity(&entity);
        }
        return Err(EntityDoesNotExistError);
    }

    fn net_entity_to_handle(
        &self,
        net_entity: &OwnedNetEntity,
    ) -> Result<EntityHandle, EntityDoesNotExistError> {
        if let Ok(entity) = self.net_entity_converter.net_entity_to_entity(net_entity) {
            return self.handle_converter.entity_to_handle(&entity);
        }
        return Err(EntityDoesNotExistError);
    }
}