main.go 3.4 KB

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