urlrouter.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package main
  2. import (
  3. "battlecamp-go-server/games"
  4. "battlecamp-go-server/player"
  5. "encoding/json"
  6. "log"
  7. "net/http"
  8. "strconv"
  9. "github.com/go-stomp/stomp"
  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. // log.Printf("Panic: %v\n", something)
  25. // http.Error(w, fmt.Sprintf("Internal server error: %v", something), http.StatusInternalServerError)
  26. //}
  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(currentGames.ListGames())
  47. }
  48. func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  49. xString := ps.ByName("cols")
  50. yString := ps.ByName("rows")
  51. var game *games.Game
  52. log.Println("Creating new game...")
  53. if xString != "" && yString != "" {
  54. x, _ := strconv.Atoi(xString)
  55. y, _ := strconv.Atoi(yString)
  56. game = currentGames.AddGame(x, y, stompConnection)
  57. } else {
  58. game = currentGames.AddGame(56, 35, stompConnection)
  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 := currentGames.GetGame(gameId)
  72. game.Join(&p)
  73. e := json.NewEncoder(w)
  74. e.Encode(&p)
  75. b, _ := json.Marshal(p)
  76. stompConnection.Send("/topic/go-battlecamp.update", "application/json;charset=utf-8", b, stomp.SendOpt.NoContentLength)
  77. log.Printf("Player %v joined game %v", p.Id, game.Id)
  78. }
  79. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  80. w.Header().Set("Content-Type", "application/json")
  81. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  82. e := json.NewEncoder(w)
  83. e.Encode(currentGames.GetGame(id))
  84. }
  85. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  86. w.Header().Set("Content-Type", "application/json")
  87. gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  88. e := json.NewEncoder(w)
  89. e.Encode(currentGames.GetGame(gameId).Players)
  90. }
  91. func showPlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  92. w.Header().Set("Content-Type", "application/json")
  93. gameId, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  94. playerId := ps.ByName("playerid")
  95. g := currentGames.GetGame(gameId)
  96. p := g.GetPlayer(playerId)
  97. e := json.NewEncoder(w)
  98. e.Encode(p)
  99. }
  100. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  101. w.Header().Set("Content-Type", "application/json")
  102. idString := ps.ByName("gameid")
  103. gameId, _ := strconv.ParseInt(idString, 10, 64)
  104. playerId := ps.ByName("playerid")
  105. g := currentGames.GetGame(gameId)
  106. p := g.GetPlayer(playerId)
  107. w.Header().Set("Content-Type", "application/json")
  108. direction := ps.ByName("direction")
  109. result := g.Move(p, direction, stompConnection)
  110. if !result {
  111. http.Error(w, "Bad request", http.StatusBadRequest)
  112. }
  113. e := json.NewEncoder(w)
  114. e.Encode(p.Pos)
  115. log.Printf("Player %v moved in direction %v\n", p.Id, direction)
  116. }
  117. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  118. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  119. w.Header().Set("Content-Type", "application/json")
  120. xString := req.FormValue("x")
  121. yString := req.FormValue("y")
  122. colsString := req.FormValue("cols")
  123. rowsString := req.FormValue("rows")
  124. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  125. b := currentGames.GetGame(id).Board
  126. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  127. x, _ := strconv.Atoi(xString)
  128. y, _ := strconv.Atoi(yString)
  129. cols, _ := strconv.Atoi(colsString)
  130. rows, _ := strconv.Atoi(rowsString)
  131. b.WriteJSON(w, x, y, cols, rows)
  132. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  133. http.Error(w, "Bad request", http.StatusBadRequest)
  134. } else {
  135. b.WriteJSON(w, 0, 0, b.Width, b.Height)
  136. }
  137. }
  138. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  139. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  140. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  141. b := currentGames.GetGame(id).Board.String()
  142. w.Write([]byte(b))
  143. }