urlrouter.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  98. log.Println("Player %v moved in direction %v", -1, "none")
  99. }
  100. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  101. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  102. w.Header().Set("Content-Type", "application/json")
  103. xString := req.FormValue("x")
  104. yString := req.FormValue("y")
  105. colsString := req.FormValue("cols")
  106. rowsString := req.FormValue("rows")
  107. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  108. b := currentGames.GetGame(id).Board
  109. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  110. x, _ := strconv.Atoi(xString)
  111. y, _ := strconv.Atoi(yString)
  112. cols, _ := strconv.Atoi(colsString)
  113. rows, _ := strconv.Atoi(rowsString)
  114. b.WriteJSON(w, x, y, cols, rows)
  115. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  116. http.Error(w, "Bad request", http.StatusBadRequest)
  117. } else {
  118. b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
  119. }
  120. }
  121. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  122. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  123. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  124. b := currentGames.GetGame(id).Board.String()
  125. w.Write([]byte(b))
  126. }