|
|
@@ -3,25 +3,24 @@ package games
|
|
|
import (
|
|
|
"battlecamp-go-server/board"
|
|
|
"battlecamp-go-server/player"
|
|
|
- "encoding/json"
|
|
|
+ "battlecamp-go-server/stomp"
|
|
|
"log"
|
|
|
"math/rand"
|
|
|
"os"
|
|
|
"runtime/pprof"
|
|
|
"sync"
|
|
|
"time"
|
|
|
-
|
|
|
- "github.com/go-stomp/stomp"
|
|
|
)
|
|
|
|
|
|
type Game struct {
|
|
|
- Id int64 `json:"id"`
|
|
|
- StartTime int64 `json:"startTime"`
|
|
|
- EndTime int64 `json:"endTime"`
|
|
|
- Board *board.Board `json:"board"`
|
|
|
- Players []*player.Player `json:"-"`
|
|
|
- mutex sync.Mutex
|
|
|
- Winner *player.Player `json:"winner,omitempty"`
|
|
|
+ Id int64 `json:"id"`
|
|
|
+ StartTime int64 `json:"startTime"`
|
|
|
+ EndTime int64 `json:"endTime"`
|
|
|
+ Board *board.Board `json:"board"`
|
|
|
+ Players []*player.Player `json:"-"`
|
|
|
+ mutex sync.Mutex
|
|
|
+ Winner *player.Player `json:"winner,omitempty"`
|
|
|
+ gameServer *GameServer
|
|
|
}
|
|
|
|
|
|
func NewGame(cols, rows int) *Game {
|
|
|
@@ -78,7 +77,7 @@ type stompGameEnd struct {
|
|
|
Payload string `json:"payload"`
|
|
|
}
|
|
|
|
|
|
-func (g *Game) Move(p *player.Player, direction string, sc *stomp.Conn) bool {
|
|
|
+func (g *Game) Move(p *player.Player, direction string) bool {
|
|
|
if !(direction == "N" || direction == "W" || direction == "S" || direction == "E") {
|
|
|
log.Printf("Illigal direction %v", direction)
|
|
|
return false
|
|
|
@@ -111,8 +110,7 @@ func (g *Game) Move(p *player.Player, direction string, sc *stomp.Conn) bool {
|
|
|
Players: g.Players,
|
|
|
}
|
|
|
|
|
|
- b, _ := json.Marshal(up)
|
|
|
- sc.Send("/topic/go-battlecamp.update", "application/json;charset=utf-8", b, stomp.SendOpt.NoContentLength)
|
|
|
+ stomp.SendJson("update", up)
|
|
|
|
|
|
if g.isWinner(p) {
|
|
|
g.EndTime = time.Now().Unix()
|
|
|
@@ -122,8 +120,8 @@ func (g *Game) Move(p *player.Player, direction string, sc *stomp.Conn) bool {
|
|
|
GameId: g.Id,
|
|
|
Payload: g.Winner.Id,
|
|
|
}
|
|
|
- c, _ := json.Marshal(ge)
|
|
|
- sc.Send("/topic/go-battlecamp.game", "application/json;charset=utf-8", c, stomp.SendOpt.NoContentLength)
|
|
|
+
|
|
|
+ stomp.SendJson("game", ge)
|
|
|
}
|
|
|
|
|
|
return true
|