# Finding the match(es) a user is in

**URL:** <https://forum.heroiclabs.com/t/finding-the-match-es-a-user-is-in/2323>\
**Category:** Runtime Framework\
**Tags:** server-framework\
**Created:** [January 31, 2022, 3:40pm UTC](https://forum.heroiclabs.com/t/finding-the-match-es-a-user-is-in/2323 "2022-01-31T15:40:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)\
**Post date:** [January 31, 2022, 3:40pm UTC](https://forum.heroiclabs.com/t/finding-the-match-es-a-user-is-in/2323/1 "2022-01-31T15:40:35Z")

</div>

Is there a way for the server side code to find any match(es) a user may be in? The use case is a game crash or other interruption on a mobile device where the user restarts the game. Upon restart we would like to be able to do a quick check to see if the user is still part of some game in progress and if so, resume the game.

The only API call that I can find that could help is:

```auto
MatchList(ctx context.Context, limit int, authoritative bool, label string, minSize, maxSize *int, query string) ([]*api.Match, error)	

```

It’s documented here: [Function Reference - Heroic Labs Documentation](https://heroiclabs.com/docs/nakama/server-framework/function-reference/#matches) but I can’t seem to find anything about the query syntax. I have seen something about it somewhere, but the docs are somewhat unorganized and really hard to find anything in. What I was looking for was something that would allow me to do approximately:

```auto
var query = "user:" + user.Id
MatchList(...., query)

```

or

```auto
UserMatchList(ctx context.Context, userId string) ([]*api.Match, error)	

```

It doesn’t seem to work like that at least. 🙂 Another approach would be to just brutally iterate through all active matches and found those where the user’s id is an active presence, but I can’t seem to find any API to do that either.

---

<div class="post-metadata">

**Author:** ![yamlcoder](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/yamlcoder/32/970_2.png) [@yamlcoder](https://forum.heroiclabs.com/u/yamlcoder)\
**Post date:** [January 31, 2022, 8:30pm UTC](https://forum.heroiclabs.com/t/finding-the-match-es-a-user-is-in/2323/2 "2022-01-31T20:30:00Z")

</div>

query argument can be used to filter matches based on user defined labels, it’s syntax is described [here](https://heroiclabs.com/docs/nakama/concepts/server-authoritative-multiplayer/#search-query)

You can keep up to date list of player ids in match labels using `MatchJoin` and `MatchLeave` , both of which have [dispatcher](https://heroiclabs.com/docs/nakama/concepts/server-authoritative-multiplayer/#match-runtime-api_1) argument with `MatchLabelUpdate` method.

`MatchLabelUpdate` accept json encoded string with a full set of labels to set on a match, so it is probably best to keep reference set of labels somewhere in the match state.

When you need to find match for a given player , you invoke `MatchList` and pass query param to find matches for a player

---

<div class="post-metadata">

**Author:** ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)\
**Post date:** [February 1, 2022, 7:48am UTC](https://forum.heroiclabs.com/t/finding-the-match-es-a-user-is-in/2323/3 "2022-02-01T07:48:00Z")

</div>

Doesn’t that become somewhat slow if the label contains a bunch of JSON that has to be decoded all the time when checking if players are in a match?

---

<div class="post-metadata">

**Author:** ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)\
**Post date:** [February 1, 2022, 8:30am UTC](https://forum.heroiclabs.com/t/finding-the-match-es-a-user-is-in/2323/4 "2022-02-01T08:30:53Z")

</div>

I’m probably somewhat daft, but I can’t see how Bleve could find something based on an array of player ids. It seems to be only about really simple queries on individual numeric or string fields, not anything more complex than that.

```auto
	var players []string

	// players are kept in our custom match state
	for _, player := range state.players {
		// the Id is Nakama's UUID
		players = append(players, player.Id)
	}

	var labelMap = map[string]interface{}{
		// the mode is, well, a game mode. A numeric value already being queried for
		"mode": state.mode, 
		"players": players,
	}

	// marshal to JSON and update the label
	...

```

It seems that an API that allowed users to do own filtering would be more handy and faster? Something like a `MatchFind` where we’d pass in a function that takes an `*api.Match` and an `interface{}` representing our custom match state, and which would return a boolean indicating if the match is included in the result or not. Optionally the function could return a second boolean to indicate that the search is now finished.

---

<div class="post-metadata">

**Author:** ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)\
**Post date:** [February 1, 2022, 9:09am UTC](https://forum.heroiclabs.com/t/finding-the-match-es-a-user-is-in/2323/5 "2022-02-01T09:09:17Z")

</div>

Maybe the ids have to be concatenated into a single string like:

```auto
var players = "aaa,bbb,ccc,ddd...."

```

and then queried using regular expressions:

```auto
var wantedId = "bbb"
var query = "+label.players:/.*" + wantedId + ".*"

```

This doesn’t seem too efficient for a real time game server.

---

<div class="post-metadata">

**Author:** ![yamlcoder](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/yamlcoder/32/970_2.png) [@yamlcoder](https://forum.heroiclabs.com/u/yamlcoder)\
**Post date:** [February 1, 2022, 1:46pm UTC](https://forum.heroiclabs.com/t/finding-the-match-es-a-user-is-in/2323/6 "2022-02-01T13:46:48Z")

</div>

If your players label contains array of player ids, you should be able to search for it with `+labels.players:$id` . There is a [test](https://github.com/heroiclabs/nakama/blob/9cc2ee0a8ff24f8f6ba754ee9908ca8564fa9f94/server/match_registry_test.go#L254) for a similar case.

MatchList uses index which is updated every `label_update_interval_ms` (default is 1s, set in your match config) , so it should be pretty efficient.

---

<div class="post-metadata">

**Author:** ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)\
**Post date:** [February 2, 2022, 10:57am UTC](https://forum.heroiclabs.com/t/finding-the-match-es-a-user-is-in/2323/7 "2022-02-02T10:57:50Z")

</div>

Thank you for the tip. I did not try it yet though, as I for now moved the functionality that was in the label out to an external component. So now when I need to check if a player has a current match going on or when doing matchmaking I ask that component instead.
