|
|
@@ -16,9 +16,9 @@ func newUrlRouter() *httprouter.Router {
|
|
|
router.GET("/", showIndex)
|
|
|
router.GET("/games", listGames)
|
|
|
router.PUT("/games", createGame)
|
|
|
- router.PUT("/games/:rows/:cols", createGame)
|
|
|
+ router.PUT("/games/:rows/:cols", putProxyHandler)
|
|
|
router.GET("/games/:gameid", showGame)
|
|
|
- router.GET("/games/:gameid/:boardid", proxyHandler)
|
|
|
+ router.GET("/games/:gameid/:boardid", getProxyHandler)
|
|
|
router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
|
|
|
router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
|
|
|
|
|
|
@@ -33,19 +33,26 @@ func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
w.Write([]byte("Go battlecamp!"))
|
|
|
}
|
|
|
|
|
|
-func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
+func getProxyHandler(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 putProxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
+
|
|
|
+ if ps.ByName("cols") == "join" {
|
|
|
+ joinGame(w, r, ps)
|
|
|
+ } else {
|
|
|
+ createGame(w, r, ps)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
e := json.NewEncoder(w)
|
|
|
@@ -72,7 +79,7 @@ func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
|
func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
- idString := ps.ByName("gameid")
|
|
|
+ idString := ps.ByName("rows") // gameId... zucht.
|
|
|
gameId, _ := strconv.ParseInt(idString, 10, 64)
|
|
|
var player player.Player
|
|
|
d := json.NewDecoder(r.Body)
|