urlrouter.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package main
  2. import (
  3. "log"
  4. "battlecamp-go-server/games"
  5. "battlecamp-go-server/player"
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "strconv"
  10. "github.com/julienschmidt/httprouter"
  11. )
  12. func newUrlRouter() *httprouter.Router {
  13. router := httprouter.New()
  14. router.GET("/", showIndex)
  15. router.GET("/games", listGames)
  16. router.PUT("/games", createGame)
  17. router.PUT("/games/:rows/:cols", createGame)
  18. router.POST("/games/:gameid/join", joinGame) // API says PUT... zuch.
  19. router.GET("/games/:gameid", showGame)
  20. router.GET("/games/:gameid/:boardid", proxyHandler)
  21. router.GET("/games/:gameid/:boardid/:playerid", showPlayer)
  22. router.POST("/games/:gameid/move/:playerid/:direction", movePlayer)
  23. router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
  24. http.Error(w, fmt.Sprintf("Internal server error: %v", something), http.StatusInternalServerError)
  25. }
  26. return router
  27. }
  28. func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  29. log.Println("Go battlecamp!")
  30. w.Write([]byte("Go battlecamp!"))
  31. }
  32. func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  33. if ps.ByName("gameid") == "asciiboard" {
  34. showAsciiBoard(w, r, ps)
  35. } else if ps.ByName("gameid") == "board" {
  36. showBoard(w, r, ps)
  37. } else if ps.ByName("boardid") == "join" {
  38. joinGame(w, r, ps)
  39. } else {
  40. listPlayers(w, r, ps)
  41. }
  42. }
  43. func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  44. e := json.NewEncoder(w)
  45. e.Encode(currentGames.ListGames())
  46. }
  47. func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  48. xString := ps.ByName("cols")
  49. yString := ps.ByName("rows")
  50. var game *games.Game
  51. if xString != "" && yString != "" {
  52. x, _ := strconv.Atoi(xString)
  53. y, _ := strconv.Atoi(yString)
  54. game = currentGames.AddGame(x, y, stompConnection)
  55. } else {
  56. game = currentGames.AddGame(56, 35, stompConnection)
  57. }
  58. w.WriteHeader(http.StatusCreated)
  59. e := json.NewEncoder(w)
  60. e.Encode(game)
  61. log.Printf("Created game %v", game.Id)
  62. }
  63. func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  64. w.Header().Set("Content-Type", "application/json")
  65. gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  66. d := json.NewDecoder(r.Body)
  67. var p player.Player
  68. d.Decode(&p)
  69. game := currentGames.GetGame(gameId)
  70. game.Join(&p)
  71. e := json.NewEncoder(w)
  72. e.Encode(&p)
  73. log.Printf("Player %v joined game %v", p.Id, game.Id)
  74. }
  75. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  76. w.Header().Set("Content-Type", "application/json")
  77. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  78. e := json.NewEncoder(w)
  79. e.Encode(currentGames.GetGame(id))
  80. }
  81. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  82. w.Header().Set("Content-Type", "application/json")
  83. gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  84. e := json.NewEncoder(w)
  85. e.Encode(currentGames.GetGame(gameId).Players)
  86. }
  87. func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  88. w.Header().Set("Content-Type", "application/json")
  89. gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  90. playerId := ps.ByName("playerid")
  91. g := currentGames.GetGame(gameId)
  92. p := g.GetPlayer(playerId)
  93. e := json.NewEncoder(w)
  94. e.Encode(p)
  95. }
  96. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  97. w.Header().Set("Content-Type", "application/json")
  98. idString := ps.ByName("gameid")
  99. gameId, _ := strconv.ParseInt(idString, 10, 64)
  100. playerId := ps.ByName("playerid")
  101. g := currentGames.GetGame(gameId)
  102. p := g.GetPlayer(playerId)
  103. w.Header().Set("Content-Type", "application/json")
  104. direction := ps.ByName("direction")
  105. result := g.Move(p, direction, stompConnection)
  106. if !result {
  107. http.Error(w, "Bad request", http.StatusBadRequest)
  108. }
  109. e := json.NewEncoder(w)
  110. e.Encode(p.Pos)
  111. log.Println("Player %v moved in direction %v", p.Id, direction)
  112. }
  113. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  114. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  115. w.Header().Set("Content-Type", "application/json")
  116. xString := req.FormValue("x")
  117. yString := req.FormValue("y")
  118. colsString := req.FormValue("cols")
  119. rowsString := req.FormValue("rows")
  120. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  121. b := currentGames.GetGame(id).Board
  122. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  123. x, _ := strconv.Atoi(xString)
  124. y, _ := strconv.Atoi(yString)
  125. cols, _ := strconv.Atoi(colsString)
  126. rows, _ := strconv.Atoi(rowsString)
  127. b.WriteJSON(w, x, y, cols, rows)
  128. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  129. http.Error(w, "Bad request", http.StatusBadRequest)
  130. } else {
  131. b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
  132. }
  133. }
  134. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  135. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  136. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  137. b := currentGames.GetGame(id).Board.String()
  138. w.Write([]byte(b))
  139. }