FindOrCreateMatch Query

I am trying to create an online random game where first the player searches for existing online random games and join it else create a new match. but my query system is wrong. tried many things with no result. here is the code. can someone point out what the issue is here?

function rpcFindOrCreateMatchWithQuery(context: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: string) {
    logger.info("rpcFindOrCreateMatchWithQuery.............................. " + payload);

    //In this line, you're type casting an empty object to RpcCreateMatchRequest. It doesn't actually create an instance of RpcCreateMatchRequest, it just treats the empty object as if it were an instance of RpcCreateMatchRequest.
    let request = {} as RpcCreateOnlineRandomMatchRequest;

    try {
        request = JSON.parse(payload);
        logger.info("rpcFindOrCreateMatchWithQuery request.............................. gameMode " + request.gameMode);
    } catch (error) {
        logger.error('........Error parsing json message: %q', error);
        throw error;
    }

    const query = `+label.gameMode:online_random`;
    logger.info("rpcFindOrCreateMatchWithQuery query.............................. " + query);
     let matches = nk.matchList(5, true, "", 2 ,4, query);

    logger.info("rpcFindOrCreateMatchWithQuery.............................. matches " + matches.length);
    // If matches exist, sort by match size and return the largest.
    if (matches.length > 0) {
        matches.sort(function (a, b) {
            return a.size >= b.size ? 1 : -1;
        });
        let matchId = matches[0].matchId;
        logger.debug('rpcFindOrCreateMatchWithQuery --> Found Match: ' + matchId);

        let response = {
            created: false,
            matchId: matchId
        };

        return JSON.stringify(response);
    }

    logger.debug('rpcFindOrCreateMatchWithQuery --> Did not find any Match Creating a new one.');


    let matchId = nk.matchCreate(online_random, { gameMode: online_random });
    logger.info("rpcFindOrCreateMatchWithQuery Created match.............................. matchId " + matchId);

    let response = {
        created: true,
        matchId: matchId
    };

    return JSON.stringify(response);
}

The match Label is empty for the match created.
image

Is this syntax wrong?

    let matchId = nk.matchCreate(online_random, { gameMode: online_random });

Tried from the Unity Client API as well but no luck

  var result = await _client.ListMatchesAsync(_session, 2, 4, 5, true, string.Empty,
                "+label.gameMode:online_random");

Shouldn’t online_random in the label object be a string?

It is a string stored in a const
const online_random = “online_random”;

Can you please share your matchInit function?

Figured out the solution. I was returning an empty string instead of an actual label in matchInit
By the way, can we read the parameters that we used when creating the match in matchInit? I can use the same to return
return {
state: initialGameState,
tickRate: TICK_RATE, // 1 tick per second = 1 MatchLoop func invocations per second
label: JSON.stringify({ gameMode: online_random })
};

After creating the match does the host join automatically or do we need to call join match manually?
When the player 2 joins the length of the presence is just 1.

const online_private_matchJoin = function (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: GameState, presences: nkruntime.Presence): {
state: GameState
} | null {
logger.debug(online_private_matchJoin --> : state ${JSON.stringify(state)}. Presences length: ${presences.length});

presences.forEach(function (p) {
    state.presences[p.sessionId] = p;
});

return {
    state
};

}

And if we are implementing just the authoritative multiplayer do we need to implement these in unity client?
_socket.ReceivedMatchPresence += OnMatchPresenceEvent;
_socket.ReceivedMatchState += OnReceivedMatchState;

  1. Yes you can store the init params in the match state, match label or a storage object depending on your needs.
  2. In an authoritative match the host is the server and each of players has to connect to a match. If you’re using a hybrid authoritative/relayed model the client that will host the match still has to connect itself to the match. You need to ensure you’ve implemented the matchJoin and matchJoinAttempt handlers correctly. Please refer to the project template for a working example.
  3. Yes you still need to handle match state messages and listen to presence change events if required.

@sesposito When player 2 joins the authoritative server in his device OnMatchPresenceEvent gets fired only his ID gets fired in the parameter. I want to access all players details who joined before him. How can I do that in unity?

The OnMatchPresenceEvent is fired for each join/leave, the client should keep track of this information if it needs to know what players are currently in the match.

@sesposito Correct. When the second player joins. How do I display an UI showing the number of players in the current match. OnMatchPresenceEvent fired with one entry which is player 2. Player 2 has no information about the player 1 who joined earlier

In your match join handler - when a player joins you can broadcast the current list of players you’re keeping in your match state.