I have set up Typescript Runtime Server using this video. As per this video everything is okay. My question is, Does the Match Handler functions are called when a match is created from client side.
main.ts
let InitModule: nkruntime.InitModule = function (
ctx: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
initializer: nkruntime.Initializer
) {
const matchInit = function (
ctx: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
params: { [key: string]: string }
): { state: nkruntime.MatchState; tickRate: number; label: string } {
logger.debug("Lobby match created");
return {
state: { presences: {}, emptyTicks: 0 },
tickRate: 1, // 1 tick per second = 1 MatchLoop func invocations per second
label: "",
};
};
const matchJoin = function (
ctx: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
dispatcher: nkruntime.MatchDispatcher,
tick: number,
state: nkruntime.MatchState,
presences: nkruntime.Presence[]
): { state: nkruntime.MatchState } | null {
presences.forEach(function (p) {
state.presences[p.sessionId] = p;
});
logger.debug("Lobby match joined");
return {
state,
};
};
const matchLeave = function (
ctx: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
dispatcher: nkruntime.MatchDispatcher,
tick: number,
state: nkruntime.MatchState,
presences: nkruntime.Presence[]
): { state: nkruntime.MatchState } | null {
presences.forEach(function (p) {
delete state.presences[p.sessionId];
});
return {
state,
};
};
const matchLoop = function (
ctx: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
dispatcher: nkruntime.MatchDispatcher,
tick: number,
state: nkruntime.MatchState,
messages: nkruntime.MatchMessage[]
): { state: nkruntime.MatchState } | null {
// If we have no presences in the match according to the match state, increment the empty ticks count
if (state.presences.length === 0) {
state.emptyTicks++;
}
// If the match has been empty for more than 100 ticks, end the match by returning null
if (state.emptyTicks > 100) {
return null;
}
return {
state,
};
};
const matchJoinAttempt = function (
ctx: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
dispatcher: nkruntime.MatchDispatcher,
tick: number,
state: nkruntime.MatchState,
presence: nkruntime.Presence,
metadata: { [key: string]: any }
): {
state: nkruntime.MatchState;
accept: boolean;
rejectMessage?: string | undefined;
} | null {
logger.debug("%q attempted to join Lobby match", ctx.userId);
return {
state,
accept: true,
};
};
const matchSignal = function (
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 received: " + data);
return {
state,
data: "Lobby match signal received: " + data,
};
};
const matchTerminate = function (
ctx: nkruntime.Context,
logger: nkruntime.Logger,
nk: nkruntime.Nakama,
dispatcher: nkruntime.MatchDispatcher,
tick: number,
state: nkruntime.MatchState,
graceSeconds: number
): { state: nkruntime.MatchState } | null {
logger.debug("Lobby match terminated");
return {
state,
};
};
initializer.registerMatch("lobby", {
matchInit,
matchJoinAttempt,
matchJoin,
matchLeave,
matchLoop,
matchSignal,
matchTerminate,
});
};
// Reference InitModule to avoid it getting removed on build
// !InitModule && InitModule.bind(null);