Use first line that starts with @chess to start game

This commit is contained in:
ekzyis 2024-09-26 18:06:00 +02:00
parent fe82dee3bb
commit bf481bc452
1 changed files with 17 additions and 1 deletions

18
main.go
View File

@ -102,13 +102,17 @@ func tickGameProgress(c *sn.Client) {
func handleGameStart(req *sn.Item) error {
var (
move = strings.Trim(strings.ReplaceAll(req.Text, "@chess", ""), " ")
move string
b *chess.Board
imgUrl string
res string
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.
// We set parentId to 0 such that parent_id will be NULL in the db and not hit foreign key constraints.
req.ParentId = 0
@ -219,3 +223,15 @@ func createComment(parentId int, text string) (*sn.Item, error) {
return comment, nil
}
func parseGameStart(input string) (string, error) {
for _, line := range strings.Split(input, "\n") {
if !strings.Contains(line, "@chess") {
continue
}
return strings.Trim(strings.ReplaceAll(line, "@chess", ""), " "), nil
}
return "", errors.New("failed to parse game start request")
}