| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package main
- import (
- "battlecamp-go-server/games"
- "battlecamp-go-server/player"
- "encoding/json"
- "fmt"
- "net/http"
- "strconv"
- "github.com/julienschmidt/httprouter"
- )
- func newUrlRouter() *httprouter.Router {
- router := httprouter.New()
- 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/:boardid/:playerid", showPlayer)
- router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
- router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
- http.Error(w, fmt.Sprintf("Internal server error: %v", something), http.StatusInternalServerError)
- }
- return router
- }
- 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) {
- 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())
- }
- func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
- xString := ps.ByName("cols")
- yString := ps.ByName("rows")
- var game *games.Game
- if xString != "" && yString != "" {
- x, _ := strconv.Atoi(xString)
- y, _ := strconv.Atoi(yString)
- game = currentGames.AddGame(x, y, stompConnection)
- } else {
- game = currentGames.AddGame(56, 35, stompConnection)
- }
- w.WriteHeader(http.StatusCreated)
- e := json.NewEncoder(w)
- e.Encode(game)
- }
- 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
- d := json.NewDecoder(r.Body)
- d.Decode(&player)
- game := currentGames.GetGame(gameId)
- game.Join(&player)
- e := json.NewEncoder(w)
- e.Encode(&player)
- }
- func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
- 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) {
- w.Header().Set("Content-Type", "application/json")
- idString := ps.ByName("gameid")
- gameId, _ := strconv.ParseInt(idString, 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)
- }
- 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}
- func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
- w.Header().Set("Content-Type", "application/json")
- xString := req.FormValue("x")
- yString := req.FormValue("y")
- colsString := req.FormValue("cols")
- rowsString := req.FormValue("rows")
- id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
- b := currentGames.GetGame(id).Board
- if xString != "" && yString != "" && colsString != "" && rowsString != "" {
- x, _ := strconv.Atoi(xString)
- y, _ := strconv.Atoi(yString)
- cols, _ := strconv.Atoi(colsString)
- rows, _ := strconv.Atoi(rowsString)
- b.WriteJSON(w, x, y, cols, rows)
- } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
- http.Error(w, "Bad request", http.StatusBadRequest)
- } else {
- b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
- }
- }
- func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
- w.Header().Set("Content-Type", "text/plain;charset=utf-8")
- id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
- b := currentGames.GetGame(id).Board.String()
- w.Write([]byte(b))
- }
|