Progress game via replies
This commit is contained in:
parent
7eea9d932f
commit
677c50d007
28
db/db.go
28
db/db.go
|
@ -2,6 +2,7 @@ package db
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
sn "github.com/ekzyis/snappy"
|
||||
|
@ -64,3 +65,30 @@ func InsertItem(item *sn.Item) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetThread(id int) ([]sn.Item, error) {
|
||||
var (
|
||||
items []sn.Item
|
||||
err error
|
||||
)
|
||||
|
||||
var item sn.Item
|
||||
item.ParentId = id
|
||||
|
||||
for item.ParentId > 0 {
|
||||
// TODO: can't select created_at because sqlite3 doesn't support timestamps natively
|
||||
// see https://github.com/mattn/go-sqlite3/issues/142
|
||||
if err = db.QueryRow(
|
||||
`SELECT id, user_id, text, COALESCE(parent_id, 0) FROM items WHERE id = ?`, item.ParentId).
|
||||
Scan(&item.Id, &item.User.Id, &item.Text, &item.ParentId); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, errors.New("item not found in db")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
items = append([]sn.Item{item}, items...)
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
|
108
main.go
108
main.go
|
@ -20,9 +20,8 @@ func main() {
|
|||
|
||||
for {
|
||||
tickGameStart(c)
|
||||
// TODO: implement game progress
|
||||
// tickGameProgress(c)
|
||||
time.Sleep(1 * time.Minute)
|
||||
tickGameProgress(c)
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,11 +41,11 @@ func tickGameStart(c *sn.Client) {
|
|||
for _, n := range mentions {
|
||||
|
||||
if exists, err := db.ItemHasReply(n.Item.Id, meId); err != nil {
|
||||
log.Printf("error during reply check: %v\n", err)
|
||||
log.Printf("error during game start check: %v\n", err)
|
||||
continue
|
||||
} else if exists {
|
||||
// TODO: check if move changed
|
||||
log.Printf("reply already exists: id=%d\n", n.Item.Id)
|
||||
log.Printf("game start already exists: id=%d\n", n.Item.Id)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -65,6 +64,12 @@ func tickGameStart(c *sn.Client) {
|
|||
continue
|
||||
}
|
||||
|
||||
n.Item.ParentId = 0
|
||||
if err = db.InsertItem(&n.Item); err != nil {
|
||||
log.Printf("error inserting item into db: %v: id=%d\n", err, n.Item.Id)
|
||||
continue
|
||||
}
|
||||
|
||||
var cId int
|
||||
parentId := n.Item.Id
|
||||
if cId, err = c.CreateComment(parentId, imgUrl); err != nil {
|
||||
|
@ -72,12 +77,6 @@ func tickGameStart(c *sn.Client) {
|
|||
continue
|
||||
}
|
||||
|
||||
n.Item.ParentId = 0
|
||||
if err = db.InsertItem(&n.Item); err != nil {
|
||||
log.Printf("error inserting item into db: %v: id=%d\n", err, n.Item.Id)
|
||||
continue
|
||||
}
|
||||
|
||||
var item *sn.Item
|
||||
if item, err = c.Item(cId); err != nil {
|
||||
log.Printf("error fetching item: %v: id=%d\n", cId)
|
||||
|
@ -91,3 +90,90 @@ func tickGameStart(c *sn.Client) {
|
|||
log.Printf("started new game: id=%d\n", n.Item.Id)
|
||||
}
|
||||
}
|
||||
|
||||
func tickGameProgress(c *sn.Client) {
|
||||
var (
|
||||
replies []sn.Notification
|
||||
err error
|
||||
)
|
||||
|
||||
if replies, err = c.Replies(); err != nil {
|
||||
log.Printf("replies error: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("fetched %d replies\n", len(replies))
|
||||
|
||||
for _, n := range replies {
|
||||
|
||||
if exists, err := db.ItemHasReply(n.Item.Id, meId); err != nil {
|
||||
log.Printf("error during game update check: %v\n", err)
|
||||
continue
|
||||
} else if exists {
|
||||
// TODO: check if move changed
|
||||
log.Printf("game update already exists: id=%d\n", n.Item.Id)
|
||||
continue
|
||||
}
|
||||
|
||||
var thread []sn.Item
|
||||
if thread, err = db.GetThread(n.Item.ParentId); err != nil {
|
||||
log.Printf("error fetching thread: %v: id=%d\n", err, n.Item.ParentId)
|
||||
continue
|
||||
}
|
||||
|
||||
b := chess.NewBoard()
|
||||
// TODO: better parsing of moves in replies using regexp for example or enforce a specific format
|
||||
// since players might include more than just the move in their replies
|
||||
move := strings.Trim(n.Item.Text, " ")
|
||||
for _, item := range thread {
|
||||
if item.User.Id == meId {
|
||||
continue
|
||||
}
|
||||
move := strings.Trim(strings.ReplaceAll(item.Text, "@chess", ""), " ")
|
||||
if err = b.Move(move); err != nil {
|
||||
log.Printf("error moving piece: %v: id=%d\n", err, item.Id)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// continue with next reply if the thread loop failed
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if err = b.Move(move); err != nil {
|
||||
log.Printf("error moving piece: %v: id=%d\n", err, n.Item.ParentId)
|
||||
continue
|
||||
}
|
||||
|
||||
img := b.Image()
|
||||
var imgUrl string
|
||||
if imgUrl, err = c.UploadImage(img); err != nil {
|
||||
log.Printf("error uploading image: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err = db.InsertItem(&n.Item); err != nil {
|
||||
log.Printf("error inserting item into db: %v: id=%d\n", err, n.Item.Id)
|
||||
continue
|
||||
}
|
||||
|
||||
var cId int
|
||||
if cId, err = c.CreateComment(n.Item.Id, imgUrl); err != nil {
|
||||
log.Printf("error creating reply: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
var item *sn.Item
|
||||
if item, err = c.Item(cId); err != nil {
|
||||
log.Printf("error fetching item: %v: id=%d\n", cId)
|
||||
continue
|
||||
}
|
||||
if err = db.InsertItem(item); err != nil {
|
||||
log.Printf("error inserting item into db: %v: id=%d\n", err, item.Id)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("continued game: move=%s id=%d\n", move, cId)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue