Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
a83ccdd054 | |||
|
d5723e7e17 | ||
ec8384f282 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
||||
!assets/*.png
|
||||
.env
|
||||
*.sqlite3
|
||||
chessbot
|
||||
|
13
Makefile
Normal file
13
Makefile
Normal file
@ -0,0 +1,13 @@
|
||||
.PHONY: build test
|
||||
|
||||
SOURCES := main.go $(shell find chess -name '*.go')
|
||||
|
||||
build: chessbot
|
||||
|
||||
chessbot: $(SOURCES)
|
||||
go build -o chessbot .
|
||||
|
||||
test:
|
||||
## -count=1 is used to disable cache
|
||||
## use -run <regexp> to only run tests that match <regexp>
|
||||
go test ./chess -v -count=1
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module github.com/ekzyis/chessbot
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/ekzyis/snappy v0.6.2
|
||||
github.com/ekzyis/snappy v0.7.0
|
||||
github.com/mattn/go-sqlite3 v1.14.23
|
||||
github.com/stretchr/testify v1.9.0
|
||||
golang.org/x/image v0.20.0
|
||||
|
4
go.sum
4
go.sum
@ -1,7 +1,7 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/ekzyis/snappy v0.6.2 h1:iOPgoS0cSUNk8leQJku0bBgnsha/+DcYUwTYqGLINY4=
|
||||
github.com/ekzyis/snappy v0.6.2/go.mod h1:UksYI0dU0+cnzz0LQjWB1P0QQP/ghx47e4atP99a5Lk=
|
||||
github.com/ekzyis/snappy v0.7.0 h1:RcFTUHdZFTBFOnh6cG9HCL/RPK6L13qdxDrjBNZFjEM=
|
||||
github.com/ekzyis/snappy v0.7.0/go.mod h1:UksYI0dU0+cnzz0LQjWB1P0QQP/ghx47e4atP99a5Lk=
|
||||
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
|
||||
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
49
main.go
49
main.go
@ -14,21 +14,54 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
c = sn.GetClient()
|
||||
// TODO: fetch our id from SN API
|
||||
// prod: 25176 | local: 21858
|
||||
meId = 25176
|
||||
c = sn.GetClient()
|
||||
me *sn.User
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
for {
|
||||
updateMe()
|
||||
tickGameStart(c)
|
||||
tickGameProgress(c)
|
||||
time.Sleep(15 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func updateMe() {
|
||||
var (
|
||||
oldMe *sn.User
|
||||
err error
|
||||
warnThreshold = 100
|
||||
)
|
||||
|
||||
maybeWarn := func() {
|
||||
if me.Privates.Sats < warnThreshold {
|
||||
log.Printf("~~~ warning: low balance ~~~\n")
|
||||
}
|
||||
}
|
||||
|
||||
if me == nil {
|
||||
// make sure first update is successful
|
||||
if me, err = c.Me(); err != nil {
|
||||
log.Fatalf("failed to fetch me: %v\n", err)
|
||||
}
|
||||
log.Printf("fetched me: id=%d name=%s balance=%d\n", me.Id, me.Name, me.Privates.Sats)
|
||||
maybeWarn()
|
||||
return
|
||||
}
|
||||
|
||||
oldMe = me
|
||||
if me, err = c.Me(); err != nil {
|
||||
log.Printf("failed to update me: %v\n", err)
|
||||
me = oldMe
|
||||
} else {
|
||||
log.Printf("updated me. balance: %d\n", me.Privates.Sats)
|
||||
}
|
||||
|
||||
maybeWarn()
|
||||
}
|
||||
|
||||
func tickGameStart(c *sn.Client) {
|
||||
var (
|
||||
mentions []sn.Notification
|
||||
@ -98,7 +131,7 @@ func tickGameProgress(c *sn.Client) {
|
||||
if parent, err := c.Item(n.Item.ParentId); err != nil {
|
||||
log.Printf("failed to fetch parent %d of %d\n", n.Item.ParentId, n.Item.Id)
|
||||
continue
|
||||
} else if parent.User.Id != meId {
|
||||
} else if parent.User.Id != me.Id {
|
||||
log.Printf("ignoring nested reply %d\n", n.Item.Id)
|
||||
continue
|
||||
}
|
||||
@ -183,7 +216,7 @@ func handleGameProgress(req *sn.Item) error {
|
||||
}
|
||||
|
||||
for _, item := range thread {
|
||||
if item.User.Id == meId {
|
||||
if item.User.Id == me.Id {
|
||||
continue
|
||||
}
|
||||
|
||||
@ -285,6 +318,8 @@ func parseGameStart(input string) (string, error) {
|
||||
}
|
||||
|
||||
func parseGameProgress(input string) (string, error) {
|
||||
input = strings.Trim(input, " ")
|
||||
|
||||
lines := strings.Split(input, "\n")
|
||||
words := strings.Split(input, " ")
|
||||
|
||||
@ -312,5 +347,5 @@ func isRecent(t time.Time) bool {
|
||||
}
|
||||
|
||||
func alreadyHandled(id int) (bool, error) {
|
||||
return db.ItemHasReply(id, meId)
|
||||
return db.ItemHasReply(id, me.Id)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user