urlrouter.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "battlecamp-go-server/games"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "github.com/julienschmidt/httprouter"
  9. )
  10. func newUrlRouter() *httprouter.Router {
  11. router := httprouter.New()
  12. router.GET("/", showIndex)
  13. router.GET("/games", listGames)
  14. router.PUT("/games", createGame)
  15. router.PUT("/games/:rows/:cols", createGame)
  16. router.GET("/games/:gameid", showGame)
  17. router.GET("/games/:gameid/:boardid", proxyHandler)
  18. router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
  19. router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
  20. router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
  21. http.Error(w, fmt.Sprintf("Internal server error: %v", something), http.StatusInternalServerError)
  22. }
  23. return router
  24. }
  25. func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  26. w.Write([]byte("Go battlecamp!"))
  27. }
  28. func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  29. if ps.ByName("gameid") == "asciiboard" {
  30. showAsciiBoard(w, r, ps)
  31. } else if ps.ByName("gameid") == "board" {
  32. showBoard(w, r, ps)
  33. } else if ps.ByName("boardid") == "join" {
  34. joinGame(w, r, ps)
  35. } else {
  36. listPlayers(w, r, ps)
  37. }
  38. }
  39. func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  40. w.Header().Set("Content-Type", "application/json")
  41. e := json.NewEncoder(w)
  42. e.Encode(currentGames.ListGames())
  43. }
  44. func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  45. xString := ps.ByName("x")
  46. yString := ps.ByName("y")
  47. var game *games.Game
  48. if xString != "" && yString != "" {
  49. x, _ := strconv.Atoi(xString)
  50. y, _ := strconv.Atoi(yString)
  51. game = currentGames.AddGame(x, y, stompConnection)
  52. } else {
  53. game = currentGames.AddGame(56, 35, stompConnection)
  54. }
  55. w.WriteHeader(http.StatusCreated)
  56. e := json.NewEncoder(w)
  57. e.Encode(game)
  58. }
  59. func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  60. http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
  61. }
  62. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  63. w.Header().Set("Content-Type", "application/json")
  64. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  65. e := json.NewEncoder(w)
  66. e.Encode(currentGames.GetGame(id))
  67. }
  68. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  69. http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
  70. }
  71. func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  72. http.Error(w, "showPlayer not implemented", http.StatusNotImplemented)
  73. }
  74. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  75. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  76. }
  77. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  78. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  79. w.Header().Set("Content-Type", "application/json")
  80. xString := req.FormValue("x")
  81. yString := req.FormValue("y")
  82. colsString := req.FormValue("cols")
  83. rowsString := req.FormValue("rows")
  84. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  85. b := currentGames.GetGame(id).Board
  86. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  87. x, _ := strconv.Atoi(xString)
  88. y, _ := strconv.Atoi(yString)
  89. cols, _ := strconv.Atoi(colsString)
  90. rows, _ := strconv.Atoi(rowsString)
  91. b.WriteJSON(w, x, y, cols, rows)
  92. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  93. http.Error(w, "Bad request", http.StatusBadRequest)
  94. } else {
  95. b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
  96. }
  97. }
  98. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  99. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  100. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  101. b := currentGames.GetGame(id).Board.String()
  102. w.Write([]byte(b))
  103. }