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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
mod models;
mod session;

use std::time::Duration;

use actix::{
    Actor, AsyncContext, Context, Handler, Message as ActixMessage, MessageResult, Recipient,
};
use fern::colors::{Color, ColoredLevelConfig};
use hashbrown::HashMap;
use log::{info, warn};
use nanoid::nanoid;
use serde::{Deserialize, Serialize};

use crate::{
    errors::AddWorldError,
    world::{Registry, World, WorldConfig},
};

pub use models::*;
pub use session::*;

#[derive(Serialize, Deserialize)]
pub struct OnJoinRequest {
    world: String,
    username: String,
}

/// A websocket server for Voxelize, holds all worlds data, and runs as a background
/// system service.
pub struct Server {
    /// The port that this voxelize server is running on.
    pub port: u16,

    /// The address that this voxelize server is running on.
    pub addr: String,

    /// Whether or not if the socket server has started as a system service.
    pub started: bool,

    /// Static folder to serve from.
    pub serve: String,

    /// Interval to tick the server at.
    pub interval: u64,

    /// A secret to join the server.
    pub secret: Option<String>,

    /// A map of all the worlds.
    worlds: HashMap<String, World>,

    /// Registry of the server.
    registry: Registry,

    /// Session IDs and addresses who haven't connected to a world.
    lost_sessions: HashMap<String, Recipient<EncodedMessage>>,

    /// What world each client ID is connected to, client ID <-> world ID.
    connections: HashMap<String, (Recipient<EncodedMessage>, String)>,
}

impl Server {
    /// Create a new Voxelize server instance used to host all the worlds.
    ///
    /// # Example
    ///
    /// ```
    /// // Create a server of port 4000 on "0.0.0.0"
    /// let server = Server::new().addr("0.0.0.0").port(4000).build();
    ///
    /// // Run the server on Voxelize
    /// Voxelize::run(server);
    /// ```
    pub fn new() -> ServerBuilder {
        Server::setup_logger();
        ServerBuilder::new()
    }

    /// Add a world instance to the server. Different worlds have different configurations, and can hold
    /// their own set of clients within. If the server has already started, the added world will be
    /// started right away.
    pub fn add_world(&mut self, mut world: World) -> Result<&mut World, AddWorldError> {
        let name = world.name.clone();
        world.ecs_mut().insert(self.registry.clone());

        if self.worlds.insert(name.to_owned(), world).is_some() {
            return Err(AddWorldError);
        };

        info!("🌎 World created: {}", name);

        Ok(self.worlds.get_mut(&name).unwrap())
    }

    /// Create a world in the server. Different worlds have different configurations, and can hold
    /// their own set of clients within. If the server has already started, the added world will be
    /// started right away.
    pub fn create_world(
        &mut self,
        name: &str,
        config: &WorldConfig,
    ) -> Result<&mut World, AddWorldError> {
        let mut world = World::new(name, config);
        world.ecs_mut().insert(self.registry.clone());
        self.add_world(world)
    }

    /// Get a world reference by name.
    pub fn get_world(&self, world_name: &str) -> Option<&World> {
        self.worlds.get(world_name)
    }

    /// Get a mutable world reference by name.
    pub fn get_world_mut(&mut self, world_name: &str) -> Option<&mut World> {
        self.worlds.get_mut(world_name)
    }

    /// Handler for client's message.
    pub fn on_request(&mut self, id: &str, data: Message) {
        if data.r#type == MessageType::Join as i32 {
            let json: OnJoinRequest = serde_json::from_str(&data.json)
                .expect("`on_join` error. Could not read JSON string.");

            if !self.lost_sessions.contains_key(id) {
                warn!("Client at {} is already in world: {}", id, json.world);
                return;
            }

            if let Some(world) = self.worlds.get_mut(&json.world) {
                if let Some(addr) = self.lost_sessions.remove(id) {
                    info!(
                        "Client at {} joined the server to world: {}",
                        id, json.world
                    );

                    world.add_client(id, &json.username, &addr);
                    self.connections.insert(id.to_owned(), (addr, json.world));
                } else {
                    info!("Something went wrong with joining. Maybe you called .join twice on the client?");
                }

                return;
            }

            warn!(
                "ID {} is attempting to connect to a non-existent world!",
                id
            );

            return;
        } else if data.r#type == MessageType::Leave as i32 {
            if let Some(world) = self.worlds.get_mut(&data.text) {
                let (addr, _) = self.connections.remove(id).unwrap();
                self.lost_sessions.insert(id.to_owned(), addr);

                world.remove_client(id);

                info!("Client at {} joined the server to world: {}", id, data.text);

                return;
            }
        }

        let connection = self.connections.get(id);
        if connection.is_none() {
            return;
        }

        let (_, world_name) = connection.unwrap().to_owned();

        if let Some(world) = self.get_world_mut(&world_name) {
            world.on_request(id, data);
        }
    }

    /// Handler for client leaving.
    pub fn on_leave(&mut self, client_id: &str) {
        if let Some((_, world_name)) = self.connections.remove(client_id) {
            if let Some(world) = self.get_world_mut(&world_name) {
                world.remove_client(client_id);
            }
        }

        info!("Client at {} left the server.", client_id);

        self.lost_sessions.remove(client_id);
    }

    /// Prepare all worlds on the server to start.
    pub fn prepare(&mut self) {
        for world in self.worlds.values_mut() {
            world.prepare();
        }
    }

    /// Tick every world on this server.
    pub fn tick(&mut self) {
        for world in self.worlds.values_mut() {
            world.tick();
        }
    }

    /// Setup Fern for debug logging.
    fn setup_logger() {
        fern::Dispatch::new()
            .format(|out, message, record| {
                let colors = ColoredLevelConfig::new().info(Color::Green);

                out.finish(format_args!(
                    "{} [{}] [{}]: {}",
                    chrono::Local::now().format("[%H:%M:%S]"),
                    colors.color(record.level()),
                    record.target(),
                    message
                ))
            })
            .level(log::LevelFilter::Debug)
            .level_for("tungstenite", log::LevelFilter::Info)
            .chain(std::io::stdout())
            .apply()
            .expect("Fern did not run successfully");
    }
}

