Clean up helpers + choose Pet

This commit is contained in:
Daniel Saewitz 2013-03-16 14:58:09 -04:00
parent 2ffe0aecf9
commit 419351494c
5 changed files with 37 additions and 27 deletions

View file

@ -95,7 +95,7 @@ module.exports.app = (appExports, model) ->
return alert "You don't own that food :\\" if foodIdx is -1
return alert "You don't own that egg :\\" if eggIdx is -1
user.push 'items.pets', egg.name + '-' + food
user.push 'items.pets', egg.text + '-' + food
user.remove 'items.food', foodIdx, 1
user.remove 'items.eggs', eggIdx, 1

View file

@ -1,25 +1,28 @@
moment = require 'moment'
# Absolute diff between two dates
module.exports.daysBetween = (yesterday, now, dayStart) ->
daysBetween = (yesterday, now, dayStart) ->
#sanity-check reset-time (is it 24h time?)
dayStart = 0 unless (dayStart? and (dayStart = parseInt(dayStart)) and dayStart >= 0 and dayStart <= 24)
Math.abs moment(yesterday).startOf('day').add('h', dayStart).diff(moment(now), 'days')
module.exports.dayMapping = dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s',7:'su'}
dayMapping = dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s',7:'su'}
# http://stackoverflow.com/questions/2532218/pick-random-property-from-a-javascript-object
# obj: object
# returns random property (the value)
module.exports.randomProp = (obj) ->
randomProp = (obj) ->
result = undefined
count = 0
for key, val of obj
result = val if Math.random() < (1 / ++count)
result
module.exports.viewHelpers = (view) ->
removeWhitespace = (str) ->
return '' unless str
str.replace /\s/g, ''
viewHelpers = (view) ->
view.fn "percent", (x, y) ->
x=1 if x==0
Math.round(x/y*100)
@ -47,6 +50,6 @@ module.exports.viewHelpers = (view) ->
loc = window?.location.host or process.env.BASE_URL
encodeURIComponent "http://#{loc}/v1/users/#{uid}/calendar.ics?apiToken=#{apiToken}"
view.fn 'removeWhitespace', (str) ->
return '' unless str
str.replace /\s/g, ''
view.fn 'removeWhitespace', removeWhitespace
module.exports = { viewHelpers, removeWhitespace, randomProp, daysBetween, dayMapping }

View file

@ -1,4 +1,5 @@
{ randomProp } = require './helpers'
_ = require 'underscore'
{ randomProp, removeWhitespace } = require './helpers'
{ pets, food } = require('./items').items
###
@ -20,10 +21,14 @@ module.exports.app = (appExports, model) ->
$('#drops-enabled-modal').modal 'show'
appExports.selectPet = (e, el) ->
name = $(el).attr('data-pet')
pet = _.findWhere pets, {name:name}
ownsThisPet = user.get('items.pets').indexOf(name)
appExports.choosePet = (e, el) ->
petArray = $(el).attr('data-pet').split '-'
text = petArray[0]
modifier = petArray[1]
petStr = "#{text}-#{modifier}"
pet = _.findWhere pets, text: text
pet.modifier = modifier
ownsThisPet = user.get('items.pets').indexOf petStr
if ownsThisPet != -1
user.set 'items.currentPet', pet
else
@ -31,7 +36,7 @@ module.exports.app = (appExports, model) ->
if tokens > pet.value
r = confirm("Buy this pet with #{pet.value} of your #{tokens} tokens?");
if r
user.push 'items.pets', name
user.push 'items.pets', text
user.set 'items.currentPet', pet
user.set 'balance', (tokens - pet.value)/4
else

View file

@ -116,10 +116,10 @@ score = (model, taskId, direction, times, batch, cron) ->
batch.commit()
# 1% chance of getting a pet or meat
if obj.flags.dropsEnabled and Math.random() < .50
if obj.flags.dropsEnabled and Math.random() < .5
if Math.random() < .5
drop = randomProp(food)
user.push 'items.food', drop.name
user.push 'items.food', drop.text
drop.type = 'Food'
else
drop = randomProp(pets)

View file

@ -190,6 +190,18 @@
<div class='row-fluid'>
<div class='span6 well'>
<menu type="list">
<h3>Eggs</h3>
{#if not(_user.items.eggs)}
<p>You don't have any eggs yet.</p>
{/if}
<li class="customize-menu">
{#each _user.items.eggs as :egg}
<div>
<button class="customize-option Pet-Egg" x-bind="click: chooseEgg"></button>
<p>{:egg.text}</p>
</div>
{/each}
</li>
<h3>Food</h3>
{#if not(_user.items.food)}
<p>You don't have any food yet.</p>
@ -199,16 +211,6 @@
<button class="customize-option" x-bind="click: chooseFood">{:food}</button>
{/each}
</li>
<h3>Eggs</h3>
{#if not(_user.items.eggs)}
<p>You don't have any eggs yet.</p>
{/if}
<li class="customize-menu">
{#each _user.items.eggs as :egg}
<button class="customize-option Pet-Egg" x-bind="click: chooseEgg"></button>
<p>{:egg.text}</p>
{/each}
</li>
</menu>
</div>
{#if _feedEgg}
@ -239,7 +241,7 @@
<p>You don't have any pets yet.</p>
{/if}
{#each _user.items.pets as :pet}
<div class="Pet-{removeWhitespace(:pet)}">{:pet}</div>
<div class="Pet-{removeWhitespace(:pet)}" data-pet="{:pet}" x-bind="click:choosePet">{:pet}</div>
{/each}
</div>
</div>