2013-05-22 10:29:24 +00:00
|
|
|
_ = require('lodash')
|
2013-05-19 17:53:13 +00:00
|
|
|
helpers = require('habitrpg-shared/script/helpers')
|
2013-02-05 06:37:13 +00:00
|
|
|
|
2013-05-02 19:16:09 +00:00
|
|
|
module.exports.app = (appExports, model, app) ->
|
2013-04-04 20:44:54 +00:00
|
|
|
browser = require './browser'
|
|
|
|
|
|
2013-05-05 17:47:50 +00:00
|
|
|
_currentTime = model.at '_currentTime'
|
2013-05-26 20:32:21 +00:00
|
|
|
_currentTime.setNull +new Date
|
|
|
|
|
# Every 60 seconds, reset the current time so that the chat can update relative times
|
|
|
|
|
setInterval (->_currentTime.set +new Date), 60000
|
2013-05-05 17:44:24 +00:00
|
|
|
|
2013-02-06 05:47:08 +00:00
|
|
|
user = model.at('_user')
|
2013-02-06 01:21:05 +00:00
|
|
|
|
2013-05-26 20:32:21 +00:00
|
|
|
appExports.groupCreate = (e,el) ->
|
2013-06-02 15:20:57 +00:00
|
|
|
type = $(el).attr('data-type')
|
|
|
|
|
newGroup =
|
2013-06-01 16:35:20 +00:00
|
|
|
name: model.get("_new.group.name")
|
|
|
|
|
description: model.get("_new.group.description")
|
2013-05-26 20:32:21 +00:00
|
|
|
leader: user.get('id')
|
|
|
|
|
members: [user.get('id')]
|
2013-06-02 15:20:57 +00:00
|
|
|
type: type
|
2013-06-03 20:55:57 +00:00
|
|
|
|
|
|
|
|
# parties - free
|
|
|
|
|
if type is 'party'
|
|
|
|
|
return model.add 'groups', newGroup, ->location.reload()
|
|
|
|
|
|
|
|
|
|
# guilds - 4G
|
2013-06-05 18:14:00 +00:00
|
|
|
unless user.get('balance') >= 1
|
2013-06-03 20:55:57 +00:00
|
|
|
return $('#more-gems-modal').modal 'show'
|
|
|
|
|
if confirm "Create Guild for 4 Gems?"
|
|
|
|
|
newGroup.privacy = (model.get("_new.group.privacy") || 'public') if type is 'guild'
|
|
|
|
|
newGroup.balance = 1 # they spent $ to open the guild, it goes into their guild bank
|
|
|
|
|
model.add 'groups', newGroup, ->
|
2013-06-05 18:14:00 +00:00
|
|
|
user.incr 'balance', -1, ->location.reload()
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-05-29 21:47:07 +00:00
|
|
|
appExports.toggleGroupEdit = (e, el) ->
|
|
|
|
|
path = "_editing.groups.#{$(el).attr('data-gid')}"
|
|
|
|
|
model.set path, !model.get(path)
|
|
|
|
|
|
2013-06-03 03:12:31 +00:00
|
|
|
appExports.toggleLeaderMessageEdit = (e, el) ->
|
|
|
|
|
path = "_editing.leaderMessage.#{$(el).attr('data-gid')}"
|
|
|
|
|
model.set path, !model.get(path)
|
|
|
|
|
|
2013-05-29 21:47:07 +00:00
|
|
|
appExports.groupAddWebsite = (e, el) ->
|
|
|
|
|
test = e.get()
|
|
|
|
|
e.at().unshift 'websites', model.get('_newGroupWebsite')
|
|
|
|
|
model.del '_newGroupWebsite'
|
|
|
|
|
|
2013-05-26 20:32:21 +00:00
|
|
|
appExports.groupInvite = (e,el) ->
|
2013-05-27 23:46:51 +00:00
|
|
|
uid = model.get('_groupInvitee').replace(/[\s"]/g, '')
|
2013-05-26 20:32:21 +00:00
|
|
|
model.set '_groupInvitee', ''
|
2013-05-27 23:46:51 +00:00
|
|
|
return if _.isEmpty(uid)
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-05-27 23:46:51 +00:00
|
|
|
model.query('users').publicInfo([uid]).fetch (err, profiles) ->
|
2013-02-05 21:11:28 +00:00
|
|
|
throw err if err
|
2013-05-27 23:46:51 +00:00
|
|
|
profile = profiles.at(0).get()
|
|
|
|
|
return model.set("_groupError", "User with id #{uid} not found.") unless profile
|
|
|
|
|
model.query('groups').withMember(uid).fetch (err, g) ->
|
|
|
|
|
throw err if err
|
2013-06-02 17:06:03 +00:00
|
|
|
group = e.get(); groups = g.get()
|
|
|
|
|
{type, name} = group; gid = group.id
|
|
|
|
|
groupError = (msg) -> model.set("_groupError", msg)
|
2013-05-27 23:46:51 +00:00
|
|
|
invite = ->
|
|
|
|
|
$.bootstrapGrowl "Invitation Sent."
|
2013-06-02 17:06:03 +00:00
|
|
|
switch type
|
|
|
|
|
when 'guild' then model.push "users.#{uid}.invitations.guilds", {id:gid, name}, ->location.reload()
|
|
|
|
|
when 'party' then model.set "users.#{uid}.invitations.party", {id:gid, name}, ->location.reload()
|
2013-05-26 20:32:21 +00:00
|
|
|
|
2013-06-02 17:06:03 +00:00
|
|
|
switch type
|
|
|
|
|
when 'guild'
|
2013-06-03 20:09:37 +00:00
|
|
|
if profile.invitations?.guilds and _.find(profile.invitations.guilds, {id:gid})
|
2013-06-02 17:06:03 +00:00
|
|
|
return groupError("User already invited to that group")
|
|
|
|
|
else if uid in group.members
|
|
|
|
|
return groupError("User already in that group")
|
|
|
|
|
else invite()
|
|
|
|
|
when 'party'
|
2013-06-03 20:09:37 +00:00
|
|
|
if profile.invitations?.party
|
2013-05-27 23:46:51 +00:00
|
|
|
return groupError("User already pending invitation.")
|
|
|
|
|
else if _.find(groups, {type:'party'})
|
|
|
|
|
return groupError("User already in a party.")
|
|
|
|
|
else invite()
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-06-01 16:35:20 +00:00
|
|
|
joinGroup = (gid) ->
|
|
|
|
|
model.push("groups.#{gid}.members", user.get('id'), ->location.reload())
|
|
|
|
|
|
2013-06-02 15:49:56 +00:00
|
|
|
appExports.joinGroup = (e, el) -> joinGroup e.get('id')
|
2013-06-01 16:35:20 +00:00
|
|
|
|
2013-05-27 23:46:51 +00:00
|
|
|
appExports.acceptInvitation = (e,el) ->
|
2013-06-01 16:35:20 +00:00
|
|
|
gid = e.get('id')
|
2013-05-27 23:46:51 +00:00
|
|
|
if $(el).attr('data-type') is 'party'
|
2013-06-01 16:35:20 +00:00
|
|
|
user.set 'invitations.party', null, ->joinGroup(gid)
|
2013-05-27 23:46:51 +00:00
|
|
|
else
|
2013-06-01 16:35:20 +00:00
|
|
|
e.at().remove ->joinGroup(gid)
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-05-27 23:46:51 +00:00
|
|
|
appExports.rejectInvitation = (e, el) ->
|
|
|
|
|
clear = -> browser.resetDom(model)
|
|
|
|
|
if e.at().path().indexOf('party') != -1
|
|
|
|
|
model.del e.at().path(), clear
|
|
|
|
|
else e.at().remove clear
|
2013-02-05 05:20:58 +00:00
|
|
|
|
2013-05-26 20:32:21 +00:00
|
|
|
appExports.groupLeave = (e,el) ->
|
2013-06-05 12:25:11 +00:00
|
|
|
if confirm("Leave this group, are you sure?") is true
|
|
|
|
|
uid = user.get('id')
|
|
|
|
|
group = model.at "groups.#{$(el).attr('data-id')}"
|
|
|
|
|
index = group.get('members').indexOf(uid)
|
|
|
|
|
if index != -1
|
|
|
|
|
group.remove 'members', index, 1, ->
|
|
|
|
|
updated = group.get()
|
|
|
|
|
# last member out, delete the party
|
|
|
|
|
if _.isEmpty(updated.members) and (updated.type is 'party')
|
|
|
|
|
group.del ->location.reload()
|
|
|
|
|
# assign new leader, so the party is editable #TODO allow old leader to assign new leader, this is just random
|
|
|
|
|
else if (updated.leader is uid)
|
|
|
|
|
group.set "leader", updated.members[0], ->location.reload()
|
|
|
|
|
else location.reload()
|
2013-05-02 18:26:25 +00:00
|
|
|
|
2013-05-02 19:16:09 +00:00
|
|
|
###
|
|
|
|
|
Chat Functionality
|
|
|
|
|
###
|
|
|
|
|
|
2013-05-26 19:21:07 +00:00
|
|
|
model.on 'unshift', '_party.chat', -> $('.chat-message').tooltip()
|
|
|
|
|
model.on 'unshift', '_habitrpg.chat', -> $('.chat-message').tooltip()
|
|
|
|
|
|
|
|
|
|
appExports.sendChat = (e,el) ->
|
|
|
|
|
text = model.get '_chatMessage'
|
2013-05-03 18:59:37 +00:00
|
|
|
# Check for non-whitespace characters
|
|
|
|
|
return unless /\S/.test text
|
2013-05-26 19:21:07 +00:00
|
|
|
|
|
|
|
|
group = e.at()
|
2013-06-04 00:46:56 +00:00
|
|
|
|
|
|
|
|
# get rid of duplicate member ids - this is a weird place to put it, but works for now
|
|
|
|
|
members = group.get('members'); uniqMembers = _.uniq(members)
|
|
|
|
|
group.set('members', uniqMembers) if !_.isEqual(uniqMembers, members)
|
|
|
|
|
|
2013-05-26 19:21:07 +00:00
|
|
|
chat = group.at('chat')
|
|
|
|
|
model.set('_chatMessage', '')
|
2013-05-09 13:29:09 +00:00
|
|
|
|
|
|
|
|
message =
|
2013-05-05 22:04: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-09 13:29:09 +00:00
|
|
|
|
2013-05-10 18:20:53 +00:00
|
|
|
# FIXME - sometimes racer will send many duplicates via chat.unshift. I think because it can't make connection, keeps
|
|
|
|
|
# trying, but all attempts go through. Unfortunately we can't do chat.set without potentially clobbering other chatters,
|
|
|
|
|
# and we can't make chat an object without using refLists. hack solution for now is to unshift, and if there are dupes
|
|
|
|
|
# after we set to unique
|
|
|
|
|
chat.unshift message, ->
|
|
|
|
|
messages = chat.get() || []
|
|
|
|
|
count = messages.length
|
|
|
|
|
messages =_.uniq messages, true, ((m) -> m?.id) # get rid of dupes
|
|
|
|
|
#There were a bunch of duplicates, let's clean it up
|
|
|
|
|
if messages.length != count
|
|
|
|
|
messages.splice(200)
|
|
|
|
|
chat.set messages
|
|
|
|
|
else
|
|
|
|
|
chat.remove(200)
|
2013-05-26 19:21:07 +00:00
|
|
|
type = $(el).attr('data-type')
|
|
|
|
|
model.set '_user.party.lastMessageSeen', chat.get()[0].id if group.get('type') is 'party'
|
2013-05-02 19:16:09 +00:00
|
|
|
|
2013-05-26 19:21:07 +00:00
|
|
|
appExports.chatKeyup = (e, el, next) ->
|
2013-05-03 18:59:37 +00:00
|
|
|
return next() unless e.keyCode is 13
|
2013-05-26 19:21:07 +00:00
|
|
|
appExports.sendChat(e, el)
|
2013-05-03 18:59:37 +00:00
|
|
|
|
2013-05-19 14:02:04 +00:00
|
|
|
appExports.deleteChatMessage = (e) ->
|
|
|
|
|
if confirm("Delete chat message?") is true
|
|
|
|
|
e.at().remove() #requires the {#with}
|
2013-05-10 10:53:20 +00:00
|
|
|
|
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')
|