Пример #1
0
func GetLobbyDataJSON(lobby models.Lobby) *simplejson.Json {
	lobbyJs := simplejson.New()
	lobbyJs.Set("id", lobby.ID)
	lobbyJs.Set("type", models.FormatMap[lobby.Type])
	lobbyJs.Set("createdAt", lobby.CreatedAt.Unix())
	lobbyJs.Set("players", lobby.GetPlayerNumber())
	lobbyJs.Set("map", lobby.MapName)
	classes := simplejson.New()

	var classMap = chelpers.FormatClassMap(lobby.Type)
	lobbyJs.Set("maxPlayers", len(classMap)*2)

	for className, slot := range classMap {
		class := simplejson.New()
		red := simplejson.New()
		blu := simplejson.New()

		steamid, name, ready := getSlotDetails(&lobby, slot)
		red.Set("steamid", steamid)
		red.Set("name", name)
		red.Set("ready", ready)

		steamid, name, ready = getSlotDetails(&lobby, slot+models.TypePlayerCount[lobby.Type])
		blu.Set("steamid", steamid)
		blu.Set("name", name)
		blu.Set("ready", ready)

		class.Set("red", red)
		class.Set("blu", blu)
		classes.Set(className, class)
	}
	lobbyJs.Set("classes", classes)

	return lobbyJs
}
Пример #2
0
func (Lobby) LobbySpectatorJoin(server *wsevent.Server, so *wsevent.Client, data []byte) []byte {
	reqerr := chelpers.FilterRequest(so, authority.AuthAction(0), true)

	if reqerr != nil {
		bytes, _ := json.Marshal(reqerr)
		return bytes
	}

	var args struct {
		Id *uint `json:"id"`
	}

	if err := chelpers.GetParams(data, &args); err != nil {
		return helpers.NewTPErrorFromError(err).Encode()
	}

	var lob *models.Lobby
	lob, tperr := models.GetLobbyById(*args.Id)

	if tperr != nil {
		return tperr.Encode()
	}

	player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
	if tperr != nil {
		return tperr.Encode()
	}

	var specSameLobby bool

	arr, tperr := player.GetSpectatingIds()
	if len(arr) != 0 {
		for _, id := range arr {
			if id == *args.Id {
				specSameLobby = true
				continue
			}

			lobby, _ := models.GetLobbyById(id)
			lobby.RemoveSpectator(player, true)

			server.RemoveClient(so.Id(), fmt.Sprintf("%d_public", id))
		}
	}

	// If the player is already in the lobby (either joined a slot or is spectating), don't add them.
	// Just Broadcast the lobby to them, so the frontend displays it.
	if id, _ := player.GetLobbyId(); id != *args.Id && !specSameLobby {
		tperr = lob.AddSpectator(player)

		if tperr != nil {
			return tperr.Encode()
		}

		chelpers.AfterLobbySpec(server, so, lob)
	}

	models.BroadcastLobbyToUser(lob, player.SteamId)
	return chelpers.EmptySuccessJS
}
Пример #3
0
func removeUnreadyPlayers(lobby *models.Lobby) {
	var players []*models.Player

	db.DB.Table("players").Joins("INNER JOIN lobby_slots ON lobby_slots.player_id = players.id").Where("lobby_slots.lobby_id = ? AND lobby_slots.ready = ?", lobby.ID, false).Find(&players)
	lobby.RemoveUnreadyPlayers(true)

	for _, player := range players {
		hooks.AfterLobbyLeave(lobby, player)
	}
}
Пример #4
0
func getSlotDetails(lobby *models.Lobby, slot int) (string, string, bool) {
	steamid := ""
	name := ""
	ready := false

	playerId, err := lobby.GetPlayerIdBySlot(slot)
	if err == nil {
		var player models.Player
		db.DB.First(&player, playerId)

		steamid = player.SteamId
		name = player.Name
		ready, _ = lobby.IsPlayerReady(&player)
	}
	return steamid, name, ready
}
Пример #5
0
func (Lobby) LobbySpectatorJoin(so *wsevent.Client, args struct {
	Id *uint `json:"id"`
}) interface{} {

	var lob *models.Lobby
	lob, tperr := models.GetLobbyByID(*args.Id)

	if tperr != nil {
		return tperr
	}

	player := chelpers.GetPlayerFromSocket(so.ID)
	var specSameLobby bool

	arr, tperr := player.GetSpectatingIds()
	if len(arr) != 0 {
		for _, id := range arr {
			if id == *args.Id {
				specSameLobby = true
				continue
			}
			//a socket should only spectate one lobby, remove socket from
			//any other lobby room
			//multiple sockets from one player can spectatte multiple lobbies
			socket.AuthServer.RemoveClient(so, fmt.Sprintf("%d_public", id))
		}
	}

	// If the player is already in the lobby (either joined a slot or is spectating), don't add them.
	// Just Broadcast the lobby to them, so the frontend displays it.
	if id, _ := player.GetLobbyID(false); id != *args.Id && !specSameLobby {
		tperr = lob.AddSpectator(player)

		if tperr != nil {
			return tperr
		}
	}

	hooks.AfterLobbySpec(socket.AuthServer, so, lob)
	models.BroadcastLobbyToUser(lob, player.SteamID)
	return chelpers.EmptySuccessJS
}
Пример #6
0
func LobbySpectatorJoin(so socketio.Socket) func(string) string {
	return chelpers.FilterRequest(so, lobbySpectatorJoinFilters,
		func(params map[string]interface{}) string {

			lobbyid := params["id"].(uint)

			player, tperr := models.GetPlayerBySteamId(chelpers.GetSteamId(so.Id()))
			if tperr != nil {
				bytes, _ := tperr.ErrorJSON().Encode()
				return string(bytes)
			}

			var lob *models.Lobby
			lob, tperr = models.GetLobbyById(uint(lobbyid))

			if tperr != nil {
				bytes, _ := tperr.ErrorJSON().Encode()
				return string(bytes)
			}

			if id, _ := player.GetLobbyId(); id != lobbyid {
				helpers.LockRecord(lob.ID, lob)
				tperr = lob.AddSpectator(player)
				helpers.UnlockRecord(lob.ID, lob)
			}

			bytes, _ := chelpers.BuildSuccessJSON(simplejson.New()).Encode()

			if tperr != nil {
				bytes, _ := tperr.ErrorJSON().Encode()
				return string(bytes)
			}

			chelpers.AfterLobbySpec(so, lob)
			models.BroadcastLobbyToUser(lob, player.SteamId)
			return string(bytes)
		})
}