2013-02-05 05:20:58 +00:00
|
|
|
_ = require('underscore')
|
2013-02-08 22:44:06 +00:00
|
|
|
character = require './character'
|
2013-02-06 18:59:56 +00:00
|
|
|
browser = require './browser'
|
2013-02-05 06:37:13 +00:00
|
|
|
|
2013-02-12 00:27:35 +00:00
|
|
|
partyUnsubscribe = (model, cb) ->
|
2013-02-08 00:13:14 +00:00
|
|
|
if window?
|
2013-02-18 18:40:09 +00:00
|
|
|
throw "unsubscribe requires cb" unless cb?
|
2013-02-08 00:13:14 +00:00
|
|
|
subs = model._subs()
|
2013-02-18 18:40:09 +00:00
|
|
|
subs.concat cb
|
2013-02-08 00:13:14 +00:00
|
|
|
model.unsubscribe.apply(model, subs)
|
2013-02-18 18:40:09 +00:00
|
|
|
else
|
|
|
|
|
cb()
|
|
|
|
|
|
|
|
|
|
###
|
2013-02-20 00:36:57 +00:00
|
|
|
Subscribe to the user, the users's party (meta info like party name, member ids, etc), and the party's members. 3 subscriptions.
|
|
|
|
|
1) If the user is solo, just subscribe to the user.
|
|
|
|
|
2) If in a an empty party, just subscribe to the user & party meta.
|
|
|
|
|
3) If full party, subscribe to everything.
|
2013-02-18 18:40:09 +00:00
|
|
|
###
|
2013-02-25 23:59:14 +00:00
|
|
|
module.exports.partySubscribe = partySubscribe = (page, model, params, next, cb) ->
|
2013-02-08 00:13:14 +00:00
|
|
|
|
|
|
|
|
# unsubscribe from everything - we're starting over
|
2013-02-18 20:26:10 +00:00
|
|
|
# partyUnsubscribe model, ->
|
2013-02-08 00:13:14 +00:00
|
|
|
|
2013-02-18 20:26:10 +00:00
|
|
|
# Restart subscription to the main user
|
2013-02-21 17:45:05 +00:00
|
|
|
selfQ = model.query('users').withId model.get('_userId') #or model.session.userId # see http://goo.gl/TPYIt
|
2013-02-19 02:53:50 +00:00
|
|
|
selfQ.subscribe (err, self) ->
|
2013-02-25 23:59:14 +00:00
|
|
|
return next(err) if err
|
2013-02-19 02:53:50 +00:00
|
|
|
u = self.at(0)
|
2013-02-18 20:26:10 +00:00
|
|
|
uObj = u.get()
|
|
|
|
|
|
2013-02-25 23:59:14 +00:00
|
|
|
# Attempted handling for 'party of undefined' error, which is caused by bustedSession (see derby-auth).
|
|
|
|
|
# Theoretically simply reloading the page should restore model.at('_userId') and the second load should work just fine
|
|
|
|
|
# bustedSession victims might hit a redirection loop if I'm wrong :/
|
|
|
|
|
return page.redirect('/') unless uObj
|
|
|
|
|
|
2013-02-19 22:33:34 +00:00
|
|
|
## (1) User is solo, just return that subscription
|
|
|
|
|
unless uObj.party?.current?
|
|
|
|
|
model.ref '_user', u
|
|
|
|
|
return cb()
|
|
|
|
|
|
2013-02-20 00:36:57 +00:00
|
|
|
###
|
|
|
|
|
Note this strange hack - we subscribe to queries incrementally. First self, then party, then party members.
|
|
|
|
|
Party members come with limited fields, so you can't hack their stuff. Strangely, subscribing to the members after
|
|
|
|
|
already subscribing to self limits self's fields to the fields which members are limited to. As a result, we have
|
|
|
|
|
to re-subscribe to self to get all the fields (otherwise everything breaks). Weirdly, this last subscription doesn't
|
|
|
|
|
do the opposite - granting all the fields back to members. I dont' know what's going on here
|
|
|
|
|
|
|
|
|
|
Another issue: `model.unsubscribe(selfQ)` would seem to mitigate the above, so we at least don't have a stray
|
|
|
|
|
subscription floating around - but alas, it doesn't seem to work (or at least never calls the callback)
|
|
|
|
|
###
|
2013-02-18 20:26:10 +00:00
|
|
|
finished = ->
|
2013-02-20 00:36:57 +00:00
|
|
|
# model.unsubscribe selfQ, ->
|
2013-02-19 23:49:57 +00:00
|
|
|
selfQ.subscribe (err, self) ->
|
2013-02-25 23:59:14 +00:00
|
|
|
return next(err) if err
|
2013-02-19 23:49:57 +00:00
|
|
|
model.ref '_user', self.at(0)
|
|
|
|
|
cb()
|
2013-02-18 20:26:10 +00:00
|
|
|
|
|
|
|
|
# User in a party
|
|
|
|
|
partiesQ = model.query('parties').withId(uObj.party.current)
|
2013-02-19 21:49:38 +00:00
|
|
|
partiesQ.fetch (err, res) ->
|
2013-02-25 23:59:14 +00:00
|
|
|
return next(err) if err
|
2013-02-18 20:26:10 +00:00
|
|
|
p = res.at(0)
|
|
|
|
|
model.ref '_party', p
|
|
|
|
|
ids = p.get('members')
|
|
|
|
|
|
|
|
|
|
# FIXME this is the kicker right here. This isn't getting triggered, and it's the reason why we have to refresh
|
|
|
|
|
# after every event. Get this working
|
|
|
|
|
#p.on '*', 'members', (ids) ->
|
|
|
|
|
# console.log("members listener got called")
|
|
|
|
|
# debugger
|
|
|
|
|
# membersSubscribe model, ids
|
|
|
|
|
|
|
|
|
|
## (2) Party has no members, just subscribe to the party itself
|
|
|
|
|
if _.isEmpty(ids)
|
|
|
|
|
return finished()
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
## (3) Party has members, subscribe to those users too
|
|
|
|
|
membersQ = model.query('users').party(ids)
|
2013-02-19 21:49:38 +00:00
|
|
|
membersQ.fetch (err, members) ->
|
2013-02-25 23:59:14 +00:00
|
|
|
return next(err) if err
|
2013-02-18 20:26:10 +00:00
|
|
|
model.ref '_partyMembers', members
|
|
|
|
|
finished()
|
2013-02-06 04:42:30 +00:00
|
|
|
|
2013-02-06 05:47:08 +00:00
|
|
|
module.exports.app = (appExports, model) ->
|
|
|
|
|
user = model.at('_user')
|
2013-02-06 01:21:05 +00:00
|
|
|
|
2013-02-08 07:01:48 +00:00
|
|
|
user.on 'set', 'flags.partyEnabled', (captures, args) ->
|
|
|
|
|
return unless captures == true
|
2013-02-08 23:45:22 +00:00
|
|
|
$('.main-avatar').popover('destroy') #remove previous popovers
|
2013-02-08 07:01:48 +00:00
|
|
|
html = """
|
|
|
|
|
<div class='party-system-popover'>
|
|
|
|
|
<img src='/img/party-unlocked.png' style='float:right;padding:5px;' />
|
|
|
|
|
Congratulations, you have unlocked the Party System! You can now group with your friends by adding their User Ids.
|
|
|
|
|
<a href='#' onClick="$('.main-avatar').popover('hide');return false;">[Close]</a>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
$('.main-avatar').popover
|
|
|
|
|
title: "Party System Unlocked"
|
|
|
|
|
placement: 'bottom'
|
|
|
|
|
trigger: 'manual'
|
|
|
|
|
html: true
|
|
|
|
|
content: html
|
|
|
|
|
$('.main-avatar').popover 'show'
|
|
|
|
|
|
2013-02-08 00:13:14 +00:00
|
|
|
model.on 'set', '_user.party.invitation', -> partySubscribe(model)
|
2013-02-06 01:21:05 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyCreate = ->
|
2013-02-05 05:20:58 +00:00
|
|
|
newParty = model.get("_newParty")
|
2013-02-05 23:12:39 +00:00
|
|
|
id = model.add 'parties', { name: newParty, leader: user.get('id'), members: [user.get('id')], invites:[] }
|
2013-02-19 21:49:38 +00:00
|
|
|
user.set 'party', {current: id, invitation: null, leader: true}, ->
|
2013-02-19 22:33:34 +00:00
|
|
|
window.location.reload true
|
2013-02-19 21:49:38 +00:00
|
|
|
# partySubscribe model, -> $('#party-modal').modal('show')
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyInvite = ->
|
2013-02-05 05:20:58 +00:00
|
|
|
id = model.get('_newPartyMember').replace(/[\s"]/g, '')
|
|
|
|
|
return if _.isEmpty(id)
|
|
|
|
|
|
|
|
|
|
obj = user.get()
|
|
|
|
|
query = model.query('users').party([id])
|
2013-02-06 05:47:08 +00:00
|
|
|
model.fetch query, (err, res) ->
|
2013-02-05 21:11:28 +00:00
|
|
|
throw err if err
|
2013-02-06 05:47:08 +00:00
|
|
|
u = res.at(0).get()
|
|
|
|
|
if !u?.id?
|
2013-02-05 05:20:58 +00:00
|
|
|
model.set "_view.partyError", "User with id #{id} not found."
|
|
|
|
|
return
|
2013-02-06 05:47:08 +00:00
|
|
|
else if u.party.current? or u.party.invitation?
|
2013-02-05 05:20:58 +00:00
|
|
|
model.set "_view.partyError", "User already in a party or pending invitation."
|
|
|
|
|
return
|
|
|
|
|
else
|
2013-02-06 05:15:00 +00:00
|
|
|
p = model.at '_party'
|
|
|
|
|
p.push "invites", id
|
|
|
|
|
model.set "users.#{id}.party.invitation", p.get('id')
|
2013-02-05 05:20:58 +00:00
|
|
|
$.bootstrapGrowl "Invitation Sent."
|
|
|
|
|
$('#party-modal').modal('hide')
|
2013-02-19 22:33:34 +00:00
|
|
|
model.set '_newPartyMember', '', -> window.location.reload true
|
2013-02-19 21:49:38 +00:00
|
|
|
#partySubscribe model
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyAccept = ->
|
2013-02-19 21:49:38 +00:00
|
|
|
partyId = user.get('party.invitation')
|
2013-02-08 00:13:14 +00:00
|
|
|
user.set 'party.invitation', null
|
2013-02-19 21:49:38 +00:00
|
|
|
user.set 'party.current', partyId
|
2013-02-19 22:08:48 +00:00
|
|
|
# model.push "parties.#{partyId}.members", user.get('id'), -> #FIXME why this not working?
|
|
|
|
|
model.query('parties').withId(partyId).fetch (err, p) ->
|
|
|
|
|
members = p.at(0).get('members')
|
|
|
|
|
members.push user.get('id')
|
|
|
|
|
p.at(0).set 'members', members, ->
|
|
|
|
|
window.location.reload true
|
2013-02-19 21:49:38 +00:00
|
|
|
# partySubscribe model, ->
|
|
|
|
|
# p = model.at('_party')
|
|
|
|
|
# p.push 'members', user.get('id')
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyReject = ->
|
2013-02-05 05:20:58 +00:00
|
|
|
user.set 'party.invitation', null
|
2013-02-06 18:59:56 +00:00
|
|
|
browser.resetDom(model)
|
2013-02-05 21:11:28 +00:00
|
|
|
# TODO splice parties.*.invites[key]
|
2013-02-05 05:20:58 +00:00
|
|
|
# TODO notify sender
|
|
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyLeave = ->
|
2013-02-05 23:52:29 +00:00
|
|
|
id = user.set 'party.current', null
|
2013-02-06 05:15:00 +00:00
|
|
|
p = model.at '_party'
|
|
|
|
|
members = p.get('members')
|
2013-02-05 23:12:39 +00:00
|
|
|
index = members.indexOf(user.get('id'))
|
2013-02-06 03:27:19 +00:00
|
|
|
members.splice(index,1)
|
2013-02-19 21:49:38 +00:00
|
|
|
p.set 'members', members, ->
|
|
|
|
|
if (members.length == 0)
|
2013-02-19 22:33:34 +00:00
|
|
|
# last member out, kill the party
|
|
|
|
|
model.del "parties.#{id}", -> window.location.reload true
|
2013-02-19 21:49:38 +00:00
|
|
|
else
|
2013-02-19 22:33:34 +00:00
|
|
|
window.location.reload true
|
2013-02-19 21:49:38 +00:00
|
|
|
# model.set '_party', null
|
|
|
|
|
# model.set '_partyMembers', null
|
|
|
|
|
# partyUnsubscribe model, ->
|
2013-02-21 17:45:05 +00:00
|
|
|
# selfQ = model.query('users').withId model.get('_userId') #or model.session.userId # see http://goo.gl/TPYIt
|
2013-02-19 21:49:38 +00:00
|
|
|
# selfQ.subscribe (err, u) ->
|
|
|
|
|
# model.ref '_user', u.at(0)
|
|
|
|
|
# browser.resetDom model
|