Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
47 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,55 @@ | ||
import content from '../../content/index'; | ||
import i18n from '../../i18n'; | ||
import { | ||
NotAuthorized, | ||
} from '../../libs/errors'; | ||
|
||
module.exports = function buyHealthPotion (user, req = {}, analytics) { | ||
let item = content.potion; | ||
let quantity = req.quantity || 1; | ||
import { AbstractGoldItemOperation} from './abstractBuyOperation'; | ||
|
||
if (user.stats.gp < item.value * quantity) { | ||
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language)); | ||
export class BuyHealthPotionOperation extends AbstractGoldItemOperation { | ||
constructor (user, req, analytics) { | ||
super(user, req, analytics); | ||
} | ||
|
||
if (item.canOwn && !item.canOwn(user)) { | ||
throw new NotAuthorized(i18n.t('cannotBuyItem', req.language)); | ||
multiplePurchaseAllowed () { | ||
return true; | ||
} | ||
|
||
if (user.stats.hp >= 50) { | ||
throw new NotAuthorized(i18n.t('messageHealthAlreadyMax', req.language)); | ||
} | ||
extractAndValidateParams (user) { | ||
let item = content.potion; | ||
let userHp = user.stats.hp; | ||
|
||
if (user.stats.hp <= 0) { | ||
throw new NotAuthorized(i18n.t('messageHealthAlreadyMin', req.language)); | ||
} | ||
super.canUserPurchase(user, item); | ||
|
||
user.stats.hp += 15 * quantity; | ||
if (user.stats.hp > 50) { | ||
user.stats.hp = 50; | ||
} | ||
if (userHp >= 50) { | ||
throw new NotAuthorized(this.i18n('messageHealthAlreadyMax')); | ||
} | ||
|
||
user.stats.gp -= item.value * quantity; | ||
if (userHp <= 0) { | ||
throw new NotAuthorized(this.i18n('messageHealthAlreadyMin')); | ||
} | ||
} | ||
|
||
let message = i18n.t('messageBought', { | ||
itemText: item.text(req.language), | ||
}, req.language); | ||
executeChanges (user, item) { | ||
user.stats.hp += 15 * this.quantity; | ||
if (user.stats.hp > 50) { | ||
user.stats.hp = 50; | ||
} | ||
|
||
this.substractCurrency(user, item, this.quantity); | ||
|
||
if (analytics) { | ||
analytics.track('acquire item', { | ||
uuid: user._id, | ||
itemKey: 'Potion', | ||
acquireMethod: 'Gold', | ||
goldCost: item.value, | ||
category: 'behavior', | ||
headers: req.headers, | ||
quantityPurchased: quantity, | ||
let message = this.i18n('messageBought', { | ||
itemText: this.item.text(this.req.language), | ||
}); | ||
|
||
return [ | ||
this.user.stats, | ||
message, | ||
]; | ||
} | ||
|
||
return [ | ||
user.stats, | ||
message, | ||
]; | ||
}; | ||
analyticsData () { | ||
let data = super.analyticsData(); | ||
data.itemKey = 'Potion'; | ||
return data; | ||
} | ||
} |