Fix board state not updating
This commit is contained in:
parent
882585bf24
commit
6041d6487d
58
board.go
58
board.go
|
@ -161,7 +161,7 @@ func (b *Board) autoSendMoves() {
|
|||
}
|
||||
|
||||
var to string
|
||||
if b.premove[i][1] == b.PlayerHomeSpace() {
|
||||
if b.premove[i][1] == b.PlayerBearOffSpace() {
|
||||
to = "off"
|
||||
} else {
|
||||
to = strconv.Itoa(b.premove[i][1])
|
||||
|
@ -188,11 +188,6 @@ func (b *Board) GetState() string {
|
|||
func (b *Board) SetState(state string) {
|
||||
b.Lock()
|
||||
|
||||
/*lastTurn := 0
|
||||
if len(b.v) > 0 {
|
||||
lastTurn = b.v[StateTurn]
|
||||
}*/
|
||||
|
||||
s := strings.Split(state, ":")
|
||||
newPlayers := s[StatePlayerName] != b.s[StatePlayerName] || s[StateOpponentName] != b.s[StateOpponentName]
|
||||
copy(b.s, s)
|
||||
|
@ -225,22 +220,6 @@ func (b *Board) SetState(state string) {
|
|||
|
||||
copy(b.v, v)
|
||||
|
||||
/*if b.v[StateTurn] != lastTurn {
|
||||
if lastTurn == b.v[StatePlayerColor] {
|
||||
b.playerDice = [2]int{0, 0}
|
||||
} else {
|
||||
b.opponentDice = [2]int{0, 0}
|
||||
}
|
||||
}*/ // TODO disabled
|
||||
|
||||
// TODO only overwrite dice when player names change
|
||||
/*if b.v[StatePlayerDice1] > 0 {
|
||||
b.playerDice = [2]int{b.v[StatePlayerDice1], b.v[StatePlayerDice2]}
|
||||
}
|
||||
if b.v[StateOpponentDice1] > 0 {
|
||||
b.opponentDice = [2]int{b.v[StateOpponentDice1], b.v[StateOpponentDice2]}
|
||||
}*/
|
||||
|
||||
b.Unlock()
|
||||
b.Draw()
|
||||
}
|
||||
|
@ -313,7 +292,6 @@ func (b *Board) renderSpace(index int, spaceValue int) []byte {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
if abs > 5 {
|
||||
abs = 5
|
||||
}
|
||||
|
@ -349,7 +327,6 @@ func (b *Board) renderSpace(index int, spaceValue int) []byte {
|
|||
// Highlight legal moves
|
||||
highlightSpace := b.ValidMove(b.selectedSpace, index)
|
||||
highlightSpace = false // TODO Make configurable, disable by default
|
||||
//+(b.v[StatePlayerDice1]*b.v[StatePlayerColor]) ||b.selectedSpace == index+(b.v[StatePlayerDice2]*b.v[StatePlayerColor])) && b.selectedNum > 0
|
||||
if b.selectedNum > 0 && highlightSpace && index != 25 && index != 0 {
|
||||
foregroundColor = "black"
|
||||
backgroundColor = "yellow"
|
||||
|
@ -413,7 +390,7 @@ func (b *Board) ResetPreMoves() {
|
|||
b.Premoveto = make(map[int]int)
|
||||
}
|
||||
|
||||
func (b *Board) homeBoardSpaces() (int, int) {
|
||||
func (b *Board) PlayerHomeSpaces() (int, int) {
|
||||
homeBoardStart := 1
|
||||
homeBoardEnd := 6
|
||||
if (b.v[StateDirection] == -1) == (b.v[StatePlayerColor] == -1) {
|
||||
|
@ -424,7 +401,7 @@ func (b *Board) homeBoardSpaces() (int, int) {
|
|||
}
|
||||
|
||||
func (b *Board) allPlayerPiecesInHomeBoard() bool {
|
||||
homeBoardStart, homeBoardEnd := b.homeBoardSpaces()
|
||||
homeBoardStart, homeBoardEnd := b.PlayerHomeSpaces()
|
||||
hasPlayerPiece := func(index int) bool {
|
||||
if index < 0 || index > 25 {
|
||||
return false
|
||||
|
@ -489,7 +466,7 @@ func (b *Board) GetValidMoves(from int) [][]int {
|
|||
}
|
||||
|
||||
if b.allPlayerPiecesInHomeBoard() {
|
||||
homeSpace := b.PlayerHomeSpace()
|
||||
homeSpace := b.PlayerBearOffSpace()
|
||||
spacesHome := from - homeSpace
|
||||
if spacesHome < 0 {
|
||||
spacesHome *= -1
|
||||
|
@ -519,10 +496,10 @@ CHECKSPACES:
|
|||
}
|
||||
|
||||
func (b *Board) PlayerBarSpace() int {
|
||||
return 25 - b.PlayerHomeSpace()
|
||||
return 25 - b.PlayerBearOffSpace()
|
||||
}
|
||||
|
||||
func (b *Board) PlayerHomeSpace() int {
|
||||
func (b *Board) PlayerBearOffSpace() int {
|
||||
if b.v[StateDirection] == -1 {
|
||||
return 0
|
||||
}
|
||||
|
@ -534,7 +511,7 @@ func (b *Board) ValidMove(f int, t int) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
if t == b.PlayerHomeSpace() {
|
||||
if t == b.PlayerBearOffSpace() {
|
||||
// TODO bear off logic, only allow high roll
|
||||
return b.allPlayerPiecesInHomeBoard()
|
||||
}
|
||||
|
@ -568,7 +545,7 @@ func (b *Board) parseMoveString(player int, s string) int {
|
|||
space = 25 - barSpace
|
||||
}
|
||||
} else if s == "off" {
|
||||
space = b.PlayerHomeSpace()
|
||||
space = b.PlayerBearOffSpace()
|
||||
}
|
||||
}
|
||||
return space
|
||||
|
@ -579,7 +556,7 @@ func (b *Board) Move(player int, f string, t string) {
|
|||
to := b.parseMoveString(player, t)
|
||||
|
||||
if from == SpaceUnknown || to == SpaceUnknown {
|
||||
// TODO debug print ("error: failed to parse move: player %d, from %s, to %s", player, f, t)
|
||||
lf("WARNING: Unknown move %s-%s", f, t)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -589,6 +566,21 @@ func (b *Board) Move(player int, f string, t string) {
|
|||
b.from[from]++
|
||||
b.to[to]++
|
||||
|
||||
spaceValue := b.v[StateBoardSpace0+to]
|
||||
|
||||
// Hit.
|
||||
if (spaceValue == -1 && player == 1) || (spaceValue == 1 && player == -1) {
|
||||
bar := 25 - b.PlayerBarSpace()
|
||||
if player == b.v[StatePlayerColor] {
|
||||
bar = b.PlayerBarSpace()
|
||||
}
|
||||
|
||||
b.v[StateBoardSpace0+bar] -= player
|
||||
}
|
||||
|
||||
b.v[StateBoardSpace0+from] -= player
|
||||
b.v[StateBoardSpace0+to] += player
|
||||
|
||||
b.v[StateTurn] = player * -1
|
||||
|
||||
b.validMoves = make(map[int][][]int)
|
||||
|
@ -630,7 +622,7 @@ func (b *Board) ResetSelection() {
|
|||
func (b *Board) addPreMove(from int, to int, num int) bool {
|
||||
// Allow bearing off when the player moves their own pieces on to the bar
|
||||
if to == 0 || to == 25 {
|
||||
to = b.PlayerHomeSpace()
|
||||
to = b.PlayerBearOffSpace()
|
||||
}
|
||||
|
||||
// Expand combined move
|
||||
|
|
39
client.go
39
client.go
|
@ -203,7 +203,7 @@ func (c *Client) handleWrite() {
|
|||
c.rawMode = false
|
||||
}()
|
||||
} else if bytes.Equal(bytes.ToLower(b), boardstate) {
|
||||
homeBoardStart, homeBoardEnd := c.Board.homeBoardSpaces()
|
||||
homeBoardStart, homeBoardEnd := c.Board.PlayerHomeSpaces()
|
||||
|
||||
lf("Board state: %s", c.Board.GetState())
|
||||
lf("Player color: %d", c.Board.v[StatePlayerColor])
|
||||
|
@ -413,8 +413,6 @@ func (c *Client) updateWhoInfo(b []byte) {
|
|||
itemText += status
|
||||
|
||||
c.who[string(s[whoInfoDataName])] = info
|
||||
|
||||
// TODO who info event
|
||||
}
|
||||
|
||||
func (c *Client) GetAllWhoInfo() []*WhoInfo {
|
||||
|
@ -511,6 +509,14 @@ func (c *Client) Connect() error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (c *Client) boardStateUpdated() {
|
||||
s := make([]string, len(c.Board.s))
|
||||
v := make([]int, len(c.Board.v))
|
||||
copy(s, c.Board.s)
|
||||
copy(v, c.Board.v)
|
||||
c.Event <- &EventBoardState{S: s, V: v}
|
||||
}
|
||||
|
||||
func (c *Client) eventLoop() {
|
||||
var setBoardStyle bool
|
||||
var turnRegexp = regexp.MustCompile(`^turn: (\w+)\.`)
|
||||
|
@ -595,8 +601,6 @@ func (c *Client) eventLoop() {
|
|||
continue
|
||||
} else if bytes.HasPrefix(b, TypeWhoInfo) {
|
||||
c.updateWhoInfo(b[2:])
|
||||
// TODO who window
|
||||
// TODO is endwhoinfo always sent?
|
||||
continue
|
||||
} else if bytes.HasPrefix(b, TypeEndWhoInfo) {
|
||||
who := make([]*WhoInfo, len(c.who))
|
||||
|
@ -622,7 +626,7 @@ func (c *Client) eventLoop() {
|
|||
continue
|
||||
} else if bytes.HasPrefix(b, TypeBoardState) {
|
||||
c.Board.SetState(string(bytes.SplitN(b, []byte(" "), 2)[0][6:]))
|
||||
c.Event <- &EventBoardState{S: c.Board.s, V: c.Board.v}
|
||||
c.boardStateUpdated()
|
||||
continue
|
||||
} else if turnRegexp.Match(b) {
|
||||
turn := turnRegexp.FindSubmatch(b)
|
||||
|
@ -631,7 +635,7 @@ func (c *Client) eventLoop() {
|
|||
} else {
|
||||
c.Board.v[StateTurn] = c.Board.v[StatePlayerColor] * -1
|
||||
}
|
||||
c.Event <- EventBoardState{S: c.Board.s, V: c.Board.v}
|
||||
c.boardStateUpdated()
|
||||
} else if rollsRegexp.Match(b) {
|
||||
roll := rollsRegexp.FindSubmatch(b)
|
||||
periodIndex := bytes.IndexRune(roll[2], '.')
|
||||
|
@ -659,12 +663,12 @@ func (c *Client) eventLoop() {
|
|||
c.Board.v[StateOpponentDice2] = dice[1]
|
||||
}
|
||||
|
||||
c.Event <- &EventBoardState{S: c.Board.s, V: c.Board.v}
|
||||
|
||||
c.Board.ResetMoves()
|
||||
|
||||
c.Board.Draw()
|
||||
c.boardStateUpdated()
|
||||
} else if movesRegexp.Match(b) {
|
||||
c.boardStateUpdated()
|
||||
|
||||
match := movesRegexp.FindSubmatch(b)
|
||||
|
||||
player := c.Board.v[StatePlayerColor]
|
||||
|
@ -690,29 +694,20 @@ func (c *Client) eventLoop() {
|
|||
To: to,
|
||||
}
|
||||
}
|
||||
|
||||
c.Board.Move(player, string(move[0]), string(move[1]))
|
||||
}
|
||||
}
|
||||
|
||||
c.Board.SimplifyMoves()
|
||||
|
||||
c.Board.v[StateTurn] = player * -1
|
||||
if c.Board.v[StateTurn] == c.Board.v[StatePlayerColor] {
|
||||
c.Board.v[StatePlayerDice1] = 0
|
||||
c.Board.v[StatePlayerDice2] = 0
|
||||
} else {
|
||||
c.Board.v[StateOpponentDice1] = 0
|
||||
c.Board.v[StateOpponentDice2] = 0
|
||||
}
|
||||
|
||||
c.Board.Draw()
|
||||
c.boardStateUpdated()
|
||||
|
||||
bs := string(b)
|
||||
if strings.HasSuffix(bs, " .") {
|
||||
bs = bs[:len(bs)-2] + "."
|
||||
}
|
||||
lg(bs)
|
||||
|
||||
continue
|
||||
} else if string(b) == "Value of 'boardstyle' set to 3." {
|
||||
continue
|
||||
|
@ -775,8 +770,8 @@ func (c *Client) eventLoop() {
|
|||
continue
|
||||
} else if newGameRegexp.Match(b) {
|
||||
c.Board.ResetMoves()
|
||||
c.Board.ResetPreMoves()
|
||||
c.Board.resetSelection()
|
||||
// TODO reset premove
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
33
util.go
33
util.go
|
@ -4,29 +4,28 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var (
|
||||
StatusWriter io.Writer
|
||||
GameWriter io.Writer
|
||||
|
||||
statusLogged bool
|
||||
gameLogged bool
|
||||
)
|
||||
|
||||
func l(s string) {
|
||||
s = strings.TrimRightFunc(s, unicode.IsSpace)
|
||||
s = strings.ReplaceAll(s, "\a", "")
|
||||
s = strings.ReplaceAll(s, "\r", "")
|
||||
|
||||
m := time.Now().Format("15:04") + "| " + s
|
||||
if StatusWriter != nil {
|
||||
if statusLogged {
|
||||
StatusWriter.Write([]byte("\n" + m))
|
||||
return
|
||||
}
|
||||
StatusWriter.Write([]byte(m))
|
||||
statusLogged = true
|
||||
StatusWriter.Write([]byte(m + "\n"))
|
||||
return
|
||||
}
|
||||
log.Print(m)
|
||||
|
||||
log.Println(m)
|
||||
}
|
||||
|
||||
func lf(format string, a ...interface{}) {
|
||||
|
@ -34,15 +33,15 @@ func lf(format string, a ...interface{}) {
|
|||
}
|
||||
|
||||
func lg(s string) {
|
||||
s = strings.TrimRightFunc(s, unicode.IsSpace)
|
||||
s = strings.ReplaceAll(s, "\a", "")
|
||||
s = strings.ReplaceAll(s, "\r", "")
|
||||
|
||||
m := time.Now().Format("15:04") + "| " + s
|
||||
if GameWriter != nil {
|
||||
if gameLogged {
|
||||
GameWriter.Write([]byte("\n" + m))
|
||||
return
|
||||
}
|
||||
GameWriter.Write([]byte(m))
|
||||
gameLogged = true
|
||||
GameWriter.Write([]byte(m + "\n"))
|
||||
return
|
||||
}
|
||||
log.Print(m)
|
||||
|
||||
log.Println(m)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue