habitica-self-host/src/app/pets.coffee

75 lines
2.8 KiB
CoffeeScript
Raw Normal View History

2013-03-16 18:58:09 +00:00
_ = require 'underscore'
{ randomVal } = require './helpers'
{ pets, hatchingPotions } = require('habitrpg-shared/script/items').items
2013-03-09 13:30:06 +00:00
2013-03-08 17:44:21 +00:00
###
app exports
###
module.exports.app = (appExports, model) ->
user = model.at '_user'
2013-03-16 20:30:45 +00:00
appExports.chooseEgg = (e, el) ->
model.ref '_hatchEgg', e.at()
2013-03-16 20:30:45 +00:00
appExports.hatchEgg = (e, el) ->
hatchingPotionName = $(el).children('select').val()
myHatchingPotion = user.get 'items.hatchingPotions'
egg = model.get '_hatchEgg'
2013-03-16 20:30:45 +00:00
eggs = user.get 'items.eggs'
myPets = user.get 'items.pets'
2013-03-16 20:30:45 +00:00
hatchingPotionIdx = myHatchingPotion.indexOf hatchingPotionName
2013-03-16 20:30:45 +00:00
eggIdx = eggs.indexOf egg
return alert "You don't own that hatching potion yet, complete more tasks!" if hatchingPotionIdx is -1
return alert "You don't own that egg yet, complete more tasks!" if eggIdx is -1
return alert "You already have that pet, hatch a different combo." if myPets and myPets.indexOf("#{egg.name}-#{hatchingPotionName}") != -1
2013-03-16 20:30:45 +00:00
user.push 'items.pets', egg.name + '-' + hatchingPotionName
eggs.splice eggIdx, 1
myHatchingPotion.splice hatchingPotionIdx, 1
user.set 'items.eggs', eggs
2013-03-27 18:57:40 +00:00
user.set 'items.hatchingPotions', myHatchingPotion
alert 'Your egg hatched! Visit your stable to equip your pet.'
#FIXME Bug: this removes from the array properly in the browser, but on refresh is has removed all items from the arrays
# user.remove 'items.hatchingPotions', hatchingPotionIdx, 1
# user.remove 'items.eggs', eggIdx, 1
2013-03-16 20:30:45 +00:00
2013-05-07 06:37:00 +00:00
appExports.choosePet = (e, el, next) ->
2013-03-27 01:52:18 +00:00
petStr = $(el).attr('data-pet')
2013-05-07 06:37:00 +00:00
return next() if user.get('items.pets').indexOf(petStr) == -1
# If user's pet is already active, deselect it
return user.set 'items.currentPet', {} if user.get('items.currentPet.str') is petStr
2013-03-27 01:52:18 +00:00
[name, modifier] = petStr.split('-')
pet = _.findWhere pets, name: name
2013-03-16 18:58:09 +00:00
pet.modifier = modifier
2013-03-16 19:09:16 +00:00
pet.str = petStr
2013-05-07 06:37:00 +00:00
user.set 'items.currentPet', pet
2013-03-16 20:30:45 +00:00
appExports.buyHatchingPotion = (e, el) ->
name = $(el).attr 'data-hatchingPotion'
2013-03-27 18:57:40 +00:00
newHatchingPotion = _.findWhere hatchingPotions, name: name
gems = user.get('balance') * 4
if gems >= newHatchingPotion.value
if confirm "Buy this hatching potion with #{newHatchingPotion.value} of your #{gems} Gems?"
user.push 'items.hatchingPotions', newHatchingPotion.name
user.set 'balance', (gems - newHatchingPotion.value) / 4
2013-03-16 20:30:45 +00:00
else
$('#more-gems-modal').modal 'show'
appExports.buyEgg = (e, el) ->
name = $(el).attr 'data-egg'
newEgg = _.findWhere pets, name: name
gems = user.get('balance') * 4
if gems >= newEgg.value
if confirm "Buy this egg with #{newEgg.value} of your #{gems} Gems?"
user.push 'items.eggs', newEgg
user.set 'balance', (gems - newEgg.value) / 4
else
$('#more-gems-modal').modal 'show'