1
0
forked from 0ad/0ad

Safeguard QueryPlayerIDInterface calls

Similar to 31df44673a

This was SVN commit r24060.
This commit is contained in:
bb 2020-09-21 18:54:32 +00:00
parent 4a4a768af6
commit c2b97cdba0
5 changed files with 33 additions and 28 deletions

View File

@ -34,10 +34,10 @@ Barter.prototype.Init = function()
this.restoreTimer = undefined;
};
Barter.prototype.GetPrices = function(playerID)
Barter.prototype.GetPrices = function(cmpPlayer)
{
let prices = { "buy": {}, "sell": {} };
let multiplier = QueryPlayerIDInterface(playerID).GetBarterMultiplier();
let multiplier = cmpPlayer.GetBarterMultiplier();
for (let resource of Resources.GetBarterableCodes())
{
let truePrice = Resources.GetResource(resource).truePrice;
@ -89,17 +89,20 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource
if (amount != 100 && amount != 500)
return;
var cmpPlayer = QueryPlayerIDInterface(playerID);
var prices = this.GetPrices(playerID);
var amountsToSubtract = {};
let cmpPlayer = QueryPlayerIDInterface(playerID);
if (!cmpPlayer)
return;
let prices = this.GetPrices(cmpPlayer);
let amountsToSubtract = {};
amountsToSubtract[resourceToSell] = amount;
if (cmpPlayer.TrySubtractResources(amountsToSubtract))
{
var amountToAdd = Math.round(prices["sell"][resourceToSell] / prices["buy"][resourceToBuy] * amount);
let amountToAdd = Math.round(prices.sell[resourceToSell] / prices.buy[resourceToBuy] * amount);
cmpPlayer.AddResource(resourceToBuy, amountToAdd);
// Display chat message to observers.
var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
let cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
if (cmpGUIInterface)
cmpGUIInterface.PushNotification({
"type": "barter",
@ -110,7 +113,7 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource
"resourceGained": resourceToBuy
});
var cmpStatisticsTracker = QueryPlayerIDInterface(playerID, IID_StatisticsTracker);
let cmpStatisticsTracker = QueryPlayerIDInterface(playerID, IID_StatisticsTracker);
if (cmpStatisticsTracker)
{
cmpStatisticsTracker.IncreaseResourcesSoldCounter(resourceToSell, amount);
@ -127,10 +130,7 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource
}
if (this.restoreTimer === undefined)
{
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
this.restoreTimer = cmpTimer.SetInterval(this.entity, IID_Barter, "ProgressTimeout", this.RESTORE_TIMER_INTERVAL, this.RESTORE_TIMER_INTERVAL, {});
}
this.restoreTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer).SetInterval(this.entity, IID_Barter, "ProgressTimeout", this.RESTORE_TIMER_INTERVAL, this.RESTORE_TIMER_INTERVAL, {});
};
Barter.prototype.ProgressTimeout = function(data)

View File

@ -125,7 +125,7 @@ GuiInterface.prototype.GetSimulationState = function()
"classCounts": cmpTechnologyManager ? cmpTechnologyManager.GetClassCounts() : null,
"typeCountsByClass": cmpTechnologyManager ? cmpTechnologyManager.GetTypeCountsByClass() : null,
"canBarter": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).PlayerHasMarket(i),
"barterPrices": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).GetPrices(i)
"barterPrices": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).GetPrices(cmpPlayer)
});
}

View File

@ -514,13 +514,11 @@ ProductionQueue.prototype.RemoveBatch = function(id)
for (let i = 0; i < this.queue.length; ++i)
{
// Find the item to remove.
let item = this.queue[i];
if (item.id != id)
continue;
// Now we've found the item to remove.
let cmpPlayer = QueryPlayerIDInterface(item.player);
// Update entity count in the EntityLimits component.
if (item.unitTemplate)
{
@ -529,6 +527,7 @@ ProductionQueue.prototype.RemoveBatch = function(id)
if (template.TrainingRestrictions)
{
let cmpPlayerEntityLimits = QueryPlayerIDInterface(item.player, IID_EntityLimits);
if (cmpPlayerEntityLimits)
cmpPlayerEntityLimits.ChangeCount(template.TrainingRestrictions.Category, -item.count);
}
}
@ -543,17 +542,22 @@ ProductionQueue.prototype.RemoveBatch = function(id)
cmpStatisticsTracker.IncreaseResourceUsedCounter(r, -totalCosts[r]);
}
let cmpPlayer = QueryPlayerIDInterface(item.player);
if (cmpPlayer)
{
cmpPlayer.AddResources(totalCosts);
// Remove reserved population slots if necessary.
if (item.productionStarted && item.unitTemplate)
cmpPlayer.UnReservePopulationSlots(item.population * item.count);
}
// Mark the research as stopped if we cancel it.
if (item.technologyTemplate)
{
// item.player is used as this.entity's owner may be invalid (deletion, etc.)
let cmpTechnologyManager = QueryPlayerIDInterface(item.player, IID_TechnologyManager);
if (cmpTechnologyManager)
cmpTechnologyManager.StoppedResearch(item.technologyTemplate, true);
this.SetAnimation("idle");
}

View File

@ -115,6 +115,7 @@ Upgrade.prototype.ChangeUpgradedEntityCount = function(amount)
return;
let cmpEntityLimits = QueryPlayerIDInterface(this.owner, IID_EntityLimits);
if (cmpEntityLimits)
cmpEntityLimits.ChangeCount(categoryTo, amount);
};

View File

@ -52,7 +52,7 @@ AddMock(SYSTEM_ENTITY, IID_Timer, {
TS_ASSERT_EQUALS(cmpBarter.restoreTimer, undefined);
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.priceDifferences, { "wood": 0, "stone": 0, "metal": 0 });
AddMock(playerEnt, IID_Player, {
let cmpPlayer = AddMock(playerEnt, IID_Player, {
"TrySubtractResources": amounts => {
sold = amounts[Object.keys(amounts)[0]];
return true;
@ -76,24 +76,24 @@ AddMock(60, IID_Foundation, {});
// GetPrices
cmpBarter.priceDifferences = { "wood": 8, "stone": 0, "metal": 0 };
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(playerID).buy, {
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).buy, {
"wood": truePrice * (100 + 8 + cmpBarter.CONSTANT_DIFFERENCE) / 100,
"stone": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) / 100,
"metal": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) / 100
});
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(playerID).sell, {
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).sell, {
"wood": truePrice * (100 + 8 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
"stone": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
"metal": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100
});
multiplier.buy.stone = 2.0;
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(playerID).buy, {
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).buy, {
"wood": truePrice * (100 + 8 + cmpBarter.CONSTANT_DIFFERENCE) / 100,
"stone": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) * 2.0 / 100,
"metal": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) / 100
});
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(playerID).sell, {
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.GetPrices(cmpPlayer).sell, {
"wood": truePrice * (100 + 8 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
"stone": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
"metal": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100