urlrouter.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.GET("/games/:gameid", showGame)
  18. router.GET("/games/:gameid/:boardid", proxyHandler)
  19. router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
  20. router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
  21. router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
  22. http.Error(w, fmt.Sprintf("Internal server error: %v", something), http.StatusInternalServerError)
  23. }
  24. return router
  25. }
  26. func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  27. w.Write([]byte("Go battlecamp!"))
  28. }
  29. func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  30. if ps.ByName("gameid") == "asciiboard" {
  31. showAsciiBoard(w, r, ps)
  32. } else if ps.ByName("gameid") == "board" {
  33. showBoard(w, r, ps)
  34. } else if ps.ByName("boardid") == "join" {
  35. joinGame(w, r, ps)
  36. } else {
  37. listPlayers(w, r, ps)
  38. }
  39. }
  40. func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  41. w.Header().Set("Content-Type", "application/json")
  42. e := json.NewEncoder(w)
  43. e.Encode(currentGames.ListGames())
  44. }
  45. func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  46. xString := ps.ByName("x")
  47. yString := ps.ByName("y")
  48. var game *games.Game
  49. if xString != "" && yString != "" {
  50. x, _ := strconv.Atoi(xString)
  51. y, _ := strconv.Atoi(yString)
  52. game = currentGames.AddGame(x, y, stompConnection)
  53. } else {
  54. game = currentGames.AddGame(56, 35, stompConnection)
  55. }
  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. w.Header().Set("Content-Type", "application/json")
  62. idString := ps.ByName("gameid")
  63. gameId, _ := strconv.ParseInt(idString, 10, 64)
  64. var player player.Player
  65. d := json.NewDecoder(r.Body)
  66. d.Decode(&player)
  67. game := currentGames.GetGame(gameId)
  68. game.Join(&player)
  69. e := json.NewEncoder(w)
  70. e.Encode(&player)
  71. }
  72. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  73. w.Header().Set("Content-Type", "application/json")
  74. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  75. e := json.NewEncoder(w)
  76. e.Encode(currentGames.GetGame(id))
  77. }
  78. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  79. w.Header().Set("Content-Type", "application/json")
  80. idString := ps.ByName("gameid")
  81. gameId, _ := strconv.ParseInt(idString, 10, 64)
  82. e := json.NewEncoder(w)
  83. e.Encode(currentGames.GetGame(gameId).Players)
  84. }
  85. func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  86. http.Error(w, "showPlayer not implemented", http.StatusNotImplemented)
  87. }
  88. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  89. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  90. }
  91. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  92. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  93. w.Header().Set("Content-Type", "application/json")
  94. xString := req.FormValue("x")
  95. yString := req.FormValue("y")
  96. colsString := req.FormValue("cols")
  97. rowsString := req.FormValue("rows")
  98. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  99. b := currentGames.GetGame(id).Board
  100. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  101. x, _ := strconv.Atoi(xString)
  102. y, _ := strconv.Atoi(yString)
  103. cols, _ := strconv.Atoi(colsString)
  104. rows, _ := strconv.Atoi(rowsString)
  105. b.WriteJSON(w, x, y, cols, rows)
  106. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  107. http.Error(w, "Bad request", http.StatusBadRequest)
  108. } else {
  109. b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
  110. }
  111. }
  112. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  113. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  114. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  115. b := currentGames.GetGame(id).Board.String()
  116. w.Write([]byte(b))
  117. }