add rollback netcode v1

This commit is contained in:
stryan 2022-02-20 18:43:31 -05:00
parent 6c356c9731
commit e8307d2929

44
game.go
View File

@ -157,17 +157,17 @@ func (g *Game) move(x, y, s, t int) (bool, error) {
if endPiece != nil { if endPiece != nil {
return false, nil return false, nil
} }
//attempt to remove starting piece first
err = g.board.Remove(x, y)
if err != nil {
return false, err
}
// then place piece in new location
res, err := g.board.Place(s, t, startPiece) res, err := g.board.Place(s, t, startPiece)
if err != nil { if err != nil {
return false, err return false, err
} }
if res { return res, nil
err = g.board.Remove(x, y)
if err != nil {
return false, err
}
}
return true, nil
} }
func (g *Game) strike(x, y, s, t int) (bool, error) { func (g *Game) strike(x, y, s, t int) (bool, error) {
@ -203,18 +203,36 @@ func (g *Game) strike(x, y, s, t int) (bool, error) {
switch r { switch r {
case -1: case -1:
//startPiece lost //startPiece lost
g.board.Remove(x, y) err = g.board.Remove(x, y)
if err != nil {
return true, err
}
case 0: case 0:
//tie //tie
g.board.Remove(x, y) err = g.board.Remove(x, y)
g.board.Remove(s, t) err2 := g.board.Remove(s, t)
if err != nil || err2 != nil {
return true, fmt.Errorf("Errors: %v %v", err, err2)
}
case 1: case 1:
//endPiece lost //endPiece lost
g.board.Remove(s, t) err := g.board.Remove(s, t)
if err != nil {
return true, err
}
//scouts replace the piece that was destroyed //scouts replace the piece that was destroyed
if startPiece.Rank == Scout { if startPiece.Rank == Scout {
g.board.Remove(x, y) err = g.board.Remove(x, y)
g.board.Place(s, t, startPiece) if err != nil {
return true, err
}
res, err := g.board.Place(s, t, startPiece)
if err != nil {
return true, err
}
if !res {
return false, errors.New("Combat was valid but somehow placing the new piece was not")
}
} }
} }
return true, nil return true, nil