Commit 52040709 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Sort match listings to show newer created matches first by default.

parent 8f2a5760
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -4,7 +4,8 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com) and this project uses [semantic versioning](http://semver.org).

## [Unreleased]

### Changed
- Sort match listings to show newer created matches first by default.

## [3.1.1] - 2021-02-15
### Changed
+7 −2
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ type MatchIndexEntry struct {
	LabelString string                 `json:"label_string"`
	TickRate    int                    `json:"tick_rate"`
	HandlerName string                 `json:"handler_name"`
	CreateTime  int64                  `json:"create_time"`
}

type MatchJoinResult struct {
@@ -95,7 +96,7 @@ type MatchRegistry interface {
	// Does not ensure the match process itself is no longer running, that must be handled separately.
	RemoveMatch(id uuid.UUID, stream PresenceStream)
	// Update the label entry for a given match.
	UpdateMatchLabel(id uuid.UUID, tickRate int, handlerName, label string) error
	UpdateMatchLabel(id uuid.UUID, tickRate int, handlerName, label string, createTime int64) error
	// List (and optionally filter) currently running matches.
	// This can list across both authoritative and relayed matches.
	ListMatches(ctx context.Context, limit int, authoritative *wrappers.BoolValue, label *wrappers.StringValue, minSize *wrappers.Int32Value, maxSize *wrappers.Int32Value, query *wrappers.StringValue) ([]*api.Match, error)
@@ -275,7 +276,7 @@ func (r *LocalMatchRegistry) RemoveMatch(id uuid.UUID, stream PresenceStream) {
	}
}

func (r *LocalMatchRegistry) UpdateMatchLabel(id uuid.UUID, tickRate int, handlerName, label string) error {
func (r *LocalMatchRegistry) UpdateMatchLabel(id uuid.UUID, tickRate int, handlerName, label string, createTime int64) error {
	if len(label) > MatchLabelMaxBytes {
		return ErrMatchLabelTooLong
	}
@@ -288,6 +289,7 @@ func (r *LocalMatchRegistry) UpdateMatchLabel(id uuid.UUID, tickRate int, handle
		TickRate:    tickRate,
		HandlerName: handlerName,
		LabelString: label,
		CreateTime:  createTime,
	})
}

@@ -322,6 +324,7 @@ func (r *LocalMatchRegistry) ListMatches(ctx context.Context, limit int, authori
		}
		searchReq := bleve.NewSearchRequestOptions(q, count, 0, false)
		searchReq.Fields = []string{"label_string", "tick_rate", "handler_name"}
		searchReq.SortBy([]string{"-create_time"})
		var err error
		labelResults, err = r.index.SearchInContext(ctx, searchReq)
		if err != nil {
@@ -347,6 +350,7 @@ func (r *LocalMatchRegistry) ListMatches(ctx context.Context, limit int, authori
		indexQuery.SetField("label_string")
		searchReq := bleve.NewSearchRequestOptions(indexQuery, count, 0, false)
		searchReq.Fields = []string{"label_string", "tick_rate", "handler_name"}
		searchReq.SortBy([]string{"-create_time"})
		var err error
		labelResults, err = r.index.SearchInContext(ctx, searchReq)
		if err != nil {
@@ -366,6 +370,7 @@ func (r *LocalMatchRegistry) ListMatches(ctx context.Context, limit int, authori
		indexQuery := bleve.NewMatchAllQuery()
		searchReq := bleve.NewSearchRequestOptions(indexQuery, count, 0, false)
		searchReq.Fields = []string{"label_string", "tick_rate", "handler_name"}
		searchReq.SortBy([]string{"-create_time"})
		var err error
		labelResults, err = r.index.SearchInContext(ctx, searchReq)
		if err != nil {
+19 −15
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ import (
	"database/sql"
	"errors"
	"fmt"
	"time"

	"github.com/gofrs/uuid"
	"github.com/heroiclabs/nakama-common/rtapi"
	"github.com/heroiclabs/nakama-common/runtime"
@@ -42,6 +44,7 @@ type RuntimeGoMatchCore struct {
	node       string
	module     string
	tickRate   int
	createTime int64
	stopped    *atomic.Bool
	idStr      string
	stream     PresenceStream
@@ -77,6 +80,7 @@ func NewRuntimeGoMatchCore(logger *zap.Logger, module string, matchRegistry Matc
		stopped:    stopped,
		idStr:      fmt.Sprintf("%v.%v", id.String(), node),
		module:     module,
		createTime: time.Now().UTC().UnixNano() / int64(time.Millisecond),
		stream: PresenceStream{
			Mode:    StreamModeMatchAuthoritative,
			Subject: id,
@@ -104,7 +108,7 @@ func (r *RuntimeGoMatchCore) MatchInit(presenceList *MatchPresenceList, deferMes
	}
	r.tickRate = tickRate

	if err := r.matchRegistry.UpdateMatchLabel(r.id, r.tickRate, r.module, label); err != nil {
	if err := r.matchRegistry.UpdateMatchLabel(r.id, r.tickRate, r.module, label, r.createTime); err != nil {
		return nil, 0, err
	}
	r.label.Store(label)
@@ -363,7 +367,7 @@ func (r *RuntimeGoMatchCore) MatchLabelUpdate(label string) error {
	if r.stopped.Load() {
		return ErrMatchStopped
	}
	if err := r.matchRegistry.UpdateMatchLabel(r.id, r.tickRate, r.module, label); err != nil {
	if err := r.matchRegistry.UpdateMatchLabel(r.id, r.tickRate, r.module, label, r.createTime); err != nil {
		return fmt.Errorf("error updating match label: %v", err.Error())
	}
	r.label.Store(label)
+19 −15
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ import (
	"encoding/json"
	"errors"
	"fmt"
	"time"

	"github.com/dop251/goja"
	"github.com/gofrs/uuid"
	"github.com/golang/protobuf/jsonpb"
@@ -42,6 +44,7 @@ type RuntimeJavaScriptMatchCore struct {
	node       string
	module     string
	tickRate   int
	createTime int64
	stopped    *atomic.Bool
	idStr      string
	stream     PresenceStream
@@ -130,6 +133,7 @@ func NewRuntimeJavascriptMatchCore(logger *zap.Logger, module string, db *sql.DB
		stopped:    stopped,
		idStr:      fmt.Sprintf("%v.%v", id.String(), node),
		module:     module,
		createTime: time.Now().UTC().UnixNano() / int64(time.Millisecond),
		stream: PresenceStream{
			Mode:    StreamModeMatchAuthoritative,
			Subject: id,
@@ -211,7 +215,7 @@ func (rm *RuntimeJavaScriptMatchCore) MatchInit(presenceList *MatchPresenceList,
		return nil, 0, errors.New("matchInit is expected to return an object with a 'state' property")
	}

	if err := rm.matchRegistry.UpdateMatchLabel(rm.id, rm.tickRate, rm.module, label); err != nil {
	if err := rm.matchRegistry.UpdateMatchLabel(rm.id, rm.tickRate, rm.module, label, rm.createTime); err != nil {
		return nil, 0, err
	}
	rm.label.Store(label)
@@ -706,7 +710,7 @@ func (rm *RuntimeJavaScriptMatchCore) matchLabelUpdate(r *goja.Runtime) func(goj

		input := getJsString(r, f.Argument(0))

		if err := rm.matchRegistry.UpdateMatchLabel(rm.id, rm.tickRate, rm.module, input); err != nil {
		if err := rm.matchRegistry.UpdateMatchLabel(rm.id, rm.tickRate, rm.module, input, rm.createTime); err != nil {
			panic(r.NewGoError(fmt.Errorf("error updating match label: %v", err.Error())))
		}
		rm.label.Store(input)
+18 −15
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import (
	"fmt"
	"strings"
	"sync"
	"time"

	"github.com/gofrs/uuid"
	"github.com/golang/protobuf/jsonpb"
@@ -44,6 +45,7 @@ type RuntimeLuaMatchCore struct {
	node       string
	module     string
	tickRate   int
	createTime int64
	stopped    *atomic.Bool
	idStr      string
	stream     PresenceStream
@@ -171,6 +173,7 @@ func NewRuntimeLuaMatchCore(logger *zap.Logger, module string, db *sql.DB, jsonp
		stopped:    stopped,
		idStr:      fmt.Sprintf("%v.%v", id.String(), node),
		module:     module,
		createTime: time.Now().UTC().UnixNano() / int64(time.Millisecond),
		stream: PresenceStream{
			Mode:    StreamModeMatchAuthoritative,
			Subject: id,
@@ -259,7 +262,7 @@ func (r *RuntimeLuaMatchCore) MatchInit(presenceList *MatchPresenceList, deferMe
	}
	r.vm.Pop(1)

	if err := r.matchRegistry.UpdateMatchLabel(r.id, r.tickRate, r.module, labelStr); err != nil {
	if err := r.matchRegistry.UpdateMatchLabel(r.id, r.tickRate, r.module, labelStr, r.createTime); err != nil {
		return nil, 0, err
	}
	r.label.Store(labelStr)
@@ -846,7 +849,7 @@ func (r *RuntimeLuaMatchCore) matchLabelUpdate(l *lua.LState) int {

	input := l.OptString(1, "")

	if err := r.matchRegistry.UpdateMatchLabel(r.id, r.tickRate, r.module, input); err != nil {
	if err := r.matchRegistry.UpdateMatchLabel(r.id, r.tickRate, r.module, input, r.createTime); err != nil {
		l.RaiseError("error updating match label: %v", err.Error())
		return 0
	}