|
|
@@ -1,17 +1,22 @@
|
|
|
package games
|
|
|
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+
|
|
|
+ extStomp "github.com/go-stomp/stomp"
|
|
|
+)
|
|
|
+
|
|
|
type Games struct {
|
|
|
addChan chan *Game
|
|
|
listChan chan listChan
|
|
|
- getChan chan getGame
|
|
|
+ getChan chan getGame
|
|
|
}
|
|
|
|
|
|
type listChan chan []*Game
|
|
|
|
|
|
type getGame struct {
|
|
|
- id int64
|
|
|
+ id int64
|
|
|
returnChan chan *Game
|
|
|
-
|
|
|
}
|
|
|
|
|
|
func New() Games {
|
|
|
@@ -19,7 +24,7 @@ func New() Games {
|
|
|
games := Games{
|
|
|
addChan: make(chan *Game),
|
|
|
listChan: make(chan listChan),
|
|
|
- getChan: make(chan getGame),
|
|
|
+ getChan: make(chan getGame),
|
|
|
}
|
|
|
|
|
|
go serveGames(games)
|
|
|
@@ -45,8 +50,19 @@ func serveGames(games Games) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (games Games) AddGame(game *Game) {
|
|
|
+type stompGameStart struct {
|
|
|
+ Type string `json:"type"`
|
|
|
+ GameId int64 `json:"gameid"`
|
|
|
+}
|
|
|
+
|
|
|
+func (games Games) AddGame(game *Game, stompConnection *extStomp.Conn) {
|
|
|
games.addChan <- game
|
|
|
+ stompGameStart := stompGameStart{
|
|
|
+ Type: "GAME_START",
|
|
|
+ GameId: game.Id,
|
|
|
+ }
|
|
|
+ b, _ := json.Marshal(stompGameStart)
|
|
|
+ stompConnection.Send("gopinguin.game", "", b)
|
|
|
}
|
|
|
|
|
|
func (games Games) ListGames() []*Game {
|
|
|
@@ -56,11 +72,10 @@ func (games Games) ListGames() []*Game {
|
|
|
}
|
|
|
|
|
|
func (games Games) GetGame(id int64) *Game {
|
|
|
- getGame := getGame {
|
|
|
- id: id,
|
|
|
+ getGame := getGame{
|
|
|
+ id: id,
|
|
|
returnChan: make(chan *Game),
|
|
|
-
|
|
|
}
|
|
|
games.getChan <- getGame
|
|
|
- return <- getGame.returnChan
|
|
|
+ return <-getGame.returnChan
|
|
|
}
|