Commit 5c06450

bryfry <bryon@fryer.io>
2017-02-20 00:27:11
add Map to Game and a channel for the Bot to get the new Game via the Event Reciever
1 parent dd9b3d7
ai.go
@@ -2,10 +2,12 @@ package main
 
 type Bot struct {
 	Updates <-chan Update
-	Map
+	Games   <-chan Game
+	Game
 }
 
 func (ai *Bot) RecieveUpdates() {
+	ai.Game = <-ai.Games
 	for {
 		u := <-ai.Updates
 		ai.Map.Patch(u)
generals.go
@@ -53,9 +53,10 @@ type Game struct {
 	TeamChatID string   `json:"team_chat_room"`
 	Usernames  []string `json:"usernames"`
 	Teams      []int    `json:"teams"`
+	Map
 }
 
-func DecodeEvent(p []byte, updates chan<- Update) (err error) {
+func DecodeEvent(p []byte, updates chan<- Update, games chan<- Game) (err error) {
 	var (
 		m []json.RawMessage
 		t string
@@ -93,7 +94,6 @@ func DecodeEvent(p []byte, updates chan<- Update) (err error) {
 	case GENIO_CHAT:
 		var room string
 		var chat ChatMessage
-
 		err = json.Unmarshal(m[1], &room)
 		if err != nil {
 			l.Error(zap.String("type", t), zap.Error(err))
@@ -115,24 +115,23 @@ func DecodeEvent(p []byte, updates chan<- Update) (err error) {
 
 	case GENIO_START:
 		var game Game
-
 		err = json.Unmarshal(m[1], &game)
 		if err != nil {
 			l.Error(zap.String("type", t), zap.Error(err))
 			return err
 		}
-		l.Info("Game Start ", zap.Any("game", game))
+		games <- game
+		l.Debug("Game sent to AI", zap.Any("game", game))
 
 	case GENIO_UPDATE:
 		var u Update
-
 		err = json.Unmarshal(m[1], &u)
 		if err != nil {
 			l.Error(zap.String("type", t), zap.Error(err))
 			return err
 		}
 		updates <- u
-		l.Debug("Update sent to AI ")
+		l.Debug("Update sent to AI ", zap.Any("update", u))
 
 	case GENIO_WIN:
 		l.Info("Game Won")
main.go
@@ -34,9 +34,9 @@ func main() {
 
 	var ai Bot
 	var g GeneralsIO
-	u := make(chan Update)
-	ai.Updates = u
-	g.Updates = u
+	u, games := make(chan Update), make(chan Game)
+	ai.Updates, ai.Games = u, games
+	g.Updates, g.Games = u, games
 	g.Connect()
 
 	err := g.Emit("set_username", user_id, username)
map.go
@@ -8,7 +8,7 @@ import (
 type Map struct {
 	Scores      []PlayerScore
 	Turn        int
-	AttackIndex int
+	AttackIndex int //TODO figure out what this is
 	Generals    []int
 	MapArray    []int
 	CitiesArray []int
@@ -37,12 +37,12 @@ const (
 )
 
 func (m *Map) Print() {
-	fmt.Println("TILES", m.Height, m.Width, m.Size)
+	fmt.Printf("Turn: %d\n", m.Turn)
 	for i := 0; i < m.Height; i++ {
-		fmt.Println()
 		for j := 0; j < m.Width; j++ {
 			m.Tiles[i][j].Print()
 		}
+		fmt.Println()
 	}
 	fmt.Println()
 }
@@ -51,6 +51,8 @@ func (m *Map) Patch(u Update) {
 	m.MapArray = patch(m.MapArray, u.MapDiff)
 	m.CitiesArray = patch(m.CitiesArray, u.CitiesDiff)
 	m.Generals = u.Generals
+	m.Turn = u.Turn
+	m.Scores = u.Scores
 	// Init
 	if m.Tiles == nil {
 		m.Width = m.MapArray[0]
@@ -60,10 +62,16 @@ func (m *Map) Patch(u Update) {
 		for i := 0; i < m.Height; i++ {
 			m.Tiles[i] = make([]Tile, m.Width)
 		}
+		// TODO - assign to each tile their neighbors / valid moves
 	}
 	// Apply patch to Map/Tiles
 	for i := 0; i < m.Height; i++ {
 		for j := 0; j < m.Width; j++ {
+			// can i skip this after init? updating map array should propigate to tiles?
+			// need to do *int instead?
+			// currently the append way of doing the patch messes up the underlying array / pointers
+			// patch would need rewritten to update the values (not create a new array)
+			// This is a performance upgrade for another day
 			m.Tiles[i][j].Armies = m.MapArray[(i*m.Width)+j+2]
 			m.Tiles[i][j].DecodeTerrain(m.MapArray[(i*m.Width)+j+2+m.Size])
 			for k := range m.Generals {
sockets.go
@@ -11,6 +11,7 @@ import (
 type GeneralsIO struct {
 	*websocket.Conn
 	Updates chan<- Update
+	Games   chan<- Game
 }
 
 const (
@@ -111,7 +112,7 @@ func (g *GeneralsIO) RecievePackets() {
 				l.Debug("Connected")
 			case SOCKETIO_EVENT:
 				// generals.go
-				err = DecodeEvent(p.Payload, g.Updates)
+				err = DecodeEvent(p.Payload, g.Updates, g.Games)
 				if err != nil {
 					l.Error("Unhandled Event", zap.String("packet", string(packet)), zap.Error(err))
 				}