|
|
@@ -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}
|