Ticket #1839: tribute-insufficient-resources.2.patch

File tribute-insufficient-resources.2.patch, 2.2 KB (added by alpha123, 11 years ago)

Refactored to not use TrySubtractResources.

  • simulation/components/Player.js

     
    174174    return amountsNeeded;
    175175};
    176176
    177 Player.prototype.TrySubtractResources = function(amounts)
     177Player.prototype.HasResourcesOrNotify = function(amounts)
    178178{
    179179    var amountsNeeded = this.GetNeededResources(amounts);
    180180
     
    189189        cmpGUIInterface.PushNotification(notification);
    190190        return false;
    191191    }
    192     else
     192
     193    // Subtract the resources
     194    for (var type in amounts)
    193195    {
     196        this.resourceCount[type] -= amounts[type];
     197    }
     198
     199    return true;
     200};
     201
     202Player.prototype.TrySubtractResources = function(amounts)
     203{
     204    var hasResources = this.HasResourcesOrNotify(amounts);
     205
     206    // If we don't have enough resources, send a notification to the player
     207    if (hasResources)
     208    {
    194209        // Subtract the resources
    195210        var cmpStatisticsTracker = QueryPlayerIDInterface(this.playerID, IID_StatisticsTracker);
    196         for (var type in amounts)
    197         {
    198             this.resourceCount[type] -= amounts[type];
    199             if (cmpStatisticsTracker)
    200                 cmpStatisticsTracker.IncreaseResourceUsedCounter(type, amounts[type]);
    201         }
     211        if (cmpStatisticsTracker)
     212            cmpStatisticsTracker.IncreaseResourceUsedCounter(type, amounts[type]);
    202213    }
    203214
    204     return true;
     215    return hasResources;
    205216};
    206217
    207218Player.prototype.GetState = function()
     
    533544    if (this.state != "active" || cmpPlayer.state != "active")
    534545        return;
    535546
    536     if (!this.GetNeededResources(amounts))
     547    if (this.HasResourcesOrNotify(amounts))
    537548    {
    538         for (var type in amounts)
    539             this.resourceCount[type] -= amounts[type];
    540 
    541549        cmpPlayer.AddResources(amounts);
    542 
     550       
    543551        var total = Object.keys(amounts).reduce(function (sum, type){ return sum + amounts[type]; }, 0);
    544552        var cmpOurStatisticsTracker = QueryPlayerIDInterface(this.playerID, IID_StatisticsTracker);
    545553        if (cmpOurStatisticsTracker)
     
    552560        var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
    553561        cmpGUIInterface.PushNotification(notification);
    554562    }
    555     // else not enough resources... TODO: send gui notification
    556563};
    557564
    558565Engine.RegisterComponentType(IID_Player, "Player", Player);