main.go 3.3 KB

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