|
|
@@ -13,18 +13,20 @@ func main() {
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
|
router := httprouter.New()
|
|
|
- router.GET("/", index)
|
|
|
- router.GET("/games/", gameListHandler)
|
|
|
- router.GET("/games/:id", gameHandler)
|
|
|
+ router.GET("/", indexHandler)
|
|
|
+ router.GET("/games", gameListHandler)
|
|
|
router.PUT("/games", createGameHandler)
|
|
|
router.PUT("/games/:rows/:cols", createGameHandler)
|
|
|
- router.POST("/games/:id/move/:playerId/:direction", moveHandler)
|
|
|
+ router.GET("/games/:id", gameHandler)
|
|
|
+ router.GET("/games/board/:id", boardHandler)
|
|
|
+ router.GET("/games/:id/players", playerListHandler)
|
|
|
// TODO: /games/{id}/players/{playerId}
|
|
|
+ router.POST("/games/:id/move/:playerId/:direction", moveHandler)
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", router))
|
|
|
}
|
|
|
|
|
|
-func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
+func indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
w.Write([]byte("Go battlecamp!"))
|
|
|
}
|
|
|
|
|
|
@@ -32,11 +34,16 @@ func gameListHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params
|
|
|
http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
}
|
|
|
|
|
|
+func createGameHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
+ http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
+}
|
|
|
+
|
|
|
func gameHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
+
|
|
|
http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
}
|
|
|
|
|
|
-func createGameHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
+func playerListHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
}
|
|
|
|
|
|
@@ -44,7 +51,12 @@ func moveHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
}
|
|
|
|
|
|
+// Request params: x={x}&y={y}&rows={rows}&cols={cols}
|
|
|
func boardHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
+ x, _ := atoi(req.FormValue("x"))
|
|
|
+ y, _ := atoi(req.FormValue("y"))
|
|
|
+ rows, _ := atoi(req.FormValue("rows"))
|
|
|
+ cols, _ := atoi(req.FormValue("cols"))
|
|
|
b := board.New(56, 35)
|
|
|
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
|
|
|
b.WriteJSON(w)
|