Allow creating a local BEI client
This commit is contained in:
parent
845da8db8e
commit
93fba875ad
5 changed files with 30 additions and 17 deletions
21
bot/bei.go
21
bot/bei.go
|
@ -28,17 +28,28 @@ func NewBEIClient(address string, quiet bool) *BEIClient {
|
|||
return c
|
||||
}
|
||||
|
||||
func NewLocalBEIClient(conn net.Conn, quiet bool) *BEIClient {
|
||||
c := &BEIClient{
|
||||
conn: conn,
|
||||
quiet: quiet,
|
||||
started: time.Now(),
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *BEIClient) Connect() error {
|
||||
if c.terminated {
|
||||
return nil
|
||||
}
|
||||
|
||||
conn, err := net.Dial("tcp", c.address)
|
||||
if err != nil {
|
||||
return err
|
||||
if c.conn == nil {
|
||||
conn, err := net.Dial("tcp", c.address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.connected = true
|
||||
c.conn = conn
|
||||
}
|
||||
c.connected = true
|
||||
c.conn = conn
|
||||
|
||||
c.conn.Write([]byte("bei\n"))
|
||||
buf, err := c.readLine()
|
||||
|
|
|
@ -59,6 +59,7 @@ func NewClient(beiAddress string, serverAddress string, username string, passwor
|
|||
if beiAddress != "" {
|
||||
c.beiClient = NewBEIClient(beiAddress, false)
|
||||
}
|
||||
go c.handleEvents()
|
||||
go c.handleTimeout()
|
||||
return c
|
||||
}
|
||||
|
@ -67,6 +68,7 @@ func NewLocalClient(conn net.Conn, serverAddress string, username string, passwo
|
|||
c := NewClient("", serverAddress, username, password, points, variant)
|
||||
c.beiClient = beiClient
|
||||
c.connecting = true
|
||||
go c.handleEvents()
|
||||
go c.connectTCP(conn)
|
||||
return c
|
||||
}
|
||||
|
@ -89,7 +91,7 @@ func (c *Client) logIn() []byte {
|
|||
if c.Username != "" && c.Password != "" {
|
||||
loginInfo = fmt.Sprintf("%s %s", strings.ReplaceAll(c.Username, " ", "_"), strings.ReplaceAll(c.Password, " ", "_"))
|
||||
}
|
||||
return []byte(fmt.Sprintf("lj bgammon-tabula-bot %s\nset autoplay 0\n", loginInfo))
|
||||
return []byte(fmt.Sprintf("lj bgammon-bei-bot %s\nset autoplay 0\n", loginInfo))
|
||||
}
|
||||
|
||||
func (c *Client) LoggedIn() bool {
|
||||
|
@ -331,7 +333,7 @@ func (c *Client) handleTimeout() {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Client) HandleEvents() {
|
||||
func (c *Client) handleEvents() {
|
||||
for e := range c.Events {
|
||||
switch ev := e.(type) {
|
||||
case *bgammon.EventWelcome:
|
||||
|
|
6
go.mod
6
go.mod
|
@ -4,8 +4,8 @@ go 1.17
|
|||
|
||||
require (
|
||||
code.rocket9labs.com/tslocum/bei v0.0.0-20240108012722-6db380cc190b
|
||||
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240320161747-3e8ec61f3bfb
|
||||
nhooyr.io/websocket v1.8.10
|
||||
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240419175805-962ec980f0d0
|
||||
nhooyr.io/websocket v1.8.11
|
||||
)
|
||||
|
||||
require code.rocket9labs.com/tslocum/tabula v0.0.0-20240207195947-691c7a5d5265 // indirect
|
||||
require code.rocket9labs.com/tslocum/tabula v0.0.0-20240422202348-09cfc96fcfc9 // indirect
|
||||
|
|
12
go.sum
12
go.sum
|
@ -1,8 +1,8 @@
|
|||
code.rocket9labs.com/tslocum/bei v0.0.0-20240108012722-6db380cc190b h1:Y0a14Kf/hSYepSmp4ZfDeE4CZZGBGBS97CNjCbKJm0c=
|
||||
code.rocket9labs.com/tslocum/bei v0.0.0-20240108012722-6db380cc190b/go.mod h1:tS60/VNAJphKvDBkSLQhKALa15msIAuWWfEKNc4oFZc=
|
||||
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240320161747-3e8ec61f3bfb h1:s6SHuRzqZzqoB9wK0+Ai9wY5X59zkifJIviaWwmhlO4=
|
||||
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240320161747-3e8ec61f3bfb/go.mod h1:NGFAKlapNeIriQfrrayRBAv2jOuxHhhJH3zokKBJ/Gg=
|
||||
code.rocket9labs.com/tslocum/tabula v0.0.0-20240207195947-691c7a5d5265 h1:B9R/SFVUAOut2o8rD7bc1fsa92eQdDKzxBFj7oxhDEc=
|
||||
code.rocket9labs.com/tslocum/tabula v0.0.0-20240207195947-691c7a5d5265/go.mod h1:WEJXESKXqrMFLAArikQ79lpRibNeeE1C0VruxXYMF5M=
|
||||
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
|
||||
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
|
||||
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240419175805-962ec980f0d0 h1:FnvEy3QkAy/8ioIxTyvF2VZnzh+anDN8USKNKhPXj8E=
|
||||
code.rocket9labs.com/tslocum/bgammon v0.0.0-20240419175805-962ec980f0d0/go.mod h1:NGFAKlapNeIriQfrrayRBAv2jOuxHhhJH3zokKBJ/Gg=
|
||||
code.rocket9labs.com/tslocum/tabula v0.0.0-20240422202348-09cfc96fcfc9 h1:9wtELMiDrOEsgRPcumaSkdVOBGxr1/KAcUii/rmpwmU=
|
||||
code.rocket9labs.com/tslocum/tabula v0.0.0-20240422202348-09cfc96fcfc9/go.mod h1:WEJXESKXqrMFLAArikQ79lpRibNeeE1C0VruxXYMF5M=
|
||||
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
|
||||
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
|
||||
|
|
2
main.go
2
main.go
|
@ -47,5 +47,5 @@ func main() {
|
|||
c := bot.NewClient(beiAddress, serverAddress, username, password, points, int8(variant))
|
||||
|
||||
go c.Connect()
|
||||
c.HandleEvents()
|
||||
select {}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue