@@ -53,20 +53,45 @@ func NewGameController() (c *GameController) {
5353 c .AutoRefServer = rcon .NewAutoRefServer ()
5454 c .AutoRefServer .LoadTrustedKeys (c .Config .Server .AutoRef .TrustedKeysDir )
5555 c .AutoRefServer .ProcessRequest = c .ProcessAutoRefRequests
56- go c .AutoRefServer .Listen (c .Config .Server .AutoRef .Address )
5756
5857 c .TeamServer = rcon .NewTeamServer ()
5958 c .TeamServer .LoadTrustedKeys (c .Config .Server .Team .TrustedKeysDir )
6059 c .TeamServer .ProcessTeamRequest = c .ProcessTeamRequests
61- go c .TeamServer .Listen (c .Config .Server .Team .Address )
6260
6361 c .Engine = NewEngine (c .Config .Game )
6462 c .timer = timer .NewTimer ()
65- c .timer .Start ()
6663
6764 return
6865}
6966
67+ // Run the GameController by starting several go-routines. This method will not block.
68+ func (c * GameController ) Run () {
69+
70+ if err := c .historyPreserver .Open (); err != nil {
71+ log .Print ("Could not open history" , err )
72+ } else {
73+ history , err := c .historyPreserver .Load ()
74+ if err != nil {
75+ log .Print ("Could not load history" , err )
76+ } else if len (* history ) > 0 {
77+ c .Engine .History = * history
78+ * c .Engine .State = c .Engine .History [len (c .Engine .History )- 1 ].State
79+ c .Engine .RefereeEvents = c .Engine .History [len (c .Engine .History )- 1 ].RefereeEvents
80+ }
81+ }
82+
83+ c .ApiServer .PublishState (* c .Engine .State )
84+ c .ApiServer .PublishRefereeEvents (c .Engine .RefereeEvents )
85+ c .TeamServer .AllowedTeamNames = []string {c .Engine .State .TeamState [TeamYellow ].Name ,
86+ c .Engine .State .TeamState [TeamBlue ].Name }
87+
88+ c .timer .Start ()
89+ go c .publishApi ()
90+ go c .publishNetwork ()
91+ go c .AutoRefServer .Listen (c .Config .Server .AutoRef .Address )
92+ go c .TeamServer .Listen (c .Config .Server .Team .Address )
93+ }
94+
7095func (c * GameController ) ProcessGeometry (data * sslproto.SSL_GeometryData ) {
7196 if int32 (math .Round (c .Engine .Geometry .FieldWidth * 1000.0 )) != * data .Field .FieldWidth {
7297 newFieldWidth := float64 (* data .Field .FieldWidth ) / 1000.0
@@ -194,29 +219,6 @@ func (c *GameController) ProcessTeamRequests(teamName string, request refproto.T
194219 return nil
195220}
196221
197- // Run the GameController by starting an endless loop in the background
198- func (c * GameController ) Run () {
199-
200- if err := c .historyPreserver .Open (); err != nil {
201- log .Print ("Could not open history" , err )
202- } else {
203- history , err := c .historyPreserver .Load ()
204- if err != nil {
205- log .Print ("Could not load history" , err )
206- } else if len (* history ) > 0 {
207- c .Engine .History = * history
208- * c .Engine .State = c .Engine .History [len (c .Engine .History )- 1 ].State
209- c .Engine .RefereeEvents = c .Engine .History [len (c .Engine .History )- 1 ].RefereeEvents
210- }
211- }
212-
213- c .ApiServer .PublishState (* c .Engine .State )
214- c .ApiServer .PublishRefereeEvents (c .Engine .RefereeEvents )
215-
216- go c .publishApi ()
217- go c .publishNetwork ()
218- }
219-
220222func (c * GameController ) publishApi () {
221223 defer c .historyPreserver .Close ()
222224 for {
0 commit comments