main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. e := json.NewEncoder(w)
  51. e.Encode(game)
  52. }
  53. func joinGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  54. http.Error(w, "joinGame not implemented", http.StatusNotImplemented)
  55. }
  56. func showGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  57. w.Header().Set("Content-Type", "application/json")
  58. id, _ := strconv.ParseInt(ps.ByName("gameid"), 10, 64)
  59. e := json.NewEncoder(w)
  60. e.Encode(currentGames.GetGame(id))
  61. }
  62. func listPlayers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  63. http.Error(w, "listPlayers not implemented", http.StatusNotImplemented)
  64. }
  65. func movePlayer(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  66. http.Error(w, "movePlayer not implemented", http.StatusNotImplemented)
  67. }
  68. // Request params: x={x}&y={y}&rows={rows}&cols={cols}
  69. func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  70. w.Header().Set("Content-Type", "application/json")
  71. xString := req.FormValue("x")
  72. yString := req.FormValue("y")
  73. colsString := req.FormValue("cols")
  74. rowsString := req.FormValue("rows")
  75. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  76. b := currentGames.GetGame(id).Board
  77. if xString != "" && yString != "" && colsString != "" && rowsString != "" {
  78. x, _ := strconv.Atoi(xString)
  79. y, _ := strconv.Atoi(yString)
  80. cols, _ := strconv.Atoi(colsString)
  81. rows, _ := strconv.Atoi(rowsString)
  82. b.WriteJSON(w, x, y, cols, rows)
  83. } else if xString != "" || yString != "" || colsString != "" || rowsString != "" {
  84. http.Error(w, "Bad request", http.StatusBadRequest)
  85. } else {
  86. b.WriteJSON(w, 0, 0, b.Cols, b.Rows)
  87. }
  88. }
  89. func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  90. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  91. id, _ := strconv.ParseInt(ps.ByName("boardid"), 10, 64)
  92. b := currentGames.GetGame(id).Board.String()
  93. w.Write([]byte(b))
  94. }