Use first line that starts with @chess to start game
This commit is contained in:
parent
fe82dee3bb
commit
bf481bc452
18
main.go
18
main.go
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue