|
@@ -11,12 +11,12 @@ import (
|
|
|
"strconv"
|
|
"strconv"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
|
|
+ "battlecamp-go-server/board"
|
|
|
"battlecamp-go-server/events"
|
|
"battlecamp-go-server/events"
|
|
|
"battlecamp-go-server/flag"
|
|
"battlecamp-go-server/flag"
|
|
|
"battlecamp-go-server/game"
|
|
"battlecamp-go-server/game"
|
|
|
"battlecamp-go-server/player"
|
|
"battlecamp-go-server/player"
|
|
|
"battlecamp-go-server/stomp"
|
|
"battlecamp-go-server/stomp"
|
|
|
- "battlecamp-go-server/board"
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
@@ -73,20 +73,15 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
|
|
|
|
|
|
|
|
players := make([]*player.Player, 5)
|
|
players := make([]*player.Player, 5)
|
|
|
|
|
|
|
|
- //retrieve finishe
|
|
|
|
|
-
|
|
|
|
|
|
|
+ //retrieve finish
|
|
|
gameUrl := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10)
|
|
gameUrl := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10)
|
|
|
fmt.Println("getGame:>", gameUrl)
|
|
fmt.Println("getGame:>", gameUrl)
|
|
|
-
|
|
|
|
|
resp, _ := http.Get(gameUrl)
|
|
resp, _ := http.Get(gameUrl)
|
|
|
-
|
|
|
|
|
g := new(game.Game)
|
|
g := new(game.Game)
|
|
|
b, _ := ioutil.ReadAll(resp.Body)
|
|
b, _ := ioutil.ReadAll(resp.Body)
|
|
|
json.Unmarshal(b, &g)
|
|
json.Unmarshal(b, &g)
|
|
|
-
|
|
|
|
|
- finish := g.Board.Finish
|
|
|
|
|
-
|
|
|
|
|
- fmt.Printf("Finish x=%v y=%v", finish.X, finish.Y)
|
|
|
|
|
|
|
+ boardSummery := g.Board
|
|
|
|
|
+ fmt.Printf("Finish x=%v y=%v\n", boardSummery.Finish.X, boardSummery.Finish.Y)
|
|
|
|
|
|
|
|
//join the game
|
|
//join the game
|
|
|
url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/join"
|
|
url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/join"
|
|
@@ -94,7 +89,6 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
|
|
|
jsonStr, _ := json.Marshal(p)
|
|
jsonStr, _ := json.Marshal(p)
|
|
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
|
|
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
- fmt.Println("URL:>", req)
|
|
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
client := &http.Client{}
|
|
|
resp, err := client.Do(req)
|
|
resp, err := client.Do(req)
|
|
@@ -102,9 +96,16 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
|
|
|
panic(err)
|
|
panic(err)
|
|
|
}
|
|
}
|
|
|
v, _ := ioutil.ReadAll(resp.Body)
|
|
v, _ := ioutil.ReadAll(resp.Body)
|
|
|
- json.Unmarshal(v, p)
|
|
|
|
|
|
|
+ json.Unmarshal(v, p) //Set my location in the player
|
|
|
|
|
+ fmt.Printf("My start position x %v y %v\n", p.Pos.X, p.Pos.Y)
|
|
|
resp.Body.Close()
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
+ //determine to avoind north or south
|
|
|
|
|
+ north := true
|
|
|
|
|
+ if boardSummery.Finish.Y > p.Pos.Y {
|
|
|
|
|
+ north = false
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
for {
|
|
for {
|
|
|
select {
|
|
select {
|
|
|
case <-gameEndChan:
|
|
case <-gameEndChan:
|
|
@@ -121,15 +122,81 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
default:
|
|
default:
|
|
|
- move(gameId, p, players, finish)
|
|
|
|
|
|
|
+ move(gameId, p, players, boardSummery, north)
|
|
|
time.Sleep(2 * time.Second)
|
|
time.Sleep(2 * time.Second)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func move(gameId int64, p *player.Player, players []*player.Player, finish board.Coordinate) {
|
|
|
|
|
|
|
+type viewport struct {
|
|
|
|
|
+ x,y,width,height int
|
|
|
|
|
+ Board *board.Board
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func getViewPort(x, y, width, height int, gameId int64, bs *board.Board) *viewport {
|
|
|
|
|
+ x = max(x, 0)
|
|
|
|
|
+ x = min(x, bs.Width)
|
|
|
|
|
+ y = max(y, 0)
|
|
|
|
|
+ y = min(y, bs.Height)
|
|
|
|
|
+ if x + width > bs.Width {
|
|
|
|
|
+ width = bs.Width - x
|
|
|
|
|
+ }
|
|
|
|
|
+ if y + height > bs.Height {
|
|
|
|
|
+ height = bs.Height - x
|
|
|
|
|
+ }
|
|
|
|
|
+ url := "http://localhost:8080/games/board/" + strconv.FormatInt(gameId, 10) + "?x=" + strconv.Itoa(x) + "&y=" + strconv.Itoa(y) + "&rows=" + strconv.Itoa(width) + "&cols=" + strconv.Itoa(height)
|
|
|
|
|
+ fmt.Println("getViewPort:>", url)
|
|
|
|
|
+ resp, _ := http.Get(url)
|
|
|
|
|
+ board := board.ReadJSON(x, y, resp.Body)
|
|
|
|
|
+ return &viewport{
|
|
|
|
|
+ x:x,
|
|
|
|
|
+ y:y,
|
|
|
|
|
+ width:width,
|
|
|
|
|
+ height:height,
|
|
|
|
|
+ Board:board,
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (v viewport) Get(x, y int) board.TileType {
|
|
|
|
|
+ fmt.Printf("retrieving from viewport x %v y %v position in viewport x %v y %v", x, y, x-v.x, y-v.y)
|
|
|
|
|
+ return v.Board.Get(x-v.x,y-v.y)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const viewPortWidth = 32
|
|
|
|
|
+const viewPortHeight = 32
|
|
|
|
|
+
|
|
|
|
|
+func max(a, b int) int {
|
|
|
|
|
+ if a >= b {
|
|
|
|
|
+ return a
|
|
|
|
|
+ }
|
|
|
|
|
+ return b
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func min(a, b int) int {
|
|
|
|
|
+ if a <= b {
|
|
|
|
|
+ return a
|
|
|
|
|
+ }
|
|
|
|
|
+ return b
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Board, north bool) {
|
|
|
//determine move
|
|
//determine move
|
|
|
- direction := "E"
|
|
|
|
|
|
|
+
|
|
|
|
|
+ viewPortX := p.Pos.X-1
|
|
|
|
|
+ viewPortY := p.Pos.Y-1
|
|
|
|
|
+ if north {
|
|
|
|
|
+ viewPortX = viewPortX-viewPortHeight
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ viewPort := getViewPort(viewPortX, viewPortY, viewPortWidth, viewPortHeight, gameId, bs)
|
|
|
|
|
+
|
|
|
|
|
+ direction := "W"
|
|
|
|
|
+
|
|
|
|
|
+ fmt.Printf("p.Pos.X+1 %v p.Pos.Y %v\n", p.Pos.X+1, p.Pos.Y)
|
|
|
|
|
+
|
|
|
|
|
+ if viewPort.Get(p.Pos.X+1, p.Pos.Y) != board.Rock {
|
|
|
|
|
+ direction = "E"
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
//send move
|
|
//send move
|
|
|
url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/move/" + p.Id + "/" + direction
|
|
url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/move/" + p.Id + "/" + direction
|
|
@@ -148,7 +215,7 @@ func move(gameId int64, p *player.Player, players []*player.Player, finish board
|
|
|
case "W":
|
|
case "W":
|
|
|
p.Pos.Y++
|
|
p.Pos.Y++
|
|
|
}
|
|
}
|
|
|
- fmt.Printf("new pos x=%v y=%v", p.Pos.X, p.Pos.Y)
|
|
|
|
|
|
|
+ fmt.Printf("new pos x=%v y=%v\n", p.Pos.X, p.Pos.Y)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func playerJoin(p *player.Player) {
|
|
func playerJoin(p *player.Player) {
|