Unverified Commit cc258714 authored by Simon Esposito's avatar Simon Esposito Committed by GitHub
Browse files

Add presence reason fields to js runtime presence (#609)

Pointerize slices in match state.
Fix a js runtime bug that caused a panic when fetching the match state of a non-existing match. Resolves #610.
parent e6bb8a46
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -254,6 +254,7 @@ func (rm *RuntimeJavaScriptMatchCore) MatchJoinAttempt(tick int64, state interfa
		ctxObj.Set(__RUNTIME_JAVASCRIPT_CTX_CLIENT_PORT, clientPort)
	}

	pointerizeSlices(state)
	args := []goja.Value{ctxObj, rm.loggerModule, rm.nakamaModule, rm.dispatcher, rm.vm.ToValue(tick), rm.vm.ToValue(state), presenceObj, rm.vm.ToValue(metadata)}
	retVal, err := rm.joinAttemptFn(goja.Null(), args...)
	if err != nil {
@@ -310,6 +311,7 @@ func (rm *RuntimeJavaScriptMatchCore) MatchJoin(tick int64, state interface{}, j
		presences = append(presences, presenceObj)
	}

	pointerizeSlices(state)
	args := []goja.Value{rm.ctx, rm.loggerModule, rm.nakamaModule, rm.dispatcher, rm.vm.ToValue(tick), rm.vm.ToValue(state), rm.vm.ToValue(presences)}
	retVal, err := rm.joinFn(goja.Null(), args...)
	if err != nil {
@@ -346,6 +348,7 @@ func (rm *RuntimeJavaScriptMatchCore) MatchLeave(tick int64, state interface{},
		presences = append(presences, presenceObj)
	}

	pointerizeSlices(state)
	args := []goja.Value{rm.ctx, rm.loggerModule, rm.nakamaModule, rm.dispatcher, rm.vm.ToValue(tick), rm.vm.ToValue(state), rm.vm.ToValue(presences)}
	retVal, err := rm.leaveFn(goja.Null(), args...)
	if err != nil {
@@ -395,6 +398,7 @@ func (rm *RuntimeJavaScriptMatchCore) MatchLoop(tick int64, state interface{}, i
		inputs = append(inputs, msgObj)
	}

	pointerizeSlices(state)
	args := []goja.Value{rm.ctx, rm.loggerModule, rm.nakamaModule, rm.dispatcher, rm.vm.ToValue(tick), rm.vm.ToValue(state), rm.vm.ToValue(inputs)}
	retVal, err := rm.loopFn(goja.Null(), args...)
	if err != nil {
@@ -423,6 +427,7 @@ func (rm *RuntimeJavaScriptMatchCore) MatchLoop(tick int64, state interface{}, i
}

func (rm *RuntimeJavaScriptMatchCore) MatchTerminate(tick int64, state interface{}, graceSeconds int) (interface{}, error) {
	pointerizeSlices(state)
	args := []goja.Value{rm.ctx, rm.loggerModule, rm.nakamaModule, rm.dispatcher, rm.vm.ToValue(tick), rm.vm.ToValue(state), rm.vm.ToValue(graceSeconds)}
	retVal, err := rm.terminateFn(goja.Null(), args...)
	if err != nil {
+17 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import (
	"encoding/pem"
	"errors"
	"fmt"
	"github.com/heroiclabs/nakama-common/runtime"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/types/known/timestamppb"
	"google.golang.org/protobuf/types/known/wrapperspb"
@@ -2118,6 +2119,7 @@ func (n *runtimeJavascriptNakamaModule) streamUserList(r *goja.Runtime) func(goj
			presenceObj["persistence"] = p.Meta.Persistence
			presenceObj["username"] = p.Meta.Username
			presenceObj["status"] = p.Meta.Status
			presenceObj["reason"] = p.Meta.Reason
		}

		return r.ToValue(presencesList)
@@ -2164,6 +2166,7 @@ func (n *runtimeJavascriptNakamaModule) streamUserGet(r *goja.Runtime) func(goja
			"persistence": meta.Persistence,
			"username":    meta.Username,
			"status":      meta.Status,
			"reason":      meta.Reason,
		})
	}
}
@@ -2634,7 +2637,16 @@ func (n *runtimeJavascriptNakamaModule) sessionDisconnect(r *goja.Runtime) func(
			panic(r.NewTypeError("expects a valid session id"))
		}

		if err := n.sessionRegistry.Disconnect(context.Background(), sessionID); err != nil {
		reason := make([]runtime.PresenceReason, 0, 1)
		if f.Argument(1) != goja.Undefined() && f.Argument(1) != goja.Null() {
			reasonInt := getJsInt(r, f.Argument(1))
			if reasonInt < 0 || reasonInt > 4 {
				panic(r.NewTypeError("invalid disconnect reason, must be a value 0-4"))
			}
			reason = append(reason, runtime.PresenceReason(reasonInt))
		}

		if err := n.sessionRegistry.Disconnect(context.Background(), sessionID, reason...); err != nil {
			panic(r.NewGoError(fmt.Errorf("failed to disconnect: %s", err.Error())))
		}

@@ -2718,6 +2730,10 @@ func (n *runtimeJavascriptNakamaModule) matchGet(r *goja.Runtime) func(goja.Func
			panic(r.NewGoError(fmt.Errorf("failed to get match: %s", err.Error())))
		}

		if result == nil {
			return goja.Null()
		}

		matchData := map[string]interface{}{
			"matchId":       result.MatchId,
			"authoritative": result.Authoritative,
+12 −1
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import (
	"encoding/json"
	"encoding/pem"
	"fmt"
	"github.com/heroiclabs/nakama-common/runtime"
	"google.golang.org/protobuf/types/known/timestamppb"
	"google.golang.org/protobuf/types/known/wrapperspb"
	"io"
@@ -3826,7 +3827,17 @@ func (n *RuntimeLuaNakamaModule) sessionDisconnect(l *lua.LState) int {
		return 0
	}

	if err := n.sessionRegistry.Disconnect(l.Context(), sessionID); err != nil {
	reason := make([]runtime.PresenceReason, 0, 1)
	reasonInt := l.OptInt64(2, 0)
	if reasonInt != 0 {
		if reasonInt < 0 || reasonInt > 4 {
			l.ArgError(2, "invalid disconnect reason, must be a value 0-4")
			return 0
		}
		reason = append(reason, runtime.PresenceReason(reasonInt))
	}

	if err := n.sessionRegistry.Disconnect(l.Context(), sessionID, reason...); err != nil {
		l.RaiseError(fmt.Sprintf("failed to disconnect: %s", err.Error()))
	}
	return 0