-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
executable file
·106 lines (97 loc) · 2.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"github.com/BurntSushi/toml"
"github.com/dolegi/uci"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
type Config struct {
Token string // done
Botname string // done
Url string // done
Engine struct {
Path string // done
Options struct {
Contempt int // done
Threads int // done
Hash int // done
MoveOverhead int // done
}
Go struct {
Nodes int // done
Depth int // done
Movetime int // done
}
}
Network struct {
Latency int // done
}
Challenge struct {
Variants []string // done
Speeds []string // done
Modes []string // done
}
Game struct {
Aborttime int
}
}
var conf Config
var white bool = true
var whiteFirst bool = true
func main() {
if len(os.Args) < 2 {
log.Fatal("Missing path to config file. Usage `lichess-bot path/to/config`")
}
configFile, err := os.Open(os.Args[1])
if err != nil {
log.Fatal("Failed to open config.toml", err)
}
configData, _ := ioutil.ReadAll(configFile)
configFile.Close()
if _, err := toml.Decode(string(configData), &conf); err != nil {
log.Fatal("Failed to decode config.toml", err)
}
log.Println(conf)
log.Printf("%v\n", getUsersStatus(conf.Botname))
if len(os.Args) >= 3 && os.Args[2] == "upgrade" {
resp := request("POST", "bot/account/upgrade")
if resp.StatusCode == http.StatusOK {
log.Println("Account upgraded to bot account")
} else {
body, _ := ioutil.ReadAll(resp.Body)
log.Println("Failed to upgrade account")
log.Println(string(body))
}
resp.Body.Close()
return
}
eng, err := uci.NewEngine(conf.Engine.Path)
if err != nil {
log.Fatal("Failed to create a new engine", err)
}
retries := 0
for _ = range time.Tick(50 * time.Millisecond) {
if eng.IsReady() {
break
}
if retries > 10 {
break
}
retries++
}
if retries > 10 {
log.Fatal("Failed to find ready engine. Max retries exceeded", retries)
}
eng.SetOption("Contempt", conf.Engine.Options.Contempt)
if conf.Engine.Options.Threads > 0 {
eng.SetOption("Threads", conf.Engine.Options.Threads)
}
if conf.Engine.Options.Hash > 0 {
eng.SetOption("Hash", conf.Engine.Options.Hash)
}
eng.SetOption("Move Overhead", conf.Engine.Options.MoveOverhead)
streamEvent(eng)
}