ALFAIRCBot.ALFAIRCBot.RunSynchronizationCycle C# (CSharp) Метод

RunSynchronizationCycle() приватный Метод

Run the database synchronization cycle.
private RunSynchronizationCycle ( int GatewayID ) : void
GatewayID int Supplies the gateway ID to synchronize. ///
Результат void
        private void RunSynchronizationCycle(int GatewayID)
        {
            int HighestRecordId = 0;

            string QueryFmt =
                "SELECT " +
                    "gw.`ID` AS record_id, " +
                    "gw.`Recipient` AS recipient_name, " +
                    "gw.`Message` AS message_text, " +
                    "c.`Name` AS character_name, " +
                    "p.`Name` AS player_name, " +
                    "p.`ID` AS player_id, " +
                    "c.`ServerID` AS character_server_id " +
                "FROM " +
                    "`irc_gateway_messages` AS gw " +
                "INNER JOIN `characters` AS c ON c.`ID` = gw.`SourceCharacterID` " +
                "INNER JOIN `players` AS p ON p.`ID` = c.`PlayerID` " +
                "WHERE gw.`GatewayID` = {0} " +
                "GROUP BY record_id " +
                "ORDER BY record_id " +
                "LIMIT 1000 ";

            try
            {
                using (MySqlDataReader Reader = ExecuteQuery(String.Format(QueryFmt, GatewayID)))
                {
                    while (Reader.Read())
                    {
                        int RecordId = Reader.GetInt32(0);
                        string Recipient = Reader.GetString(1);
                        string Message = Reader.GetString(2).TrimStart(new char[] { '\t', ' ' });
                        string CharacterName = Reader.GetString(3);
                        string PlayerName = Reader.GetString(4);
                        int PlayerId = Reader.GetInt32(5);
                        int CharacterServerId = Reader.GetInt32(6);

                        HighestRecordId = RecordId;

                        if (!Recipient.StartsWith("#"))
                        {
                            SendMessageToPlayer(PlayerId, CharacterServerId, "Error: Recipient must be a channel.");
                            continue;
                        }

                        if (Message.StartsWith("/"))
                        {
                            OnInGameIrcCommand(PlayerId, CharacterServerId, Message.Substring(1));
                            continue;
                        }

                        if (Message.IndexOfAny(new char[] { '\r', '\n' }) != -1)
                        {
                            SendMessageToPlayer(PlayerId, CharacterServerId, "Error: Message must not have newlines.");
                            continue;
                        }

                        if (Recipient.StartsWith("#"))
                        {
                            if (!HomeChannels.Contains(Recipient))
                            {
                                SendMessageToPlayer(PlayerId, CharacterServerId, "Error: You cannot send a message to that recipient channel.");
                                continue;
                            }
                        }

                        if (!Client.IsConnected)
                        {
                            SendMessageToPlayer(PlayerId, CharacterServerId, "Error: IRC gateway is offline.");
                            continue;
                        }

                        string FormattedMessage = String.Format(
                            "{0} ({1}): {2}",
                            CharacterName,
                            PlayerName,
                            Message);

                        SendMessage(SendType.Message, Recipient, FormattedMessage);
                    }
                }
            }
            finally
            {
                //
                // Now delete all of the records that we processed.
                //

                if (HighestRecordId != 0)
                {
                    ExecuteQueryNoReader(String.Format(
                        "DELETE FROM `irc_gateway_messages` WHERE `GatewayID` = {0} AND `ID` < {1}",
                        GatewayID,
                        HighestRecordId + 1));
                }
            }
        }