2016-03-07 22:02:42 +00:00
import content from '../content/index' ;
import i18n from '../i18n' ;
import _ from 'lodash' ;
import splitWhitespace from '../libs/splitWhitespace' ;
import planGemLimits from '../libs/planGemLimits' ;
2016-03-08 16:45:14 +00:00
module . exports = function ( user , req , cb , analytics ) {
2016-03-07 22:02:42 +00:00
var analyticsData , convCap , convRate , item , key , price , ref , ref1 , ref2 , ref3 , type ;
ref = req . params , type = ref . type , key = ref . key ;
if ( type === 'gems' && key === 'gem' ) {
2016-03-08 16:45:14 +00:00
ref1 = api . planGemLimits , convRate = ref1 . convRate , convCap = ref1 . convCap ;
2016-03-07 22:02:42 +00:00
convCap += user . purchased . plan . consecutive . gemCapExtra ;
2016-03-08 16:45:14 +00:00
if ( ! ( ( ref2 = user . purchased ) != null ? ( ref3 = ref2 . plan ) != null ? ref3 . customerId : void 0 : void 0 ) ) {
return typeof cb === "function" ? cb ( {
2016-03-07 22:02:42 +00:00
code : 401 ,
2016-03-08 16:45:14 +00:00
message : "Must subscribe to purchase gems with GP"
2016-03-07 22:02:42 +00:00
} , req ) : void 0 ;
}
if ( ! ( user . stats . gp >= convRate ) ) {
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb ( {
2016-03-07 22:02:42 +00:00
code : 401 ,
2016-03-08 16:45:14 +00:00
message : "Not enough Gold"
2016-03-07 22:02:42 +00:00
} ) : void 0 ;
}
if ( user . purchased . plan . gemsBought >= convCap ) {
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb ( {
2016-03-07 22:02:42 +00:00
code : 401 ,
2016-03-08 16:45:14 +00:00
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."
2016-03-07 22:02:42 +00:00
} ) : void 0 ;
}
user . balance += . 25 ;
user . purchased . plan . gemsBought ++ ;
user . stats . gp -= convRate ;
analyticsData = {
uuid : user . _id ,
itemKey : key ,
acquireMethod : 'Gold' ,
goldCost : convRate ,
category : 'behavior'
} ;
2016-03-08 16:45:14 +00:00
if ( analytics != null ) {
2016-03-07 22:02:42 +00:00
analytics . track ( 'purchase gems' , analyticsData ) ;
}
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb ( {
2016-03-07 22:02:42 +00:00
code : 200 ,
2016-03-08 16:45:14 +00:00
message : "+1 Gem"
2016-03-07 22:02:42 +00:00
} , _ . pick ( user , splitWhitespace ( 'stats balance' ) ) ) : void 0 ;
}
if ( type !== 'eggs' && type !== 'hatchingPotions' && type !== 'food' && type !== 'quests' && type !== 'gear' ) {
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb ( {
2016-03-07 22:02:42 +00:00
code : 404 ,
2016-03-08 16:45:14 +00:00
message : ":type must be in [eggs,hatchingPotions,food,quests,gear]"
2016-03-07 22:02:42 +00:00
} , req ) : void 0 ;
}
if ( type === 'gear' ) {
item = content . gear . flat [ key ] ;
if ( user . items . gear . owned [ key ] ) {
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb ( {
2016-03-07 22:02:42 +00:00
code : 401 ,
message : i18n . t ( 'alreadyHave' , req . language )
} ) : void 0 ;
}
price = ( item . twoHanded || item . gearSet === 'animal' ? 2 : 1 ) / 4 ;
} else {
item = content [ type ] [ key ] ;
price = item . value / 4 ;
}
if ( ! item ) {
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb ( {
2016-03-07 22:02:42 +00:00
code : 404 ,
2016-03-08 16:45:14 +00:00
message : ":key not found for Content." + type
2016-03-07 22:02:42 +00:00
} , req ) : void 0 ;
}
if ( ! item . canBuy ( user ) ) {
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb ( {
2016-03-07 22:02:42 +00:00
code : 403 ,
message : i18n . t ( 'messageNotAvailable' , req . language )
} ) : void 0 ;
}
if ( ( user . balance < price ) || ! user . balance ) {
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb ( {
2016-03-07 22:02:42 +00:00
code : 403 ,
message : i18n . t ( 'notEnoughGems' , req . language )
} ) : void 0 ;
}
user . balance -= price ;
if ( type === 'gear' ) {
user . items . gear . owned [ key ] = true ;
} else {
if ( ! ( user . items [ type ] [ key ] > 0 ) ) {
user . items [ type ] [ key ] = 0 ;
}
user . items [ type ] [ key ] ++ ;
}
analyticsData = {
uuid : user . _id ,
itemKey : key ,
itemType : 'Market' ,
acquireMethod : 'Gems' ,
gemCost : item . value ,
category : 'behavior'
} ;
2016-03-08 16:45:14 +00:00
if ( analytics != null ) {
2016-03-07 22:02:42 +00:00
analytics . track ( 'acquire item' , analyticsData ) ;
}
2016-03-08 16:45:14 +00:00
return typeof cb === "function" ? cb ( null , _ . pick ( user , splitWhitespace ( 'items balance' ) ) ) : void 0 ;
2016-03-07 22:02:42 +00:00
} ;