Bläddra i källkod

Implement showPlayer, logging toegevoegd

Harry de Boer 10 år sedan
förälder
incheckning
64206435bb
2 ändrade filer med 42 tillägg och 19 borttagningar
  1. 18 7
      games/game.go
  2. 24 12
      urlrouter.go

+ 18 - 7
games/game.go

@@ -44,7 +44,7 @@ func (g *Game) placePlayer(p *player.Player) {
 	for {
 		x := rand.Intn(xRange)
 		y := rand.Intn(yRange)
-		
+
 		if g.isValidPlayerPos(x, y) {
 			p.Pos.X = x
 			p.Pos.Y = y
@@ -60,17 +60,28 @@ func (g *Game) isValidPlayerPos(x, y int) bool {
 	if t == board.Rock {
 		return false;
 	}
-	
-	return g.getPlayer(x, y) == nil
+
+	return g.getPlayerAt(x, y) == nil
 }
 
-func (g *Game) getPlayer(x, y int) *player.Player {
-	
+func (g *Game) getPlayerAt(x, y int) *player.Player {
+
 	for _, p := range g.Players {
 		if (p.Pos.X == x && p.Pos.Y == y) {
 			return p
 		}
 	}
-	
-	return nil	
+
+	return nil
+}
+
+func (g *Game) GetPlayer(playerId string) *player.Player {
+
+	for _, p := range g.Players {
+		if p.Id == playerId{
+			return p
+		}
+	}
+
+	return nil
 }

+ 24 - 12
urlrouter.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"log"
 	"battlecamp-go-server/games"
 	"battlecamp-go-server/player"
 	"encoding/json"
@@ -19,7 +20,7 @@ func newUrlRouter() *httprouter.Router {
 	router.PUT("/games/:rows/:cols", createGame)
 	router.POST("/games/:gameid/join", joinGame) // API says PUT... zuch.
 	router.GET("/games/:gameid", showGame)
-	router.GET("/games/:gameid/:boardid", getProxyHandler)
+	router.GET("/games/:gameid/:boardid", proxyHandler)
 	router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
 	router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
 
@@ -31,22 +32,24 @@ func newUrlRouter() *httprouter.Router {
 }
 
 func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
+	log.Println("Go battlecamp!")
 	w.Write([]byte("Go battlecamp!"))
 }
 
-func getProxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
+func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 
 	if ps.ByName("gameid") == "asciiboard" {
 		showAsciiBoard(w, r, ps)
 	} else if ps.ByName("gameid") == "board" {
 		showBoard(w, r, ps)
+	} else if ps.ByName("boardid") == "join" {
+		joinGame(w, r, ps)
 	} else {
 		listPlayers(w, r, ps)
 	}
 }
 
 func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
-	w.Header().Set("Content-Type", "application/json")
 	e := json.NewEncoder(w)
 	e.Encode(currentGames.ListGames())
 }
@@ -67,19 +70,22 @@ func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	w.WriteHeader(http.StatusCreated)
 	e := json.NewEncoder(w)
 	e.Encode(game)
+	log.Printf("Created game %v", game.Id)
 }
 
 func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	w.Header().Set("Content-Type", "application/json")
-	idString := ps.ByName("gameid")
-	gameId, _ := strconv.ParseInt(idString, 10, 64)
-	var player player.Player
+	gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
 	d := json.NewDecoder(r.Body)
-	d.Decode(&player)
+	var p player.Player
+	d.Decode(&p)
+
 	game := currentGames.GetGame(gameId)
-	game.Join(&player)
+	game.Join(&p)
+
 	e := json.NewEncoder(w)
-	e.Encode(&player)
+	e.Encode(&p)
+	log.Printf("Player %v joined game %v", p.Id, game.Id)
 }
 
 func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -91,18 +97,24 @@ func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 
 func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	w.Header().Set("Content-Type", "application/json")
-	idString := ps.ByName("gameid")
-	gameId, _ := strconv.ParseInt(idString, 10, 64)
+	gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
 	e := json.NewEncoder(w)
 	e.Encode(currentGames.GetGame(gameId).Players)
 }
 
 func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-	http.Error(w, "showPlayer not implemented", http.StatusNotImplemented)
+	w.Header().Set("Content-Type", "application/json")
+	gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
+	playerId := ps.ByName("playerid")
+	g := currentGames.GetGame(gameId)
+	p := g.GetPlayer(playerId)
+	e := json.NewEncoder(w)
+	e.Encode(p)
 }
 
 func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
+	log.Println("Player %v moved in direction %v", -1, "none")
 }
 
 // Request params: x={x}&y={y}&rows={rows}&cols={cols}