heroiclabs / nakama

Distributed server for social and realtime games and apps.
https://heroiclabs.com
Apache License 2.0
8.85k stars 1.08k forks source link

Unable to fetch userID from nkruntime.context passed ot the MatchJoinFunction #702

Closed tkbala closed 2 years ago

tkbala commented 2 years ago

I have a Nakama server setup using TypeScript. I have JS clients which connect successfully to the nakama server. Through seeing the presences in the Nakama's web console, I am able to confirm this.

When I query ctx.userID, in the matchJoin function, it seems to be undefined. Butt I am able to see the userId in the presences array in the matchJoin function, when I print it out. Also, ctx.userId seems to works in the matchJoinAttempt function.

Here's my two functions

const matchJoinAttempt = (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 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.debug('%q joined Lobby match', ctx.userId);
    logger.info(ctx.userId+" Joining room")

    return {
        state
    };
}

Log from the server is below:

backend        | {"level":"debug","ts":"2021-10-15T05:25:48.918Z","caller":"server/runtime_javascript_logger.go:98","msg":"\"273d1986-6771-46db-8798-c0e224e14342\" attempted to join Lobby match","mid":"446c6d08-7585-4eab-bd18-5c183727ca46"}
backend        | {"level":"debug","ts":"2021-10-15T05:25:48.918Z","caller":"server/runtime_javascript_logger.go:98","msg":"%!q(<nil>) joined Lobby match","mid":"446c6d08-7585-4eab-bd18-5c183727ca46"}
backend        | {"level":"info","ts":"2021-10-15T05:25:48.918Z","caller":"server/runtime_javascript_logger.go:68","msg":"undefined Joining room","mid":"446c6d08-7585-4eab-bd18-5c183727ca46"}
backend        | {"level":"debug","ts":"2021-10-15T05:25:48.918Z","caller":"server/tracker.go:835","msg":"Processing presence event","joins":1,"leaves":0}

I am unsure what is wrong. These are the code that is present at https://heroiclabs.com/docs/nakama/concepts/server-authoritative-multiplayer/

sesposito commented 2 years ago

@tkbala I'm closing this this as it is intended behaviour.

The matchJoinAttempt is executed once per player attempting to join, while matchJoin batches multiple joins in a single operation. The data of each player that joined is contained in presences array, so you should iterate over its entries and handle them accordingly.

tkbala commented 2 years ago

image Ah ok. Looks like the code in the Typescript documentation can be misleading. This needs to be updated

tkbala commented 2 years ago

Also, does presences array contain only the users that joined or does it contain all the users? This is not clear from the documentation

sesposito commented 2 years ago

Only the presences that have joined the match. Thank you for pointing out the error in the documentation, we'll fix it soon.

docsncode commented 2 years ago

Hi @tkbala we've fixed the docs https://heroiclabs.com/docs/nakama/concepts/server-authoritative-multiplayer/#full-example_2 thanks for reporting 😄

tkbala commented 2 years ago

Awesome! Thanks for the quick fix :D