urlrouter.go 5.0 KB

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