-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathchat.rs
More file actions
61 lines (49 loc) · 1.95 KB
/
Copy pathchat.rs
File metadata and controls
61 lines (49 loc) · 1.95 KB
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
use common::Game;
use quill::{chat::ChatPreference, ChatBox};
use vane::{Component, EntityBuilder, SysResult, SystemExecutor};
use crate::{ClientId, Server};
/// Marker component for the console entity.
struct Console;
impl Component for Console {}
pub fn register(game: &mut Game, systems: &mut SystemExecutor<Game>) {
// Create the console entity so the console can receive messages
let mut console = EntityBuilder::new();
console.add(Console).add(ChatBox::new(ChatPreference::All));
// We can use the raw spawn method because
// the console isn't a "normal" entity.
game.ecs.spawn_builder(&mut console);
systems.add_system(flush_console_chat_box);
systems.group::<Server>().add_system(flush_chat_boxes);
systems.group::<Server>().add_system(flush_title_chat_boxes);
}
/// Flushes players' chat mailboxes and sends the needed packets.
fn flush_chat_boxes(game: &mut Game, server: &mut Server) -> SysResult {
for (_, (client_id, mut mailbox)) in game.ecs.query::<(&ClientId, &mut ChatBox)>().iter() {
if let Some(client) = server.clients.get(*client_id) {
for message in mailbox.drain() {
client.send_chat_message(message);
}
}
}
Ok(())
}
/// Prints chat messages to the console.
fn flush_console_chat_box(game: &mut Game) -> SysResult {
for (_, (_console, mut mailbox)) in game.ecs.query::<(&Console, &mut ChatBox)>().iter() {
for message in mailbox.drain() {
// TODO: properly display chat message
log::info!("{:?}", message.text());
}
}
Ok(())
}
fn flush_title_chat_boxes(game: &mut Game, server: &mut Server) -> SysResult {
for (_, (client_id, mut mailbox)) in game.ecs.query::<(&ClientId, &mut ChatBox)>().iter() {
if let Some(client) = server.clients.get(*client_id) {
for message in mailbox.drain_titles() {
client.send_title(message);
}
}
}
Ok(())
}