Skip to content

Commit 9cb8e37

Browse files
committed
[feature] Allow sending different game events in autoRef test client
1 parent 1b06354 commit 9cb8e37

File tree

1 file changed

+65
-13
lines changed

1 file changed

+65
-13
lines changed

cmd/ssl-auto-ref-client/main.go

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package main
22

33
import (
4+
"bufio"
45
"crypto/rsa"
56
"flag"
7+
"fmt"
68
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/client"
79
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
810
"github.com/RoboCup-SSL/ssl-go-tools/pkg/sslconn"
911
"log"
1012
"net"
13+
"os"
14+
"strings"
1115
"time"
1216
)
1317

@@ -48,12 +52,37 @@ func main() {
4852
c.conn = conn
4953

5054
c.register()
51-
c.sendGameEvent()
5255

53-
c.sendAutoRefMessage("Hello World")
56+
go func() {
57+
for {
58+
time.Sleep(1 * time.Second)
59+
c.sendEmptyMessage()
60+
}
61+
}()
5462

63+
reader := bufio.NewReader(os.Stdin)
5564
for {
56-
time.Sleep(1 * time.Second)
65+
fmt.Print("-> ")
66+
text, err := reader.ReadString('\n')
67+
if err != nil {
68+
log.Print("Can not read from stdin: ", err)
69+
for {
70+
time.Sleep(1 * time.Second)
71+
}
72+
}
73+
// convert CRLF to LF
74+
text = strings.Replace(text, "\n", "", -1)
75+
76+
if strings.Compare("ballLeftField", text) == 0 {
77+
c.sendBallLeftField()
78+
} else if strings.Compare("botCrashUnique", text) == 0 {
79+
c.sendBotCrashUnique()
80+
} else {
81+
fmt.Println("Available commands: ")
82+
fmt.Printf(" %-20s: %s\n", "help", "Show this help text")
83+
fmt.Printf(" %-20s: %s\n", "ballLeftField", "Send game event")
84+
fmt.Printf(" %-20s: %s\n", "botCrashUnique", "Send game event")
85+
}
5786
}
5887
}
5988

@@ -96,7 +125,7 @@ func (c *Client) register() {
96125
}
97126
}
98127

99-
func (c *Client) sendGameEvent() {
128+
func (c *Client) sendBallLeftField() {
100129
event := refproto.GameEvent_BallLeftFieldTouchLine{}
101130
event.BallLeftFieldTouchLine = new(refproto.GameEvent_BallLeftField)
102131
event.BallLeftFieldTouchLine.ByBot = new(uint32)
@@ -109,33 +138,50 @@ func (c *Client) sendGameEvent() {
109138
gameEvent := refproto.GameEvent{Event: &event, Type: new(refproto.GameEventType)}
110139
*gameEvent.Type = refproto.GameEventType_BALL_LEFT_FIELD_TOUCH_LINE
111140
request := refproto.AutoRefToController{GameEvent: &gameEvent}
112-
c.sendRequest(&request)
141+
c.sendRequest(&request, true)
113142
}
114143

115-
func (c *Client) sendAutoRefMessage(msg string) {
116-
message := refproto.AutoRefMessage{Message: &refproto.AutoRefMessage_Custom{Custom: msg}}
117-
request := refproto.AutoRefToController{AutoRefMessage: &message}
118-
c.sendRequest(&request)
144+
func (c *Client) sendBotCrashUnique() {
145+
event := refproto.GameEvent_BotCrashUnique_{}
146+
event.BotCrashUnique = new(refproto.GameEvent_BotCrashUnique)
147+
event.BotCrashUnique.Violator = new(uint32)
148+
*event.BotCrashUnique.Violator = 2
149+
event.BotCrashUnique.Victim = new(uint32)
150+
*event.BotCrashUnique.Victim = 5
151+
event.BotCrashUnique.ByTeam = new(refproto.Team)
152+
*event.BotCrashUnique.ByTeam = refproto.Team_BLUE
153+
event.BotCrashUnique.Location = &refproto.Location{X: new(float32), Y: new(float32)}
154+
*event.BotCrashUnique.Location.X = 1
155+
*event.BotCrashUnique.Location.Y = 4.5
156+
gameEvent := refproto.GameEvent{Event: &event, Type: new(refproto.GameEventType)}
157+
*gameEvent.Type = refproto.GameEventType_BALL_LEFT_FIELD_TOUCH_LINE
158+
request := refproto.AutoRefToController{GameEvent: &gameEvent}
159+
c.sendRequest(&request, true)
119160
}
120161

121-
func (c *Client) sendRequest(request *refproto.AutoRefToController) {
162+
func (c *Client) sendEmptyMessage() {
163+
request := refproto.AutoRefToController{}
164+
c.sendRequest(&request, false)
165+
}
166+
167+
func (c *Client) sendRequest(request *refproto.AutoRefToController, doLog bool) {
122168
if privateKey != nil {
123169
request.Signature = &refproto.Signature{Token: &c.token, Pkcs1V15: []byte{}}
124170
request.Signature.Pkcs1V15 = client.Sign(privateKey, request)
125171
}
126172

127-
log.Print("Sending ", request)
173+
logIf(doLog, "Sending ", request)
128174

129175
if err := sslconn.SendMessage(c.conn, request); err != nil {
130176
log.Fatalf("Failed sending request: %v (%v)", request, err)
131177
}
132178

133-
log.Print("Waiting for reply...")
179+
logIf(doLog, "Waiting for reply...")
134180
reply := refproto.ControllerToAutoRef{}
135181
if err := sslconn.ReceiveMessage(c.conn, &reply); err != nil {
136182
log.Fatal("Failed receiving controller reply: ", err)
137183
}
138-
log.Print("Received reply: ", reply)
184+
logIf(doLog, "Received reply: ", reply)
139185
if reply.GetControllerReply() == nil || reply.GetControllerReply().StatusCode == nil || *reply.GetControllerReply().StatusCode != refproto.ControllerReply_OK {
140186
log.Fatal("Message rejected: ", *reply.GetControllerReply().Reason)
141187
}
@@ -145,3 +191,9 @@ func (c *Client) sendRequest(request *refproto.AutoRefToController) {
145191
c.token = ""
146192
}
147193
}
194+
195+
func logIf(doLog bool, v ...interface{}) {
196+
if doLog {
197+
log.Print(v)
198+
}
199+
}

0 commit comments

Comments
 (0)