Exemplo n.º 1
0
//GetAllSlots returns a list of all occupied slots in the lobby
func (lobby *Lobby) ShuffleAllSlots() error {
	if lobby.GetPlayerNumber() == lobby.RequiredPlayers() {
		return errors.New("Cannot shuffle a full lobby")
	}

	lobby.Lock()
	lobby.GetAllSlots()
	classes := format.GetClasses(lobby.Type)
	swapClass := make(map[string]bool)

	for _, className := range classes {
		swapClass[className] = rand.Intn(2) == 1
	}

	err := db.DB.Delete(&LobbySlot{}, "lobby_id = ?", lobby.ID).Error
	if err != nil {
		return err
	}

	numClasses := len(classes)
	for i := range lobby.Slots {
		slot := &lobby.Slots[i]
		if swapClass[classes[slot.Slot%numClasses]] {
			slot.Slot = (slot.Slot + numClasses) % (2 * numClasses)
		}
		if err = db.DB.Create(&slot).Error; err != nil {
			return err
		}
	}
	lobby.Unlock()

	lobby.OnChange(true)
	return nil
}
Exemplo n.º 2
0
func DecorateLobbyData(lobby *Lobby, playerInfo bool) LobbyData {
	lobbyData := LobbyData{
		ID:                lobby.ID,
		Mode:              lobby.Mode,
		Type:              format.FriendlyNamesMap[lobby.Type],
		Players:           lobby.GetPlayerNumber(),
		Map:               lobby.MapName,
		League:            lobby.League,
		Mumble:            lobby.Mumble,
		Discord:           lobby.Discord,
		TwitchChannel:     lobby.TwitchChannel,
		TwitchRestriction: lobby.TwitchRestriction.String(),
		RegionLock:        lobby.RegionLock,
		RedTeamName:       lobby.RedTeamName,
		BluTeamName:       lobby.BluTeamName,

		SteamGroup: lobby.PlayerWhitelist,
	}

	lobbyData.Region.Name = lobby.RegionName
	lobbyData.Region.Code = lobby.RegionCode

	classList := format.GetClasses(lobby.Type)

	classes := make([]ClassDetails, len(classList))
	lobbyData.MaxPlayers = format.NumberOfClassesMap[lobby.Type] * 2

	for slot, className := range classList {
		class := ClassDetails{
			Red:   decorateSlotDetails(lobby, slot, playerInfo),
			Blu:   decorateSlotDetails(lobby, slot+format.NumberOfClassesMap[lobby.Type], playerInfo),
			Class: className,
		}

		classes[slot] = class
	}

	lobbyData.Classes = classes
	lobbyData.WhitelistID = lobby.Whitelist

	if !playerInfo {
		return lobbyData
	}

	if lobby.CreatedBySteamID != "" { // == "" during tests
		leader, _ := player.GetPlayerBySteamID(lobby.CreatedBySteamID)
		leader.SetPlayerSummary()
		lobbyData.Leader = *leader
	}

	lobbyData.CreatedAt = lobby.CreatedAt.Unix()
	lobbyData.State = int(lobby.State)

	var specIDs []uint
	db.DB.Table("spectators_players_lobbies").Where("lobby_id = ?", lobby.ID).Pluck("player_id", &specIDs)

	spectators := make([]SpecDetails, len(specIDs))

	for i, spectatorID := range specIDs {
		specPlayer, _ := player.GetPlayerByID(spectatorID)

		specJs := SpecDetails{
			Name:    specPlayer.Alias(),
			SteamID: specPlayer.SteamID,
		}

		spectators[i] = specJs
	}

	lobbyData.Spectators = spectators

	return lobbyData
}