Safeguard QueryPlayerIDInterface calls
Similar to 31df44673a
This was SVN commit r24060.
This commit is contained in:
parent
4a4a768af6
commit
c2b97cdba0
@ -34,10 +34,10 @@ Barter.prototype.Init = function()
|
|||||||
this.restoreTimer = undefined;
|
this.restoreTimer = undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
Barter.prototype.GetPrices = function(playerID)
|
Barter.prototype.GetPrices = function(cmpPlayer)
|
||||||
{
|
{
|
||||||
let prices = { "buy": {}, "sell": {} };
|
let prices = { "buy": {}, "sell": {} };
|
||||||
let multiplier = QueryPlayerIDInterface(playerID).GetBarterMultiplier();
|
let multiplier = cmpPlayer.GetBarterMultiplier();
|
||||||
for (let resource of Resources.GetBarterableCodes())
|
for (let resource of Resources.GetBarterableCodes())
|
||||||
{
|
{
|
||||||
let truePrice = Resources.GetResource(resource).truePrice;
|
let truePrice = Resources.GetResource(resource).truePrice;
|
||||||
@ -89,17 +89,20 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource
|
|||||||
if (amount != 100 && amount != 500)
|
if (amount != 100 && amount != 500)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var cmpPlayer = QueryPlayerIDInterface(playerID);
|
let cmpPlayer = QueryPlayerIDInterface(playerID);
|
||||||
var prices = this.GetPrices(playerID);
|
if (!cmpPlayer)
|
||||||
var amountsToSubtract = {};
|
return;
|
||||||
|
|
||||||
|
let prices = this.GetPrices(cmpPlayer);
|
||||||
|
let amountsToSubtract = {};
|
||||||
amountsToSubtract[resourceToSell] = amount;
|
amountsToSubtract[resourceToSell] = amount;
|
||||||
if (cmpPlayer.TrySubtractResources(amountsToSubtract))
|
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);
|
cmpPlayer.AddResource(resourceToBuy, amountToAdd);
|
||||||
|
|
||||||
// Display chat message to observers.
|
// Display chat message to observers.
|
||||||
var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
let cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
||||||
if (cmpGUIInterface)
|
if (cmpGUIInterface)
|
||||||
cmpGUIInterface.PushNotification({
|
cmpGUIInterface.PushNotification({
|
||||||
"type": "barter",
|
"type": "barter",
|
||||||
@ -110,7 +113,7 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource
|
|||||||
"resourceGained": resourceToBuy
|
"resourceGained": resourceToBuy
|
||||||
});
|
});
|
||||||
|
|
||||||
var cmpStatisticsTracker = QueryPlayerIDInterface(playerID, IID_StatisticsTracker);
|
let cmpStatisticsTracker = QueryPlayerIDInterface(playerID, IID_StatisticsTracker);
|
||||||
if (cmpStatisticsTracker)
|
if (cmpStatisticsTracker)
|
||||||
{
|
{
|
||||||
cmpStatisticsTracker.IncreaseResourcesSoldCounter(resourceToSell, amount);
|
cmpStatisticsTracker.IncreaseResourcesSoldCounter(resourceToSell, amount);
|
||||||
@ -127,10 +130,7 @@ Barter.prototype.ExchangeResources = function(playerID, resourceToSell, resource
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.restoreTimer === undefined)
|
if (this.restoreTimer === undefined)
|
||||||
{
|
this.restoreTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer).SetInterval(this.entity, IID_Barter, "ProgressTimeout", this.RESTORE_TIMER_INTERVAL, this.RESTORE_TIMER_INTERVAL, {});
|
||||||
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, {});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Barter.prototype.ProgressTimeout = function(data)
|
Barter.prototype.ProgressTimeout = function(data)
|
||||||
|
@ -125,7 +125,7 @@ GuiInterface.prototype.GetSimulationState = function()
|
|||||||
"classCounts": cmpTechnologyManager ? cmpTechnologyManager.GetClassCounts() : null,
|
"classCounts": cmpTechnologyManager ? cmpTechnologyManager.GetClassCounts() : null,
|
||||||
"typeCountsByClass": cmpTechnologyManager ? cmpTechnologyManager.GetTypeCountsByClass() : null,
|
"typeCountsByClass": cmpTechnologyManager ? cmpTechnologyManager.GetTypeCountsByClass() : null,
|
||||||
"canBarter": Engine.QueryInterface(SYSTEM_ENTITY, IID_Barter).PlayerHasMarket(i),
|
"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)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -514,13 +514,11 @@ ProductionQueue.prototype.RemoveBatch = function(id)
|
|||||||
|
|
||||||
for (let i = 0; i < this.queue.length; ++i)
|
for (let i = 0; i < this.queue.length; ++i)
|
||||||
{
|
{
|
||||||
|
// Find the item to remove.
|
||||||
let item = this.queue[i];
|
let item = this.queue[i];
|
||||||
if (item.id != id)
|
if (item.id != id)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Now we've found the item to remove.
|
|
||||||
let cmpPlayer = QueryPlayerIDInterface(item.player);
|
|
||||||
|
|
||||||
// Update entity count in the EntityLimits component.
|
// Update entity count in the EntityLimits component.
|
||||||
if (item.unitTemplate)
|
if (item.unitTemplate)
|
||||||
{
|
{
|
||||||
@ -529,7 +527,8 @@ ProductionQueue.prototype.RemoveBatch = function(id)
|
|||||||
if (template.TrainingRestrictions)
|
if (template.TrainingRestrictions)
|
||||||
{
|
{
|
||||||
let cmpPlayerEntityLimits = QueryPlayerIDInterface(item.player, IID_EntityLimits);
|
let cmpPlayerEntityLimits = QueryPlayerIDInterface(item.player, IID_EntityLimits);
|
||||||
cmpPlayerEntityLimits.ChangeCount(template.TrainingRestrictions.Category, -item.count);
|
if (cmpPlayerEntityLimits)
|
||||||
|
cmpPlayerEntityLimits.ChangeCount(template.TrainingRestrictions.Category, -item.count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -543,18 +542,23 @@ ProductionQueue.prototype.RemoveBatch = function(id)
|
|||||||
cmpStatisticsTracker.IncreaseResourceUsedCounter(r, -totalCosts[r]);
|
cmpStatisticsTracker.IncreaseResourceUsedCounter(r, -totalCosts[r]);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmpPlayer.AddResources(totalCosts);
|
let cmpPlayer = QueryPlayerIDInterface(item.player);
|
||||||
|
if (cmpPlayer)
|
||||||
|
{
|
||||||
|
cmpPlayer.AddResources(totalCosts);
|
||||||
|
|
||||||
// Remove reserved population slots if necessary.
|
// Remove reserved population slots if necessary.
|
||||||
if (item.productionStarted && item.unitTemplate)
|
if (item.productionStarted && item.unitTemplate)
|
||||||
cmpPlayer.UnReservePopulationSlots(item.population * item.count);
|
cmpPlayer.UnReservePopulationSlots(item.population * item.count);
|
||||||
|
}
|
||||||
|
|
||||||
// Mark the research as stopped if we cancel it.
|
// Mark the research as stopped if we cancel it.
|
||||||
if (item.technologyTemplate)
|
if (item.technologyTemplate)
|
||||||
{
|
{
|
||||||
// item.player is used as this.entity's owner may be invalid (deletion, etc.)
|
// item.player is used as this.entity's owner may be invalid (deletion, etc.)
|
||||||
let cmpTechnologyManager = QueryPlayerIDInterface(item.player, IID_TechnologyManager);
|
let cmpTechnologyManager = QueryPlayerIDInterface(item.player, IID_TechnologyManager);
|
||||||
cmpTechnologyManager.StoppedResearch(item.technologyTemplate, true);
|
if (cmpTechnologyManager)
|
||||||
|
cmpTechnologyManager.StoppedResearch(item.technologyTemplate, true);
|
||||||
this.SetAnimation("idle");
|
this.SetAnimation("idle");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +115,8 @@ Upgrade.prototype.ChangeUpgradedEntityCount = function(amount)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
let cmpEntityLimits = QueryPlayerIDInterface(this.owner, IID_EntityLimits);
|
let cmpEntityLimits = QueryPlayerIDInterface(this.owner, IID_EntityLimits);
|
||||||
cmpEntityLimits.ChangeCount(categoryTo, amount);
|
if (cmpEntityLimits)
|
||||||
|
cmpEntityLimits.ChangeCount(categoryTo, amount);
|
||||||
};
|
};
|
||||||
|
|
||||||
Upgrade.prototype.CanUpgradeTo = function(template)
|
Upgrade.prototype.CanUpgradeTo = function(template)
|
||||||
|
@ -52,7 +52,7 @@ AddMock(SYSTEM_ENTITY, IID_Timer, {
|
|||||||
TS_ASSERT_EQUALS(cmpBarter.restoreTimer, undefined);
|
TS_ASSERT_EQUALS(cmpBarter.restoreTimer, undefined);
|
||||||
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.priceDifferences, { "wood": 0, "stone": 0, "metal": 0 });
|
TS_ASSERT_UNEVAL_EQUALS(cmpBarter.priceDifferences, { "wood": 0, "stone": 0, "metal": 0 });
|
||||||
|
|
||||||
AddMock(playerEnt, IID_Player, {
|
let cmpPlayer = AddMock(playerEnt, IID_Player, {
|
||||||
"TrySubtractResources": amounts => {
|
"TrySubtractResources": amounts => {
|
||||||
sold = amounts[Object.keys(amounts)[0]];
|
sold = amounts[Object.keys(amounts)[0]];
|
||||||
return true;
|
return true;
|
||||||
@ -76,24 +76,24 @@ AddMock(60, IID_Foundation, {});
|
|||||||
|
|
||||||
// GetPrices
|
// GetPrices
|
||||||
cmpBarter.priceDifferences = { "wood": 8, "stone": 0, "metal": 0 };
|
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,
|
"wood": truePrice * (100 + 8 + cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
||||||
"stone": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
"stone": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
||||||
"metal": 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,
|
"wood": truePrice * (100 + 8 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
||||||
"stone": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
"stone": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
||||||
"metal": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100
|
"metal": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100
|
||||||
});
|
});
|
||||||
|
|
||||||
multiplier.buy.stone = 2.0;
|
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,
|
"wood": truePrice * (100 + 8 + cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
||||||
"stone": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) * 2.0 / 100,
|
"stone": truePrice * (100 + cmpBarter.CONSTANT_DIFFERENCE) * 2.0 / 100,
|
||||||
"metal": 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,
|
"wood": truePrice * (100 + 8 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
||||||
"stone": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
"stone": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100,
|
||||||
"metal": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100
|
"metal": truePrice * (100 - cmpBarter.CONSTANT_DIFFERENCE) / 100
|
||||||
|
Loading…
Reference in New Issue
Block a user