Skip to content

Instantly share code, notes, and snippets.

@SabreCat
Created December 25, 2014 18:56
Show Gist options
  • Save SabreCat/beedc81740c481dcd701 to your computer and use it in GitHub Desktop.
Save SabreCat/beedc81740c481dcd701 to your computer and use it in GitHub Desktop.
Purchasing
purchase: (req, cb, ga) ->
{type,key} = req.params
if type is 'gems' and key is 'gem'
{convRate, convCap} = api.planGemLimits
convCap += user.purchased.plan.consecutive.gemCapExtra
return cb?({code:401,message:"Must subscribe to purchase gems with GP"},req) unless user.purchased?.plan?.customerId
return cb?({code:401,message:"Not enough Gold"}) unless user.stats.gp >= convRate
return cb?({code:401,message:"You've reached the Gold=>Gem conversion cap (#{convCap}) for this month. We have this to prevent abuse / farming. The cap will reset within the first three days of next month."}) if user.purchased.plan.gemsBought >= convCap
user.balance += .25
user.purchased.plan.gemsBought++
user.stats.gp -= convRate
return cb? {code:200,message:"+1 Gems"}, _.pick(user,$w 'stats balance')
return cb?({code:404,message:":type must be in [hatchingPotions,eggs,food,quests,special,weapon,armor,head,shield]"},req) unless type in ['eggs','hatchingPotions','food','quests','special','weapon','armor','head','shield']
item = content[type][key]
return cb?({code:404,message:":key not found for Content.#{type}"},req) unless item
if type in ['weapon','armor','head','shield']
return cb?({code:401, message: i18n.t('alreadyHave', req.language)}) if user.items[type][key]
return cb?({code:401, message: i18n.t('notEnoughGems', req.language)}) if user.balance < ((1 + item.twoHanded) / 4)
user.balance -= ((1 + item.twoHanded) / 4)
user.items[type][key] = true
else
return cb?({code:401, message: i18n.t('notEnoughGems', req.language)}) if user.balance < (item.value / 4)
user.balance -= (item.value / 4)
user.items[type][key] = 0 unless user.items[type][key] > 0
user.items[type][key]++
cb? null, _.pick(user,$w 'items balance')
ga?.event('purchase', key).send()
$scope.purchase = function(type, item){
var gems = User.user.balance * 4;
var string = (type == 'weapon') ? window.env.t('weapon') : (type == 'armor') ? window.env.t('armor') : (type == 'head') ? window.env.t('headgear') : (type == 'shield') ? window.env.t('offhand') : (type == 'hatchingPotions') ? window.env.t('hatchingPotion') : (type == 'eggs') ? window.env.t('eggSingular') : (type == 'quests') ? window.env.t('quest') : (item.key == 'Saddle') ? window.env.t('foodSaddleText').toLowerCase() : type; // this is ugly but temporary, once the purchase modal is done this will be removed
if (type == 'weapon' || type == 'armor' || type == 'head' || type == 'shield') {
if (gems < ((item.specialClass == "wizard") && (item.type == "weapon")) + 1) return $rootScope.openModal('buyGems');
var message = window.env.t('buyThis', {text: string, price: ((item.specialClass == "wizard") && (item.type == "weapon")) + 1, gems: gems})
} else {
if(gems < item.value) return $rootScope.openModal('buyGems');
var message = window.env.t('buyThis', {text: string, price: item.value, gems: gems})
}
if($window.confirm(message))
User.user.ops.purchase({params:{type:type,key:item.key}});
}
@SabreCat
Copy link
Author

I'm getting a TypeError: Cannot read property 'weapon_special_candycane' of undefined in line 16 of index.coffee there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment