// This function is called from the message handler to parse the first message for every new connection. // It check for existing user in the DB and logs him if the password is correct. // If the user is new he is initiated and a new home planet nad solar system are generated. func login(ws *websocket.Conn) (*Client, response.Responser, error) { player, twitter, err := authenticate(ws) if err != nil { return nil, response.NewLoginFailed(), err } client := NewClient(ws, player, twitter) homePlanetEntity, err := entities.Get(player.HomePlanet) if err != nil { return nil, nil, errors.New("Player's home planet is missing!") } homePlanet := homePlanetEntity.(*entities.Planet) loginSuccess := response.NewLoginSuccess(player, homePlanet) planetEntities := entities.Find("planet.*") planets := make([]*entities.Planet, 0, len(planetEntities)) sites := make([]voronoi.Vertex, 0, len(planetEntities)) x0, xn, y0, yn := 0.0, 0.0, 0.0, 0.0 for i, planetEntity := range planetEntities { planets = append(planets, planetEntity.(*entities.Planet)) if x0 > planets[i].Position.X { x0 = planets[i].Position.X } if xn < planets[i].Position.X { xn = planets[i].Position.X } if y0 > planets[i].Position.Y { y0 = planets[i].Position.Y } if yn < planets[i].Position.Y { yn = planets[i].Position.Y } sites = append(sites, voronoi.Vertex{planets[i].Position.X, planets[i].Position.Y}) } bbox := voronoi.NewBBox(x0, xn, y0, yn) response.Diagram = voronoi.ComputeDiagram(sites, bbox, true) return client, loginSuccess, nil }
// New generates a Voronoi diagram, relaxed by Lloyd's algorithm func New(w, h float64, c, r int) *Diagram { bbox := voronoi.NewBBox(0, w, 0, h) sites := utils.RandomSites(bbox, c) // Compute voronoi diagram. d := voronoi.ComputeDiagram(sites, bbox, true) // Max number of iterations is 16 if r > 16 { r = 16 } // Relax using Lloyd's algorithm for i := 0; i < r; i++ { sites = utils.LloydRelaxation(d.Cells) d = voronoi.ComputeDiagram(sites, bbox, true) } center := voronoi.Vertex{float64(w / 2), float64(h / 2)} return &Diagram{d, center} }