urlrouter.go 5.1 KB

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