()
| 1991 | }; |
| 1992 | |
| 1993 | const handleHeartbeatsReceived = () => { |
| 1994 | const p2pMessagesMap = gdjs.multiplayerPeerJsHelper.getAllMessagesMap(); |
| 1995 | const messageNamesArray = Array.from(p2pMessagesMap.keys()); |
| 1996 | const heartbeatMessageNames = messageNamesArray.filter((messageName) => |
| 1997 | messageName.startsWith(heartbeatMessageNamePrefix) |
| 1998 | ); |
| 1999 | heartbeatMessageNames.forEach((messageName) => { |
| 2000 | const messagesList = p2pMessagesMap.get(messageName); |
| 2001 | if (!messagesList) return; // Should not happen. |
| 2002 | const messages = messagesList.getMessages(); |
| 2003 | if (!messages.length) return; // No messages to process for this name. |
| 2004 | messages.forEach((message) => { |
| 2005 | const messageData = message.getData(); |
| 2006 | const messageSender = message.getSender(); |
| 2007 | const matches = heartbeatMessageRegex.exec(messageName); |
| 2008 | if (!matches) { |
| 2009 | return; |
| 2010 | } |
| 2011 | const playerNumber = parseInt(matches[1], 10); |
| 2012 | // Ensure we know who is who. |
| 2013 | _peerIdToPlayerNumber[messageSender] = playerNumber; |
| 2014 | |
| 2015 | // If we are not the host, save what the host told us about the other players info |
| 2016 | // and respond with a heartbeat immediately, informing the host of our playerId and username. |
| 2017 | if (!gdjs.multiplayer.isCurrentPlayerHost()) { |
| 2018 | const currentPlayerNumber = |
| 2019 | gdjs.multiplayer.getCurrentPlayerNumber(); |
| 2020 | const currentlyKnownPlayerNumbers = Object.keys(_playersInfo).map( |
| 2021 | (playerNumber) => parseInt(playerNumber, 10) |
| 2022 | ); |
| 2023 | const receivedPlayerNumbers = Object.keys( |
| 2024 | messageData.playersInfo |
| 2025 | ).map((playerNumber) => parseInt(playerNumber, 10)); |
| 2026 | const currentlyKnownPingForCurrentUser = |
| 2027 | _playersInfo[currentPlayerNumber] && |
| 2028 | _playersInfo[currentPlayerNumber].ping; |
| 2029 | // If there are no players info yet, we're probably just connecting. |
| 2030 | // This can happen when joining a game that is already running. |
| 2031 | // Do not handle this case to avoid displaying too many notifications. |
| 2032 | if (!!currentlyKnownPlayerNumbers.length) { |
| 2033 | // Look at the players info received to know if there are new players who just connected. |
| 2034 | const newPlayerNumbers = receivedPlayerNumbers.filter( |
| 2035 | (playerNumber) => |
| 2036 | !currentlyKnownPlayerNumbers.includes(playerNumber) && |
| 2037 | playerNumber !== currentPlayerNumber // Do not consider ourselves as a new player. |
| 2038 | ); |
| 2039 | _playerNumbersWhoJustJoined.push(...newPlayerNumbers); |
| 2040 | // Or players who have disconnected. |
| 2041 | const playerNumbersWhoHaveDisconnected = |
| 2042 | currentlyKnownPlayerNumbers.filter( |
| 2043 | (playerNumber) => |
| 2044 | !receivedPlayerNumbers.includes(playerNumber) |
| 2045 | ); |
| 2046 | _playerNumbersWhoJustLeft.push( |
| 2047 | ...playerNumbersWhoHaveDisconnected |
| 2048 | ); |
| 2049 | for (const playerNumber of playerNumbersWhoHaveDisconnected) { |
| 2050 | // Temporarily save the username in another variable to be used for the notification, |
nothing calls this directly
no test coverage detected