2013-02-05 05:20:58 +00:00
|
|
|
_ = require('underscore')
|
2013-02-05 06:37:13 +00:00
|
|
|
|
2013-04-19 00:18:38 +00:00
|
|
|
partyUnsubscribe = (model, cb) ->
|
|
|
|
|
if window?
|
|
|
|
|
throw "unsubscribe requires cb" unless cb?
|
|
|
|
|
subs = model._subs()
|
|
|
|
|
subs.concat cb
|
|
|
|
|
model.unsubscribe.apply(model, subs)
|
|
|
|
|
else
|
|
|
|
|
cb()
|
|
|
|
|
|
2013-02-18 18:40:09 +00:00
|
|
|
###
|
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-04-19 00:18:38 +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
|
|
|
|
2013-04-19 00:18:38 +00:00
|
|
|
# unsubscribe from everything - we're starting over
|
|
|
|
|
# partyUnsubscribe model, ->
|
|
|
|
|
|
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
|
2013-04-19 00:18:38 +00:00
|
|
|
selfQ.fetch (err, user) ->
|
2013-02-25 23:59:14 +00:00
|
|
|
return next(err) if err
|
2013-04-24 17:21:01 +00:00
|
|
|
unless user.get()
|
|
|
|
|
#return next("User not found - this shouldn't be happening!")
|
2013-04-24 17:21:59 +00:00
|
|
|
console.error "User not found - this shouldn't be happening!"
|
|
|
|
|
return page.redirect('/logout') #delete model.session.userId
|
2013-02-26 21:37:47 +00:00
|
|
|
|
2013-04-19 00:18:38 +00:00
|
|
|
finished = (descriptors, paths) ->
|
2013-05-02 20:09:50 +00:00
|
|
|
descriptors.push 'tavern'; paths.push '_tavern'
|
2013-04-19 00:18:38 +00:00
|
|
|
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-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-04-19 00:18:38 +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)
|
2013-04-19 00:18:38 +00:00
|
|
|
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-04-19 00:18:38 +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)
|
2013-04-19 00:18:38 +00:00
|
|
|
return finished [partyQ, membersQ, selfQ], ['_party', '_partyMembers', '_user']
|
2013-02-06 04:42:30 +00:00
|
|
|
|
2013-05-02 19:16:09 +00:00
|
|
|
module.exports.app = (appExports, model, app) ->
|
2013-04-04 20:44:54 +00:00
|
|
|
character = require './character'
|
|
|
|
|
browser = require './browser'
|
2013-05-02 18:26:25 +00:00
|
|
|
helpers = require './helpers'
|
2013-04-04 20:44:54 +00:00
|
|
|
|
2013-02-06 05:47:08 +00:00
|
|
|
user = model.at('_user')
|
2013-02-06 01:21:05 +00:00
|
|
|
|
2013-02-27 21:40:17 +00:00
|
|
|
model.on 'set', '_user.party.invitation', (after, before) ->
|
|
|
|
|
if !before? and after? # they just got invited
|
2013-03-26 21:54:14 +00:00
|
|
|
partyQ = model.query('parties').withId(after)
|
|
|
|
|
partyQ.fetch (err, party) ->
|
|
|
|
|
return next(err) if err
|
|
|
|
|
model.ref '_party', party
|
|
|
|
|
browser.resetDom(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)
|
|
|
|
|
|
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-04-08 00:29:13 +00:00
|
|
|
model.set "_partyError", "User with id #{id} not found."
|
2013-02-05 05:20:58 +00:00
|
|
|
return
|
2013-02-06 05:47:08 +00:00
|
|
|
else if u.party.current? or u.party.invitation?
|
2013-04-08 00:29:13 +00:00
|
|
|
model.set "_partyError", "User already in a party or pending invitation."
|
2013-02-05 05:20:58 +00:00
|
|
|
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-05-02 18:26:25 +00:00
|
|
|
# browser.resetDom model
|
|
|
|
|
|
2013-05-02 19:16:09 +00:00
|
|
|
###
|
|
|
|
|
Chat Functionality
|
|
|
|
|
###
|
|
|
|
|
|
2013-05-02 20:09:50 +00:00
|
|
|
sendChat = (path, input) ->
|
|
|
|
|
chat = model.at path
|
2013-05-03 18:59:37 +00:00
|
|
|
text = model.get input
|
|
|
|
|
# Check for non-whitespace characters
|
|
|
|
|
return unless /\S/.test text
|
2013-05-02 18:26:25 +00:00
|
|
|
chat.unshift
|
2013-05-02 19:16:09 +00:00
|
|
|
id: model.id()
|
2013-05-04 15:04:39 +00:00
|
|
|
uuid: user.get('id')
|
2013-05-04 15:03:03 +00:00
|
|
|
contributor: user.get('backer.contributor')
|
|
|
|
|
npc: user.get('backer.npc')
|
2013-05-03 18:59:37 +00:00
|
|
|
text: text
|
2013-05-02 18:26:25 +00:00
|
|
|
user: helpers.username(model.get('_user.auth'), model.get('_user.profile.name'))
|
|
|
|
|
timestamp: +new Date
|
2013-05-02 20:09:50 +00:00
|
|
|
model.set(input, '')
|
2013-05-02 19:16:09 +00:00
|
|
|
chat.remove 200 # keep a max messages cap
|
|
|
|
|
|
2013-05-04 15:04:39 +00:00
|
|
|
model.on 'unshift', '_party.chat', -> $('.chat-message').tooltip()
|
|
|
|
|
model.on 'unshift', '_tavern.chat.messages', -> $('.chat-message').tooltip()
|
2013-05-04 15:03:03 +00:00
|
|
|
|
2013-05-02 20:09:50 +00:00
|
|
|
appExports.partySendChat = ->
|
|
|
|
|
sendChat('_party.chat', '_chatMessage')
|
|
|
|
|
model.set '_user.party.lastMessageSeen', model.get('_party.chat')[0].id
|
|
|
|
|
|
|
|
|
|
appExports.tavernSendChat = ->
|
|
|
|
|
model.setNull '_tavern.chat', {messages:[]} #we can remove this later, first time run only
|
|
|
|
|
sendChat('_tavern.chat.messages', '_tavernMessage')
|
|
|
|
|
|
2013-05-05 17:07:36 +00:00
|
|
|
appExports.partyMessageKeyup = (e, el, next) ->
|
|
|
|
|
return next() unless e.keyCode is 13
|
|
|
|
|
appExports.partySendChat()
|
|
|
|
|
|
2013-05-03 18:59:37 +00:00
|
|
|
appExports.tavernMessageKeyup = (e, el, next) ->
|
|
|
|
|
return next() unless e.keyCode is 13
|
|
|
|
|
appExports.tavernSendChat()
|
|
|
|
|
|
2013-05-02 19:16:09 +00:00
|
|
|
app.on 'render', (ctx) ->
|
|
|
|
|
$('#party-tab-link').on 'shown', (e) ->
|
|
|
|
|
messages = model.get('_party.chat')
|
|
|
|
|
return false unless messages?.length > 0
|
|
|
|
|
model.set '_user.party.lastMessageSeen', messages[0].id
|
|
|
|
|
|
|
|
|
|
appExports.gotoPartyChat = ->
|
|
|
|
|
model.set '_gamePane', true, ->
|
|
|
|
|
$('#party-tab-link').tab('show')
|