Ver código fonte

refactored events and first version bot

Ronald Peterson 10 anos atrás
pai
commit
72346056ff

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


+ 41 - 0
battlecamp-go-bot/main.go

@@ -0,0 +1,41 @@
+package main
+
+import (
+	"encoding/json"
+	"log"
+	"os"
+	
+	"battlecamp-go-server/stomp"
+	"battlecamp-go-server/flag"
+	"battlecamp-go-server/events"
+)
+
+var Server *string
+
+func main() {
+	initLogging()
+	log.Println("Game bot version 0.1")
+	
+	flag.ParseFlags()
+	
+	sub := stomp.Subscribe("game")
+	
+	for {
+		announcement := <- sub
+		gs := new(events.GameStart)
+		json.Unmarshal(announcement.Body, &gs)
+		log.Printf("announcement type: %v ", gs.Type)
+	}
+	
+}
+
+func initLogging() {
+	logFile, err := os.Create("server.log")
+
+	if err == nil {
+		log.SetOutput(logFile)
+	} else {
+		log.Println("ERROR: Cannot open log file, using console.")
+		log.Printf("%v=n", err)
+	}
+}

+ 2 - 2
board/board.go

@@ -31,8 +31,8 @@ type response struct {
 
 const (
 	maxIceWidth  = 26
-	regionWidth  = 1024
-	regionHeight = 1024
+	regionWidth  = 32
+	regionHeight = 32
 )
 
 // Create a new randomly generated board.

+ 21 - 0
events/events.go

@@ -0,0 +1,21 @@
+package events
+
+import (
+	"battlecamp-go-server/player"
+)
+
+type GameStart struct {
+	Type   string `json:"type"`
+	GameId int64  `json:"gameId"`
+}
+
+type GameEnd struct {
+	Type    string `json:"type"`
+	GameId  int64  `json:"gameId"`
+	Payload string `json:"payload"`
+}
+
+type PlayerUpdate struct {
+	GameId  int64            `json:"gameId"`
+	Players []*player.Player `json:"players"`
+}

+ 10 - 20
game/game.go

@@ -2,6 +2,7 @@ package game
 
 import (
 	"battlecamp-go-server/board"
+	"battlecamp-go-server/events"
 	"battlecamp-go-server/player"
 	"battlecamp-go-server/stomp"
 	"log"
@@ -11,13 +12,13 @@ import (
 )
 
 type Game struct {
-	Id         int64            `json:"id"`
-	StartTime  int64            `json:"startTime"`
-	EndTime    int64            `json:"endTime"`
-	Board      *board.Board     `json:"board"`
-	Players    []*player.Player `json:"-"`
-	mutex      sync.Mutex
-	Winner     *player.Player `json:"winner,omitempty"`
+	Id        int64            `json:"id"`
+	StartTime int64            `json:"startTime"`
+	EndTime   int64            `json:"endTime"`
+	Board     *board.Board     `json:"board"`
+	Players   []*player.Player `json:"-"`
+	mutex     sync.Mutex
+	Winner    *player.Player `json:"winner,omitempty"`
 }
 
 func NewGame(cols, rows int) *Game {
@@ -56,17 +57,6 @@ func (g *Game) placePlayer(p *player.Player) {
 
 }
 
-type updatePlayers struct {
-	GameId  int64            `json:"gameId"`
-	Players []*player.Player `json:"players"`
-}
-
-type stompGameEnd struct {
-	Type    string `json:"type"`
-	GameId  int64  `json:"gameId"`
-	Payload string `json:"payload"`
-}
-
 func (g *Game) Move(p *player.Player, direction string) bool {
 	if !(direction == "N" || direction == "W" || direction == "S" || direction == "E") {
 		log.Printf("Illigal direction %v", direction)
@@ -95,7 +85,7 @@ func (g *Game) Move(p *player.Player, direction string) bool {
 	p.Pos.X = newX
 	p.Pos.Y = newY
 	// END TODO make tread safe
-	up := updatePlayers{
+	up := events.PlayerUpdate{
 		GameId:  g.Id,
 		Players: g.Players,
 	}
@@ -105,7 +95,7 @@ func (g *Game) Move(p *player.Player, direction string) bool {
 	if g.isWinner(p) {
 		g.EndTime = time.Now().Unix()
 		g.Winner = p
-		ge := stompGameEnd{
+		ge := events.GameEnd{
 			Type:    "GAME_END",
 			GameId:  g.Id,
 			Payload: g.Winner.Id,

+ 2 - 6
gameserver/gameserver.go

@@ -4,6 +4,7 @@ import (
 	"battlecamp-go-server/flag"
 	"battlecamp-go-server/game"
 	"battlecamp-go-server/stomp"
+	"battlecamp-go-server/events"
 	"log"
 	"net/http"
 	"strconv"
@@ -66,11 +67,6 @@ func serveGames(gameServer GameServer) {
 	}
 }
 
-type stompGameStart struct {
-	Type   string `json:"type"`
-	GameId int64  `json:"gameId"`
-}
-
 func (games GameServer) AddGame(x, y int) *game.Game {
 	game := game.NewGame(x, y) // TODO
 	addGame := addGameChan{
@@ -81,7 +77,7 @@ func (games GameServer) AddGame(x, y int) *game.Game {
 	games.addGameChan <- addGame
 	gameId := <-addGame.id
 
-	stompGameStart := stompGameStart{
+	stompGameStart := events.GameStart{
 		Type:   "GAME_START",
 		GameId: gameId,
 	}

+ 5 - 0
stomp/stomp.go

@@ -28,3 +28,8 @@ func SendJson(topic string, v interface{}) {
 	b, _ := json.Marshal(v)
 	getStompConnection().Send("/topic/go-battlecamp."+topic, "application/json;charset=utf-8", b, stomp.SendOpt.NoContentLength)
 }
+
+func Subscribe(topic string) chan *stomp.Message {
+	result, _ := getStompConnection().Subscribe("/topic/go-battlecamp."+topic, stomp.AckAuto)
+	return result.C
+}