Skip to content

Commit

Permalink
Merge pull request #19 from Brent-Call/buyItem_callback_refactor
Browse files Browse the repository at this point in the history
Change signature of callback function to be backwards-compatible
  • Loading branch information
bloodrizer authored Nov 18, 2023
2 parents 9e206e8 + 53ef409 commit cc47346
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 105 deletions.
42 changes: 23 additions & 19 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,8 @@ dojo.declare("com.nuclearunicorn.game.ui.ButtonController", null, {
* @param event (OPTIONAL) Object. If given, contains information such as whether the shift key was pressed. This is
* used by buildings to determine if we're buying 1, buying 10, buying all, etc. If null, uses default behavior.
* @param callback Function. This is how we communicate the results of trying to buy the item. The callback function is called
* with a single parameter, an object containing two fields:
* itemBought Boolean. True if one or more items were bought, false if nothing happened.
* with two parameters. The first is itemBought, a Boolean encoding whether one or more items were bought.
* The second is an object containing the following field:
* reason String. Used as an enumeration encoding *why* the result happened the way it did.
* If itemBought is true, can be one of the following values:
* "paid-for" We met the requirements & bought the item legitimately.
Expand All @@ -741,11 +741,11 @@ dojo.declare("com.nuclearunicorn.game.ui.ButtonController", null, {
*/
buyItem: function(model, event, callback){
if (!this.hasResources(model)) {
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, {reason: "cannot-afford" });
return;
}
if (!model.enabled) {
callback({ itemBought: false, reason: "not-enabled" });
callback(false /*itemBought*/, {reason: "not-enabled" });
return;
}
//Else, we meet all requirements to buy this item:
Expand All @@ -758,7 +758,7 @@ dojo.declare("com.nuclearunicorn.game.ui.ButtonController", null, {
}

//A lot of normal UI buttons that don't involve building things use this method, so check if things are free:
callback({ itemBought: true, reason: ((model.prices && model.prices.length) ? "paid-for" : "item-is-free") });
callback(true /*itemBought*/, {reason: ((model.prices && model.prices.length) ? "paid-for" : "item-is-free") });
},

refund: function(model){
Expand Down Expand Up @@ -939,8 +939,12 @@ dojo.declare("com.nuclearunicorn.game.ui.Button", com.nuclearunicorn.core.Contro
onClick: function(event){
this.animate();
var self = this;
this.controller.buyItem(this.model, event, function(result) {
if (typeof(result) == "object" && result.itemBought) {
this.controller.buyItem(this.model, event, function(itemBought, extendedInfo) {
if (typeof(itemBought) !== "boolean" || typeof(extendedInfo) !== "object") {
console.error("Invalid arguments passed to callback function.");
return;
}
if (itemBought) {
self.update();
}
});
Expand Down Expand Up @@ -2000,7 +2004,7 @@ dojo.declare("com.nuclearunicorn.game.ui.BuildingStackableBtnController", com.nu
buyItem: function(model, event, callback) {
var isInDevMode = this.game.devMode;
if (!this.hasResources(model) && !isInDevMode) {
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, {reason: "cannot-afford" });
return;
}
if (!model.enabled && !isInDevMode) {
Expand All @@ -2013,7 +2017,7 @@ dojo.declare("com.nuclearunicorn.game.ui.BuildingStackableBtnController", com.nu
whyArentWeEnabled = "already-bought";
}

callback({ itemBought: false, reason: whyArentWeEnabled });
callback(false /*itemBought*/, {reason: whyArentWeEnabled });
return;
}
//Else, we meet all the requirements for being able to buy this item:
Expand All @@ -2024,7 +2028,7 @@ dojo.declare("com.nuclearunicorn.game.ui.BuildingStackableBtnController", com.nu
this.game.ui.confirm("", $I("iron.will.break.confirmation.msg"), function() {
self._buyItem_step2(model, event, callback);
}, function() {
callback({ itemBought: false, reason: "player-denied" /*The player decided not to buy this after all.*/ });
callback(false /*itemBought*/, {reason: "player-denied" /*The player decided not to buy this after all.*/ });
});
} else {
this._buyItem_step2(model, event, callback);
Expand All @@ -2034,29 +2038,29 @@ dojo.declare("com.nuclearunicorn.game.ui.BuildingStackableBtnController", com.nu
_buyItem_step2: function(model, event, callback) {
if (!event) { event = {}; /*event is an optional parameter*/ }
//This is what we pass to the callback function if we succeed:
var resultIfBuySuccessful = { itemBought: true, reason: (this.game.devMode ? "dev-mode" : "paid-for") };
var resultIfBuySuccessful = { reason: (this.game.devMode ? "dev-mode" : "paid-for") };

var meta = model.metadata;
if (!meta.noStackable && event.shiftKey) {
var maxBld = 10000;
if (this.game.opts.noConfirm) {
this.build(model, maxBld);
callback(resultIfBuySuccessful);
callback(true /*itemBought*/, resultIfBuySuccessful);
} else {
var self = this;
this.game.ui.confirm($I("construct.all.confirmation.title"), $I("construct.all.confirmation.msg"), function() {
self.build(model, maxBld);
callback(resultIfBuySuccessful);
callback(true /*itemBought*/, resultIfBuySuccessful);
}, function() {
callback({ itemBought: false, reason: "player-denied" /*The player decided not to buy this after all.*/});
callback(false /*itemBought*/, {reason: "player-denied" /*The player decided not to buy this after all.*/});
});
}
} else if (!meta.noStackable && (event.ctrlKey || event.metaKey /*osx tears*/)) {
this.build(model, this.game.opts.batchSize || 10);
callback(resultIfBuySuccessful);
callback(true /*itemBought*/, resultIfBuySuccessful);
} else {
this.build(model, 1);
callback(resultIfBuySuccessful);
callback(true /*itemBought*/, resultIfBuySuccessful);
}
},

Expand Down Expand Up @@ -2185,18 +2189,18 @@ dojo.declare("com.nuclearunicorn.game.ui.BuildingNotStackableBtnController", com
buyItem: function(model, event, callback) {
var isInDevMode = this.game.devMode;
if (model.metadata.researched && !isInDevMode) {
callback({ itemBought: false, reason: "already-bought" });
callback(false /*itemBought*/, {reason: "already-bought" });
return;
}
if (!this.hasResources(model) && !isInDevMode) {
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, {reason: "cannot-afford" });
return;
}
//Else, we can buy it:
this.payPrice(model);
this.onPurchase(model);

callback({ itemBought: true, reason: (isInDevMode ? "dev-mode" : "paid-for") });
callback(true /*itemBought*/, {reason: (isInDevMode ? "dev-mode" : "paid-for") });
this.game.render();
return;
},
Expand Down
2 changes: 1 addition & 1 deletion js/buildings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2764,7 +2764,7 @@ dojo.declare("classes.game.ui.GatherCatnipButtonController", com.nuclearunicorn.
}

this.game.bld.gatherCatnip();
callback({ itemBought: true, reason: "item-is-free" /*It costs no resources to gather catnip, so we can't fail to buy it*/});
callback(true /*itemBought*/, {reason: "item-is-free" /*It costs no resources to gather catnip, so we can't fail to buy it*/});
}
});

Expand Down
2 changes: 1 addition & 1 deletion js/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ dojo.declare("classes.ui.ChallengeBtnController", com.nuclearunicorn.game.ui.Bui

buyItem: function(model, event, callback) {
this.togglePending(model);
callback({ itemBought: true, reason: "item-is-free" /*We just toggled the pending state; simple, really*/});
callback(true /*itemBought*/, {reason: "item-is-free" /*We just toggled the pending state; simple, really*/});
},

togglePending: function(model){
Expand Down
2 changes: 1 addition & 1 deletion js/prestige.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ dojo.declare("classes.ui.PrestigeBtnController", com.nuclearunicorn.game.ui.Buil
if (this.game.science.get("metaphysics").researched) {
this.inherited(arguments);
} else {
callback({ itemBought: false, reason: "not-unlocked" });
callback(false /*itemBought*/, { reason: "not-unlocked" });
}
},

Expand Down
28 changes: 14 additions & 14 deletions js/religion.js
Original file line number Diff line number Diff line change
Expand Up @@ -1382,24 +1382,24 @@ dojo.declare("classes.ui.religion.TransformBtnController", com.nuclearunicorn.ga

buyItem: function(model, event, callback) {
if (!this.hasResources(model)) {
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, { reason: "cannot-afford" });
return;
}
if (!model.enabled) {
//As far as I can tell, this shouldn't ever happen because being
//unable to afford it is the only reason for it to be disabled.
callback({ itemBought: false, reason: "not-enabled" });
callback(false /*itemBought*/, { reason: "not-enabled" });
return;
}
if (!event) { event = {}; /*event is an optional parameter*/ }
var batchSize = event.shiftKey ? 10000 :
event.ctrlKey || event.metaKey ? this.game.opts.batchSize : 1;
var didWeSucceed = this._transform(model, batchSize);
if (didWeSucceed) {
callback({ itemBought: true, reason: "paid-for" });
callback(true /*itemBought*/, { reason: "paid-for" });
} else {
//_transform(model, amt) returns false if we can't afford it
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, { reason: "cannot-afford" });
}
},

Expand All @@ -1410,21 +1410,21 @@ dojo.declare("classes.ui.religion.TransformBtnController", com.nuclearunicorn.ga
transform: function(model, divider, event, callback) {
var amt = Math.floor(this._canAfford(model) / divider);
if (amt < 1) {
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, { reason: "cannot-afford" });
return;
}
if (!model.enabled) {
//As far as I can tell, this shouldn't ever happen because being
//unable to afford it is the only reason for it to be disabled.
callback({ itemBought: false, reason: "not-enabled" });
callback(false /*itemBought*/, { reason: "not-enabled" });
return;
}
var didWeSucceed = this._transform(model, amt);
if (didWeSucceed) {
callback({ itemBought: true, reason: "paid-for" });
callback(true /*itemBought*/, { reason: "paid-for" });
} else {
//_transform(model, amt) returns false if we can't afford it
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, { reason: "cannot-afford" });
}
},

Expand Down Expand Up @@ -1508,19 +1508,19 @@ dojo.declare("classes.ui.religion.RefineTearsBtnController", com.nuclearunicorn.

buyItem: function(model, event, callback, count){
if (!this.hasResources(model)) {
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, { reason: "cannot-afford" });
return;
}
if (!model.enabled) {
//As far as I can tell, this shouldn't ever happen because being
//unable to afford it is the only reason for it to be disabled.
callback({ itemBought: false, reason: "not-enabled" });
callback(false /*itemBought*/, { reason: "not-enabled" });
return;
}
if (this.game.resPool.get("sorrow").value >= this.game.resPool.get("sorrow").maxValue){
//We can't refine because we're at the limit.
this.game.msg($I("religion.refineTearsBtn.refine.msg.failure"));
callback({ itemBought: false, reason: "already-bought" });
callback(false /*itemBought*/, { reason: "already-bought" });
return;
}
if (!event) { event = {}; /*event is an optional parameter*/ }
Expand All @@ -1532,7 +1532,7 @@ dojo.declare("classes.ui.religion.RefineTearsBtnController", com.nuclearunicorn.
this.payPrice(model);
this.refine();
}
callback({ itemBought: true, reason: "paid-for" });
callback(true /*itemBought*/, { reason: "paid-for" });
},

refine: function(){
Expand Down Expand Up @@ -1631,11 +1631,11 @@ dojo.declare("classes.ui.PactsPanel", com.nuclearunicorn.game.ui.Panel, {
this.updateEnabled(model);

if (!this.hasResources(model) && !this.game.devMode) {
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, { reason: "cannot-afford" });
return;
}
if(!this.shouldBeBough(model, this.game)){
callback({ itemBought: false, reason: "already-bought" /*No more pacts available*/ });
callback(false /*itemBought*/, { reason: "already-bought" /*No more pacts available*/ });
return;
}
this._buyItem_step2(model, event, callback);
Expand Down
27 changes: 13 additions & 14 deletions js/science.js
Original file line number Diff line number Diff line change
Expand Up @@ -1874,55 +1874,54 @@ dojo.declare("classes.ui.PolicyBtnController", com.nuclearunicorn.game.ui.Buildi
}
},

//Returns an object with two fields:
// itemBought: Boolean. Are we able to buy this policy at this time?
//Returns an object with one field:
// reason: String. If we can't buy it, why not? If we can buy it, returns
// "paid-for" regardless of whether we can actually afford it or not.
shouldBeBought: function(model, game){ //fail checks:
if(this.game.village.leader && model.metadata.requiredLeaderJob && this.game.village.leader.job != model.metadata.requiredLeaderJob){
var jobTitle = this.game.village.getJob(model.metadata.requiredLeaderJob).title;
this.game.msg($I("msg.policy.wrongLeaderJobForResearch", [model.metadata.label, jobTitle]), "important");
return { itemBought: false, reason: "blocked" };
return { reason: "blocked" };
}else if(model.metadata.name == "transkittenism" && this.game.bld.getBuildingExt("aiCore").meta.effects["aiLevel"] >= 15){
this.game.msg($I("msg.policy.aiNotMerges"),"alert", "ai");
return { itemBought: false, reason: "blocked" };
return { reason: "blocked" };
}else if(model.metadata.blocked != true) {
for(var i = 0; i < model.metadata.blocks.length; i++){
if(this.game.science.getPolicy(model.metadata.blocks[i]).researched){
model.metadata.blocked = true;
return { itemBought: false, reason: "blocked" };
return { reason: "blocked" };
}
}
var confirmed = false; //confirmation:
if(game.opts.noConfirm){
return { itemBought: true, reason: "paid-for" };
return { reason: "paid-for" };
}
game.ui.confirm($I("policy.confirmation.title"), $I("policy.confirmation.title"), function() {
confirmed = true;
});
return confirmed ? { itemBought: true, reason: "paid-for" } : { itemBought: false, reason: "player-denied" };
return confirmed ? { reason: "paid-for" } : { reason: "player-denied" };
}
//Else, the policy was blocked:
return { itemBought: false, reason: "blocked" };
return { reason: "blocked" };
},
buyItem: function(model, event, callback) {
var isInDevMode = this.game.devMode;
if (model.metadata.researched && !isInDevMode) {
callback({ itemBought: false, reason: "already-bought" });
callback(false /*itemBought*/, { reason: "already-bought" });
return;
}
if (!this.hasResources(model) && !isInDevMode) {
callback({ itemBought: false, reason: "cannot-afford" });
callback(false /*itemBought*/, { reason: "cannot-afford" });
return;
}
var result = this.shouldBeBought(model, this.game);
if(!result.itemBought){
callback(result); //Tell them *why* we failed to buy the item.
var extendedInfo = this.shouldBeBought(model, this.game);
if(extendedInfo.reason !== "paid-for"){
callback(false /*itemBought*/, extendedInfo); //Tell them *why* we failed to buy the item.
return;
}
this.payPrice(model);
this.onPurchase(model);
callback({ itemBought: true, reason: (this.game.devMode ? "dev-mode" : "paid-for") });
callback(true /*itemBought*/, { reason: (this.game.devMode ? "dev-mode" : "paid-for") });
this.game.render();
},
onPurchase: function(model){
Expand Down
2 changes: 1 addition & 1 deletion js/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ dojo.declare("com.nuclearunicorn.game.ui.SpaceProgramBtnController", com.nuclear
if (model.metadata.val == 0) {
this.inherited(arguments);
} else {
callback({ itemBought: false, reason: "already-bought" });
callback(false /*itemBought*/, { reason: "already-bought" });
}
},

Expand Down
Loading

0 comments on commit cc47346

Please sign in to comment.