|
| 1 | +import { EntityManager } from '../../../../../shared/system/EntityManager.js' |
| 2 | +import { MessageListComponent } from '../../../../../shared/component/MessageComponent.js' |
| 3 | +import { Entity } from '../../../../../shared/entity/Entity.js' |
| 4 | + import { MessageEvent } from '../../component/events/MessageEvent.js' |
| 5 | +import { ChatComponent } from '../../component/tag/TagChatComponent.js' |
| 6 | +import { EventSystem } from '../../../../../shared/system/EventSystem.js' |
| 7 | +import { SerializedMessageType } from '../../../../../shared/network/server/serialized.js' |
| 8 | + |
| 9 | +export class MessageEventSystem { |
| 10 | + private MAX_MESSAGES: number = 20 |
| 11 | + private MAX_CONTENT_LENGTH: number = 160 |
| 12 | + |
| 13 | + update(entities: Entity[]) { |
| 14 | + const chatMessageEvents = EventSystem.getEvents(MessageEvent) |
| 15 | + |
| 16 | + // Check if there are any chat messages to process |
| 17 | + if (chatMessageEvents.length === 0) { |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + // Find Chat Entity |
| 22 | + // For now, we assume there is only one chat entity, but we could have multiple chat entities (local, group, etc) |
| 23 | + const chatEntity = EntityManager.getFirstEntityWithComponent(entities, ChatComponent) |
| 24 | + |
| 25 | + if (!chatEntity) { |
| 26 | + console.error('ChatEventSystem : A chat entity is required to send messages.') |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + for (const chatMessageEvent of chatMessageEvents) { |
| 31 | + const chatListComponent = chatEntity.getComponent(MessageListComponent) |
| 32 | + if (chatListComponent) { |
| 33 | + let content = chatMessageEvent.content |
| 34 | + const sender = chatMessageEvent.sender |
| 35 | + const messageType = chatMessageEvent.messageType |
| 36 | + const targetPlayerIds = chatMessageEvent.targetPlayerIds |
| 37 | + |
| 38 | + // Limit content length |
| 39 | + if (content.length > this.MAX_CONTENT_LENGTH) { |
| 40 | + content = content.substring(0, this.MAX_CONTENT_LENGTH) |
| 41 | + } |
| 42 | + |
| 43 | + // Handle different message types |
| 44 | + switch (messageType) { |
| 45 | + case SerializedMessageType.GLOBAL_NOTIFICATION: |
| 46 | + // Add global notification |
| 47 | + chatListComponent.addMessage(sender, content, SerializedMessageType.GLOBAL_NOTIFICATION) |
| 48 | + break; |
| 49 | + |
| 50 | + case SerializedMessageType.TARGETED_NOTIFICATION: |
| 51 | + case SerializedMessageType.TARGETED_CHAT: |
| 52 | + // Check if we have valid target player IDs |
| 53 | + if (targetPlayerIds && targetPlayerIds.length > 0) { |
| 54 | + // Add targeted message with appropriate message type |
| 55 | + chatListComponent.addMessage(sender, content, messageType, targetPlayerIds) |
| 56 | + } else { |
| 57 | + console.warn(`ChatEventSystem: ${messageType} without target player IDs`) |
| 58 | + // Fall back to regular chat message |
| 59 | + this.addGlobalChatMessage(chatListComponent, sender, content) |
| 60 | + } |
| 61 | + break; |
| 62 | + |
| 63 | + case SerializedMessageType.GLOBAL_CHAT: |
| 64 | + default: |
| 65 | + // Regular chat message |
| 66 | + this.addGlobalChatMessage(chatListComponent, sender, content) |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private addGlobalChatMessage(chatListComponent: MessageListComponent, sender: string, content: string) { |
| 74 | + // Limit history to maxMessages |
| 75 | + if (chatListComponent.list.length >= this.MAX_MESSAGES) { |
| 76 | + chatListComponent.list.shift() |
| 77 | + } |
| 78 | + |
| 79 | + chatListComponent.addMessage(sender, content, SerializedMessageType.GLOBAL_CHAT) |
| 80 | + } |
| 81 | +} |
0 commit comments