Support multiple initial moves

This commit is contained in:
ekzyis 2024-09-23 06:52:58 +02:00
parent 9a6579b6f8
commit 11b2238c86
2 changed files with 13 additions and 8 deletions

View File

@ -56,10 +56,10 @@ func NewBoard() *Board {
return board return board
} }
func NewGame(move string) (*Board, error) { func NewGame(moves string) (*Board, error) {
board := NewBoard() board := NewBoard()
if err := board.Move(move); err != nil { if err := board.Parse(moves); err != nil {
return nil, err return nil, err
} }
@ -149,16 +149,20 @@ func (b *Board) AlgebraicNotation() string {
text += fmt.Sprintf(" %s ", m) text += fmt.Sprintf(" %s ", m)
} }
} }
return text return fmt.Sprintf("`%s`", text)
} }
func (b *Board) Parse(pgn string) error { func (b *Board) Parse(pgn string) error {
var ( var (
moves = strings.Split(pgn, " ") moves = strings.Split(strings.Trim(pgn, " "), " ")
err error err error
) )
for _, move := range moves { for _, move := range moves {
move = strings.Trim(move, " ")
if move == "" {
continue
}
if err = b.Move(move); err != nil { if err = b.Move(move); err != nil {
return err return err
} }

View File

@ -71,7 +71,7 @@ func tickGameStart(c *sn.Client) {
continue continue
} }
text := fmt.Sprintf("`%s`\n\n%s", b.AlgebraicNotation(), imgUrl) text := strings.Trim(fmt.Sprintf("%s\n\n%s", b.AlgebraicNotation(), imgUrl), " ")
var cId int var cId int
if cId, err = c.CreateComment(n.Item.Id, text); err != nil { if cId, err = c.CreateComment(n.Item.Id, text); err != nil {
log.Printf("error creating reply: %v\n", err) log.Printf("error creating reply: %v\n", err)
@ -130,8 +130,9 @@ func tickGameProgress(c *sn.Client) {
if item.User.Id == meId { if item.User.Id == meId {
continue continue
} }
move := strings.Trim(strings.ReplaceAll(item.Text, "@chess", ""), " ")
if err = b.Move(move); err != nil { moves := strings.Trim(strings.ReplaceAll(item.Text, "@chess", ""), " ")
if err = b.Parse(moves); err != nil {
log.Printf("error moving piece: %v: id=%d\n", err, item.Id) log.Printf("error moving piece: %v: id=%d\n", err, item.Id)
break break
} }
@ -142,7 +143,7 @@ func tickGameProgress(c *sn.Client) {
continue continue
} }
if err = b.Move(move); err != nil { if err = b.Parse(move); err != nil {
log.Printf("error moving piece: %v: id=%d\n", err, n.Item.ParentId) log.Printf("error moving piece: %v: id=%d\n", err, n.Item.ParentId)
continue continue
} }