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 {
|
func handleGameStart(req *sn.Item) error {
|
||||||
var (
|
var (
|
||||||
move = strings.Trim(strings.ReplaceAll(req.Text, "@chess", ""), " ")
|
move string
|
||||||
b *chess.Board
|
b *chess.Board
|
||||||
imgUrl string
|
imgUrl string
|
||||||
res string
|
res string
|
||||||
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
|
||||||
|
@ -219,3 +223,15 @@ func createComment(parentId int, text string) (*sn.Item, error) {
|
||||||
|
|
||||||
return comment, nil
|
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