Compare commits
No commits in common. "develop" and "v0.2.1" have entirely different histories.
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,4 +2,3 @@
|
|||||||
!assets/*.png
|
!assets/*.png
|
||||||
.env
|
.env
|
||||||
*.sqlite3
|
*.sqlite3
|
||||||
chessbot
|
|
||||||
|
|||||||
13
Makefile
13
Makefile
@ -1,13 +0,0 @@
|
|||||||
.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
|
|
||||||
@ -12,7 +12,6 @@ import (
|
|||||||
|
|
||||||
"golang.org/x/image/font"
|
"golang.org/x/image/font"
|
||||||
"golang.org/x/image/font/basicfont"
|
"golang.org/x/image/font/basicfont"
|
||||||
"golang.org/x/image/font/opentype"
|
|
||||||
"golang.org/x/image/math/fixed"
|
"golang.org/x/image/math/fixed"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -198,17 +197,11 @@ func drawCoordinate(img *image.RGBA, x, y int, flipped bool) {
|
|||||||
} else if !flipped {
|
} else if !flipped {
|
||||||
color = Light
|
color = Light
|
||||||
}
|
}
|
||||||
|
// TODO: use SN font and make it bold
|
||||||
face, err := loadFontFace("lightningvolt.ttf")
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error loading font: %v\n", err)
|
|
||||||
face = basicfont.Face7x13
|
|
||||||
}
|
|
||||||
|
|
||||||
d := &font.Drawer{
|
d := &font.Drawer{
|
||||||
Dst: img,
|
Dst: img,
|
||||||
Src: image.NewUniform(color),
|
Src: image.NewUniform(color),
|
||||||
Face: face,
|
Face: basicfont.Face7x13,
|
||||||
Dot: origin,
|
Dot: origin,
|
||||||
}
|
}
|
||||||
d.DrawString(s)
|
d.DrawString(s)
|
||||||
@ -221,36 +214,11 @@ func drawCoordinate(img *image.RGBA, x, y int, flipped bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if row != "" {
|
if row != "" {
|
||||||
origin = fixed.P((x+1)*128-20, y*128+23)
|
origin = fixed.P((x+1)*128-12, y*128+15)
|
||||||
drawString(row, origin)
|
drawString(row, origin)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadFontFace(path string) (font.Face, error) {
|
|
||||||
fontBytes, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ttfFont, err := opentype.Parse(fontBytes)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
faceOptions := &opentype.FaceOptions{
|
|
||||||
Size: 32,
|
|
||||||
DPI: 72,
|
|
||||||
Hinting: font.HintingNone,
|
|
||||||
}
|
|
||||||
|
|
||||||
fontFace, err := opentype.NewFace(ttfFont, faceOptions)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fontFace, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Board) SetPiece(name PieceName, color Color, position string) error {
|
func (b *Board) SetPiece(name PieceName, color Color, position string) error {
|
||||||
var (
|
var (
|
||||||
piece *Piece
|
piece *Piece
|
||||||
|
|||||||
3
go.mod
3
go.mod
@ -3,7 +3,7 @@ module github.com/ekzyis/chessbot
|
|||||||
go 1.23.0
|
go 1.23.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ekzyis/snappy v0.7.0
|
github.com/ekzyis/snappy v0.6.2
|
||||||
github.com/mattn/go-sqlite3 v1.14.23
|
github.com/mattn/go-sqlite3 v1.14.23
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.9.0
|
||||||
golang.org/x/image v0.20.0
|
golang.org/x/image v0.20.0
|
||||||
@ -12,7 +12,6 @@ require (
|
|||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
golang.org/x/text v0.18.0 // indirect
|
|
||||||
gopkg.in/guregu/null.v4 v4.0.0 // indirect
|
gopkg.in/guregu/null.v4 v4.0.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
6
go.sum
6
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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/ekzyis/snappy v0.7.0 h1:RcFTUHdZFTBFOnh6cG9HCL/RPK6L13qdxDrjBNZFjEM=
|
github.com/ekzyis/snappy v0.6.2 h1:iOPgoS0cSUNk8leQJku0bBgnsha/+DcYUwTYqGLINY4=
|
||||||
github.com/ekzyis/snappy v0.7.0/go.mod h1:UksYI0dU0+cnzz0LQjWB1P0QQP/ghx47e4atP99a5Lk=
|
github.com/ekzyis/snappy v0.6.2/go.mod h1:UksYI0dU0+cnzz0LQjWB1P0QQP/ghx47e4atP99a5Lk=
|
||||||
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
|
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/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
@ -10,8 +10,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
|
|||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw=
|
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw=
|
||||||
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM=
|
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM=
|
||||||
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
|
||||||
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/guregu/null.v4 v4.0.0 h1:1Wm3S1WEA2I26Kq+6vcW+w0gcDo44YKYD7YIEJNHDjg=
|
gopkg.in/guregu/null.v4 v4.0.0 h1:1Wm3S1WEA2I26Kq+6vcW+w0gcDo44YKYD7YIEJNHDjg=
|
||||||
|
|||||||
Binary file not shown.
49
main.go
49
main.go
@ -14,54 +14,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
c = sn.GetClient()
|
c = sn.GetClient()
|
||||||
me *sn.User
|
// TODO: fetch our id from SN API
|
||||||
|
// prod: 25176 | local: 21858
|
||||||
|
meId = 25176
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
updateMe()
|
|
||||||
tickGameStart(c)
|
tickGameStart(c)
|
||||||
tickGameProgress(c)
|
tickGameProgress(c)
|
||||||
time.Sleep(15 * time.Second)
|
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) {
|
func tickGameStart(c *sn.Client) {
|
||||||
var (
|
var (
|
||||||
mentions []sn.Notification
|
mentions []sn.Notification
|
||||||
@ -131,7 +98,7 @@ func tickGameProgress(c *sn.Client) {
|
|||||||
if parent, err := c.Item(n.Item.ParentId); err != nil {
|
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)
|
log.Printf("failed to fetch parent %d of %d\n", n.Item.ParentId, n.Item.Id)
|
||||||
continue
|
continue
|
||||||
} else if parent.User.Id != me.Id {
|
} else if parent.User.Id != meId {
|
||||||
log.Printf("ignoring nested reply %d\n", n.Item.Id)
|
log.Printf("ignoring nested reply %d\n", n.Item.Id)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -216,7 +183,7 @@ func handleGameProgress(req *sn.Item) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, item := range thread {
|
for _, item := range thread {
|
||||||
if item.User.Id == me.Id {
|
if item.User.Id == meId {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,8 +285,6 @@ func parseGameStart(input string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseGameProgress(input string) (string, error) {
|
func parseGameProgress(input string) (string, error) {
|
||||||
input = strings.Trim(input, " ")
|
|
||||||
|
|
||||||
lines := strings.Split(input, "\n")
|
lines := strings.Split(input, "\n")
|
||||||
words := strings.Split(input, " ")
|
words := strings.Split(input, " ")
|
||||||
|
|
||||||
@ -347,5 +312,5 @@ func isRecent(t time.Time) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func alreadyHandled(id int) (bool, error) {
|
func alreadyHandled(id int) (bool, error) {
|
||||||
return db.ItemHasReply(id, me.Id)
|
return db.ItemHasReply(id, meId)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user