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()
|
|
|
|
|
|
|
|
|
|
###
|
|
|
|
|
Subscribe to the user, the users's party (meta), and the party's members. 3 subscriptions.
|
|
|
|
|
If the user is solo, just subscribe to the user. If in a an empty party, just subscribe to the party. If full party,
|
|
|
|
|
subscribe to everything.
|
2013-02-08 00:13:14 +00:00
|
|
|
|
2013-02-18 18:40:09 +00:00
|
|
|
Note a strange hack - later model.queries override previous model.queries'
|
|
|
|
|
returned fields. Aka, we need this here otherwise we only get the "public" fields for the current user, which
|
|
|
|
|
are defined in model.query('users').party(). So we need to subscribe to the main user last
|
|
|
|
|
###
|
2013-02-08 00:13:14 +00:00
|
|
|
module.exports.partySubscribe = partySubscribe = (model, cb) ->
|
|
|
|
|
|
|
|
|
|
# unsubscribe from everything - we're starting over
|
2013-02-18 18:40:09 +00:00
|
|
|
partyUnsubscribe model, ->
|
2013-02-08 00:13:14 +00:00
|
|
|
|
2013-02-18 18:40:09 +00:00
|
|
|
# Restart subscription to the main user
|
|
|
|
|
selfQ = model.query('users').withId(model.get('_userId') or model.session.userId)
|
|
|
|
|
selfQ.fetch (err, res) ->
|
|
|
|
|
throw err if err
|
|
|
|
|
u = res.at(0)
|
|
|
|
|
uObj = u.get()
|
2013-02-18 17:49:53 +00:00
|
|
|
|
2013-02-18 18:40:09 +00:00
|
|
|
finished = ->
|
|
|
|
|
selfQ.subscribe (err, res) ->
|
|
|
|
|
model.ref '_user', res.at(0)
|
|
|
|
|
browser.resetDom(model) if window?
|
|
|
|
|
cb() if cb?
|
2013-02-08 00:13:14 +00:00
|
|
|
|
2013-02-18 18:40:09 +00:00
|
|
|
## (1) User is solo, just return that subscription
|
|
|
|
|
return finished() unless uObj.party?.current
|
2013-02-08 00:13:14 +00:00
|
|
|
|
|
|
|
|
|
2013-02-18 18:40:09 +00:00
|
|
|
# User in a party
|
|
|
|
|
partiesQ = model.query('parties').withId(uObj.party.current)
|
|
|
|
|
partiesQ.subscribe (err, res) ->
|
|
|
|
|
throw err if err
|
|
|
|
|
p = res.at(0)
|
|
|
|
|
model.ref '_party', p
|
|
|
|
|
ids = p.get('members')
|
2013-02-18 17:49:53 +00:00
|
|
|
|
2013-02-18 18:40:09 +00:00
|
|
|
# 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
|
2013-02-08 00:13:14 +00:00
|
|
|
|
2013-02-18 18:40:09 +00:00
|
|
|
## (2) Party has no members, just subscribe to the party itself
|
|
|
|
|
if _.isEmpty(ids)
|
|
|
|
|
return finished()
|
2013-02-06 18:59:56 +00:00
|
|
|
|
2013-02-18 18:40:09 +00:00
|
|
|
else
|
|
|
|
|
## (3) Party has members, subscribe to those users too
|
|
|
|
|
membersQ = model.query('users').party(ids)
|
|
|
|
|
membersQ.subscribe (err, members) ->
|
|
|
|
|
throw err if err
|
|
|
|
|
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-05 05:20:58 +00:00
|
|
|
user.set 'party', {current: id, invitation: null, leader: true}
|
2013-02-08 00:13:14 +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-05 23:12:39 +00:00
|
|
|
model.set '_newPartyMember', ''
|
2013-02-08 00:13:14 +00:00
|
|
|
partySubscribe model
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
appExports.partyAccept = ->
|
2013-02-08 00:13:14 +00:00
|
|
|
user.set 'party.current', user.get('party.invitation')
|
|
|
|
|
user.set 'party.invitation', null
|
|
|
|
|
partySubscribe model, ->
|
|
|
|
|
p = model.at('_party')
|
2013-02-06 05:15:00 +00:00
|
|
|
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-06 05:15:00 +00:00
|
|
|
p.set 'members', members
|
2013-02-05 23:23:56 +00:00
|
|
|
if (members.length == 0)
|
2013-02-05 23:12:39 +00:00
|
|
|
# last member out, kill the party
|
2013-02-05 23:52:29 +00:00
|
|
|
model.del "parties.#{id}"
|
2013-02-08 00:13:14 +00:00
|
|
|
model.set '_party', null
|
|
|
|
|
model.set '_partyMembers', null
|
2013-02-12 00:41:43 +00:00
|
|
|
setTimeout (-> window.location.reload true), 10
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# This should handle not requiring a refresh, but alas.
|
2013-02-12 00:27:35 +00:00
|
|
|
partyUnsubscribe model, ->
|
|
|
|
|
selfQ = model.query('users').withId(model.get('_userId') or model.session.userId)
|
|
|
|
|
selfQ.subscribe (err, u) ->
|
|
|
|
|
model.ref '_user', u.at(0)
|
|
|
|
|
browser.resetDom model
|
2013-02-12 00:41:43 +00:00
|
|
|
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-02-05 23:12:39 +00:00
|
|
|
#exports.partyDisband = ->
|