main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "strconv"
  11. "time"
  12. "battlecamp-go-server/events"
  13. "battlecamp-go-server/flag"
  14. "battlecamp-go-server/game"
  15. "battlecamp-go-server/player"
  16. "battlecamp-go-server/stomp"
  17. "battlecamp-go-server/board"
  18. )
  19. func main() {
  20. initLogging()
  21. log.Println("Game bot version 0.1")
  22. flag.ParseFlags()
  23. pu := make(chan *events.PlayerUpdate)
  24. go subscribeToUpdate(pu)
  25. subscribeToGame(pu)
  26. }
  27. func subscribeToGame(pu chan *events.PlayerUpdate) {
  28. sub := stomp.Subscribe("game")
  29. for {
  30. announcement := <-sub
  31. gs := new(events.GameStart)
  32. json.Unmarshal(announcement.Body, &gs)
  33. log.Printf("announcement type: %v ", strconv.FormatInt(gs.GameId, 10))
  34. gameEndChan := make(chan bool)
  35. if "GAME_START" == gs.Type {
  36. go joinGame(gs.GameId, gameEndChan, pu)
  37. } else {
  38. gameEndChan <- true
  39. }
  40. }
  41. }
  42. func subscribeToUpdate(pu chan *events.PlayerUpdate) {
  43. sub := stomp.Subscribe("update")
  44. for {
  45. announcement := <-sub
  46. if "vnd.battlecamp.player" == announcement.ContentType {
  47. p := new(player.Player)
  48. json.Unmarshal(announcement.Body, &p)
  49. playerJoin(p)
  50. } else {
  51. pue := new(events.PlayerUpdate)
  52. json.Unmarshal(announcement.Body, &pue)
  53. pu <- pue
  54. }
  55. }
  56. }
  57. func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate) {
  58. //join game
  59. p := &player.Player{
  60. Id: "Zeus",
  61. Color: "#238b02",
  62. Type: 1,
  63. }
  64. players := make([]*player.Player, 5)
  65. //retrieve finishe
  66. gameUrl := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10)
  67. fmt.Println("getGame:>", gameUrl)
  68. resp, _ := http.Get(gameUrl)
  69. g := new(game.Game)
  70. b, _ := ioutil.ReadAll(resp.Body)
  71. json.Unmarshal(b, &g)
  72. finish := g.Board.Finish
  73. fmt.Printf("Finish x=%v y=%v", finish.X, finish.Y)
  74. //join the game
  75. url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/join"
  76. jsonStr, _ := json.Marshal(p)
  77. req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  78. req.Header.Set("Content-Type", "application/json")
  79. fmt.Println("URL:>", req)
  80. client := &http.Client{}
  81. resp, err := client.Do(req)
  82. if err != nil {
  83. panic(err)
  84. }
  85. v, _ := ioutil.ReadAll(resp.Body)
  86. json.Unmarshal(v, p)
  87. resp.Body.Close()
  88. for {
  89. select {
  90. case <-gameEndChan:
  91. return
  92. case playerUpdate := <-pu:
  93. if playerUpdate.GameId == gameId {
  94. players = playerUpdate.Players
  95. for _, pup := range players {
  96. if pup.Id == p.Id {
  97. p.Pos.X = pup.Pos.X
  98. p.Pos.Y = pup.Pos.Y
  99. fmt.Printf("Set position to x=%v y=%v\n", pup.Pos.X, pup.Pos.Y)
  100. }
  101. }
  102. }
  103. default:
  104. move(gameId, p, players, finish)
  105. time.Sleep(2 * time.Second)
  106. }
  107. }
  108. }
  109. func move(gameId int64, p *player.Player, players []*player.Player, finish board.Coordinate) {
  110. //determine move
  111. direction := "E"
  112. //send move
  113. url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/move/" + p.Id + "/" + direction
  114. fmt.Println("move:>", url)
  115. http.Post(url, "text/plain", nil)
  116. //update x,y
  117. switch direction {
  118. case "N":
  119. p.Pos.Y++
  120. case "E":
  121. p.Pos.X++
  122. case "S":
  123. p.Pos.Y--
  124. case "W":
  125. p.Pos.Y++
  126. }
  127. fmt.Printf("new pos x=%v y=%v", p.Pos.X, p.Pos.Y)
  128. }
  129. func playerJoin(p *player.Player) {
  130. }
  131. func initLogging() {
  132. logFile, err := os.Create("server.log")
  133. if err == nil {
  134. log.SetOutput(logFile)
  135. } else {
  136. log.Println("ERROR: Cannot open log file, using console.")
  137. log.Printf("%v=n", err)
  138. }
  139. }