Harry de Boer преди 10 години
родител
ревизия
60fb70ec13
променени са 2 файла, в които са добавени 55 реда и са изтрити 20 реда
  1. 38 5
      board/board.go
  2. 17 15
      main.go

+ 38 - 5
board/board.go

@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
+	"log"
 	"math/rand"
 )
 
@@ -59,17 +60,49 @@ type jsonTile struct {
 	Player string `json:"player,omitempty"`
 }
 
-func (b Board) WriteJSON(w io.Writer) {
-	//fmt.Fpr
-	fmt.Fprintf(w, "{rows=\"%v\",columns=\"%v\",tiles={", b.Rows, b.Cols)
-	for y := 0; y < b.Rows; y++ {
-		for x := 0; x < b.Cols; x++ {
+func (b Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
+	fmt.Fprintf(w, "{x=\"%v\",y=\"%v\",rows=\"%v\",columns=\"%v\",tiles={", startCol, startRow, cols, rows)
+
+	if startCol > b.Cols {
+		startCol = b.Cols
+	}
+	if startCol < 0 {
+		cols += startCol
+		startCol = 0
+	}
+	if startRow > b.Rows {
+		startRow = b.Rows
+	}
+	if startRow < 0 {
+		rows += startRow
+		startRow = 0
+	}
+	log.Printf("startCol: %v", startCol)
+	if startCol+cols > b.Cols {
+		cols = b.Cols - startCol
+	}
+	log.Printf("startRow: %v", startRow)
+	if startRow+rows > b.Cols {
+		rows = b.Cols - startRow
+	}
+	if cols < 0 {
+		cols = 0
+	}
+	if rows < 0 {
+		rows = 0
+	}
+
+	log.Printf("x=%v, y=%v, cols=%v, rows=%v \n", startCol, startRow, cols, rows)
+
+	for y := startRow; y < startRow+rows; y++ {
+		for x := startCol; x < startCol+cols; x++ {
 			t := &jsonTile{x, y, b.Get(x, y).Name(), ""}
 			bs, _ := json.Marshal(t)
 			w.Write(bs)
 			w.Write([]byte{','})
 		}
 	}
+
 	fmt.Fprintf(w, "}}")
 }
 

+ 17 - 15
main.go

@@ -28,6 +28,10 @@ func main() {
 	// TODO: /games/{id}/players/{playerId}
 	router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
 	
+	router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
+		http.Error(w, "Internal server error", http.StatusInternalServerError)
+	}
+
 	log.Fatal(http.ListenAndServe(":8080", router))
 }
 
@@ -97,23 +101,21 @@ func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
 	yString := req.FormValue("y")
 	colsString := req.FormValue("cols")
 	rowsString := req.FormValue("rows")
-	var x, y, cols, rows int
-	
-	if xString != "" && yString != "" && colsString != "" && rowsString != "" {
-		x, _ = strconv.Atoi(xString)
-		y, _ = strconv.Atoi(yString)
-		cols, _ = strconv.Atoi(colsString)
-		rows, _ = strconv.Atoi(rowsString)
-	}
-	
-	log.Printf("%v %v %v %v", x, y, cols, rows)
+
 	id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
 	b := currentGames.GetGame(id).Board
-	b.WriteJSON(w)
-	//x, _ := strconv.Atoi()
-	//y, _ := strconv.Atoi(req.FormValue("y"))
-	//rows, _ := strconv.Atoi(req.FormValue("rows"))
-	//cols, _ := strconv.Atoi(req.FormValue("cols"))
+
+	if xString != "" && yString != "" && colsString != "" && rowsString != "" {
+		x, _ := strconv.Atoi(xString)
+		y, _ := strconv.Atoi(yString)
+		cols, _ := strconv.Atoi(colsString)
+		rows, _ := strconv.Atoi(rowsString)
+		b.WriteJSON(w, x, y, cols, rows)
+	} else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
+		http.Error(w, "Bad request", http.StatusBadRequest)
+	} else {
+		b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
+	}
 }
 
 func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {