game.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package games
  2. import (
  3. "battlecamp-go-server/board"
  4. "battlecamp-go-server/player"
  5. "encoding/json"
  6. "log"
  7. "math/rand"
  8. "sync"
  9. "time"
  10. "github.com/go-stomp/stomp"
  11. )
  12. type Game struct {
  13. Id int64 `json:"id"`
  14. StartTime int64 `json:"startTime"`
  15. EndTime int64 `json:"endTime"`
  16. Board *board.Board `json:"board"`
  17. Players []*player.Player `json:"-"`
  18. mutex sync.Mutex
  19. Winner *player.Player `json:"winner,omitempty"`
  20. }
  21. func NewGame(cols, rows int) *Game {
  22. createTime := time.Now().Unix()
  23. game := &Game{
  24. StartTime: createTime,
  25. Board: board.New(cols, rows),
  26. Players: make([]*player.Player, 0),
  27. }
  28. return game
  29. }
  30. func (g *Game) Join(p *player.Player) {
  31. g.mutex.Lock()
  32. g.placePlayer(p)
  33. g.Players = append(g.Players, p)
  34. g.mutex.Unlock()
  35. }
  36. func (g *Game) placePlayer(p *player.Player) {
  37. xRange := g.Board.Cols / 5
  38. yRange := g.Board.Rows
  39. for {
  40. x := rand.Intn(xRange)
  41. y := rand.Intn(yRange)
  42. if g.isValidPlayerPos(x, y) {
  43. p.Pos.X = x
  44. p.Pos.Y = y
  45. return
  46. }
  47. }
  48. }
  49. type updatePlayers struct {
  50. GameId int64 `json:"gameId"`
  51. Players []*player.Player `json:"players"`
  52. }
  53. func (g *Game) Move(p *player.Player, direction string, sc *stomp.Conn) bool {
  54. if !(direction == "N" || direction == "W" || direction == "S" || direction == "E") {
  55. log.Printf("Illigal direction %v", direction)
  56. return false
  57. }
  58. newX := p.Pos.X
  59. newY := p.Pos.Y
  60. switch direction {
  61. case "N":
  62. newY--
  63. case "W":
  64. newX--
  65. case "S":
  66. newY++
  67. case "E":
  68. newX++
  69. }
  70. //TODO make thread safe
  71. if !g.isValidPlayerPos(newX, newY) {
  72. log.Printf("Illigal player pos oldX %v oldY %v newX %v newY %v dir %v ", p.Pos.X, p.Pos.Y, newX, newY, direction)
  73. return false
  74. }
  75. p.Pos.X = newX
  76. p.Pos.Y = newY
  77. // END TODO make tread safe
  78. up := updatePlayers{
  79. GameId: g.Id,
  80. Players: g.Players,
  81. }
  82. b, _ := json.Marshal(up)
  83. sc.Send("/topic/go-battlecamp.update", "", b)
  84. return true
  85. }
  86. func (g *Game) isValidPlayerPos(x, y int) bool {
  87. if x < 0 || y < 0 || x >= g.Board.Cols || y >= g.Board.Rows {
  88. return false
  89. }
  90. t := g.Board.Get(x, y)
  91. if t == board.Rock {
  92. return false
  93. }
  94. return g.getPlayerAt(x, y) == nil
  95. }
  96. func (g *Game) getPlayerAt(x, y int) *player.Player {
  97. for _, p := range g.Players {
  98. if p.Pos.X == x && p.Pos.Y == y {
  99. return p
  100. }
  101. }
  102. return nil
  103. }
  104. func (g *Game) GetPlayer(playerId string) *player.Player {
  105. for _, p := range g.Players {
  106. if p.Id == playerId {
  107. return p
  108. }
  109. }
  110. return nil
  111. }