/// New chat session is created
#[derive(ActixMessage)]
#[rtype(result = "String")]
pub struct Connect {
    pub id: Option<String>,
    pub addr: Recipient<EncodedMessage>,
}

#[derive(ActixMessage, Clone)]
#[rtype(result = "()")]
pub struct EncodedMessage(pub Vec<u8>);

/// Session is disconnected
#[derive(ActixMessage)]
#[rtype(result = "()")]
pub struct Disconnect {
    pub id: String,
}

/// Send message to specific world
#[derive(ActixMessage)]
#[rtype(result = "()")]
pub struct ClientMessage {
    /// Id of the client session
    pub id: String,

    /// Protobuf message
    pub data: Message,
}

/// Make actor from `ChatServer`
impl Actor for Server {
    /// We are going to use simple Context, we just need ability to communicate
    /// with other actors.
    type Context = Context<Self>;

    fn started(&mut self, ctx: &mut Self::Context) {
        ctx.run_interval(Duration::from_millis(self.interval), |act, _| {
            act.tick();
        });
    }
}

/// Handler for Connect message.
///
/// Register new session and assign unique id to this session
impl Handler<Connect> for Server {
    type Result = MessageResult<Connect>;

    fn handle(&mut self, msg: Connect, _: &mut Context<Self>) -> Self::Result {
        // notify all users in same room
        // self.send_message("Main", "Someone joined", 0);

        // register session with random id
        let id = if msg.id.is_none() {
            nanoid!()
        } else {
            msg.id.unwrap()
        };

        if self.lost_sessions.contains_key(&id) {
            return MessageResult(nanoid!());
        }

        self.lost_sessions.insert(id.to_owned(), msg.addr);

        // send id back
        MessageResult(id)
    }
}

/// Handler for Disconnect message.
impl Handler<Disconnect> for Server {
    type Result = ();

    fn handle(&mut self, msg: Disconnect, _: &mut Context<Self>) {
        if let Some((_, world_name)) = self.connections.remove(&msg.id) {
            if let Some(world) = self.worlds.get_mut(&world_name) {
                world.remove_client(&msg.id);
            }
        }
    }
}

/// Handler for Message message.
impl Handler<ClientMessage> for Server {
    type Result = ();

    fn handle(&mut self, msg: ClientMessage, _: &mut Context<Self>) {
        self.on_request(&msg.id, msg.data);
    }
}

const DEFAULT_PORT: u16 = 4000;
const DEFAULT_ADDR: &str = "0.0.0.0";
const DEFAULT_SERVE: &str = "./";
const DEFAULT_INTERVAL: u64 = 8;

/// Builder for a voxelize server.
pub struct ServerBuilder {
    port: u16,
    addr: String,
    serve: String,
    interval: u64,
    secret: Option<String>,
    registry: Option<Registry>,
}

impl ServerBuilder {
    /// Create a new server builder instance.
    pub fn new() -> Self {
        Self {
            port: DEFAULT_PORT,
            addr: DEFAULT_ADDR.to_owned(),
            serve: DEFAULT_SERVE.to_owned(),
            interval: DEFAULT_INTERVAL,
            secret: None,
            registry: None,
        }
    }

    /// Configure the port to the voxelize server.
    pub fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    /// Configure the address of the voxelize server.
    pub fn addr(mut self, addr: &str) -> Self {
        self.addr = addr.to_owned();
        self
    }

    /// Configure the static folder to serve.
    pub fn serve(mut self, serve: &str) -> Self {
        self.serve = serve.to_owned();
        self
    }

    /// Configure the interval for the server to tick at.
    pub fn interval(mut self, interval: u64) -> Self {
        self.interval = interval;
        self
    }

    /// Configure the secret for the server to be able to join.
    pub fn secret(mut self, secret: &str) -> Self {
        self.secret = Some(secret.to_owned());
        self
    }

    /// Configure the block registry of the server. Once a registry is configured, mutating it wouldn't
    /// change the server's block list.
    pub fn registry(mut self, registry: &Registry) -> Self {
        self.registry = Some(registry.to_owned());
        self
    }

    /// Instantiate a voxelize server instance.
    pub fn build(self) -> Server {
        let mut registry = self.registry.unwrap_or_default();
        registry.generate();

        Server {
            port: self.port,
            addr: self.addr,
            serve: self.serve,
            interval: self.interval,
            secret: self.secret,

            registry,

            started: false,

            connections: HashMap::default(),
            lost_sessions: HashMap::default(),
            worlds: HashMap::default(),
        }
    }
}