Exemplo n.º 1
0
//OnDisconnect is connected when a player with a given socketID disconnects
func OnDisconnect(socketID string) {
	pprof.Clients.Add(-1)
	defer chelpers.DeauthenticateSocket(socketID)
	if chelpers.IsLoggedInSocket(socketID) {
		steamid := chelpers.GetSteamId(socketID)
		sessions.RemoveSocket(socketID, steamid)
		player, tperr := models.GetPlayerBySteamID(steamid)
		if tperr != nil || player == nil {
			logrus.Error(tperr.Error())
			return
		}

		ids, tperr := player.GetSpectatingIds()
		if tperr != nil {
			logrus.Error(tperr.Error())
			return
		}

		for _, id := range ids {
			//if this _specific_ socket is spectating this lobby, remove them from it
			//player might be spectating other lobbies in another tab, but we don't care
			if sessions.IsSpectating(socketID, id) {
				lobby, _ := models.GetLobbyByID(id)
				err := lobby.RemoveSpectator(player, true)
				if err != nil {
					logrus.Error(err.Error())
					continue
				}
				sessions.RemoveSpectator(socketID)
				//logrus.Debug("removing %s from %d", player.SteamId, id)
			}
		}

		id, _ := player.GetLobbyID(true)
		//if player is in a waiting lobby, and hasn't connected for > 30 seconds,
		//remove him from it. Here, connected = player isn't connected from any tab/window
		if id != 0 && sessions.ConnectedSockets(player.SteamID) == 0 {
			time.AfterFunc(time.Second*30, func() {
				if !sessions.IsConnected(player.SteamID) {
					//player may have changed lobbies during this time
					//fetch lobby ID again
					id, err := player.GetLobbyID(true)
					if err != nil {
						return
					}

					lobby := &models.Lobby{}
					db.DB.First(lobby, id)
					lobby.RemovePlayer(player)
				}
			})
		}
	}

}
Exemplo n.º 2
0
func AfterLobbySpec(server *wsevent.Server, so *wsevent.Client, lobby *models.Lobby) {
	//remove socket from room of the previous lobby the socket was spectating (if any)
	lobbyID, ok := sessions.GetSpectating(so.ID)
	if ok {
		server.RemoveClient(so, fmt.Sprintf("%d_public", lobbyID))
		sessions.RemoveSpectator(so.ID)
	}

	server.AddClient(so, fmt.Sprintf("%s_public", GetLobbyRoom(lobby.ID)))
	chelpers.BroadcastScrollback(so, lobby.ID)
	sessions.SetSpectator(so.ID, lobby.ID)
}
Exemplo n.º 3
0
func (Unauth) LobbySpectatorLeave(so *wsevent.Client, args struct {
	ID *uint `json:"id"`
}) interface{} {

	id, ok := sessions.GetSpectating(so.ID)
	if ok {
		socket.UnauthServer.Leave(so, fmt.Sprintf("%d_public", id))
		sessions.RemoveSpectator(so.ID)
	}

	return emptySuccess
}
Exemplo n.º 4
0
func AfterLobbySpec(server *wsevent.Server, so *wsevent.Client, player *player.Player, lob *lobby.Lobby) {
	//remove socket from room of the previous lobby the socket was spectating (if any)
	lobbyID, ok := sessions.GetSpectating(so.ID)
	if ok {
		server.Leave(so, fmt.Sprintf("%d_public", lobbyID))
		sessions.RemoveSpectator(so.ID)
		if player != nil {
			prevLobby, _ := lobby.GetLobbyByID(lobbyID)
			prevLobby.RemoveSpectator(player, true)
		}
	}

	server.Join(so, fmt.Sprintf("%d_public", lob.ID))
	chelpers.BroadcastScrollback(so, lob.ID)
	sessions.SetSpectator(so.ID, lob.ID)
}
Exemplo n.º 5
0
func AfterLobbySpecLeave(so *wsevent.Client, lobby *models.Lobby) {
	socket.AuthServer.RemoveClient(so, fmt.Sprintf("%s_public", GetLobbyRoom(lobby.ID)))
	sessions.RemoveSpectator(so.ID)
}