urlrouter.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. import (
  3. "battlecamp-go-server/games"
  4. "battlecamp-go-server/player"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "strconv"
  9. "github.com/julienschmidt/httprouter"
  10. )
  11. func newUrlRouter() *httprouter.Router {
  12. router := httprouter.New()
  13. router.GET("/", showIndex)
  14. router.GET("/games", listGames)
  15. router.PUT("/games", createGame)
  16. router.PUT("/games/:rows/:cols", createGame)
  17. router.POST("/games/:gameid/join", joinGame) // API says PUT... zuch.
  18. router.GET("/games/:gameid", showGame)
  19. router.GET("/games/:gameid/:boardid", getProxyHandler)
  20. router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
  21. router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
  22. router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
  23. http.Error(w, fmt.Sprintf("Internal server error: %v", something), http.StatusInternalServerError)
  24. }
  25. return router
  26. }
  27. func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  28. w.Write([]byte("Go battlecamp!"))
  29. }
  30. func getProxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  31. if ps.ByName("gameid") == "asciiboard" {
  32. showAsciiBoard(w, r, ps)
  33. } else if ps.ByName("gameid") == "board" {
  34. showBoard(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("cols")
  46. yString := ps.ByName("rows")
  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. w.Header().Set("Content-Type", "application/json")
  61. idString := ps.ByName("gameid")
  62. gameId, _ := strconv.ParseInt(idString, 10, 64)
  63. var player player.Player
  64. d := json.NewDecoder(r.Body)
  65. d.Decode(&player)
  66. game := currentGames.GetGame(gameId)
  67. game.Join(&player)
  68. e := json.NewEncoder(w)
  69. e.Encode(&player)
  70. }
  71. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  72. w.Header().Set("Content-Type", "application/json")
  73. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  74. e := json.NewEncoder(w)
  75. e.Encode(currentGames.GetGame(id))
  76. }
  77. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  78. w.Header().Set("Content-Type", "application/json")
  79. idString := ps.ByName("gameid")
  80. gameId, _ := strconv.ParseInt(idString, 10, 64)
  81. e := json.NewEncoder(w)
  82. e.Encode(currentGames.GetGame(gameId).Players)
  83. }
  84. func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  85. http.Error(w, "showPlayer not implemented", http.StatusNotImplemented)
  86. }
  87. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  88. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  89. }
  90. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  91. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  92. w.Header().Set("Content-Type", "application/json")
  93. xString := req.FormValue("x")
  94. yString := req.FormValue("y")
  95. colsString := req.FormValue("cols")
  96. rowsString := req.FormValue("rows")
  97. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  98. b := currentGames.GetGame(id).Board
  99. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  100. x, _ := strconv.Atoi(xString)
  101. y, _ := strconv.Atoi(yString)
  102. cols, _ := strconv.Atoi(colsString)
  103. rows, _ := strconv.Atoi(rowsString)
  104. b.WriteJSON(w, x, y, cols, rows)
  105. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  106. http.Error(w, "Bad request", http.StatusBadRequest)
  107. } else {
  108. b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
  109. }
  110. }
  111. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  112. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  113. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  114. b := currentGames.GetGame(id).Board.String()
  115. w.Write([]byte(b))
  116. }