Only reply on successful parse
This commit is contained in:
parent
d7645edea9
commit
f3cd10cbb8
57
main.go
57
main.go
|
@ -53,6 +53,14 @@ func tickGameStart(c *sn.Client) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = handleGameStart(&n.Item); err != nil {
|
if err = handleGameStart(&n.Item); err != nil {
|
||||||
|
|
||||||
|
// don't reply to mentions that we failed to parse as a game start
|
||||||
|
// to support unrelated mentions
|
||||||
|
if err.Error() == "failed to parse game start" {
|
||||||
|
log.Printf("ignoring error for item %d: %v\n", n.Item.Id, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if _, err2 := createComment(n.Item.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
|
if _, err2 := createComment(n.Item.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
|
||||||
log.Printf("failed to reply with error to item %d: %v\n", n.Item.Id, err2)
|
log.Printf("failed to reply with error to item %d: %v\n", n.Item.Id, err2)
|
||||||
} else {
|
} else {
|
||||||
|
@ -89,6 +97,12 @@ func tickGameProgress(c *sn.Client) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = handleGameProgress(&n.Item); err != nil {
|
if err = handleGameProgress(&n.Item); err != nil {
|
||||||
|
|
||||||
|
if err.Error() == "failed to parse game update" {
|
||||||
|
log.Printf("ignoring error for item %d: %v\n", n.Item.Id, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if _, err2 := createComment(n.Item.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
|
if _, err2 := createComment(n.Item.Id, fmt.Sprintf("`%v`", err)); err2 != nil {
|
||||||
log.Printf("failed to reply with error to item %d: %v\n", n.Item.Id, err2)
|
log.Printf("failed to reply with error to item %d: %v\n", n.Item.Id, err2)
|
||||||
} else {
|
} else {
|
||||||
|
@ -109,10 +123,6 @@ func handleGameStart(req *sn.Item) error {
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if move, err = parseGameStart(req.Text); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Immediately save game start request to db so we can store our reply to it in case of error.
|
// Immediately save game start request to db so we can store our reply to it in case of error.
|
||||||
// We set parentId to 0 such that parent_id will be NULL in the db and not hit foreign key constraints.
|
// We set parentId to 0 such that parent_id will be NULL in the db and not hit foreign key constraints.
|
||||||
req.ParentId = 0
|
req.ParentId = 0
|
||||||
|
@ -121,6 +131,10 @@ func handleGameStart(req *sn.Item) error {
|
||||||
return fmt.Errorf("failed to insert item %d into db: %v\n", req.Id, err)
|
return fmt.Errorf("failed to insert item %d into db: %v\n", req.Id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if move, err = parseGameStart(req.Text); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// create board with initial move(s)
|
// create board with initial move(s)
|
||||||
if b, err = chess.NewGame(move); err != nil {
|
if b, err = chess.NewGame(move); err != nil {
|
||||||
if rand.Float32() > 0.99 {
|
if rand.Float32() > 0.99 {
|
||||||
|
@ -169,9 +183,10 @@ func handleGameProgress(req *sn.Item) error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: better parsing of moves in replies using regexp for example or enforce a specific format
|
var moves string
|
||||||
// since players might include more than just the move in their replies
|
if moves, err = parseGameProgress(item.Text); err != nil {
|
||||||
moves := strings.Trim(strings.ReplaceAll(item.Text, "@chess", ""), " ")
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// parse and execute existing moves
|
// parse and execute existing moves
|
||||||
if err = b.Parse(moves); err != nil {
|
if err = b.Parse(moves); err != nil {
|
||||||
|
@ -180,6 +195,11 @@ func handleGameProgress(req *sn.Item) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse and execute new move
|
// parse and execute new move
|
||||||
|
|
||||||
|
if move, err = parseGameProgress(move); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if err = b.Parse(move); err != nil {
|
if err = b.Parse(move); err != nil {
|
||||||
if rand.Float32() > 0.99 {
|
if rand.Float32() > 0.99 {
|
||||||
// easter egg error message
|
// easter egg error message
|
||||||
|
@ -225,6 +245,27 @@ func createComment(parentId int, text string) (*sn.Item, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseGameStart(input string) (string, error) {
|
func parseGameStart(input string) (string, error) {
|
||||||
|
for _, line := range strings.Split(input, "\n") {
|
||||||
|
line = strings.Trim(line, " ")
|
||||||
|
|
||||||
|
if !strings.HasPrefix(line, "@chess") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Trim(strings.ReplaceAll(line, "@chess", ""), " "), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", errors.New("failed to parse game start")
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseGameProgress(input string) (string, error) {
|
||||||
|
lines := strings.Split(input, "\n")
|
||||||
|
words := strings.Split(input, " ")
|
||||||
|
|
||||||
|
if len(lines) == 1 && len(words) == 1 {
|
||||||
|
return strings.Trim(input, " "), nil
|
||||||
|
}
|
||||||
|
|
||||||
for _, line := range strings.Split(input, "\n") {
|
for _, line := range strings.Split(input, "\n") {
|
||||||
if !strings.Contains(line, "@chess") {
|
if !strings.Contains(line, "@chess") {
|
||||||
continue
|
continue
|
||||||
|
@ -233,5 +274,5 @@ func parseGameStart(input string) (string, error) {
|
||||||
return strings.Trim(strings.ReplaceAll(line, "@chess", ""), " "), nil
|
return strings.Trim(strings.ReplaceAll(line, "@chess", ""), " "), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("failed to parse game start request")
|
return "", errors.New("failed to parse game update")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue