Pārlūkot izejas kodu

Implement showGame en showBoard

Harry de Boer 10 gadi atpakaļ
vecāks
revīzija
6433c7daad
1 mainītis faili ar 26 papildinājumiem un 8 dzēšanām
  1. 26 8
      main.go

+ 26 - 8
main.go

@@ -2,8 +2,8 @@
 package main
 
 import (
-	"encoding/json"
 	"battlecamp-go-server/games"
+	"encoding/json"
 	"log"
 	"net/http"
 	"runtime"
@@ -27,7 +27,7 @@ func main() {
 	//router.GET("/games/:gameid/players", playerListHandler)
 	// TODO: /games/{id}/players/{playerId}
 	router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
-
+	
 	log.Fatal(http.ListenAndServe(":8080", router))
 }
 
@@ -47,7 +47,7 @@ func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
 }
 
 func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
-	w.Header().Set("Content-Type", "text/plain;charset=utf-8")
+	w.Header().Set("Content-Type", "application/json")
 	e := json.NewEncoder(w)
 	e.Encode(currentGames.ListGames())
 }
@@ -74,10 +74,10 @@ func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 }
 
 func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
-
-	w.Header().Set("Content-Type", "text/plain;charset=utf-8")
-
-	http.Error(w, "showGame not implemented", http.StatusNotImplemented)
+	w.Header().Set("Content-Type", "application/json")
+	id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
+	e := json.NewEncoder(w)
+	e.Encode(currentGames.GetGame(id))
 }
 
 func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@@ -90,7 +90,25 @@ func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 
 // Request params: x={x}&y={y}&rows={rows}&cols={cols}
 func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
-	//x, _ := strconv.Atoi(req.FormValue("x"))
+	w.Header().Set("Content-Type", "application/json")
+	xString := req.FormValue("x")
+	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"))