Przeglądaj źródła

Refactored json events

Ronald Peterson 10 lat temu
rodzic
commit
3c3a48164a

BIN
battlecamp-go-bot/battlecamp-go-bot


+ 57 - 2
battlecamp-go-bot/main.go

@@ -8,6 +8,7 @@ import (
 	"battlecamp-go-server/stomp"
 	"battlecamp-go-server/flag"
 	"battlecamp-go-server/events"
+	"battlecamp-go-server/player"
 )
 
 var Server *string
@@ -17,7 +18,11 @@ func main() {
 	log.Println("Game bot version 0.1")
 	
 	flag.ParseFlags()
-	
+	go subscribeToGame()
+	subscribeToUpdate()
+}
+
+func subscribeToGame() {
 	sub := stomp.Subscribe("game")
 	
 	for {
@@ -25,7 +30,57 @@ func main() {
 		gs := new(events.GameStart)
 		json.Unmarshal(announcement.Body, &gs)
 		log.Printf("announcement type: %v ", gs.Type)
+		gameEndChan := make(chan bool)
+		if("GAME_START" == gs.Type) {
+			go joinGame(gs.GameId, gameEndChan)
+		} else {
+			gameEndChan <- true
+		}
 	}
+}
+
+
+func subscribeToUpdate() {
+	sub := stomp.Subscribe("update")
+	
+	for {
+		announcement := <- sub
+		if("vnd.battlecamp.player" == announcement.ContentType) {
+			p := new(player.Player)
+			json.Unmarshal(announcement.Body, &p)
+			playerJoin(p)
+		} else {
+			pu := new(events.PlayerUpdate)
+			json.Unmarshal(announcement.Body, &pu)
+			playerUpdate(pu)
+		}
+	}
+}
+
+func joinGame(gameId int64, gameEndChan chan bool) {
+	//join game
+	
+	//retrieve board
+	
+	for {
+		select {
+			case <-gameEndChan:
+				return
+			default:
+				move()
+		}
+	}
+}
+
+func move() {
+	
+}
+
+func playerJoin(p *player.Player) {
+	
+}
+
+func playerUpdate(pu *events.PlayerUpdate) {
 	
 }
 
@@ -38,4 +93,4 @@ func initLogging() {
 		log.Println("ERROR: Cannot open log file, using console.")
 		log.Printf("%v=n", err)
 	}
-}
+}

+ 1 - 1
game/game.go

@@ -90,7 +90,7 @@ func (g *Game) Move(p *player.Player, direction string) bool {
 		Players: g.Players,
 	}
 
-	stomp.SendJson("update", up)
+	stomp.SendJsonPlayerUpdate("update", up)
 
 	if g.isWinner(p) {
 		g.EndTime = time.Now().Unix()

+ 1 - 1
gameserver/urlrouter.go

@@ -90,7 +90,7 @@ func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	e := json.NewEncoder(w)
 	e.Encode(&p)
 
-	stomp.SendJson("update", p)
+	stomp.SendJsonPlayerJoin("update", p)
 
 	log.Printf("Player %v joined game %v", p.Id, game.Id)
 }

+ 13 - 1
stomp/stomp.go

@@ -24,9 +24,21 @@ func getStompConnection() *stomp.Conn {
 	return stompConnection
 }
 
+func SendJsonPlayerJoin(topic string, v interface{}) {
+	sendJson(topic, "vnd.battlecamp.player", v)
+}
+
+func SendJsonPlayerUpdate(topic string, v interface{}) {
+	sendJson(topic, "vnd.battlecamp.update", v)
+}
+
 func SendJson(topic string, v interface{}) {
+	sendJson(topic, "application/json;charset=utf-8", v)
+}
+
+func sendJson(topic, contentType string, v interface{}) {
 	b, _ := json.Marshal(v)
-	getStompConnection().Send("/topic/go-battlecamp."+topic, "application/json;charset=utf-8", b, stomp.SendOpt.NoContentLength)
+	getStompConnection().Send("/topic/go-battlecamp."+topic, contentType, b, stomp.SendOpt.NoContentLength)
 }
 
 func Subscribe(topic string) chan *stomp.Message {