Skip to content

Commit 1d2d13f

Browse files
committed
Support team registration when both teams have the same name
The team protocol was based on the team name only, so if a team wants to play against itself, there was a conflict with the team name. Now, if the team can be send with the team name to solve this conflict.
1 parent 91608bf commit 1d2d13f

24 files changed

+108
-74
lines changed

internal/app/api/ssl_gc_api.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/ci/ssl_gc_ci.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/engine/ssl_gc_engine.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/engine/ssl_gc_engine_config.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/geom/ssl_gc_geometry.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/rcon/server.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,11 @@ func (s *Server) CloseConnection(id string) {
136136
log.Printf("Connection to %v closed", id)
137137
}
138138

139-
func (c *Client) Ok() (reply ControllerReply) {
139+
func (c *Client) Ok() (reply *ControllerReply) {
140+
reply = new(ControllerReply)
140141
reply.StatusCode = new(ControllerReply_StatusCode)
141142
*reply.StatusCode = ControllerReply_OK
142-
c.addVerification(&reply)
143+
c.addVerification(reply)
143144
return
144145
}
145146

@@ -156,13 +157,14 @@ func (c *Client) addVerification(reply *ControllerReply) {
156157
}
157158
}
158159

159-
func (c *Client) Reject(reason string) (reply ControllerReply) {
160+
func (c *Client) Reject(reason string) (reply *ControllerReply) {
160161
log.Print("Reject request: " + reason)
162+
reply = new(ControllerReply)
161163
reply.StatusCode = new(ControllerReply_StatusCode)
162164
*reply.StatusCode = ControllerReply_REJECTED
163165
reply.Reason = new(string)
164166
*reply.Reason = reason
165-
c.addVerification(&reply)
167+
c.addVerification(reply)
166168
return
167169
}
168170

internal/app/rcon/server_autoref.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,23 @@ func (s *AutoRefServer) handleClientConnection(conn net.Conn) {
118118
continue
119119
}
120120
}
121-
if err := s.processRequest(client.id, req); err != nil {
121+
if err := s.processRequest(client.id, &req); err != nil {
122122
client.reply(client.Reject(err.Error()))
123123
} else {
124124
client.reply(client.Ok())
125125
}
126126
}
127127
}
128128

