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-26 21:37:47 +00:00
|
|
|
|
|
|
|
|
Note a 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 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-26 21:37:47 +00:00
|
|
|
selfQ = model.query('users').withId (model.get('_userId') or model.session.userId) # see http://goo.gl/TPYIt
|
|
|
|
|
selfQ.fetch (err, user) ->
|
2013-02-25 23:59:14 +00:00
|
|
|
return next(err) if err
|
2013-02-26 21:37:47 +00:00
|
|
|
return next("User not found - this shouldn't be happening!") unless user.get()
|
|
|
|
|
|
|
|
|
|
finished = (descriptors, paths) ->
|
|
|
|
|
model.subscribe.apply model, descriptors.concat ->
|
|
|
|
|
[err, refs] = [arguments[0], arguments]
|
|
|
|
|
return next(err) if err
|
|
|
|
|
_.each paths, (path, idx) -> model.ref path, refs[idx+1]
|
|
|
|
|
cb()
|
|
|
|
|
|
2013-02-18 20:26:10 +00:00
|
|
|
|
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 :/
|
2013-02-26 21:37:47 +00:00
|
|
|
# return page.redirect('/') unless uObj
|
|
|
|
|
|
|
|
|
|
partyId = user.get('party.current')
|
|
|
|
|
|
|
|
|
|
# (1) Solo player
|
2013-02-26 21:54:31 +00:00
|
|
|
return finished([selfQ], ['_user']) unless partyId
|
2013-02-18 20:26:10 +00:00
|
|
|
|
|
|
|
|
# User in a party
|
2013-02-26 21:37:47 +00:00
|
|
|
partyQ = model.query('parties').withId(partyId)
|
|
|
|
|
partyQ.fetch (err, party) ->
|
2013-02-25 23:59:14 +00:00
|
|
|
return next(err) if err
|
2013-02-26 21:37:47 +00:00
|
|
|
members = party.get('members')
|
2013-02-18 20:26:10 +00:00
|
|
|
|
|
|
|
|
## (2) Party has no members, just subscribe to the party itself
|
2013-02-26 21:37:47 +00:00
|
|
|
return finished([partyQ, selfQ], ['_party', '_user']) if _.isEmpty(members)
|
2013-02-18 20:26:10 +00:00
|
|
|
|
2013-02-26 21:37:47 +00:00
|
|
|
## (3) Party has members, subscribe to those users too
|
|
|
|
|
membersQ = model.query('users').party(members)
|
|
|
|
|
return finished [partyQ, membersQ, selfQ], ['_party', '_partyMembers', '_user']
|
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-03-15 22:39:39 +00:00
|
|
|
unlockPartiesNotification = ->
|
2013-03-26 19:54:34 +00:00
|
|
|
$('.main-herobox').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.
|
2013-03-26 19:54:34 +00:00
|
|
|
<a href='#' onClick="$('.main-herobox').popover('hide');return false;">[Close]</a>
|
2013-02-08 07:01:48 +00:00
|
|
|
</div>
|
|
|
|
|
"""
|
2013-03-26 19:54:34 +00:00
|
|
|
$('.main-herobox').popover
|
2013-02-08 07:01:48 +00:00
|
|
|
title: "Party System Unlocked"
|
|
|
|
|
placement: 'bottom'
|
|
|
|
|
trigger: 'manual'
|
|
|
|
|
html: true
|
|
|
|
|
content: html
|
2013-03-26 19:54:34 +00:00
|
|
|
$('.main-herobox').popover 'show'
|
2013-02-08 07:01:48 +00:00
|
|
|
|
2013-03-15 22:39:39 +00:00
|
|
|
appExports.manuallyUnlockParties = ->
|
|
|
|
|
$("#settings-modal").modal("hide")
|
|
|
|
|
user.set('flags.partyEnabled', true)
|
|
|
|
|
unlockPartiesNotification()
|
|
|
|
|
|
|
|
|
|
user.on 'set', 'flags.partyEnabled', (captures, args) ->
|
|
|
|
|
unlockPartiesNotification() if captures == true
|
|
|
|
|
|
2013-02-27 21:21:06 +00:00
|
|
|
#TODO implement this when we have unsubscribe working properly
|
2013-02-27 21:40:17 +00:00
|
|
|
model.on 'set', '_user.party.invitation', (after, before) ->
|
|
|
|
|
if !before? and after? # they just got invited
|
|
|
|
|
# if they haven't unlocked parties yet, unlock it for them
|
|
|
|
|
user.set('flags.partyEnabled',true) unless user.get('flags.partyEnabled')
|
|
|
|
|
window.setTimeout (-> window.location.reload true), 1000
|
|
|
|
|
#partySubscribe(null, model, null, null, null)
|
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)
|
|
|
|
|
|
2013-02-27 21:21:06 +00:00
|
|
|
model.query('users').party([id]).fetch (err, res) ->
|
2013-02-05 21:11:28 +00:00
|
|
|
throw err if err
|
2013-02-27 21:21:06 +00:00
|
|
|
u = res.at(0).get()
|
|
|
|
|
if !u?
|
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'
|
2013-02-27 21:21:06 +00:00
|
|
|
#p.push "invites", id
|
2013-02-05 05:20:58 +00:00
|
|
|
$.bootstrapGrowl "Invitation Sent."
|
|
|
|
|
$('#party-modal').modal('hide')
|
2013-02-27 21:21:06 +00:00
|
|
|
model.set "users.#{id}.party.invitation", p.get('id'), -> window.location.reload(true)
|
|
|
|
|
#model.set '_newPartyMember', ''
|
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-27 21:21:06 +00:00
|
|
|
model.at("parties.#{partyId}.members").push user.get('id'), -> window.location.reload(true)
|
|
|
|
|
# model.query('parties').withId(partyId).fetch (err, p) ->
|
|
|
|
|
# members = p.get('members')
|
|
|
|
|
# members.push user.get('id')
|
|
|
|
|
# p.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) ->
|
2013-02-26 21:37:47 +00:00
|
|
|
# model.ref '_user', u
|
2013-02-19 21:49:38 +00:00
|
|
|
# browser.resetDom model
|