ACR_ServerCommunicator.GameWorldManager.PerformInitialSynchronization C# (CSharp) Method

PerformInitialSynchronization() private method

This method performs the initial synchronization step at first run that downloads the initial character list. A bulk query is issued here to reduce the number of database round-trips at startup time. Note that no attempt is made to mark offline characters here. That step is done in the normal synchronization round, as this is the initial round anyway.
private PerformInitialSynchronization ( ) : void
return void
        private void PerformInitialSynchronization()
        {
            IALFADatabase Database = DatabaseLinkQueryThread;
            List<InitialSynchronizationRow> Rowset = new List<InitialSynchronizationRow>();

            Database.ACR_SQLQuery(
                "SELECT " +
                    "`characters`.`ID` AS character_id, " +                  //  0
                    "`characters`.`PlayerID` AS player_id, " +               //  1
                    "`characters`.`Name` AS character_name, " +              //  2
                    "`characters`.`ServerID` AS server_id, " +               //  3
                    "`characters`.`Location` AS character_location, " +      //  4
                    "`players`.`IsDM` AS player_is_dm, " +                   //  5
                    "`players`.`Name` AS player_name, " +                    //  6
                    "`servers`.`IPAddress` AS server_address_string, " +     //  7
                    "`servers`.`Name` AS server_name " +                     //  8
                "FROM " +
                    "`characters` " +
                "INNER JOIN `servers` ON `characters`.`ServerID` = `servers`.`ID` " +
                "INNER JOIN `players` ON `characters`.`PlayerID` = `players`.`ID` " +
                "WHERE " +
                    "`characters`.`IsOnline` = 1 "
                );

            while (Database.ACR_SQLFetch())
            {
                InitialSynchronizationRow Row;

                Row.CharacterId = Convert.ToInt32(Database.ACR_SQLGetData(0));
                Row.PlayerId = Convert.ToInt32(Database.ACR_SQLGetData(1));
                Row.CharacterName = Database.ACR_SQLGetData(2);
                Row.ServerId = Convert.ToInt32(Database.ACR_SQLGetData(3));
                Row.CharacterLocation = Database.ACR_SQLGetData(4);
                Row.PlayerIsDM = ConvertToBoolean(Database.ACR_SQLGetData(5));
                Row.PlayerName = Database.ACR_SQLGetData(6);
                Row.ServerAddressString = Database.ACR_SQLGetData(7);
                Row.ServerName = Database.ACR_SQLGetData(8);

                Rowset.Add(Row);
            }

            lock (this)
            {
                //
                // Update entries.
                //

                foreach (InitialSynchronizationRow Row in Rowset)
                {
                    GameServer Server = (from S in Servers
                                         where S.ServerId == Row.ServerId
                                         select S).FirstOrDefault();

                    if (Server == null)
                    {
                        Server = new GameServer(this);

                        Server.ServerName = Row.ServerName;
                        Server.ServerId = Row.ServerId;
                        Server.SetHostnameAndPort(Row.ServerAddressString);

                        InsertNewServer(Server, Database);
                    }

                    GamePlayer Player = (from P in Players
                                         where P.PlayerId == Row.PlayerId
                                         select P).FirstOrDefault();

                    if (Player == null)
                    {
                        Player = new GamePlayer(this);

                        Player.PlayerName = Row.PlayerName;
                        Player.PlayerId = Row.PlayerId;
                        Player.IsDM = Row.PlayerIsDM;

                        InsertNewPlayer(Player, Database);
                    }

                    GameCharacter Character = (from C in Characters
                                               where C.CharacterId == Row.CharacterId
                                               select C).FirstOrDefault();

                    if (Character == null)
                    {
                        Character = new GameCharacter(this);

                        Character.CharacterId = Row.CharacterId;
                        Character.PlayerId = Row.PlayerId;
                        Character.Online = true;
                        Character.CharacterName = Row.CharacterName;
                        Character.LocationString = Row.CharacterLocation;

                        InsertNewCharacter(Character, Row.ServerId, Database, null);
                    }
                }

#if DEBUG_MODE
                WriteDiagnosticLog(String.Format("GameWorldManager.PerformInitialSynchronization: Synchronized {0} servers, {1} players, {2} characters.",
                    ServerList.Count,
                    PlayerList.Count,
                    CharacterList.Count));
#endif
            }
        }