main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // API doc: https://wiki.summercamp.local/display/PIN/Pinguin+server+API
  2. package main
  3. import (
  4. "battlecamp-go-server/games"
  5. "encoding/json"
  6. "log"
  7. "net/http"
  8. "runtime"
  9. "strconv"
  10. "github.com/julienschmidt/httprouter"
  11. )
  12. var currentGames games.Games = games.New()
  13. func main() {
  14. runtime.GOMAXPROCS(runtime.NumCPU())
  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.GET("/games/:gameid", showGame)
  21. router.GET("/games/:gameid/:boardid", proxyHandler)
  22. //router.GET("/games/:gameid/players", playerListHandler)
  23. // TODO: /games/{id}/players/{playerId}
  24. router.POST("/games/:gameid/move/:playerId/:direction", movePlayer)
  25. router.PanicHandler = func(w http.ResponseWriter, r *http.Request, something interface{}) {
  26. http.Error(w, "Internal server error", http.StatusInternalServerError)
  27. }
  28. log.Fatal(http.ListenAndServe(":8080", router))
  29. }
  30. func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  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. w.Header().Set("Content-Type", "application/json")
  46. e := json.NewEncoder(w)
  47. e.Encode(currentGames.ListGames())
  48. }
  49. func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  50. xString := ps.ByName("x")
  51. yString := ps.ByName("y")
  52. var game *games.Game
  53. if xString != "" && yString != "" {
  54. x, _ := strconv.Atoi(xString)
  55. y, _ := strconv.Atoi(yString)
  56. game = games.NewGame(x, y)
  57. } else {
  58. game = games.NewGame(56, 35)
  59. }
  60. currentGames.AddGame(game)
  61. w.WriteHeader(http.StatusCreated)
  62. }
  63. func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  64. http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
  65. }
  66. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  67. w.Header().Set("Content-Type", "application/json")
  68. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  69. e := json.NewEncoder(w)
  70. e.Encode(currentGames.GetGame(id))
  71. }
  72. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  73. http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
  74. }
  75. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  76. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  77. }
  78. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  79. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  80. w.Header().Set("Content-Type", "application/json")
  81. xString := req.FormValue("x")
  82. yString := req.FormValue("y")
  83. colsString := req.FormValue("cols")
  84. rowsString := req.FormValue("rows")
  85. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  86. b := currentGames.GetGame(id).Board
  87. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  88. x, _ := strconv.Atoi(xString)
  89. y, _ := strconv.Atoi(yString)
  90. cols, _ := strconv.Atoi(colsString)
  91. rows, _ := strconv.Atoi(rowsString)
  92. b.WriteJSON(w, x, y, cols, rows)
  93. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  94. http.Error(w, "Bad request", http.StatusBadRequest)
  95. } else {
  96. b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
  97. }
  98. }
  99. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  100. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  101. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  102. b := currentGames.GetGame(id).Board.String()
  103. w.Write([]byte(b))
  104. }