ACR_ServerCommunicator.GameWorldManager.InsertNewCharacter C# (CSharp) Method

InsertNewCharacter() private method

This function inserts a character into the various character lists and issues the character join event.
private InsertNewCharacter ( ACR_ServerCommunicator.GameCharacter Character, int ServerId, IALFADatabase Database, bool InitialDMState ) : void
Character ACR_ServerCommunicator.GameCharacter Supplies the character object to insert. ///
ServerId int Supplies the server id that the player is /// logged on to (only meaningful if the character is online).
Database IALFADatabase Supplies the database connection to use for /// queries, if required. The active rowset may be consumed.
InitialDMState bool Supplies the initial DM state of the /// backing player object to update, for a synchronization of an /// existing player with a new character.
return void
        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;
            }
        }