Hi,
I’m running into a small conundrum while implementing an RPC call. Basically the RPC call needs to be able to access a match that the current player is in and mutate that state. I can’t seem to find any way to get the match state in my RPC callback. The callback is pretty basic and basically looks like:
func leaveMatch(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
// the payload is some JSON data containing the match id
var matchId = ...
match, err := nk.MatchGet(ctx, matchId)
if err != nil {
// match not found
return "", err
}
// now what? How can I get the custom match state that I created in MatchInit()?
}
There doesn’t seem to be anything that I can call to get my state stored in Nakama’s MatchHandler
struct. So, one alternative is to signal the Match
itself to do the mutating. For this I use the below code in my RPC handler:
signalResult, err := nk.MatchSignal(ctx, matchId, common.MatchSignalLeave)
Here I get my own state interface{}
which contains everything I need. In my callback for the signal I try to get the current user id so that I can map that to a player in the state:
func (m *Match) MatchSignal(ctx context.Context, logger runtime.Logger, _ *sql.DB, _ runtime.NakamaModule, _ runtime.MatchDispatcher, tick int64, state interface{}, data string) (interface{}, string) {
userId, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
if !ok {
return nil, errors.New("invalid context, missing user id")
}
....
}
Alas, it seems that the context.Context
does not contain runtime.RUNTIME_CTX_USER_ID
, probably because it’s a different context. So I can’t find the user/player that triggered the signal and can’t do my mutation here.
A final fugly kludge would be to encode the user id into the signal’s data
some way and use that, but that reeks of Bad Design® and is a last resort.
What is a correct way to do this? It would seem to me to be a pretty common thing you’d want to do?