in the rpc call i write some code like this:
userName, ok := ctx.Value(runtime.RUNTIME_CTX_USERNAME).(string)
if !ok {
return "", internal.ErrNoUserNameFound
}
sessionID, ok := ctx.Value(runtime.RUNTIME_CTX_SESSION_ID).(string)
if !ok {
logger.Error("context did not contain session ID.")
return "", internal.ErrNoUserSessionFound
}
node, ok := ctx.Value(runtime.RUNTIME_CTX_NODE).(string)
if !ok {
logger.Error("context did not contain node.")
return "", internal.ErrNoUserSessionFound
}
presence := &MatchPresence{
Node: node,
UserID: userid,
SessionID: sessionID,
Username: userName,
}
matchID, err := nk.MatchCreate(ctx, MatchModuleName, map[string]interface{}{"request": request, "presence": presence})
if err != nil {
logger.Error("error creating match: %v", err)
return "", internal.ErrInternalError
}
and in the MatchInit func i write this
func (m *MatchHandler) MatchInit(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, params map[string]interface{}) (interface{}, int, string) {
request, ok := params["request"].(*pb.RpcCreateMatchReq)
if !ok {
logger.Error("invalid match init parameter \"request\"")
return nil, 0, ""
}
presence, ok := params["presence"].(*MatchPresence)
if !ok {
logger.Error("invalid match init parameter \"presence\"")
return nil, 0, ""
}
open := 0
if request.Open {
open = 1
}
label := &MatchLabel{
Start: 0,
Open: open,
MatchName: request.MatchName,
}
labelJSON, err := json.Marshal(label)
if err != nil {
logger.WithField("error", err).Error("match init failed")
labelJSON = []byte("{}")
}
state := &MatchState{
random: rand.New(rand.NewSource(time.Now().UnixNano())),
label: label,
open: request.Open,
roomPassword: request.Password,
presences: make(map[string]runtime.Presence, maxSize),
}
state.presences[presence.GetUserId()] = presence
return state, tickRate, string(labelJSON)
}
Am I right?