()
| 934 | }; |
| 935 | |
| 936 | const handleAcknowledgeMessagesReceived = () => { |
| 937 | if (!gdjs.multiplayer.isReadyToSendOrReceiveGameUpdateMessages()) { |
| 938 | // Acknowledgment messages are mainly a response for ownership change, destruction, and custom messages, |
| 939 | // which are not sent when the game is not ready. |
| 940 | return; |
| 941 | } |
| 942 | |
| 943 | const p2pMessagesMap = gdjs.multiplayerPeerJsHelper.getAllMessagesMap(); |
| 944 | const messageNamesArray = Array.from(p2pMessagesMap.keys()); |
| 945 | // When we receive acknowledgement messages, save it in the extension, to avoid sending the message again. |
| 946 | const acknowledgedMessageNames = messageNamesArray.filter( |
| 947 | isMessageAcknowledgement |
| 948 | ); |
| 949 | acknowledgedMessageNames.forEach((messageName) => { |
| 950 | const messagesList = p2pMessagesMap.get(messageName); |
| 951 | if (!messagesList) return; // Should not happen. |
| 952 | const messages = messagesList.getMessages(); |
| 953 | if (!messages.length) return; // No messages to process for this name. |
| 954 | messages.forEach((message) => { |
| 955 | const messageData = message.getData(); |
| 956 | const messageSender = message.getSender(); |
| 957 | debugLogger.info( |
| 958 | `Received acknowledgment for message ${messageName}.` |
| 959 | ); |
| 960 | const regex = getRegexFromAckMessageName(messageName); |
| 961 | if (!regex) { |
| 962 | // This should not happen. |
| 963 | logger.error(`Invalid acknowledgment message ${messageName}.`); |
| 964 | return; |
| 965 | } |
| 966 | |
| 967 | const matches = regex.exec(messageName); |
| 968 | if (!matches) { |
| 969 | // This should not happen. |
| 970 | logger.error(`Invalid acknowledgment message ${messageName}.`); |
| 971 | return; |
| 972 | } |
| 973 | if (!expectedMessageAcknowledgements[messageName]) { |
| 974 | // This should not happen, but if we receive an acknowledgment for a message we did not expect, let's not error. |
| 975 | return; |
| 976 | } |
| 977 | if (!expectedMessageAcknowledgements[messageName][messageSender]) { |
| 978 | // This should not happen, but if we receive an acknowledgment from a sender we did not expect, let's not error. |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | // If a clock is provided in the message, ensure that we only process the message if the clock is newer than the last one received. |
| 983 | const messageInstanceClock = messageData['_clock']; |
| 984 | if (messageInstanceClock !== undefined) { |
| 985 | const instanceNetworkId = matches[3]; |
| 986 | const sceneNetworkId = matches[4]; |
| 987 | const lastClock = getLastClockReceivedForInstanceOnScene({ |
| 988 | sceneNetworkId, |
| 989 | instanceNetworkId, |
| 990 | }); |
| 991 | if (messageInstanceClock <= lastClock) { |
| 992 | // Ignore old messages. |
| 993 | return; |
nothing calls this directly
no test coverage detected