Beispiel #1
0
// Send usage data using ElasticSearch's bulk load UDP API
func (m *Usage) Send() {
	conn, err := net.Dial("udp", elasticSearchConnString)
	if err != nil {
		glog.Warningf("Couldn't dial: %s", err.Error())
	}
	defer conn.Close()

	coord, err := json.Marshal(map[string]interface{}{
		"index": map[string]string{
			"_index": "microcosm",
			"_type":  "log",
			"_id":    uuid.NewRandom().String(),
		},
	})
	if err != nil {
		glog.Warningf("Failed to marshal log coord: %s", err.Error())
	}
	usage, err := json.Marshal(m)
	if err != nil {
		glog.Warningf("Failed to marshal usage: %s", err.Error())
	}

	payload := fmt.Sprintf("%s\n%s\n", string(coord), string(usage))

	_, err = conn.Write([]byte(payload))
	if err != nil {
		glog.Warningf("Couldn't write usage to conn: %s", err.Error())
	}
}
Beispiel #2
0
// NewUser creates a new user with a new Gopher to manage the user's new ws
// connection.
func NewUser(ctx log.Interface, ws *websocket.Conn) User {
	id := uuid.NewRandom().String()[:3]

	return User{
		ID:     id,
		Gopher: NewGopher(id, RandomCoordinates(boardSize)),
		Log:    ctx.WithField("module", "User"),
		send:   make(chan []byte, 256),
		ws:     ws,
	}
}
Beispiel #3
0
// NewShot returns a new shot from a given Gopher. The location is set to the
// middle of the gopher and is fired at the speed of the gopher plus the shot's
// own speed in the direction of the angle of the Gopher.
func NewShot(u *User, g Gopher) Shot {
	posx := g.Position.X + math.Cos(g.Angle)*halfGopherSize
	posy := g.Position.Y + math.Sin(g.Angle)*halfGopherSize

	velx := g.Velocity.X + math.Cos(g.Angle)*shotSpeed
	vely := g.Velocity.Y + math.Sin(g.Angle)*shotSpeed

	return Shot{
		ID:         uuid.NewRandom().String(),
		Gopher:     g.UserID,
		User:       u,
		Entity:     NewEntity(thrustAcceleration, posx, posy, velx, vely, g.Angle),
		Lifecycles: int64(defaultLifeCycles),
	}
}
Beispiel #4
0
// Return uuid string.
func uid() string {
	return uuid.NewRandom().String()
}
Beispiel #5
0
// NewGameID creates a new game id to be used.
func NewGameID() string {
	return uuid.NewRandom().String()[0:8]
}