main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. log.Fatal(http.ListenAndServe(":8080", router))
  26. }
  27. func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  28. w.Write([]byte("Go battlecamp!"))
  29. }
  30. func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  31. if ps.ByName("gameid") == "asciiboard" {
  32. showAsciiBoard(w, r, ps)
  33. } else if ps.ByName("gameid") == "board" {
  34. showBoard(w, r, ps)
  35. } else if ps.ByName("boardid") == "join" {
  36. joinGame(w, r, ps)
  37. } else {
  38. listPlayers(w, r, ps)
  39. }
  40. }
  41. func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  42. w.Header().Set("Content-Type", "application/json")
  43. e := json.NewEncoder(w)
  44. e.Encode(currentGames.ListGames())
  45. }
  46. func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  47. xString := ps.ByName("x")
  48. yString := ps.ByName("y")
  49. var game *games.Game
  50. if xString != "" && yString != "" {
  51. x, _ := strconv.Atoi(xString)
  52. y, _ := strconv.Atoi(yString)
  53. game = games.NewGame(x, y)
  54. } else {
  55. game = games.NewGame(56, 35)
  56. }
  57. currentGames.AddGame(game)
  58. w.WriteHeader(http.StatusCreated)
  59. }
  60. func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  61. http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
  62. }
  63. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  64. w.Header().Set("Content-Type", "application/json")
  65. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  66. e := json.NewEncoder(w)
  67. e.Encode(currentGames.GetGame(id))
  68. }
  69. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  70. http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
  71. }
  72. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  73. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  74. }
  75. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  76. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  77. w.Header().Set("Content-Type", "application/json")
  78. xString := req.FormValue("x")
  79. yString := req.FormValue("y")
  80. colsString := req.FormValue("cols")
  81. rowsString := req.FormValue("rows")
  82. var x, y, cols, rows int
  83. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  84. x, _ = strconv.Atoi(xString)
  85. y, _ = strconv.Atoi(yString)
  86. cols, _ = strconv.Atoi(colsString)
  87. rows, _ = strconv.Atoi(rowsString)
  88. }
  89. log.Printf("%v %v %v %v", x, y, cols, rows)
  90. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  91. b := currentGames.GetGame(id).Board
  92. b.WriteJSON(w)
  93. //x, _ := strconv.Atoi()
  94. //y, _ := strconv.Atoi(req.FormValue("y"))
  95. //rows, _ := strconv.Atoi(req.FormValue("rows"))
  96. //cols, _ := strconv.Atoi(req.FormValue("cols"))
  97. }
  98. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  99. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  100. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  101. b := currentGames.GetGame(id).Board.String()
  102. w.Write([]byte(b))
  103. }