private void ApplyExecutedMessages(Account account)
 {
     this.workingMessages.Clear();
     this.workingMessages.AddRange(base.AIEntity.AIPlayer.Blackboard.GetMessages <EvaluableMessage>(BlackboardLayerID.City));
     this.workingMessages.AddRange(base.AIEntity.AIPlayer.Blackboard.GetMessages <EvaluableMessage>(BlackboardLayerID.Empire));
     this.workingMessages.RemoveAll((EvaluableMessage match) => match.AccountTag != account.AccountTag || match.ChosenBuyEvaluation == null);
     for (int i = 0; i < this.workingMessages.Count; i++)
     {
         EvaluableMessage evaluableMessage = this.workingMessages[i];
         if (evaluableMessage.EvaluationState == EvaluableMessage.EvaluableMessageState.Cancel || evaluableMessage.EvaluationState == EvaluableMessage.EvaluableMessageState.Pending || evaluableMessage.ChosenBuyEvaluation.State != BuyEvaluation.EvaluationState.Purchased)
         {
             account.PromisedAmount -= evaluableMessage.ChosenBuyEvaluation.DustCost;
             if (account.PromisedAmount < 0f)
             {
                 account.PromisedAmount = 0f;
             }
             evaluableMessage.RevokeChosenBuyEvaluation();
         }
         else if (evaluableMessage.EvaluationState == EvaluableMessage.EvaluableMessageState.Obtained && evaluableMessage.ChosenBuyEvaluation.State == BuyEvaluation.EvaluationState.Purchased)
         {
             account.PromisedAmount -= evaluableMessage.ChosenBuyEvaluation.DustCost;
             if (account.PromisedAmount < 0f)
             {
                 account.PromisedAmount = 0f;
             }
             account.EstimatedBalance -= evaluableMessage.ChosenBuyEvaluation.DustCost;
             if (account.EstimatedBalance < 0f)
             {
                 account.EstimatedBalance = 0f;
             }
         }
     }
     if (account.PromisedAmount > 0f && this.workingMessages.Count == 0)
     {
         account.PromisedAmount = 0f;
     }
 }
    private void ValidateAccountMessagesByResource(StaticString resourceName)
    {
        int[] array = this.accountIndexByResource[resourceName];
        for (int i = 0; i < array.Length; i++)
        {
            this.accounts[array[i]].WantedAmount         = 0f;
            this.accounts[array[i]].WantedAmountPriority = 0f;
            this.accounts[array[i]].PromisedAmount       = 0f;
        }
        this.GatherMessages(array);
        if (this.workingMessages.Count == 0)
        {
            return;
        }
        int[] bestEvaluationIndex = new int[this.workingMessages.Count];
        int[] array2 = new int[this.workingMessages.Count];
        float availableResourceStock = this.aiLayerEconomy.GetAvailableResourceStock(resourceName);
        float num = availableResourceStock;

        for (int j = 0; j < array.Length; j++)
        {
            this.accounts[array[j]].EstimatedBalance = availableResourceStock;
        }
        for (int k = 0; k < this.workingMessages.Count; k++)
        {
            EvaluableMessage evaluableMessage = this.workingMessages[k];
            array2[k] = k;
            bestEvaluationIndex[k] = -1;
            float num2 = 0f;
            for (int l = 0; l < evaluableMessage.BuyEvaluations.Count; l++)
            {
                BuyEvaluation buyEvaluation = evaluableMessage.BuyEvaluations[l];
                if (evaluableMessage.EvaluationState != EvaluableMessage.EvaluableMessageState.Obtaining || buyEvaluation == null || evaluableMessage.ChosenProductionEvaluation == null || !(buyEvaluation.CityGuid != evaluableMessage.ChosenProductionEvaluation.CityGuid))
                {
                    buyEvaluation.ComputeFinalBuyoutScore(evaluableMessage.Interest, num);
                    if (num2 < buyEvaluation.BuyoutFinalScore)
                    {
                        bestEvaluationIndex[k] = l;
                        num2 = buyEvaluation.BuyoutFinalScore;
                    }
                }
            }
        }
        Array.Sort <int>(array2, delegate(int left, int right)
        {
            if (bestEvaluationIndex[left] == -1)
            {
                return(1);
            }
            if (bestEvaluationIndex[right] == -1)
            {
                return(-1);
            }
            return(-1 * this.workingMessages[left].BuyEvaluations[bestEvaluationIndex[left]].BuyoutFinalScore.CompareTo(this.workingMessages[right].BuyEvaluations[bestEvaluationIndex[right]].BuyoutFinalScore));
        });
        for (int m = 0; m < this.workingMessages.Count; m++)
        {
            EvaluableMessage evaluableMessage2 = this.workingMessages[m];
            if (evaluableMessage2.ChosenBuyEvaluation != null)
            {
                if (num < evaluableMessage2.ChosenBuyEvaluation.DustCost)
                {
                    evaluableMessage2.RevokeChosenBuyEvaluation();
                }
                else
                {
                    num -= evaluableMessage2.ChosenBuyEvaluation.DustCost;
                    this.accounts[this.EnsureAccount(evaluableMessage2.AccountTag)].PromisedAmount += evaluableMessage2.ChosenBuyEvaluation.DustCost;
                }
            }
        }
        int num3 = 0;

        while (num3 < array2.Length && num > 0f)
        {
            int num4 = array2[num3];
            EvaluableMessage evaluableMessage3 = this.workingMessages[num4];
            int num5 = bestEvaluationIndex[num4];
            if (num5 >= 0 && num5 < evaluableMessage3.BuyEvaluations.Count)
            {
                BuyEvaluation buyEvaluation2 = evaluableMessage3.BuyEvaluations[num5];
                Account       account        = this.accounts[this.EnsureAccount(evaluableMessage3.AccountTag)];
                if (num < buyEvaluation2.DustCost)
                {
                    if (num3 == 0)
                    {
                        account.WantedAmount         = buyEvaluation2.DustCost;
                        account.WantedAmountPriority = buyEvaluation2.BuyoutFinalScore;
                    }
                    num -= account.GetAvailableAmount() * this.percentRetainWhenNotEnough;
                }
                else if (buyEvaluation2.BuyoutFinalScore > 0f)
                {
                    evaluableMessage3.ValidateBuyEvaluation(buyEvaluation2);
                    account.PromisedAmount += buyEvaluation2.DustCost;
                    num -= buyEvaluation2.DustCost;
                }
            }
            num3++;
        }
    }