129-
func (c *AutoRefClient) reply(reply ControllerReply) {
130-
msg := ControllerToAutoRef_ControllerReply{ControllerReply: &reply}
129+
func (c *AutoRefClient) reply(reply *ControllerReply) {
130+
msg := ControllerToAutoRef_ControllerReply{ControllerReply: reply}
131131
response := ControllerToAutoRef{Msg: &msg}
132132
if err := sslconn.SendMessage(c.conn, &response); err != nil {
133133
log.Print("Failed to send reply: ", err)
134134
}
135135
}
136136

137-
func (s *AutoRefServer) processRequest(id string, request AutoRefToController) error {
137+
func (s *AutoRefServer) processRequest(id string, request *AutoRefToController) error {
138138

139139
if request.GameEvent != nil {
140140
request.GameEvent.Origin = []string{id}

internal/app/rcon/server_remotecontrol.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (s *RemoteControlServer) handleClientConnection(conn net.Conn) {
112112
continue
113113
}
114114
}
115-
if response, err := s.processRequest(*client.team, req); err != nil {
115+
if response, err := s.processRequest(*client.team, &req); err != nil {
116116
client.reply(client.Reject(err.Error()))
117117
} else {
118118
client.replyWithPayload(client.Ok(), response)
@@ -129,24 +129,24 @@ func (s *RemoteControlServer) updateConnectionState(client RemoteControlClient,
129129
})
130130
}
131131

132-
func (s *RemoteControlServer) SendRequest(teamName string, request ControllerToRemoteControl) error {
132+
func (s *RemoteControlServer) SendRequest(teamName string, request *ControllerToRemoteControl) error {
133133
if client, ok := s.clients[teamName]; ok {
134-
return sslconn.SendMessage(client.conn, &request)
134+
return sslconn.SendMessage(client.conn, request)
135135
}
136136
return errors.Errorf("Remote control client '%v' not connected", teamName)
137137
}
138138

139-
func (c *RemoteControlClient) reply(reply ControllerReply) {
140-
response := ControllerToRemoteControl{ControllerReply: &reply}
139+
func (c *RemoteControlClient) reply(reply *ControllerReply) {
140+
response := ControllerToRemoteControl{ControllerReply: reply}
141141

142142
if err := sslconn.SendMessage(c.conn, &response); err != nil {
143143
log.Print("Failed to send reply: ", err)
144144
}
145145
}
146146

147-
func (c *RemoteControlClient) replyWithPayload(reply ControllerReply, response *ControllerToRemoteControl) {
147+
func (c *RemoteControlClient) replyWithPayload(reply *ControllerReply, response *ControllerToRemoteControl) {
148148
if response != nil {
149-
response.ControllerReply = &reply
149+
response.ControllerReply = reply
150150

151151
if err := sslconn.SendMessage(c.conn, response); err != nil {
152152
log.Print("Failed to send reply: ", err)
@@ -172,7 +172,7 @@ func gameEventPresent(events []*state.GameEvent, eventType state.GameEvent_Type,
172172
return
173173
}
174174

175-
func (s *RemoteControlServer) processRequest(team state.Team, request RemoteControlToController) (*ControllerToRemoteControl, error) {
175+
func (s *RemoteControlServer) processRequest(team state.Team, request *RemoteControlToController) (*ControllerToRemoteControl, error) {
176176

177177
if request.GetRequest() == RemoteControlToController_PING {
178178
return nil, nil
@@ -195,7 +195,7 @@ func (s *RemoteControlServer) processRequest(team state.Team, request RemoteCont
195195
}, nil
196196
}
197197

198-
log.Print("Received request from remote-control: ", proto.MarshalTextString(&request))
198+
log.Print("Received request from remote-control: ", proto.MarshalTextString(request))
199199

200200
currentState := s.gcEngine.CurrentState()
201201
teamState := *currentState.TeamInfo(team)

internal/app/rcon/server_team.go

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type TeamServer struct {
2020
}
2121

2222
type TeamClient struct {
23+
teamName string
24+
team state.Team
2325
*Client
2426
}
2527

@@ -38,8 +40,10 @@ func (c *TeamClient) receiveRegistration(reader *bufio.Reader, server *TeamServe
3840
}
3941

4042
var allowedTeamNames []string
41-
for _, teamInfo := range server.gcEngine.CurrentState().TeamState {
43+
nameToTeamMap := map[string]state.Team{}
44+
for team, teamInfo := range server.gcEngine.CurrentState().TeamState {
4245
allowedTeamNames = append(allowedTeamNames, *teamInfo.Name)
46+
nameToTeamMap[*teamInfo.Name] = state.Team(state.Team_value[team])
4347
}
4448

4549
if registration.TeamName == nil {
@@ -48,11 +52,22 @@ func (c *TeamClient) receiveRegistration(reader *bufio.Reader, server *TeamServe
4852
if !isAllowedTeamName(*registration.TeamName, allowedTeamNames) {
4953
return errors.Errorf("Invalid team name: '%v'. Expecting one of these: %v", *registration.TeamName, allowedTeamNames)
5054
}
51-
c.id = *registration.TeamName
55+
var team state.Team
56+
if registration.Team != nil {
57+
team = *registration.Team
58+
} else if len(nameToTeamMap) == 2 {
59+
team = nameToTeamMap[*registration.TeamName]
60+
} else {
61+
return errors.Errorf("No team specified and both teams have the same name. Specify the team (-color).")
62+
}
63+
64+
c.id = team.String() + "-" + *registration.TeamName
65+
c.team = team
66+
c.teamName = *registration.TeamName
5267
if _, exists := server.clients[c.id]; exists {
5368
return errors.New("Team with given name already registered: " + c.id)
5469
}
55-
c.pubKey = server.trustedKeys[c.id]
70+
c.pubKey = server.trustedKeys[*registration.TeamName]
5671
if c.pubKey != nil {
5772
if err := c.Client.verifyMessage(&registration); err != nil {
5873
return err
@@ -97,21 +112,21 @@ func (s *TeamServer) handleClientConnection(conn net.Conn) {
97112

98113
s.clients[client.id] = client.Client
99114
defer func() {
100-
team := s.gcEngine.CurrentState().TeamByName(client.id)
101115
s.gcEngine.UpdateGcState(func(gcState *engine.GcState) {
102-
if teamState, ok := gcState.TeamState[team.String()]; ok {
116+
if teamState, ok := gcState.TeamState[client.team.String()]; ok {
103117
connected := false
104118
teamState.Connected = &connected
105119
teamState.ConnectionVerified = &connected
120+
} else {
121+
log.Println("Team not connected: " + client.team.String())
106122
}
107123
})
108124
s.CloseConnection(client.id)
109125
}()
110126

111127
log.Printf("Team Client %v connected", client.id)
112-
team := s.gcEngine.CurrentState().TeamByName(client.id)
113128
s.gcEngine.UpdateGcState(func(gcState *engine.GcState) {
114-
if teamState, ok := gcState.TeamState[team.String()]; ok {
129+
if teamState, ok := gcState.TeamState[client.team.String()]; ok {
115130
connected := true
116131
teamState.Connected = &connected
117132
teamState.ConnectionVerified = &client.verifiedConnection
@@ -133,53 +148,52 @@ func (s *TeamServer) handleClientConnection(conn net.Conn) {
133148
continue
134149
}
135150
}
136-
if err := s.processRequest(client.id, req); err != nil {
151+
if err := s.processRequest(client, &req); err != nil {
137152
client.reply(client.Reject(err.Error()))
138153
} else {
139154
client.reply(client.Ok())
140155
}
141156
}
142157
}
143158

144-
func (s *TeamServer) SendRequest(teamName string, request ControllerToTeam) error {
159+
func (s *TeamServer) SendRequest(teamName string, request *ControllerToTeam) error {
145160
if client, ok := s.clients[teamName]; ok {
146-
return sslconn.SendMessage(client.conn, &request)
161+
return sslconn.SendMessage(client.conn, request)
147162
}
148163
return errors.Errorf("Team Client '%v' not connected", teamName)
149164
}
150165

151-
func (c *TeamClient) reply(reply ControllerReply) {
152-
msg := ControllerToTeam_ControllerReply{ControllerReply: &reply}
166+
func (c *TeamClient) reply(reply *ControllerReply) {
167+
msg := ControllerToTeam_ControllerReply{ControllerReply: reply}
153168
response := ControllerToTeam{Msg: &msg}
154169
if err := sslconn.SendMessage(c.conn, &response); err != nil {
155170
log.Print("Failed to send reply: ", err)
156171
}
157172
}
158173

159-
func (s *TeamServer) processRequest(teamName string, request TeamToController) error {
174+
func (s *TeamServer) processRequest(teamClient TeamClient, request *TeamToController) error {
160175

161176
if request.GetPing() {
162177
return nil
163178
}
164179

165-
log.Print("Received request from team: ", proto.MarshalTextString(&request))
180+
log.Print("Received request from team: ", proto.MarshalTextString(request))
166181

167182
currentState := s.gcEngine.CurrentState()
168-
team := currentState.TeamByName(teamName)
169-
if team == state.Team_UNKNOWN {
183+
if teamClient.team == state.Team_UNKNOWN {
170184
return errors.New("Your team is not playing?!")
171185
}
172186

173-
teamState := *currentState.TeamInfo(team)
187+
teamState := *currentState.TeamInfo(teamClient.team)
174188

175189
if x, ok := request.GetMsg().(*TeamToController_SubstituteBot); ok {
176190
if *timeSet(teamState.RequestsBotSubstitutionSince) != x.SubstituteBot {
177-
log.Printf("Team %v requests to change bot substituation intent to %v", team, x.SubstituteBot)
191+
log.Printf("Team %v requests to change bot substituation intent to %v", teamClient.id, x.SubstituteBot)
178192
s.gcEngine.Enqueue(&statemachine.Change{
179-
Origin: &teamName,
193+
Origin: &teamClient.id,
180194
Change: &statemachine.Change_UpdateTeamState{
181195
UpdateTeamState: &statemachine.UpdateTeamState{
182-
ForTeam: &team,
196+
ForTeam: &teamClient.team,
183197
RequestsBotSubstitution: &x.SubstituteBot,
184198
}},
185199
})
@@ -195,12 +209,12 @@ func (s *TeamServer) processRequest(teamName string, request TeamToController) e
195209
if err := mayChangeKeeper(s.gcEngine.CurrentGcState(), &teamState); err != nil {
196210
return errors.Wrap(err, "Team requests to change keeper, but: ")
197211
}
198-
log.Printf("Team %v requests to change keeper to %v", team, x.DesiredKeeper)
212+
log.Printf("Team %v requests to change keeper to %v", teamClient.team, x.DesiredKeeper)
199213
s.gcEngine.Enqueue(&statemachine.Change{
200-
Origin: &teamName,
214+
Origin: &teamClient.teamName,
201215
Change: &statemachine.Change_UpdateTeamState{
202216
UpdateTeamState: &statemachine.UpdateTeamState{
203-
ForTeam: &team,
217+
ForTeam: &teamClient.team,
204218
Goalkeeper: &x.DesiredKeeper,
205219
}},
206220
})

internal/app/rcon/ssl_gc_rcon.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)