-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayers.go
211 lines (188 loc) · 6.29 KB
/
players.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"encoding/base64"
"encoding/json"
"log"
"os"
"path/filepath"
"runtime"
"github.com/BurntSushi/toml"
"github.com/swz-git/go-interface/flat"
"github.com/wailsapp/mimetype"
)
type PlayerJs struct {
Sort string `json:"sort"`
Player json.RawMessage `json:"player"`
}
func (playerJs PlayerJs) ToPlayer() Player {
switch playerJs.Sort {
case "rlbot":
var correct BotInfo
if err := json.Unmarshal([]byte(playerJs.Player), &correct); err != nil {
log.Fatal("unable to unmarshal PlayerJs")
}
return correct
case "psyonix":
var correct PsyonixBotInfo
if err := json.Unmarshal([]byte(playerJs.Player), &correct); err != nil {
log.Fatal("unable to unmarshal PlayerJs")
}
return correct
case "human":
var correct HumanInfo
if err := json.Unmarshal([]byte(playerJs.Player), &correct); err != nil {
log.Fatal("unable to unmarshal PlayerJs")
}
return correct
}
log.Println("ERROR: invalid sort field in PlayerJs")
return PsyonixBotInfo{}
}
type Player interface {
ToPlayerConfig(team uint32) *flat.PlayerConfigurationT
}
type PsyonixBotInfo struct {
// Beginner: 0, Rookie: 1, Pro: 2, AllStar: 3
Skill byte `json:"skill"`
}
func (info PsyonixBotInfo) ToPlayerConfig(team uint32) *flat.PlayerConfigurationT {
return &flat.PlayerConfigurationT{
Variety: &flat.PlayerClassT{
Type: flat.PlayerClassPsyonix,
Value: &flat.PsyonixT{
BotSkill: flat.PsyonixSkill(info.Skill),
},
},
Name: "Psyonix Bot",
Team: team,
RootDir: "",
RunCommand: "",
Loadout: &flat.PlayerLoadoutT{},
SpawnId: 0,
Hivemind: false,
}
}
type HumanInfo struct{}
func (info HumanInfo) ToPlayerConfig(team uint32) *flat.PlayerConfigurationT {
return &flat.PlayerConfigurationT{
Variety: &flat.PlayerClassT{
Type: flat.PlayerClassHuman,
Value: &flat.HumanT{},
},
Name: "Human",
Team: team,
RootDir: "",
RunCommand: "",
Loadout: &flat.PlayerLoadoutT{},
SpawnId: 0,
Hivemind: false,
}
}
type BotInfo struct {
Config BotConfig `json:"config"`
TomlPath string `json:"tomlPath"`
}
func (botInfo BotInfo) ToPlayerConfig(team uint32) *flat.PlayerConfigurationT {
var runCommand string
if runtime.GOOS == "windows" {
runCommand = botInfo.Config.Settings.RunCommand
} else if runtime.GOOS == "linux" {
runCommand = botInfo.Config.Settings.RunCommandLinux
}
return &flat.PlayerConfigurationT{
Variety: &flat.PlayerClassT{
Type: flat.PlayerClassRLBot,
Value: &flat.RLBotT{},
},
Name: botInfo.Config.Settings.Name,
AgentId: botInfo.Config.Settings.AgentId,
Team: team,
RootDir: botInfo.Config.Settings.RootDir,
RunCommand: runCommand,
// TODO: read player loadout file from LooksConfig
Loadout: &flat.PlayerLoadoutT{},
SpawnId: 0, // let core do this
Hivemind: botInfo.Config.Settings.Hivemind,
}
}
type BotConfig struct {
Settings BotSettings `toml:"settings" json:"settings"`
Details BotDetails `toml:"details" json:"details"`
}
type BotSettings struct {
// In-game name of the bot
Name string `toml:"name" json:"name"`
// A unique string identifying this type of bot, typically on the form "<developer>/<botname>"
AgentId string `toml:"agent_id" json:"agentId"`
// Path to looks.toml, describing the bots "loadout"
LooksConfig string `toml:"looks_config" json:"looksConfig"`
// Optional working dir of the bot
RootDir string `toml:"root_dir" json:"rootDir"`
// Path to the logo of the bot, if ignored, RLBot will look for logo.png
LogoFile string `toml:"logo_file" json:"logoFile"`
// The command RLBot will call to start your bot on Windows
RunCommand string `toml:"run_command" json:"runCommand"`
// The command RLBot will call to start your bot on Linux
// If not defined, RLBot may try to run your bot under wine
RunCommandLinux string `toml:"run_command_linux" json:"runCommandLinux"`
// If bot can handle multiple agents with one client
Hivemind bool `toml:"hivemind" json:"hivemind"`
}
type BotDetails struct {
// Short description of thebot
Description string `toml:"description" json:"description"`
// A fun fact about the bot
FunFact string `toml:"fun_fact" json:"funFact"`
// Link to the source code of the bot (if its available)
SourceLink string `toml:"source_link" json:"sourceLink"` // TODO: Rename this field to repo?
// Name(s) of the bot developer(s)
Developer string `toml:"developer" json:"developer"`
// Programming language the bot is written in.
// (RLGym for example is also valid even though it is written in Python)
Language string `toml:"language" json:"language"`
// ALL POSSIBLE TAGS: 1v1, teamplay, goalie, hoops, dropshot, snow-day, spike-rush, heatseaker, memebot
// NOTE: Only add the goalie tag if your bot only plays as a goalie; this directly contrasts with the teamplay tag!
// NOTE: Only add a tag for a special game mode if you bot properly supports it
Tags []string `toml:"tags" json:"tags"`
}
func (a *App) GetBots(paths []string) []BotInfo {
// TODO: Search recursive in paths
potentialConfigs := []string{}
for _, path := range paths {
new, err := recursiveFileSearch(path, "bot.toml")
if err != nil {
println("WARN: failed to search path: " + path)
continue
}
potentialConfigs = append(potentialConfigs, new...)
}
infos := []BotInfo{}
for _, potentialConfigPath := range potentialConfigs {
data, err := os.ReadFile(potentialConfigPath)
if err != nil {
println("WARN: skipping config, couldn't read config at " + potentialConfigPath)
continue
}
var conf BotConfig
toml.Decode(string(data), &conf)
// make location path relative to parent of bot.toml
conf.Settings.RootDir = filepath.Join(filepath.Dir(potentialConfigPath), conf.Settings.RootDir)
// Read logo file and convert it to data url so the frontend can use it
if conf.Settings.LogoFile != "" {
conf.Settings.LogoFile = filepath.Join(conf.Settings.RootDir, conf.Settings.LogoFile)
logo_data, err := os.ReadFile(conf.Settings.LogoFile)
if err != nil {
println("WARN: failed to read logo file at " + conf.Settings.LogoFile)
} else {
mtype := mimetype.Detect(logo_data)
b64data := base64.StdEncoding.EncodeToString(logo_data)
conf.Settings.LogoFile = "data:" + mtype.String() + ";base64," + b64data
}
}
infos = append(infos, BotInfo{
Config: conf,
TomlPath: potentialConfigPath,
})
}
return infos
}