فهرست منبع

Retrieve finish coordinates

Ronald Peterson 10 سال پیش
والد
کامیت
05280f8257
1فایلهای تغییر یافته به همراه41 افزوده شده و 25 حذف شده
  1. 41 25
      battlecamp-go-bot/main.go

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

@@ -1,20 +1,22 @@
 package main
 
 import (
-	"time"
-	"io/ioutil"
-	"strconv"
 	"bytes"
 	"encoding/json"
 	"fmt"
+	"io/ioutil"
 	"log"
 	"net/http"
 	"os"
+	"strconv"
+	"time"
 
 	"battlecamp-go-server/events"
 	"battlecamp-go-server/flag"
+	"battlecamp-go-server/game"
 	"battlecamp-go-server/player"
 	"battlecamp-go-server/stomp"
+	"battlecamp-go-server/board"
 )
 
 func main() {
@@ -68,34 +70,50 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
 		Color: "#238b02",
 		Type:  1,
 	}
-	
+
 	players := make([]*player.Player, 5)
 
+	//retrieve finishe
+
+	gameUrl := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10)
+	fmt.Println("getGame:>", gameUrl)
+
+	resp, _ := http.Get(gameUrl)
+
+	g := new(game.Game)
+	b, _ := ioutil.ReadAll(resp.Body)
+	json.Unmarshal(b, &g)
+
+	finish := g.Board.Finish
+	
+	fmt.Printf("Finish x=%v y=%v", finish.X, finish.Y)
+
+	//join the game
 	url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/join"
 
 	jsonStr, _ := json.Marshal(p)
 	req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
 	req.Header.Set("Content-Type", "application/json")
 	fmt.Println("URL:>", req)
-	
+
 	client := &http.Client{}
 	resp, err := client.Do(req)
 	if err != nil {
 		panic(err)
 	}
 	v, _ := ioutil.ReadAll(resp.Body)
-	json.Unmarshal(v, p) 
+	json.Unmarshal(v, p)
 	resp.Body.Close()
 
 	for {
 		select {
 		case <-gameEndChan:
 			return
-		case playerUpdate := <- pu:
-			if(playerUpdate.GameId == gameId) {
+		case playerUpdate := <-pu:
+			if playerUpdate.GameId == gameId {
 				players = playerUpdate.Players
 				for _, pup := range players {
-					if(pup.Id == p.Id) {
+					if pup.Id == p.Id {
 						p.Pos.X = pup.Pos.X
 						p.Pos.Y = pup.Pos.Y
 						fmt.Printf("Set position to x=%v y=%v\n", pup.Pos.X, pup.Pos.Y)
@@ -103,36 +121,34 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
 				}
 			}
 		default:
-			move(gameId, p, players)
+			move(gameId, p, players, finish)
 			time.Sleep(2 * time.Second)
 		}
 	}
 }
 
-func move(gameId int64, p *player.Player, players []*player.Player) {
-	//retrieve viewport
-	
+func move(gameId int64, p *player.Player, players []*player.Player, finish board.Coordinate) {
 	//determine move
 	direction := "E"
-	
+
 	//send move
 	url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/move/" + p.Id + "/" + direction
 	fmt.Println("move:>", url)
-	
+
 	http.Post(url, "text/plain", nil)
-	
+
 	//update x,y
 	switch direction {
-		case "N":
-			p.Pos.Y++
-		case "E":
-			p.Pos.X++
-		case "S":
-			p.Pos.Y--
-		case "W":
-			p.Pos.Y++
+	case "N":
+		p.Pos.Y++
+	case "E":
+		p.Pos.X++
+	case "S":
+		p.Pos.Y--
+	case "W":
+		p.Pos.Y++
 	}
-
+	fmt.Printf("new pos x=%v y=%v", p.Pos.X, p.Pos.Y)
 }
 
 func playerJoin(p *player.Player) {