forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_match.go
162 lines (138 loc) · 4.46 KB
/
pipeline_match.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
// Copyright 2017 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 (
"github.com/satori/go.uuid"
"github.com/uber-go/zap"
)
func (p *pipeline) matchCreate(logger zap.Logger, session *session, envelope *Envelope) {
matchID := uuid.NewV4()
p.tracker.Track(session.id, "match:"+matchID.String(), session.userID, PresenceMeta{})
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Match{Match: &TMatch{
Id: matchID.Bytes(),
Self: &UserPresence{
UserId: session.userID.Bytes(),
SessionId: session.id.Bytes(),
},
}}})
}
func (p *pipeline) matchJoin(logger zap.Logger, session *session, envelope *Envelope) {
matchIDBytes := envelope.GetMatchJoin().MatchId
matchID, err := uuid.FromBytes(matchIDBytes)
if err != nil {
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Invalid match ID"}}})
return
}
topic := "match:" + matchID.String()
ps := p.tracker.ListByTopic(topic)
if len(ps) == 0 {
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Match not found"}}})
return
}
p.tracker.Track(session.id, topic, session.userID, PresenceMeta{})
userPresences := make([]*UserPresence, len(ps)+1)
for i := 0; i < len(ps)-1; i++ {
p := ps[i]
userPresences[i] = &UserPresence{
UserId: p.UserID.Bytes(),
SessionId: p.ID.SessionID.Bytes(),
}
}
self := &UserPresence{
UserId: session.userID.Bytes(),
SessionId: session.id.Bytes(),
}
userPresences[len(ps)-1] = self
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_MatchPresences{MatchPresences: &TMatchPresences{
Presences: userPresences,
Self: self,
}}})
}
func (p *pipeline) matchLeave(logger zap.Logger, session *session, envelope *Envelope) {
matchIDBytes := envelope.GetMatchLeave().MatchId
matchID, err := uuid.FromBytes(matchIDBytes)
if err != nil {
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Invalid match ID"}}})
return
}
topic := "match:" + matchID.String()
ps := p.tracker.ListByTopic(topic)
if len(ps) == 0 {
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Match not found"}}})
return
}
found := false
for _, p := range ps {
if p.ID.SessionID == session.id && p.UserID == session.userID {
found = true
break
}
}
// If sender wasn't part of the match.
if !found {
session.Send(&Envelope{CollationId: envelope.CollationId, Payload: &Envelope_Error{&Error{Reason: "Match not found"}}})
return
}
p.tracker.Untrack(session.id, topic, session.userID)
session.Send(&Envelope{CollationId: envelope.CollationId})
}
func (p *pipeline) matchDataSend(logger zap.Logger, session *session, envelope *Envelope) {
incoming := envelope.GetMatchDataSend()
matchIDBytes := incoming.MatchId
matchID, err := uuid.FromBytes(matchIDBytes)
if err != nil {
// TODO send an error to the client?
return
}
topic := "match:" + matchID.String()
// TODO check membership before looking up all members.
ps := p.tracker.ListByTopic(topic)
if len(ps) == 0 {
// TODO send an error to the client?
return
}
// Don't echo back to sender.
found := false
for i := 0; i < len(ps); i++ {
if p := ps[i]; p.ID.SessionID == session.id && p.UserID == session.userID {
ps[i] = ps[len(ps)-1]
ps = ps[:len(ps)-1]
found = true
break
}
}
// If sender wasn't in the presences for this match, they're not a member.
if !found {
// TODO send an error to the client?
return
}
// Check if sender was the only one in the match.
if len(ps) == 0 {
return
}
outgoing := &Envelope{
Payload: &Envelope_MatchData{
MatchData: &MatchData{
MatchId: matchIDBytes,
Presence: &UserPresence{
UserId: session.userID.Bytes(),
SessionId: session.id.Bytes(),
},
OpCode: incoming.OpCode,
Data: incoming.Data,
},
},
}
p.messageRouter.Send(logger, ps, outgoing)
}