|
|
@@ -63,6 +63,11 @@ type updatePlayers struct {
|
|
|
Players []*player.Player `json:"players"`
|
|
|
}
|
|
|
|
|
|
+type stompGameEnd struct {
|
|
|
+ Type string `json:"type"`
|
|
|
+ GameId int64 `json:"gameId"`
|
|
|
+}
|
|
|
+
|
|
|
func (g *Game) Move(p *player.Player, direction string, sc *stomp.Conn) bool {
|
|
|
if !(direction == "N" || direction == "W" || direction == "S" || direction == "E") {
|
|
|
log.Printf("Illigal direction %v", direction)
|
|
|
@@ -91,19 +96,31 @@ func (g *Game) Move(p *player.Player, direction string, sc *stomp.Conn) bool {
|
|
|
p.Pos.X = newX
|
|
|
p.Pos.Y = newY
|
|
|
// END TODO make tread safe
|
|
|
-
|
|
|
up := updatePlayers{
|
|
|
GameId: g.Id,
|
|
|
Players: g.Players,
|
|
|
}
|
|
|
|
|
|
b, _ := json.Marshal(up)
|
|
|
+ sc.Send("/topic/go-battlecamp.update", "application/json;charset=utf-8", b, stomp.SendOpt.NoContentLength)
|
|
|
|
|
|
- sc.Send("/topic/go-battlecamp.update", "", b)
|
|
|
+ if g.isWinner(p) {
|
|
|
+ g.EndTime = time.Now().Unix()
|
|
|
+ ge := stompGameEnd{
|
|
|
+ Type: "GAME_END",
|
|
|
+ GameId: g.Id,
|
|
|
+ }
|
|
|
+ c, _ := json.Marshal(ge)
|
|
|
+ sc.Send("/topic/go-battlecamp.game", "application/json;charset=utf-8", c, stomp.SendOpt.NoContentLength)
|
|
|
+ }
|
|
|
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
+func (g *Game) isWinner(p *player.Player) bool {
|
|
|
+ return g.Board.Finish.X == p.Pos.X && g.Board.Finish.Y == p.Pos.Y
|
|
|
+}
|
|
|
+
|
|
|
func (g *Game) isValidPlayerPos(x, y int) bool {
|
|
|
if x < 0 || y < 0 || x >= g.Board.Cols || y >= g.Board.Rows {
|
|
|
return false
|