main.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // API doc: https://wiki.summercamp.local/display/PIN/Pinguin+server+API
  2. package main
  3. import (
  4. "encoding/json"
  5. "battlecamp-go-server/games"
  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", "text/plain;charset=utf-8")
  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", "text/plain;charset=utf-8")
  63. http.Error(w, "showGame not implemented", http.StatusNotImplemented)
  64. }
  65. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  66. http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
  67. }
  68. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  69. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  70. }
  71. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  72. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  73. //x, _ := strconv.Atoi(req.FormValue("x"))
  74. //y, _ := strconv.Atoi(req.FormValue("y"))
  75. //rows, _ := strconv.Atoi(req.FormValue("rows"))
  76. //cols, _ := strconv.Atoi(req.FormValue("cols"))
  77. }