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);
}