Updating Match Labels

Hi,
I am trying to update match labels and want to check if what I’m doing is the correct method. This is my Typescript code below which is stored within the MatchJoin function.

var matchLabel = {
                playerID: player.presence.userId
            };
            var matchLabelString = JSON.stringify(matchLabel);
            dispatcher.matchLabelUpdate(matchLabelString);

It’s my attempt at adding player IDs to the match label “playerID”. As each match will have up to 2 players, will this add both player IDs to the “playerID” label? I’ve seen a few examples of updating the match label but they were only showing how to add a single string to the label, not a multiple array of strings (i.e., each player’s ID who are in the match).

Here’s the full code for reference:

let matchJoin: nkruntime.MatchJoinFunction = function (context: nkruntime.Context, logger: nkruntime.Logger, nakama: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) {
    let gameState = state as GameState;

    let presencesOnMatch: nkruntime.Presence[] = [];
    gameState.players.forEach(player => { if (player != undefined) presencesOnMatch.push(player.presence); });
    for (let presence of presences) {
        var account: nkruntime.Account = nakama.accountGetId(presence.userId);
        let player: Player =
        {
            presence: presence,
            displayName: account.user.displayName,
        }

        let nextPlayerNumber: number = getNextPlayerNumber(gameState.players);
        gameState.players[nextPlayerNumber] = player;
        presencesOnMatch.push(presence);
        logger.info("Joined Match: " + player.displayName);

        var matchLabel = {
                playerID: player.presence.userId
            };
            var matchLabelString = JSON.stringify(matchLabel);
            dispatcher.matchLabelUpdate(matchLabelString);
    }
    if (getPlayersCount(gameState.players) == 2) {
        dispatcher.broadcastMessage(Code.SetupMatch, "2");
        logger.info("Sending code: SetupMatch");
    }
    else { logger.info("Awaiting second player"); }
    //gameState.countdown = DurationLobby * TickRate;
    return { state: gameState };
}

{Details}

  1. Versions: Nakama {3.5}, {Windows, Mac, Linux binary or Docker}, {client library (SDK) and version}
  2. Server Framework Runtime language (If relevant) {Go, TS/JS, Lua}
{code or log snippet}

:tv: Media:

Your code seems to be overwriting the previous update, so the label will only contain the last iterated playerID.
Try calling matchLabelUpdate() only once, outside the loop, with all the IDs already in the label object.

1 Like