made it so that everything can start in parallel too
This commit is contained in:
parent
e4bbd4a0f4
commit
819ba2f46d
36
main.go
36
main.go
@ -36,24 +36,28 @@ func main() {
|
||||
} else {
|
||||
gameName = flag.Arg(0)
|
||||
}
|
||||
// XXX: add TurnSpeed, WeaponDamage, WeaponSpeed
|
||||
var wg sync.WaitGroup
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < *botcount; i++ {
|
||||
r, err := NewRobot(
|
||||
*server,
|
||||
*port,
|
||||
gameName,
|
||||
fmt.Sprintf("%s%d", *botname, i),
|
||||
*hp,
|
||||
*speed,
|
||||
*acceleration,
|
||||
*weaponRadius,
|
||||
*scannerRadius,
|
||||
*fireRate,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
r := &robot{
|
||||
server: *server,
|
||||
port: *port,
|
||||
name: fmt.Sprintf("%s%d", *botname, i),
|
||||
game: GameParam{
|
||||
name: gameName,
|
||||
},
|
||||
// XXX: update with missing fields
|
||||
stats: StatsRequest{
|
||||
Hp: *hp,
|
||||
Speed: *speed,
|
||||
Acceleration: *acceleration,
|
||||
WeaponRadius: *weaponRadius,
|
||||
ScannerRadius: *scannerRadius,
|
||||
TurnSpeed: 50,
|
||||
FireRate: *fireRate,
|
||||
WeaponDamage: 50,
|
||||
WeaponSpeed: 50,
|
||||
},
|
||||
}
|
||||
log.Printf("my robot: %+v", r)
|
||||
go r.play()
|
||||
|
62
robot.go
62
robot.go
@ -9,64 +9,33 @@ import (
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
func connect() (*websocket.Conn, error) {
|
||||
func connect(server string, port int) (*websocket.Conn, error) {
|
||||
origin := "http://localhost/"
|
||||
url := fmt.Sprintf("ws://%s:%d/ws/", *server, *port)
|
||||
url := fmt.Sprintf("ws://%s:%d/ws/", server, port)
|
||||
return websocket.Dial(url, "", origin)
|
||||
}
|
||||
|
||||
type robot struct {
|
||||
server string
|
||||
port int
|
||||
ws *websocket.Conn
|
||||
game GameParam
|
||||
playerId string
|
||||
name string
|
||||
stats StatsRequest
|
||||
}
|
||||
|
||||
func NewRobot(
|
||||
server string,
|
||||
port int,
|
||||
gameName string,
|
||||
botName string,
|
||||
hp,
|
||||
speed,
|
||||
acceleration,
|
||||
weaponRadius,
|
||||
scannerRadius,
|
||||
fireRate int) (*robot, error) {
|
||||
ws, err := connect()
|
||||
func (r *robot) negociate() (err error) {
|
||||
log.Printf("trying to connect to game '%s'", r.game.name)
|
||||
r.ws, err = connect(r.server, r.port)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("connection failure: %s", err))
|
||||
return errors.New(fmt.Sprintf("connection failure: %s", err))
|
||||
}
|
||||
r := &robot{
|
||||
ws: ws,
|
||||
name: botName,
|
||||
}
|
||||
|
||||
// XXX: update with missing fields
|
||||
statsReq := StatsRequest{
|
||||
Hp: hp,
|
||||
Speed: speed,
|
||||
Acceleration: acceleration,
|
||||
WeaponRadius: weaponRadius,
|
||||
ScannerRadius: scannerRadius,
|
||||
TurnSpeed: 50,
|
||||
FireRate: fireRate,
|
||||
WeaponDamage: 50,
|
||||
WeaponSpeed: 50,
|
||||
}
|
||||
|
||||
err = r.negociate(statsReq, gameName)
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (r *robot) negociate(req StatsRequest, gameName string) (err error) {
|
||||
log.Printf("trying to connect to game '%s'", gameName)
|
||||
|
||||
err = websocket.JSON.Send(r.ws, struct {
|
||||
Id string `json:"id"`
|
||||
}{
|
||||
gameName,
|
||||
r.game.name,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@ -98,13 +67,12 @@ func (r *robot) negociate(req StatsRequest, gameName string) (err error) {
|
||||
if r.game.Type != "gameparam" {
|
||||
return errors.New("didn't receive a good gameparam")
|
||||
}
|
||||
r.game.name = gameName
|
||||
log.Printf("game parameters: %+v", r.game)
|
||||
|
||||
conf := ClientConfig{
|
||||
ID: gameName,
|
||||
ID: r.game.name,
|
||||
Stats: map[string]StatsRequest{
|
||||
r.name: req,
|
||||
r.name: r.stats,
|
||||
},
|
||||
}
|
||||
|
||||
@ -126,6 +94,12 @@ func (r *robot) negociate(req StatsRequest, gameName string) (err error) {
|
||||
|
||||
func (r *robot) play() {
|
||||
var err error
|
||||
err = r.negociate()
|
||||
if err != nil {
|
||||
log.Fatal("failed to negociate:", err)
|
||||
}
|
||||
|
||||
log.Printf("me: %+v", r)
|
||||
// TODO: var target govector.Point2d
|
||||
moveto := govector.Point2d{
|
||||
X: rand.Float32() * r.game.BoardSize.Width,
|
||||
|
Loading…
Reference in New Issue
Block a user