main.go 2.9 KB

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