urlrouter.go 4.8 KB

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