forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparty_registry.go
215 lines (171 loc) · 6.59 KB
/
party_registry.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2021 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 server
import (
"context"
"errors"
"sync"
"github.com/gofrs/uuid"
"github.com/heroiclabs/nakama-common/rtapi"
"go.uber.org/zap"
)
var ErrPartyNotFound = errors.New("party not found")
type PartyRegistry interface {
Create(open bool, maxSize int, leader *rtapi.UserPresence) *PartyHandler
Delete(id uuid.UUID)
Join(id uuid.UUID, presences []*Presence)
Leave(id uuid.UUID, presences []*Presence)
PartyJoinRequest(ctx context.Context, id uuid.UUID, node string, presence *Presence) (bool, error)
PartyPromote(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string, presence *rtapi.UserPresence) error
PartyAccept(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string, presence *rtapi.UserPresence) error
PartyRemove(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string, presence *rtapi.UserPresence) error
PartyClose(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string) error
PartyJoinRequestList(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string) ([]*rtapi.UserPresence, error)
PartyMatchmakerAdd(ctx context.Context, id uuid.UUID, node, sessionID, fromNode, query string, minCount, maxCount, countMultiple int, stringProperties map[string]string, numericProperties map[string]float64) (string, []*PresenceID, error)
PartyMatchmakerRemove(ctx context.Context, id uuid.UUID, node, sessionID, fromNode, ticket string) error
PartyDataSend(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string, opCode int64, data []byte) error
}
type LocalPartyRegistry struct {
logger *zap.Logger
matchmaker Matchmaker
tracker Tracker
streamManager StreamManager
router MessageRouter
node string
parties *sync.Map
}
func NewLocalPartyRegistry(logger *zap.Logger, matchmaker Matchmaker, tracker Tracker, streamManager StreamManager, router MessageRouter, node string) PartyRegistry {
return &LocalPartyRegistry{
logger: logger,
matchmaker: matchmaker,
tracker: tracker,
streamManager: streamManager,
router: router,
node: node,
parties: &sync.Map{},
}
}
func (p *LocalPartyRegistry) Create(open bool, maxSize int, presence *rtapi.UserPresence) *PartyHandler {
id := uuid.Must(uuid.NewV4())
partyHandler := NewPartyHandler(p.logger, p, p.matchmaker, p.tracker, p.streamManager, p.router, id, p.node, open, maxSize, presence)
p.parties.Store(id, partyHandler)
return partyHandler
}
func (p *LocalPartyRegistry) Delete(id uuid.UUID) {
p.parties.Delete(id)
}
func (p *LocalPartyRegistry) Join(id uuid.UUID, presences []*Presence) {
ph, found := p.parties.Load(id)
if !found {
return
}
ph.(*PartyHandler).Join(presences)
}
func (p *LocalPartyRegistry) Leave(id uuid.UUID, presences []*Presence) {
ph, found := p.parties.Load(id)
if !found {
return
}
ph.(*PartyHandler).Leave(presences)
}
func (p *LocalPartyRegistry) PartyJoinRequest(ctx context.Context, id uuid.UUID, node string, presence *Presence) (bool, error) {
if node != p.node {
return false, ErrPartyNotFound
}
ph, found := p.parties.Load(id)
if !found {
return false, ErrPartyNotFound
}
return ph.(*PartyHandler).JoinRequest(presence)
}
func (p *LocalPartyRegistry) PartyPromote(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string, presence *rtapi.UserPresence) error {
if node != p.node {
return ErrPartyNotFound
}
ph, found := p.parties.Load(id)
if !found {
return ErrPartyNotFound
}
return ph.(*PartyHandler).Promote(sessionID, fromNode, presence)
}
func (p *LocalPartyRegistry) PartyAccept(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string, presence *rtapi.UserPresence) error {
if node != p.node {
return ErrPartyNotFound
}
ph, found := p.parties.Load(id)
if !found {
return ErrPartyNotFound
}
return ph.(*PartyHandler).Accept(sessionID, fromNode, presence)
}
func (p *LocalPartyRegistry) PartyRemove(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string, presence *rtapi.UserPresence) error {
if node != p.node {
return ErrPartyNotFound
}
ph, found := p.parties.Load(id)
if !found {
return ErrPartyNotFound
}
return ph.(*PartyHandler).Remove(sessionID, fromNode, presence)
}
func (p *LocalPartyRegistry) PartyClose(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string) error {
if node != p.node {
return ErrPartyNotFound
}
ph, found := p.parties.Load(id)
if !found {
return ErrPartyNotFound
}
return ph.(*PartyHandler).Close(sessionID, fromNode)
}
func (p *LocalPartyRegistry) PartyJoinRequestList(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string) ([]*rtapi.UserPresence, error) {
if node != p.node {
return nil, ErrPartyNotFound
}
ph, found := p.parties.Load(id)
if !found {
return nil, ErrPartyNotFound
}
return ph.(*PartyHandler).JoinRequestList(sessionID, fromNode)
}
func (p *LocalPartyRegistry) PartyMatchmakerAdd(ctx context.Context, id uuid.UUID, node, sessionID, fromNode, query string, minCount, maxCount, countMultiple int, stringProperties map[string]string, numericProperties map[string]float64) (string, []*PresenceID, error) {
if node != p.node {
return "", nil, ErrPartyNotFound
}
ph, found := p.parties.Load(id)
if !found {
return "", nil, ErrPartyNotFound
}
return ph.(*PartyHandler).MatchmakerAdd(sessionID, fromNode, query, minCount, maxCount, countMultiple, stringProperties, numericProperties)
}
func (p *LocalPartyRegistry) PartyMatchmakerRemove(ctx context.Context, id uuid.UUID, node, sessionID, fromNode, ticket string) error {
if node != p.node {
return ErrPartyNotFound
}
ph, found := p.parties.Load(id)
if !found {
return ErrPartyNotFound
}
return ph.(*PartyHandler).MatchmakerRemove(sessionID, fromNode, ticket)
}
func (p *LocalPartyRegistry) PartyDataSend(ctx context.Context, id uuid.UUID, node, sessionID, fromNode string, opCode int64, data []byte) error {
if node != p.node {
return ErrPartyNotFound
}
ph, found := p.parties.Load(id)
if !found {
return ErrPartyNotFound
}
return ph.(*PartyHandler).DataSend(sessionID, fromNode, opCode, data)
}