2013-03-16 18:58:09 +00:00
|
|
|
_ = require 'underscore'
|
2013-03-23 04:17:25 +00:00
|
|
|
{ randomVal } = require './helpers'
|
2013-03-09 20:34:48 +00:00
|
|
|
{ pets, food } = require('./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'
|
|
|
|
|
|
|
|
|
|
user.on 'set', 'flags.dropsEnabled', (captures, args) ->
|
|
|
|
|
return unless captures == true
|
2013-03-09 13:30:06 +00:00
|
|
|
|
2013-03-21 19:55:31 +00:00
|
|
|
egg = randomVal pets
|
2013-03-09 13:30:06 +00:00
|
|
|
|
2013-03-10 01:03:24 +00:00
|
|
|
dontPersist = model._dontPersist
|
|
|
|
|
|
2013-03-23 04:17:25 +00:00
|
|
|
model._dontPersist = false
|
2013-03-10 01:03:24 +00:00
|
|
|
user.push 'items.eggs', egg
|
2013-03-23 04:17:25 +00:00
|
|
|
model._dontPersist = dontPersist
|
2013-03-09 13:30:06 +00:00
|
|
|
|
2013-03-13 15:47:37 +00:00
|
|
|
$('#drops-enabled-modal').modal 'show'
|
2013-03-09 17:03:38 +00:00
|
|
|
|
2013-03-16 20:30:45 +00:00
|
|
|
appExports.chooseEgg = (e, el) ->
|
2013-03-23 04:17:25 +00:00
|
|
|
model.ref '_feedEgg', e.at()
|
2013-03-16 20:30:45 +00:00
|
|
|
|
|
|
|
|
appExports.feedEgg = (e, el) ->
|
|
|
|
|
foodName = $(el).children('select').val()
|
|
|
|
|
myFood = user.get 'items.food'
|
|
|
|
|
egg = model.get '_feedEgg'
|
|
|
|
|
eggs = user.get 'items.eggs'
|
2013-03-25 18:00:23 +00:00
|
|
|
myPets = user.get 'items.pets'
|
2013-03-16 20:30:45 +00:00
|
|
|
|
|
|
|
|
foodIdx = myFood.indexOf foodName
|
|
|
|
|
eggIdx = eggs.indexOf egg
|
|
|
|
|
|
|
|
|
|
return alert "You don't own that food :\\" if foodIdx is -1
|
|
|
|
|
return alert "You don't own that egg :\\" if eggIdx is -1
|
2013-03-25 18:00:23 +00:00
|
|
|
return alert "You already have that pet." if myPets and myPets.indexOf("#{egg.name}-#{foodName}") != -1
|
2013-03-16 20:30:45 +00:00
|
|
|
|
2013-03-23 04:17:25 +00:00
|
|
|
user.push 'items.pets', egg.name + '-' + foodName
|
2013-03-25 15:42:30 +00:00
|
|
|
|
|
|
|
|
eggs.splice eggIdx, 1
|
|
|
|
|
myFood.splice foodIdx, 1
|
|
|
|
|
user.set 'items.eggs', eggs
|
|
|
|
|
user.set 'items.food', myFood
|
|
|
|
|
|
2013-03-27 01:24:12 +00:00
|
|
|
alert 'Your egg hatched! Visit your stable to equip your pet.'
|
|
|
|
|
|
2013-03-25 15:42:30 +00:00
|
|
|
#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.food', foodIdx, 1
|
|
|
|
|
# user.remove 'items.eggs', eggIdx, 1
|
2013-03-16 20:30:45 +00:00
|
|
|
|
2013-03-16 18:58:09 +00:00
|
|
|
appExports.choosePet = (e, el) ->
|
2013-03-27 01:52:18 +00:00
|
|
|
petStr = $(el).attr('data-pet')
|
|
|
|
|
[name, modifier] = petStr.split('-')
|
2013-03-23 04:17:25 +00:00
|
|
|
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-03-16 18:58:09 +00:00
|
|
|
ownsThisPet = user.get('items.pets').indexOf petStr
|
2013-03-09 17:03:38 +00:00
|
|
|
if ownsThisPet != -1
|
2013-03-16 19:17:55 +00:00
|
|
|
# If user's pet is already active, deselect it
|
|
|
|
|
pet = {} if user.get('items.currentPet.str') is petStr
|
2013-03-09 17:03:38 +00:00
|
|
|
user.set 'items.currentPet', pet
|
2013-03-16 20:30:45 +00:00
|
|
|
|
|
|
|
|
appExports.buyFood = (e, el) ->
|
|
|
|
|
name = $(el).attr 'data-food'
|
|
|
|
|
newFood = _.findWhere food, name: name
|
|
|
|
|
tokens = user.get('balance') * 4
|
|
|
|
|
if tokens > newFood.value
|
|
|
|
|
if confirm "Buy this food with #{newFood.value} of your #{tokens} tokens?"
|
2013-03-23 04:17:25 +00:00
|
|
|
user.push 'items.food', newFood.name
|
2013-03-16 20:30:45 +00:00
|
|
|
user.set 'balance', (tokens - newFood.value) / 4
|
|
|
|
|
else
|
|
|
|
|
$('#more-tokens-modal').modal 'show'
|
2013-03-25 21:41:41 +00:00
|
|
|
|
|
|
|
|
appExports.buyEgg = (e, el) ->
|
|
|
|
|
name = $(el).attr 'data-egg'
|
|
|
|
|
newEgg = _.findWhere pets, name: name
|
|
|
|
|
tokens = user.get('balance') * 4
|
|
|
|
|
if tokens > newEgg.value
|
|
|
|
|
if confirm "Buy this egg with #{newEgg.value} of your #{tokens} tokens?"
|
|
|
|
|
user.push 'items.eggs', newEgg
|
|
|
|
|
user.set 'balance', (tokens - newEgg.value) / 4
|
|
|
|
|
else
|
|
|
|
|
$('#more-tokens-modal').modal 'show'
|