private void InsertNewCharacter(GameCharacter Character, int ServerId, IALFADatabase Database, bool? InitialDMState)
{
GameServer Server;
Character.Player = ReferencePlayerById(Character.PlayerId, Database);
if (Character.Player == null)
throw new ApplicationException(String.Format("Character {0} references invalid player id {1}", Character.CharacterId, Character.PlayerId));
if (InitialDMState != null)
Character.Player.IsDM = (InitialDMState != false);
Character.Player.Characters.Add(Character);
try
{
CharacterList.Add(Character);
try
{
if (Character.Online)
{
Server = ReferenceServerById(ServerId, Database);
if (Server == null)
throw new ApplicationException(String.Format("Character {0} is online but references invalid server id {1}", Character.CharacterId, ServerId));
Character.Server = Server;
//
// If the character is coming online, but its associated server is
// not actually online, then mark the character as offline.
//
if (Character.Online && !Character.Server.Online)
{
Character.Online = false;
return;
}
//
// Mark the character as visited so that if we come in
// on the main thread during the middle of a character
// synchronization cycle, we won't immediate offline
// the character.
//
Character.Visited = true;
Character.Server = Server;
Character.Server.Characters.Add(Character);
try
{
OnlineCharacterList.Add(Character);
try
{
Character.Player.UpdateOnlineCharacter();
OnCharacterJoin(Character);
}
catch
{
OnlineCharacterList.Remove(Character);
throw;
}
}
catch
{
Character.Server.Characters.Remove(Character);
throw;
}
}
}
catch
{
CharacterList.Remove(Character);
throw;
}
}
catch
{
Character.Player.Characters.Remove(Character);
Character.Player.UpdateOnlineCharacter();
throw;
}
}