Skip to content

Commit 9945902

Browse files
committed
Calculate botSubstitutionTimeLeft centrally and distribute it via refMsg and remote-control
1 parent 7fd47ef commit 9945902

15 files changed

+314
-226
lines changed

frontend/src/proto/ssl_gc_referee_message.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ export interface Referee_TeamInfo {
467467
botSubstitutionAllowed?: boolean;
468468
/** The number of bot substitutions left by the team in this halftime */
469469
botSubstitutionsLeft?: number;
470+
/** The number of microseconds left for current bot substitution */
471+
botSubstitutionTimeLeft?: number;
470472
}
471473

472474
/**
@@ -576,6 +578,7 @@ export const Referee_TeamInfo = {
576578
: false,
577579
botSubstitutionAllowed: isSet(object.botSubstitutionAllowed) ? Boolean(object.botSubstitutionAllowed) : false,
578580
botSubstitutionsLeft: isSet(object.botSubstitutionsLeft) ? Number(object.botSubstitutionsLeft) : 0,
581+
botSubstitutionTimeLeft: isSet(object.botSubstitutionTimeLeft) ? Number(object.botSubstitutionTimeLeft) : 0,
579582
};
580583
},
581584

@@ -603,6 +606,8 @@ export const Referee_TeamInfo = {
603606
(obj.ballPlacementFailuresReached = message.ballPlacementFailuresReached);
604607
message.botSubstitutionAllowed !== undefined && (obj.botSubstitutionAllowed = message.botSubstitutionAllowed);
605608
message.botSubstitutionsLeft !== undefined && (obj.botSubstitutionsLeft = Math.round(message.botSubstitutionsLeft));
609+
message.botSubstitutionTimeLeft !== undefined &&
610+
(obj.botSubstitutionTimeLeft = Math.round(message.botSubstitutionTimeLeft));
606611
return obj;
607612
},
608613
};

frontend/src/proto/ssl_gc_state.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export interface TeamInfo {
232232
challengeFlags?: number;
233233
botSubstitutionAllowed?: boolean;
234234
botSubstitutionsLeft?: number;
235+
botSubstitutionTimeLeft?: Duration;
235236
}
236237

237238
export interface State {
@@ -429,6 +430,9 @@ export const TeamInfo = {
429430
challengeFlags: isSet(object.challengeFlags) ? Number(object.challengeFlags) : 0,
430431
botSubstitutionAllowed: isSet(object.botSubstitutionAllowed) ? Boolean(object.botSubstitutionAllowed) : false,
431432
botSubstitutionsLeft: isSet(object.botSubstitutionsLeft) ? Number(object.botSubstitutionsLeft) : 0,
433+
botSubstitutionTimeLeft: isSet(object.botSubstitutionTimeLeft)
434+
? Duration.fromJSON(object.botSubstitutionTimeLeft)
435+
: undefined,
432436
};
433437
},
434438

@@ -471,6 +475,9 @@ export const TeamInfo = {
471475
message.challengeFlags !== undefined && (obj.challengeFlags = Math.round(message.challengeFlags));
472476
message.botSubstitutionAllowed !== undefined && (obj.botSubstitutionAllowed = message.botSubstitutionAllowed);
473477
message.botSubstitutionsLeft !== undefined && (obj.botSubstitutionsLeft = Math.round(message.botSubstitutionsLeft));
478+
message.botSubstitutionTimeLeft !== undefined && (obj.botSubstitutionTimeLeft = message.botSubstitutionTimeLeft
479+
? Duration.toJSON(message.botSubstitutionTimeLeft)
480+
: undefined);
474481
return obj;
475482
},
476483
};

internal/app/engine/engine.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ func initializeAddedTeamInfoFields(teamInfo *state.TeamInfo) {
252252
if teamInfo.BotSubstitutionsLeft == nil {
253253
teamInfo.BotSubstitutionsLeft = new(int32)
254254
}
255+
if teamInfo.BotSubstitutionTimeLeft == nil {
256+
teamInfo.BotSubstitutionTimeLeft = durationpb.New(0)
257+
}
255258
}
256259

257260
// Stop stops the go routine that processes the change queue

internal/app/engine/process_bot_substitution.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@ package engine
22

33
import (
44
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
5-
"time"
5+
"google.golang.org/protobuf/types/known/durationpb"
66
)
77

88
func (e *Engine) processBotSubstitution() {
99
if e.currentState.GameState.IsHalted() {
1010
for _, team := range state.BothTeams() {
11-
if !*e.currentState.TeamInfo(team).BotSubstitutionAllowed {
12-
continue
13-
}
14-
events := e.currentState.FindGameEventsByTeam(state.GameEvent_BOT_SUBSTITUTION, team)
15-
if len(events) == 0 {
16-
continue
17-
}
18-
botSubstitutionEvent := events[len(events)-1]
19-
eventCreated := time.UnixMicro(int64(*botSubstitutionEvent.CreatedTimestamp))
20-
if e.timeProvider().Sub(eventCreated) > e.gameConfig.BotSubstitutionTime {
21-
e.Enqueue(createBotSubstitutionEventChange(team))
11+
if *e.currentState.TeamInfo(team).BotSubstitutionAllowed {
12+
if e.currentState.TeamInfo(team).BotSubstitutionTimeLeft.AsDuration() <= 0 {
13+
e.Enqueue(createBotSubstitutionEventChange(team))
14+
e.currentState.TeamInfo(team).BotSubstitutionTimeLeft = durationpb.New(e.gameConfig.BotSubstitutionTime)
15+
}
16+
} else {
17+
e.currentState.TeamInfo(team).BotSubstitutionTimeLeft = durationpb.New(0)
2218
}
2319
}
2420
}

internal/app/engine/process_tick.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ func (e *Engine) processTick() {
3131
}
3232
}
3333

34-
if e.countCardTime() {
35-
for _, teamState := range e.currentState.TeamState {
34+
for _, teamState := range e.currentState.TeamState {
35+
if e.countCardTime() {
3636
e.updateYellowCardTimes(teamState, delta)
3737
}
38+
if *teamState.BotSubstitutionAllowed {
39+
addDur(teamState.BotSubstitutionTimeLeft, -delta)
40+
if teamState.BotSubstitutionTimeLeft.AsDuration() < 0 {
41+
*teamState.BotSubstitutionTimeLeft = *durationpb.New(0)
42+
}
43+
}
3844
}
3945

4046
if *e.currentState.Command.Type == state.Command_TIMEOUT {

internal/app/publish/messagegenerator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func updateTeam(teamInfo *state.Referee_TeamInfo, teamState *state.TeamInfo) {
129129
teamInfo.YellowCardTimes = mapYellowCardTimes(teamState.YellowCards)
130130
*teamInfo.YellowCards = unsigned(len(teamState.YellowCards))
131131
*teamInfo.Timeouts = unsigned32(*teamState.TimeoutsLeft)
132+
*teamInfo.TimeoutTime = mapTime(teamState.TimeoutTimeLeft.AsDuration())
132133
*teamInfo.Goalkeeper = unsigned32(*teamState.Goalkeeper)
133134
*teamInfo.FoulCounter = unsigned(len(teamState.Fouls))
134135
*teamInfo.BallPlacementFailures = unsigned32(*teamState.BallPlacementFailures)
@@ -138,8 +139,7 @@ func updateTeam(teamInfo *state.Referee_TeamInfo, teamState *state.TeamInfo) {
138139
*teamInfo.BotSubstitutionIntent = teamState.RequestsBotSubstitutionSince != nil
139140
*teamInfo.BotSubstitutionAllowed = *teamState.BotSubstitutionAllowed
140141
*teamInfo.BotSubstitutionsLeft = unsigned32(*teamState.BotSubstitutionsLeft)
141-
timeoutTime := teamState.TimeoutTimeLeft.AsDuration()
142-
*teamInfo.TimeoutTime = mapTime(timeoutTime)
142+
*teamInfo.BotSubstitutionTimeLeft = mapTime(teamState.BotSubstitutionTimeLeft.AsDuration())
143143
}
144144

145145
func newRefereeMessage() (m *state.Referee) {
@@ -176,6 +176,7 @@ func newTeamInfo() (t *state.Referee_TeamInfo) {
176176
t.BotSubstitutionIntent = new(bool)
177177
t.BotSubstitutionAllowed = new(bool)
178178
t.BotSubstitutionsLeft = new(uint32)
179+
t.BotSubstitutionTimeLeft = new(uint32)
179180
return
180181
}
181182

internal/app/rcon/server_remotecontrol.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,25 @@ func (c *RemoteControlClient) replyWithState(reply *ControllerReply) {
154154
robotsOnField := c.gcEngine.TrackerState().NumTeamRobots(*c.team)
155155
timeoutTimeLeft := float32(teamState.TimeoutTimeLeft.AsDuration().Seconds())
156156
canSubstituteRobot := c.canSubstituteRobot()
157+
botSubstitutionsLeft := uint32(*teamState.BotSubstitutionsLeft)
158+
botSubstitutionTimeLeft := float32(teamState.BotSubstitutionTimeLeft.AsDuration().Seconds())
157159

158160
response := &ControllerToRemoteControl{
159161
State: &RemoteControlTeamState{
160-
Team: c.team,
161-
KeeperId: teamState.Goalkeeper,
162-
AvailableRequests: availableRequests,
163-
ActiveRequests: activeRequests,
164-
EmergencyStopIn: &emergencyStopIn,
165-
TimeoutsLeft: teamState.TimeoutsLeft,
166-
TimeoutTimeLeft: &timeoutTimeLeft,
167-
ChallengeFlagsLeft: teamState.ChallengeFlags,
168-
MaxRobots: teamState.MaxAllowedBots,
169-
RobotsOnField: &robotsOnField,
170-
YellowCardsDue: yellowCardsDue,
171-
CanSubstituteRobot: &canSubstituteRobot,
162+
Team: c.team,
163+
KeeperId: teamState.Goalkeeper,
164+
AvailableRequests: availableRequests,
165+
ActiveRequests: activeRequests,
166+
EmergencyStopIn: &emergencyStopIn,
167+
TimeoutsLeft: teamState.TimeoutsLeft,
168+
TimeoutTimeLeft: &timeoutTimeLeft,
169+
ChallengeFlagsLeft: teamState.ChallengeFlags,
170+
MaxRobots: teamState.MaxAllowedBots,
171+
RobotsOnField: &robotsOnField,
172+
YellowCardsDue: yellowCardsDue,
173+
CanSubstituteRobot: &canSubstituteRobot,
174+
BotSubstitutionsLeft: &botSubstitutionsLeft,
175+
BotSubstitutionTimeLeft: &botSubstitutionTimeLeft,
172176
},
173177
ControllerReply: reply,
174178
}

internal/app/rcon/ssl_gc_rcon_remotecontrol.pb.go

Lines changed: 42 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)