-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(game-service): add chat message handler
- Loading branch information
Showing
5 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/game-service/Game.Application/Handlers/ChatMessageHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Game.Application.Components; | ||
using Game.Application.Objects; | ||
using Game.Application.Objects.Components; | ||
using Game.Messages; | ||
using Game.MessageTools; | ||
|
||
namespace Game.Application.Handlers | ||
{ | ||
public class ChatMessageHandler : IMessageHandler<ChatMessage> | ||
{ | ||
private readonly IGameClientCollection gameClientCollection; | ||
private readonly IMessageSender messageSender; | ||
|
||
public ChatMessageHandler(IGameObject player, IGameClientCollection gameClientCollection) | ||
{ | ||
this.gameClientCollection = gameClientCollection; | ||
|
||
messageSender = player.Components.Get<IMessageSender>(); | ||
} | ||
|
||
public void Handle(ChatMessage message) | ||
{ | ||
var senderId = message.SenderId; | ||
var name = message.Name; | ||
var content = message.Content; | ||
|
||
SendChatMessage(senderId, name, content); | ||
SendBubbleNotification(senderId, content); | ||
} | ||
|
||
private void SendChatMessage(int senderId, string name, string content) | ||
{ | ||
foreach (var gameClient in gameClientCollection) | ||
{ | ||
var onnectionProvider = gameClient.Components.Get<IWebSocketConnectionProvider>(); | ||
var messageCode = (byte)MessageCodes.ChatMessage; | ||
var message = new ChatMessage() | ||
{ | ||
SenderId = senderId, | ||
Name = name, | ||
Content = content, | ||
ContentFormatted = GenerateMessageFormatted(name, content) | ||
}; | ||
|
||
onnectionProvider.SendMessage(messageCode, message); | ||
} | ||
} | ||
|
||
private void SendBubbleNotification(int senderId, string content) | ||
{ | ||
var messageCode = (byte)MessageCodes.BubbleNotification; | ||
var message = new BubbleNotificationMessage() | ||
{ | ||
NotifierId = senderId, | ||
Message = content, | ||
Time = 3 // Seconds | ||
}; | ||
|
||
messageSender.SendMessageToNearbyGameObjects(messageCode, message); | ||
} | ||
|
||
private string GenerateMessageFormatted(string name, string content) | ||
{ | ||
return $"<b>{name}: {content}</b>"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Game.Messages | ||
{ | ||
public struct ChatMessage | ||
{ | ||
public int SenderId { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public string Content { get; set; } | ||
|
||
public string ContentFormatted { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ public enum MessageCodes | |
Attacked, | ||
BubbleNotification, | ||
EnteredScene, | ||
SceneChanged | ||
SceneChanged, | ||
ChatMessage | ||
} | ||
} |