MCPcopy
hub / github.com/4ian/GDevelop / handleUpdateSceneMessagesReceived

Function handleUpdateSceneMessagesReceived

Extensions/Multiplayer/messageManager.ts:1689–1740  ·  view source on GitHub ↗
(
      runtimeScene: gdjs.RuntimeScene
    )

Source from the content-addressed store, hash-verified

1687 };
1688
1689 const handleUpdateSceneMessagesReceived = (
1690 runtimeScene: gdjs.RuntimeScene
1691 ) => {
1692 const p2pMessagesMap = gdjs.multiplayerPeerJsHelper.getAllMessagesMap();
1693 const messageNamesArray = Array.from(p2pMessagesMap.keys());
1694 const updateSceneMessageNames = messageNamesArray.filter((messageName) =>
1695 messageName.startsWith(updateSceneMessageNamePrefix)
1696 );
1697 updateSceneMessageNames.forEach((messageName) => {
1698 const messagesList = p2pMessagesMap.get(messageName);
1699 if (!messagesList) return; // Should not happen.
1700 const messages = messagesList.getMessages();
1701 if (!messages.length) return; // No messages to process for this name.
1702 messages.forEach((message) => {
1703 const messageData = message.getData();
1704 const messageSender = message.getSender();
1705 const sceneNetworkId = messageData.id;
1706
1707 if (gdjs.multiplayer.isReadyToSendOrReceiveGameUpdateMessages()) {
1708 if (sceneNetworkId !== runtimeScene.networkId) {
1709 debugLogger.info(
1710 `Received update of scene ${sceneNetworkId}, but we are on ${runtimeScene.networkId}. Skipping.`
1711 );
1712 // The scene is not the current scene.
1713 return;
1714 }
1715
1716 runtimeScene.updateFromNetworkSyncData(messageData, {});
1717 } else {
1718 // If the game is not ready to receive game update messages, we need to save the data for later use.
1719 // This can happen when joining a game that is already running.
1720 debugLogger.info(
1721 `Saving scene ${sceneNetworkId} update message for later use.`
1722 );
1723 lastReceivedSceneSyncDataUpdates.store(messageData);
1724 return;
1725 }
1726
1727 // If we are are the host,
1728 // we need to relay the scene update to others except the player who sent the update message.
1729 if (gdjs.multiplayer.isCurrentPlayerHost()) {
1730 const connectedPeerIds = gdjs.multiplayerPeerJsHelper.getAllPeers();
1731 // We don't need to send the message to the player who sent the update message.
1732 const otherPeerIds = connectedPeerIds.filter(
1733 (peerId) => peerId !== messageSender
1734 );
1735
1736 sendDataTo(otherPeerIds, messageName, messageData);
1737 }
1738 });
1739 });
1740 };
1741
1742 const updateGameMessageNamePrefix = '#updateGame';
1743 const createUpdateGameMessage = ({

Callers

nothing calls this directly

Calls 12

forEachMethod · 0.80
infoMethod · 0.80
sendDataToFunction · 0.70
filterMethod · 0.65
getMethod · 0.65
getMessagesMethod · 0.65
getDataMethod · 0.65
getSenderMethod · 0.65
storeMethod · 0.65
fromMethod · 0.45
keysMethod · 0.45

Tested by

no test coverage detected