-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_match_frontend.go
160 lines (131 loc) · 5.27 KB
/
open_match_frontend.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"database/sql"
"encoding/json"
"sync"
"time"
"log"
nakamaCommands "github.com/challenge-league/nakama-go/commands"
"github.com/heroiclabs/nakama-common/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"open-match.dev/open-match/pkg/pb"
)
const (
// The endpoint for the Open Match Frontend service.
omFrontendEndpoint = "open-match-frontend.open-match.svc.cluster.local:50504"
)
var (
openMatchFrontendServiceClientBuilder *openMatchFrontendServiceClient
once sync.Once
kacp = keepalive.ClientParameters{
Time: 10 * time.Second, // send pings every 10 seconds if there is no activity
Timeout: time.Second, // wait 1 second for ping ack before considering the connection dead
PermitWithoutStream: true, // send pings even without active streams
}
)
type openMatchFrontendServiceClient struct {
conn *grpc.ClientConn
client pb.FrontendServiceClient
}
func NewOpenMatchFrontEndSingleton() *openMatchFrontendServiceClient {
once.Do(func() {
// Connect to Open Match Frontend.
conn, err := grpc.Dial(omFrontendEndpoint, grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp))
if err != nil {
log.Fatalf("Failed to connect to Open Match, got %v", err)
}
//defer conn.Close()
openMatchFrontendServiceClientBuilder = &openMatchFrontendServiceClient{
conn: conn,
client: pb.NewFrontendServiceClient(conn),
}
})
//log.Printf("DKOZLOV %+v", openMatchFrontendServiceClientBuilder.conn.GetState().String())
return openMatchFrontendServiceClientBuilder
}
func (b *openMatchFrontendServiceClient) SetClient(client pb.FrontendServiceClient) *openMatchFrontendServiceClient {
b.client = client
return b
}
func (b *openMatchFrontendServiceClient) GetClient() pb.FrontendServiceClient {
return b.client
}
func (b *openMatchFrontendServiceClient) SetConn(conn *grpc.ClientConn) *openMatchFrontendServiceClient {
b.conn = conn
return b
}
func (b *openMatchFrontendServiceClient) GetConn() *grpc.ClientConn {
return b.conn
}
func OpenMatchFrontendTicketCreateRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
var ticketCreateRequest pb.CreateTicketRequest
json.Unmarshal([]byte(payload), &ticketCreateRequest)
log.Printf(MarshalIndent(ticketCreateRequest))
// TODO: Allow only limited number of tickets per user
fe := NewOpenMatchFrontEndSingleton().GetClient()
resp, err := fe.CreateTicket(context.Background(), &ticketCreateRequest)
if err != nil {
log.Printf("Failed to Create Ticket, got %s", err.Error())
return "", err
}
var user *nakamaCommands.User
json.Unmarshal(resp.Extensions["user"].Value, &user)
log.Printf("Ticket created successfully, id: %v", resp.Id)
return MarshalIndent(resp), nil
}
func OpenMatchFrontendTicketGetRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
var ticketGetRequest pb.GetTicketRequest
json.Unmarshal([]byte(payload), &ticketGetRequest)
log.Printf(MarshalIndent(ticketGetRequest))
fe := NewOpenMatchFrontEndSingleton().GetClient()
got, err := fe.GetTicket(context.Background(), &pb.GetTicketRequest{TicketId: ticketGetRequest.TicketId})
if err != nil {
log.Printf("Failed to Get Ticket %v, got %s", ticketGetRequest.TicketId, err.Error())
return "", err
}
if got.GetAssignment() != nil {
log.Printf("Ticket %v got assignment %v", got.GetId(), got.GetAssignment())
}
return MarshalIndent(got), nil
}
func OpenMatchFrontendTicketDelete(ticketID string) error {
fe := NewOpenMatchFrontEndSingleton().GetClient()
_, err := fe.DeleteTicket(context.Background(), &pb.DeleteTicketRequest{TicketId: ticketID})
if err != nil {
log.Printf("Failed to Delete Ticket %v from OpenMatchFrontend, got %s", ticketID, err.Error())
return err
}
return nil
}
func OpenMatchFrontendTicketDeleteRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
var ticketDeleteRequest pb.DeleteTicketRequest
json.Unmarshal([]byte(payload), &ticketDeleteRequest)
log.Printf(MarshalIndent(ticketDeleteRequest))
err := OpenMatchFrontendTicketDelete(ticketDeleteRequest.TicketId)
if err != nil {
return "", err
}
return "", nil
}
func OpenMatchFrontendTicketWatchAssignmentsRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
var watchAssignmentsRequest *pb.WatchAssignmentsRequest
json.Unmarshal([]byte(payload), &watchAssignmentsRequest)
log.Printf(MarshalIndent(watchAssignmentsRequest))
fe := NewOpenMatchFrontEndSingleton().GetClient()
watchAssignmentsClient, err := fe.WatchAssignments(context.Background(), &pb.WatchAssignmentsRequest{TicketId: watchAssignmentsRequest.TicketId})
if err != nil {
log.Printf("Failed to watch assignments request %v, got %s", watchAssignmentsRequest.TicketId, err.Error())
return "", err
}
watchAssignmentsResponse, err := watchAssignmentsClient.Recv()
if err != nil {
log.Printf("Failed to get assignments response %v, got %s", watchAssignmentsResponse.String, err.Error())
return "", err
}
return MarshalIndent(watchAssignmentsResponse), nil
}
func init() {
NewOpenMatchFrontEndSingleton()
}