How to make MatchHandler work?

Hi,

sorry for the newbie question. I’m trying to setup a server-authoritative on Windows 10 machine.
after I finished installing CockroachDB & Nakama. My game can now connect to server, start matchmaking, play against other player without any problem.

but my TypeScript runtime seem not to work.

When Nakama server start, I can see log text from InitModule.
but other match handler function didn’t show log text at all.

Could anyone point out what I have done wrong in my code below?
Thanks in advance for any help!

let InitModule : nkruntime.InitModule = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
	logger.info('My Custom Module initialized.');
	initializer.registerMatch('lobby', {
			matchInit,
			matchJoinAttempt,
			matchJoin,
			matchLoop,
			matchLeave,
			matchTerminate,
			matchSignal
	});
}

const matchInit = (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, params: {[key: string]: string}) : {state: nkruntime.MatchState, tickRate: number, label: string} => {
	logger.info('My Custom Match initialized.');
	return {
			state: { },
			tickRate: 1,
			label: ''
	};
};

const matchJoinAttempt = (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence, metadata : {[key : string]: any}) : {state: nkruntime.MatchState, accept: boolean, rejectMessage?: string | undefined} | null => {
	logger.info('My Custom Match JoinAttempt');
	return {
			state,
			accept: true
	};
};

const matchJoin = (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : {state: nkruntime.MatchState} | null => {
	logger.info('My Custom Match Join');
	return {state};
};

const matchLeave = (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : {state: nkruntime.MatchState} | null => {
	logger.info('My Custom Match Leave');
	return {state};
};

const matchLoop = (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, messages: nkruntime.MatchMessage[]) : {state: nkruntime.MatchState} | null => {
	return {state};
};

const matchTerminate = (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, graceSeconds: number) : {state: nkruntime.MatchState} | null => {
	return {state};
};

const matchSignal = (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, data: string) : {state: nkruntime.MatchState, data?: string} | null => {
	logger.debug('Lobby match signal recieved: ' + data);
	return {state, data};
};

Hi @bodin,

Please can you share where you’re creating the authoritative match?

If you’re using the matchmaker you should be using a hook to create the "lobby" match when a matchmaker matched event occurs.

Alternatively, you can manually create a match and expose it via an RPC.

If you’re doing either of the above and you’re still not seeing the log entries when the match is running please let me know.

1 Like

Hi tom,

I created match with CreateMatchAsync(MatchName);
after registered new RPC function for creating match instead, it’s now working as expected.
Thank you very much for your guide!

1 Like

Hi @bodin, I’m glad you got it working. Just for reference, calling CreateMatchAsync from the client will create a server relayed (or client authoritative) match, which is why your server authoritative match handler won’t have been invoked. :slight_smile:

1 Like

thank you.
at first, I thought that match handler event also working on client authoritative.
now I know I was wrong. :sweat_smile: