|
@@ -1,58 +1,89 @@
|
|
|
|
|
+// API doc: https://wiki.summercamp.local/display/PIN/Pinguin+server+API
|
|
|
package main
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"battlecamp-go-server/board"
|
|
"battlecamp-go-server/board"
|
|
|
|
|
+ "battlecamp-go-server/games"
|
|
|
"log"
|
|
"log"
|
|
|
"net/http"
|
|
"net/http"
|
|
|
"runtime"
|
|
"runtime"
|
|
|
|
|
+ "strconv"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
"github.com/julienschmidt/httprouter"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+var currentGames games.Games = games.New()
|
|
|
|
|
+
|
|
|
func main() {
|
|
func main() {
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
|
|
|
router := httprouter.New()
|
|
router := httprouter.New()
|
|
|
- router.GET("/", indexHandler)
|
|
|
|
|
- router.GET("/games", gameListHandler)
|
|
|
|
|
- router.PUT("/games", createGameHandler)
|
|
|
|
|
- router.PUT("/games/:rows/:cols", createGameHandler)
|
|
|
|
|
- router.GET("/games/:id", gameHandler)
|
|
|
|
|
- router.GET("/games/board/:id", boardHandler)
|
|
|
|
|
- router.GET("/games/:id/players", playerListHandler)
|
|
|
|
|
|
|
+ router.GET("/", showIndex)
|
|
|
|
|
+ router.GET("/games", listGames)
|
|
|
|
|
+ router.PUT("/games", createGame)
|
|
|
|
|
+ router.PUT("/games/:rows/:cols", createGame)
|
|
|
|
|
+ router.GET("/games/:gameid", showGame)
|
|
|
|
|
+ router.GET("/games/:gameid/:boardid", proxyHandler)
|
|
|
|
|
+ //router.GET("/games/:gameid/players", playerListHandler)
|
|
|
// TODO: /games/{id}/players/{playerId}
|
|
// TODO: /games/{id}/players/{playerId}
|
|
|
- router.POST("/games/:id/move/:playerId/:direction", moveHandler)
|
|
|
|
|
|
|
+ router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
|
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", router))
|
|
log.Fatal(http.ListenAndServe(":8080", router))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
|
|
|
+func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
w.Write([]byte("Go battlecamp!"))
|
|
w.Write([]byte("Go battlecamp!"))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func gameListHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
|
- http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
|
|
|
|
+func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
+
|
|
|
|
|
+ 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 createGameHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
- http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
|
|
|
|
+func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
|
+ http.Error(w, "listGames not implemented", http.StatusNotImplemented)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func gameHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
|
|
+func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
+ xString := ps.ByName("x")
|
|
|
|
|
+ yString := ps.ByName("y")
|
|
|
|
|
+ var game *games.Game
|
|
|
|
|
+
|
|
|
|
|
+ if xString != "" && yString != "" {
|
|
|
|
|
+ x, _ := strconv.Atoi(xString)
|
|
|
|
|
+ y, _ := strconv.Atoi(yString)
|
|
|
|
|
+ game = games.NewGame(x, y)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ game = games.NewGame(56, 35)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ currentGames.AddGame(game)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
+ http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
|
|
|
|
+func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
+ http.Error(w, "showGame not implemented", http.StatusNotImplemented)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func playerListHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
- http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
|
|
|
|
+func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
+ http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func moveHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
- http.Error(w, "Not implemented", http.StatusNotImplemented)
|
|
|
|
|
|
|
+func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
|
+ http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Request params: x={x}&y={y}&rows={rows}&cols={cols}
|
|
// Request params: x={x}&y={y}&rows={rows}&cols={cols}
|
|
|
-func boardHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
|
|
|
|
|
|
+func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
|
|
//x, _ := strconv.Atoi(req.FormValue("x"))
|
|
//x, _ := strconv.Atoi(req.FormValue("x"))
|
|
|
//y, _ := strconv.Atoi(req.FormValue("y"))
|
|
//y, _ := strconv.Atoi(req.FormValue("y"))
|
|
|
//rows, _ := strconv.Atoi(req.FormValue("rows"))
|
|
//rows, _ := strconv.Atoi(req.FormValue("rows"))
|