示例#1
0
//OnDisconnect is connected when a player with a given socketID disconnects
func OnDisconnect(socketID string, token *jwt.Token) {
	if token != nil { //player was logged in
		player := chelpers.GetPlayer(token)
		if player == nil {
			return
		}

		sessions.RemoveSocket(socketID, player.SteamID)
		id, _ := sessions.GetSpectating(socketID)
		if id != 0 {
			lob, _ := lobby.GetLobbyByID(id)
			lob.RemoveSpectator(player, true)
		}

		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 {
			sessions.AfterDisconnectedFunc(player.SteamID, time.Second*30, func() {
				lob, _ := lobby.GetLobbyByID(id)
				if lob.State == lobby.Waiting {
					lob.RemovePlayer(player)
				}
			})
		}
	}

}
示例#2
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)
				}
			})
		}
	}

}