I am currently tackling the leave feature in my game using TypeScript. The server correctly receives the message and deletes the corresponding presence using delete state.presences[p.sessionId];
However, the player still receives messages from the game even after having its presence record deleted. How does the broadcast function actually work?
Client side
public void OnLeave()
{
LeaveMessage message = new() { playerId = gameStateManager.PlayerID, };
NakamaConnection.SendMessage(OpCodes.LEAVE_GAME, message);
//QuitMatch(); ---> Weirdly this function call does not work
}
/// <summary>
/// Quits the current match.
/// </summary>
public void QuitMatch()
{
// Ask Nakama to leave the match.
NakamaConnection.Socket.LeaveMatchAsync(currentMatch);
// Reset the currentMatch and localUser variables.
currentMatch = null;
localUser = null;
}
Server side
// Correctly deleting the presence on message receive
case OpCode.LEAVE_GAME:
logger.debug(`Leave Game ${message.sender.username}`);
delete state.presences[message.sender.sessionId];
if (state.game_state == GameState.LOBBY) {
sendPlayersInLobby(state, dispatcher);
} else {
let player = getPlayerFromId(state, message.sender.username);
for (let i = 0; i < state.players.length; i++) {
if (player?.username == state.players[i].username) {
state.players[i].hp -= 99;
removeDead(state, dispatcher, logger);
}
}
}
break;
// Problem can then be seen here among other places
const playersPresence: nkruntime.Presence[] = [];
for (let userId in state.presences) {
let presence = state.presences[userId];
if (presence) {
playersPresence.push(presence);
}
}
playersPresence.forEach((presence) => {
//Output nothing because no presence is found when playing against bots
logger.debug(presence.username);
});
logger.debug("Time up");
// Still sends to the player that has been removed
dispatcher.broadcastMessage(OpCode.DECISION_TIME_UP);
Am I doing something wrong?