Aura.Channel.Network.ChannelClient.CleanUp C# (CSharp) Method

CleanUp() public method

Saves characters, despawns and disposes them, etc.
public CleanUp ( ) : void
return void
		public override void CleanUp()
		{
			// Moved here to always be called when a client is being killed off.
			if (this.Controlling != null)
				ChannelServer.Instance.Events.OnPlayerDisconnect(this.Controlling);

			// Dispose creatures, to remove subscriptions and stuff.
			// Do this before unspawning, the creature might need the region.
			foreach (var creature in this.Creatures.Values)
				creature.Dispose();

			foreach (var creature in this.Creatures.Values.Where(a => a.Region != Region.Limbo))
			{
				// Close NPC sessions
				if (creature.Client.NpcSession.Script != null)
					creature.Client.NpcSession.Clear();

				var newLocation = new Location();

				// Use fallback location if creature is in a temp region.
				if (creature.Region is DynamicRegion)
					newLocation = creature.FallbackLocation;

				// Use dungeon exit as fallback location if in a dungeon.
				var dungeonRegion = creature.Region as DungeonRegion;
				if (dungeonRegion != null)
				{
					try
					{
						newLocation = new Location(dungeonRegion.Dungeon.Data.Exit);
					}
					catch (Exception ex)
					{
						Log.Exception(ex, "Failed to fallback warp character in dungeon.");
						newLocation = new Location(1, 12800, 38100); // Tir square
					}

					if (dungeonRegion.Dungeon.Script != null)
						dungeonRegion.Dungeon.Script.OnLeftEarly(dungeonRegion.Dungeon, creature);
				}

				// Finish running cutscenes
				var cutscene = creature.Temp.CurrentCutscene;
				if (cutscene != null && cutscene.Leader == creature)
					cutscene.Finish();

				// Unspawn creature
				creature.Region.RemoveCreature(creature);

				// Set new location (if applicable) after everyting else is done,
				// in case on of the previous calls needs the creature's
				// original position.
				if (newLocation.RegionId != 0)
					creature.SetLocation(newLocation);

				// Update online status
				var playerCreature = creature as PlayerCreature;
				if (playerCreature != null)
					ChannelServer.Instance.Database.UpdateOnlineStatus(playerCreature.CreatureId, false);
			}

			// Save everything after we're done cleaning up
			if (this.Account != null)
			{
				ChannelServer.Instance.Database.SaveAccount(this.Account);
				ChannelServer.Instance.Database.SetAccountLoggedIn(this.Account.Id, false);
			}

			this.Creatures.Clear();
			this.Account = null;
		}
	}