urlrouter.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 = games.NewGame(x, y)
  52. } else {
  53. game = games.NewGame(56, 35)
  54. }
  55. currentGames.AddGame(game, stompConnection)
  56. w.WriteHeader(http.StatusCreated)
  57. e := json.NewEncoder(w)
  58. e.Encode(game)
  59. }
  60. func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  61. http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
  62. }
  63. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  64. w.Header().Set("Content-Type", "application/json")
  65. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  66. e := json.NewEncoder(w)
  67. e.Encode(currentGames.GetGame(id))
  68. }
  69. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  70. http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
  71. }
  72. func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  73. http.Error(w, "showPlayer not implemented", http.StatusNotImplemented)
  74. }
  75. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  76. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  77. }
  78. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  79. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  80. w.Header().Set("Content-Type", "application/json")
  81. xString := req.FormValue("x")
  82. yString := req.FormValue("y")
  83. colsString := req.FormValue("cols")
  84. rowsString := req.FormValue("rows")
  85. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  86. b := currentGames.GetGame(id).Board
  87. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  88. x, _ := strconv.Atoi(xString)
  89. y, _ := strconv.Atoi(yString)
  90. cols, _ := strconv.Atoi(colsString)
  91. rows, _ := strconv.Atoi(rowsString)
  92. b.WriteJSON(w, x, y, cols, rows)
  93. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  94. http.Error(w, "Bad request", http.StatusBadRequest)
  95. } else {
  96. b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
  97. }
  98. }
  99. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  100. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  101. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  102. b := currentGames.GetGame(id).Board.String()
  103. w.Write([]byte(b))
  104. }