// Initialize the leaderboard func InitLeaderboard(board *leaderboard.Leaderboard) { log.Println("Initializing the leaderboard...") allPlayers := make(map[string]*leaderboard.Player) playerEntities := entities.Find("player.*") planetEntities := entities.Find("planet.*") for key, value := range cfg.Race { board.AddRace( key, value.Id, ) } for _, playerEntity := range playerEntities { player := playerEntity.(*entities.Player) leaderboardPlayer := &leaderboard.Player{ Username: player.Username, RaceId: player.RaceID, Planets: 0, } allPlayers[player.Username] = leaderboardPlayer board.Add(leaderboardPlayer) } for _, entity := range planetEntities { planet, ok := entity.(*entities.Planet) if !planet.HasOwner() || !ok { continue } player, _ := allPlayers[planet.Owner] if planet.IsHome { player.HomePlanet = planet.Name } player.Planets++ } board.Sort() board.RecountRacesPlanets() leaderBoard = board }
// 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 }
// Spawns missionary for all mission records found // in the database when the server is started func SpawnDbMissions() { for _, entity := range entities.Find("mission.*") { mission, ok := entity.(*entities.Mission) if !ok { log.Printf("Record %s does not seem to be a mission!?\n", mission.Key()) } sourceKey := fmt.Sprintf("planet.%s", mission.Source.Name) sourceEntity, err := entities.Get(sourceKey) if err != nil { log.Printf("Can't find planet %s for mission %s!?\n", sourceKey, mission.Key()) } source := sourceEntity.(*entities.Planet) mission.SetAreaSet(source.AreaSet()) log.Printf( "Spawning %s's mission from %s to %s...\n", mission.Player, mission.Source.Name, mission.Target.Name, ) go StartMissionary(mission) } }