1 module voxelman.chat.plugin;
2 
3 import voxelman.log;
4 import pluginlib;
5 import voxelman.text.messagewindow : MessageWindow;
6 
7 import voxelman.core.events;
8 import voxelman.net.events;
9 import voxelman.net.packets;
10 
11 import voxelman.command.plugin;
12 import voxelman.eventdispatcher.plugin;
13 import voxelman.net.plugin;
14 import voxelman.session;
15 
16 
17 final class ChatPluginClient : IPlugin
18 {
19 	// IPlugin stuff
20 	mixin IdAndSemverFrom!"voxelman.chat.plugininfo";
21 
22 	private ClientSession session;
23 	private NetClientPlugin connection;
24 	private EventDispatcherPlugin evDispatcher;
25 	//MessageWindow messageWindow;
26 	float alpha;
27 
28 	override void preInit()
29 	{
30 		//messageWindow.init();
31 		//messageWindow.messageHandler =
32 		//	(string msg) => connection.send(MessagePacket(msg), 1);
33 	}
34 
35 	override void init(IPluginManager pluginman)
36 	{
37 		session = pluginman.getPlugin!ClientSession;
38 		evDispatcher = pluginman.getPlugin!EventDispatcherPlugin;
39 		evDispatcher.subscribeToEvent(&onMessageEvent);
40 
41 		connection = pluginman.getPlugin!NetClientPlugin;
42 	}
43 
44 	void onMessageEvent(ref MessageEvent event)
45 	{
46 		if (event.endpoint == MessageEndpoint.chat)
47 		{
48 			// TODO
49 			info(event.msg);
50 		}
51 	}
52 }
53 
54 final class ChatPluginServer : IPlugin
55 {
56 	// IPlugin stuff
57 	mixin IdAndSemverFrom!"voxelman.chat.plugininfo";
58 
59 	private NetServerPlugin connection;
60 	private ClientManager clientMan;
61 
62 	override void init(IPluginManager pluginman)
63 	{
64 		connection = pluginman.getPlugin!NetServerPlugin;
65 		clientMan = pluginman.getPlugin!ClientManager;
66 		auto commandPlugin = pluginman.getPlugin!CommandPluginServer;
67 		commandPlugin.registerCommand(CommandInfo("msg", &messageCommand, ["<message>"], "Sends a chat message to all clients"));
68 	}
69 
70 	void messageCommand(CommandParams params)
71 	{
72 		auto strippedMsg = params.rawStrippedArgs;
73 		connection.sendToAll(MessagePacket(strippedMsg, params.sourceType));
74 	}
75 
76 	void onMessageEvent(ref MessageEvent event)
77 	{
78 		if (event.endpoint == MessageEndpoint.chat)
79 		{
80 			connection.sendToAll(event.packet);
81 			infof("%s> %s", event.clientName, event.msg);
82 		}
83 	}
84 }