-
Notifications
You must be signed in to change notification settings - Fork 43
/
match_rpc.go
99 lines (85 loc) · 3.01 KB
/
match_rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2020 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"database/sql"
"fmt"
"github.com/heroiclabs/nakama-common/runtime"
"github.com/heroiclabs/nakama-project-template/api"
"google.golang.org/protobuf/encoding/protojson"
)
type nakamaRpcFunc func(context.Context, runtime.Logger, *sql.DB, runtime.NakamaModule, string) (string, error)
func rpcFindMatch(marshaler *protojson.MarshalOptions, unmarshaler *protojson.UnmarshalOptions) nakamaRpcFunc {
return func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
_, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
if !ok {
return "", errNoUserIdFound
}
request := &api.RpcFindMatchRequest{}
if err := unmarshaler.Unmarshal([]byte(payload), request); err != nil {
return "", errUnmarshal
}
// If AI flag is set just create a brand-new match
if request.Ai {
matchID, err := nk.MatchCreate(
ctx, moduleName, map[string]interface{}{
"ai": true, "fast": request.Fast})
if err != nil {
logger.Error("error creating match: %v", err)
return "", errInternalError
}
response, err := marshaler.Marshal(&api.RpcFindMatchResponse{
MatchIds: []string{matchID}})
if err != nil {
logger.Error("error marshaling response payload: %v", err.Error())
return "", errMarshal
}
logger.Info("new AI match created %s", matchID)
return string(response), nil
}
maxSize := 1
var fast int
if request.Fast {
fast = 1
}
query := fmt.Sprintf("+label.open:1 +label.fast:%d", fast)
matchIDs := make([]string, 0, 10)
matches, err := nk.MatchList(ctx, 10, true, "", nil, &maxSize, query)
if err != nil {
logger.Error("error listing matches: %v", err)
return "", errInternalError
}
if len(matches) > 0 {
// There are one or more ongoing matches the user could join.
for _, match := range matches {
matchIDs = append(matchIDs, match.MatchId)
}
} else {
// No available matches found, create a new one.
matchID, err := nk.MatchCreate(ctx, moduleName, map[string]interface{}{"fast": request.Fast})
if err != nil {
logger.Error("error creating match: %v", err)
return "", errInternalError
}
matchIDs = append(matchIDs, matchID)
}
response, err := marshaler.Marshal(&api.RpcFindMatchResponse{MatchIds: matchIDs})
if err != nil {
logger.Error("error marshaling response payload: %v", err.Error())
return "", errMarshal
}
return string(response), nil
}
}