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

Function handleUpdateGameMessagesReceived

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

Source from the content-addressed store, hash-verified

1850 };
1851
1852 const handleUpdateGameMessagesReceived = (
1853 runtimeScene: gdjs.RuntimeScene
1854 ) => {
1855 const p2pMessagesMap = gdjs.multiplayerPeerJsHelper.getAllMessagesMap();
1856 const messageNamesArray = Array.from(p2pMessagesMap.keys());
1857 const updateGameMessageNames = messageNamesArray.filter((messageName) =>
1858 messageName.startsWith(updateGameMessageNamePrefix)
1859 );
1860 updateGameMessageNames.forEach((messageName) => {
1861 const messagesList = p2pMessagesMap.get(messageName);
1862 if (!messagesList) return; // Should not happen.
1863 const messages = messagesList.getMessages();
1864 if (!messages.length) return; // No messages to process for this name.
1865 messages.forEach((message) => {
1866 const messageData = message.getData();
1867 const messageSender = message.getSender();
1868 if (gdjs.multiplayer.isReadyToSendOrReceiveGameUpdateMessages()) {
1869 runtimeScene.getGame().updateFromNetworkSyncData(messageData, {});
1870 } else {
1871 // If the game is not ready to receive game update messages, we need to save the data for later use.
1872 // This can happen when joining a game that is already running.
1873 debugLogger.info(`Saving game update message for later use.`);
1874 lastReceivedGameSyncDataUpdates.store(messageData);
1875 return;
1876 }
1877
1878 // If we are are the host,
1879 // we need to relay the game update to others except the player who sent the update message.
1880 if (gdjs.multiplayer.isCurrentPlayerHost()) {
1881 const connectedPeerIds = gdjs.multiplayerPeerJsHelper.getAllPeers();
1882 // We don't need to send the message to the player who sent the update message.
1883 const otherPeerIds = connectedPeerIds.filter(
1884 (peerId) => peerId !== messageSender
1885 );
1886
1887 sendDataTo(otherPeerIds, messageName, messageData);
1888 }
1889 });
1890 });
1891 };
1892
1893 const handleSavedUpdateMessages = (runtimeScene: gdjs.RuntimeScene) => {
1894 // Reapply the game saved updates.

Callers

nothing calls this directly

Calls 13

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