private void OnChatKeyDown(UIComponent component, UIKeyEventParameter eventParam) { // Don't run this code if the user has typed nothing in if (string.IsNullOrEmpty(_chatText.text)) { return; } // Run this code when the user presses the enter key if (eventParam.keycode == KeyCode.Return || eventParam.keycode == KeyCode.KeypadEnter) { // Get and clear the text var text = _chatText.text; _chatText.text = string.Empty; // If a command, parse it if (text.StartsWith("/")) { ParseCommand(text); return; } // If not connected to a server / hosting a server, tell the user and return if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.None) { AddMessage(MessageType.Warning, "<CSM> You can only use the chat feature when hosting or connected."); return; } // Get the player name var playerName = "Player"; if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Client) { playerName = MultiplayerManager.Instance.CurrentClient.Config.Username; } else if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Server) { playerName = MultiplayerManager.Instance.CurrentServer.Config.Username; } // Build and send this message var message = new ChatMessageCommand { Username = playerName, Message = text }; Command.SendToAll(message); // Add the message to the chat UI AddMessage(MessageType.Normal, $"<{playerName}> {text}"); } }
private void SubmitText(string text) { // If a command, parse it if (text.StartsWith("/")) { ChatCommand command = _chatCommands.Find(x => x.Command == text.TrimStart('/')); if (command == null) { PrintGameMessage(MessageType.Warning, $"'{text.TrimStart('/')}' is not a valid command."); return; } // Run the command command.Action.Invoke(text.TrimStart('/')); return; } // If not connected to a server / hosting a server, tell the user and return //if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.None) //{ // PrintGameMessage(MessageType.Warning, "You can only use the chat feature when hosting or connected."); // return; //} // Get the player name string playerName = "Local"; if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Client) { playerName = MultiplayerManager.Instance.CurrentClient.Config.Username; } else if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Server) { playerName = MultiplayerManager.Instance.CurrentServer.Config.Username; } // Build and send this message ChatMessageCommand message = new ChatMessageCommand { Username = playerName, Message = text }; Command.SendToAll(message); // Add the message to the chat UI PrintChatMessage(playerName, text); }
private void OnChatKeyDown(UIComponent component, UIKeyEventParameter eventParam) { // Don't run this code if the user has typed nothing in if (string.IsNullOrEmpty(_chatText.text)) { return; } // Run this code when the user presses the enter key if (eventParam.keycode == KeyCode.Return || eventParam.keycode == KeyCode.KeypadEnter) { // Get and clear the text string text = _chatText.text; _chatText.text = string.Empty; // If a command, parse it if (text.StartsWith("/")) { ChatCommand command = _chatCommands.Find(x => x.Command == text.TrimStart('/')); if (command == null) { PrintGameMessage(MessageType.Warning, $"'{text.TrimStart('/')}' is not a valid command."); return; } // Run the command command.Action.Invoke(text.TrimStart('/')); return; } // If not connected to a server / hosting a server, tell the user and return //if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.None) //{ // PrintGameMessage(MessageType.Warning, "You can only use the chat feature when hosting or connected."); // return; //} // Get the player name string playerName = "Local"; if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Client) { playerName = MultiplayerManager.Instance.CurrentClient.Config.Username; } else if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Server) { playerName = MultiplayerManager.Instance.CurrentServer.Config.Username; } // Build and send this message ChatMessageCommand message = new ChatMessageCommand { Username = playerName, Message = text }; Command.SendToAll(message); // Add the message to the chat UI PrintChatMessage(playerName, text); } }
public Task <Unit> SendMessage(ChatMessageCommand command, CancellationToken cancellationToken) { return(_mediator.Send(command, cancellationToken)); }