main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // API doc: https://wiki.summercamp.local/display/PIN/Pinguin+server+API
  2. package main
  3. import (
  4. "encoding/json"
  5. "log"
  6. "net/http"
  7. "strconv"
  8. "github.com/julienschmidt/httprouter"
  9. "battlecamp-go-server/games"
  10. "battlecamp-go-server/stomp"
  11. )
  12. var currentGames games.Games = games.New()
  13. func main() {
  14. conn := stomp.DailStomp()
  15. defer conn.Disconnect()
  16. log.Fatal(http.ListenAndServe(":8080", initRouter()))
  17. }
  18. func showIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  19. w.Write([]byte("Go battlecamp!"))
  20. }
  21. func proxyHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  22. if ps.ByName("gameid") == "asciiboard" {
  23. showAsciiBoard(w, r, ps)
  24. } else if ps.ByName("gameid") == "board" {
  25. showBoard(w, r, ps)
  26. } else if ps.ByName("boardid") == "join" {
  27. joinGame(w, r, ps)
  28. } else {
  29. listPlayers(w, r, ps)
  30. }
  31. }
  32. func listGames(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
  33. w.Header().Set("Content-Type", "application/json")
  34. e := json.NewEncoder(w)
  35. e.Encode(currentGames.ListGames())
  36. }
  37. func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  38. xString := ps.ByName("x")
  39. yString := ps.ByName("y")
  40. var game *games.Game
  41. if xString != "" && yString != "" {
  42. x, _ := strconv.Atoi(xString)
  43. y, _ := strconv.Atoi(yString)
  44. game = games.NewGame(x, y)
  45. } else {
  46. game = games.NewGame(56, 35)
  47. }
  48. currentGames.AddGame(game)
  49. w.WriteHeader(http.StatusCreated)
  50. }
  51. func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  52. http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
  53. }
  54. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  55. w.Header().Set("Content-Type", "application/json")
  56. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  57. e := json.NewEncoder(w)
  58. e.Encode(currentGames.GetGame(id))
  59. }
  60. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  61. http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
  62. }
  63. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  64. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  65. }
  66. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  67. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  68. w.Header().Set("Content-Type", "application/json")
  69. xString := req.FormValue("x")
  70. yString := req.FormValue("y")
  71. colsString := req.FormValue("cols")
  72. rowsString := req.FormValue("rows")
  73. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  74. b := currentGames.GetGame(id).Board
  75. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  76. x, _ := strconv.Atoi(xString)
  77. y, _ := strconv.Atoi(yString)
  78. cols, _ := strconv.Atoi(colsString)
  79. rows, _ := strconv.Atoi(rowsString)
  80. b.WriteJSON(w, x, y, cols, rows)
  81. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  82. http.Error(w, "Bad request", http.StatusBadRequest)
  83. } else {
  84. b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
  85. }
  86. }
  87. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  88. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  89. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  90. b := currentGames.GetGame(id).Board.String()
  91. w.Write([]byte(b))
  92. }