main.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // API doc: https://wiki.summercamp.local/display/PIN/Pinguin+server+API
  2. package main
  3. import (
  4. "battlecamp-go-server/board"
  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. http.Error(w, "listGames not implemented", http.StatusNotImplemented)
  41. }
  42. func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  43. xString := ps.ByName("x")
  44. yString := ps.ByName("y")
  45. var game *games.Game
  46. if xString != "" && yString != "" {
  47. x, _ := strconv.Atoi(xString)
  48. y, _ := strconv.Atoi(yString)
  49. game = games.NewGame(x, y)
  50. } else {
  51. game = games.NewGame(56, 35)
  52. }
  53. currentGames.AddGame(game)
  54. }
  55. func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  56. http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
  57. }
  58. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  59. http.Error(w, "showGame not implemented", http.StatusNotImplemented)
  60. }
  61. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  62. http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
  63. }
  64. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  65. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  66. }
  67. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  68. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  69. //x, _ := strconv.Atoi(req.FormValue("x"))
  70. //y, _ := strconv.Atoi(req.FormValue("y"))
  71. //rows, _ := strconv.Atoi(req.FormValue("rows"))
  72. //cols, _ := strconv.Atoi(req.FormValue("cols"))
  73. b := board.New(56, 35)
  74. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  75. b.WriteJSON(w)
  76. }