()
| 1526 | }; |
| 1527 | |
| 1528 | const handleCustomMessagesReceived = (): void => { |
| 1529 | if (!gdjs.multiplayer.isReadyToSendOrReceiveGameUpdateMessages()) { |
| 1530 | // Assume that the custom messages are not worth saving for later use. |
| 1531 | return; |
| 1532 | } |
| 1533 | |
| 1534 | const p2pMessagesMap = gdjs.multiplayerPeerJsHelper.getAllMessagesMap(); |
| 1535 | const messageNamesArray = Array.from(p2pMessagesMap.keys()); |
| 1536 | const customMessageNames = messageNamesArray.filter((messageName) => |
| 1537 | messageName.startsWith(customMessageNamePrefix) |
| 1538 | ); |
| 1539 | customMessageNames.forEach((messageName) => { |
| 1540 | const messagesList = p2pMessagesMap.get(messageName); |
| 1541 | if (!messagesList) { |
| 1542 | logger.error(`No messages list found for ${messageName}.`); |
| 1543 | return; // Should not happen. |
| 1544 | } |
| 1545 | const messages = messagesList.getMessages(); |
| 1546 | if (!messages.length) { |
| 1547 | return; // No messages to process for this name. |
| 1548 | } |
| 1549 | messages.forEach((message) => { |
| 1550 | const messageData = message.getData(); |
| 1551 | const messageSender = message.getSender(); |
| 1552 | const uniqueMessageId = messageData.uniqueId; |
| 1553 | debugLogger.info( |
| 1554 | `Received custom message ${messageName} with data ${JSON.stringify( |
| 1555 | messageData |
| 1556 | )}.` |
| 1557 | ); |
| 1558 | const matches = customMessageRegex.exec(messageName); |
| 1559 | if (!matches) { |
| 1560 | // This should not happen. |
| 1561 | logger.error(`Invalid custom message ${messageName}.`); |
| 1562 | return; |
| 1563 | } |
| 1564 | |
| 1565 | const customMessageCacheKey = `${messageName}#${uniqueMessageId}`; |
| 1566 | if (processedCustomMessagesCache.has(customMessageCacheKey)) { |
| 1567 | // Message has already been processed recently. This can happen if the message is sent multiple times, |
| 1568 | // after not being acknowledged properly. |
| 1569 | debugLogger.info( |
| 1570 | `Message ${messageName} has already been processed, skipping.` |
| 1571 | ); |
| 1572 | return; |
| 1573 | } |
| 1574 | |
| 1575 | const acknowledgmentMessageName = |
| 1576 | createAcknowledgeCustomMessageNameFromCustomMessage(messageName); |
| 1577 | debugLogger.info( |
| 1578 | `Sending acknowledgment of custom message ${messageName} to ${messageSender}.` |
| 1579 | ); |
| 1580 | sendDataTo([messageSender], acknowledgmentMessageName, {}); |
| 1581 | |
| 1582 | // If we are the host, |
| 1583 | // so we need to relay the message to others. |
| 1584 | if (gdjs.multiplayer.isCurrentPlayerHost()) { |
| 1585 | // In the case of custom messages, we relay the message to all players, including the sender. |
nothing calls this directly
no test coverage detected