Skip to content

Commit 3d63311

Browse files
committed
[refactoring] Improve modify event and gameEvent String() methods
1 parent f26c911 commit 3d63311

File tree

2 files changed

+23
-63
lines changed

2 files changed

+23
-63
lines changed

internal/app/controller/events.go

Lines changed: 17 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"time"
67
)
@@ -92,72 +93,25 @@ type EventModifyCardTime struct {
9293
type EventModifyValue struct {
9394
ForTeam Team `json:"forTeam"`
9495

95-
Goals *int `json:"goals"`
96-
Goalie *int `json:"goalie"`
97-
YellowCards *int `json:"yellowCards"`
98-
YellowCardTime *EventModifyCardTime `json:"yellowCardTime"`
99-
RedCards *int `json:"redCards"`
100-
TimeoutsLeft *int `json:"timeoutsLeft"`
101-
TimeoutTimeLeft *string `json:"timeoutTimeLeft"`
102-
OnPositiveHalf *bool `json:"onPositiveHalf"`
103-
TeamName *string `json:"teamName"`
104-
FoulCounter *int `json:"foulCounter"`
105-
BallPlacementFailures *int `json:"ballPlacementFailures"`
106-
CanPlaceBall *bool `json:"canPlaceBall"`
107-
Division *Division `json:"division"`
108-
AutoContinue *bool `json:"autoContinue"`
96+
Goals *int `json:"goals,omitempty"`
97+
Goalie *int `json:"goalie,omitempty"`
98+
YellowCards *int `json:"yellowCards,omitempty"`
99+
YellowCardTime *EventModifyCardTime `json:"yellowCardTime,omitempty"`
100+
RedCards *int `json:"redCards,omitempty"`
101+
TimeoutsLeft *int `json:"timeoutsLeft,omitempty"`
102+
TimeoutTimeLeft *string `json:"timeoutTimeLeft,omitempty"`
103+
OnPositiveHalf *bool `json:"onPositiveHalf,omitempty"`
104+
TeamName *string `json:"teamName,omitempty"`
105+
FoulCounter *int `json:"foulCounter,omitempty"`
106+
BallPlacementFailures *int `json:"ballPlacementFailures,omitempty"`
107+
CanPlaceBall *bool `json:"canPlaceBall,omitempty"`
108+
Division *Division `json:"division,omitempty"`
109+
AutoContinue *bool `json:"autoContinue,omitempty"`
109110
}
110111

111112
func (m EventModifyValue) String() string {
112-
str := "modify"
113-
if m.ForTeam != TeamUnknown {
114-
str += fmt.Sprintf(" for team %v:", m.ForTeam)
115-
} else {
116-
str += ":"
117-
}
118-
if m.Goals != nil {
119-
return fmt.Sprintf("%v Goals=%v", str, *m.Goals)
120-
}
121-
if m.Goalie != nil {
122-
return fmt.Sprintf("%v Goalie=%v", str, *m.Goalie)
123-
}
124-
if m.YellowCards != nil {
125-
return fmt.Sprintf("%v YellowCards=%v", str, *m.YellowCards)
126-
}
127-
if m.YellowCardTime != nil {
128-
return fmt.Sprintf("%v YellowCardTime=%v", str, *m.YellowCardTime)
129-
}
130-
if m.RedCards != nil {
131-
return fmt.Sprintf("%v RedCards=%v", str, *m.RedCards)
132-
}
133-
if m.TimeoutsLeft != nil {
134-
return fmt.Sprintf("%v TimeoutsLeft=%v", str, *m.TimeoutsLeft)
135-
}
136-
if m.TimeoutTimeLeft != nil {
137-
return fmt.Sprintf("%v TimeoutTimeLeft=%v", str, *m.TimeoutTimeLeft)
138-
}
139-
if m.OnPositiveHalf != nil {
140-
return fmt.Sprintf("%v OnPositiveHalf=%v", str, *m.OnPositiveHalf)
141-
}
142-
if m.TeamName != nil {
143-
return fmt.Sprintf("%v TeamName=%v", str, *m.TeamName)
144-
}
145-
if m.FoulCounter != nil {
146-
return fmt.Sprintf("%v FoulCounter=%v", str, *m.FoulCounter)
147-
}
148-
if m.BallPlacementFailures != nil {
149-
return fmt.Sprintf("%v BallPlacementFailures=%v", str, *m.BallPlacementFailures)
150-
}
151-
if m.CanPlaceBall != nil {
152-
return fmt.Sprintf("%v CanPlaceBall=%v", str, *m.CanPlaceBall)
153-
}
154-
if m.Division != nil {
155-
return fmt.Sprintf("%v Division=%v", str, *m.Division)
156-
}
157-
if m.AutoContinue != nil {
158-
return fmt.Sprintf("%v AutoContinue=%v", str, *m.AutoContinue)
159-
}
160-
return fmt.Sprintf("%v undefined", str)
113+
b, _ := json.Marshal(&m)
114+
return string(b)
161115
}
162116

163117
// EventTrigger is an event that can be applied

internal/app/controller/gameEvent.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
67
"log"
@@ -55,6 +56,11 @@ type GameEvent struct {
5556
Details GameEventDetails `json:"details"`
5657
}
5758

59+
func (m GameEvent) String() string {
60+
b, _ := json.Marshal(&m)
61+
return string(b)
62+
}
63+
5864
// ByTeam extracts the `ByTeam` attribute from the game event details
5965
func (e GameEvent) ByTeam() Team {
6066
v := reflect.ValueOf(e.Details)

0 commit comments

Comments
 (0)