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