-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchateau.coffee
271 lines (207 loc) · 7.48 KB
/
chateau.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Chat Based MUD
ChateauPresenter = require "./presenters/chateau"
Model = require "model"
{Bindable, Modal, Observable} = UI = require "ui"
Drop = require "./lib/drop"
Auth = require "./module/auth"
RemoteInterface = require "./module/remote-interface"
Renderer = require "./module/renderer"
shaUpload = require "./sha-upload"
rand = (n) ->
Math.floor(Math.random() * n)
Member = require "./models/member"
Room = require "./models/room"
Prop = Member
module.exports = (I={}, self=Model(I)) ->
self.include Auth, Bindable, RemoteInterface
self.extend
displayNameInput: Observable "duder"
displayNameFormClass: ->
"changed" if self.displayNameInput() != self.displayName()
displayNameFormSubmit: (e) ->
e.preventDefault()
self.currentUser().update
name: self.displayNameInput()
.sync()
resetDisplayNameInput: (e) ->
e?.preventDefault()
self.displayNameInput self.displayName()
displayName: ->
self.currentUser()?.name() or ""
currentRoom: Observable null
currentUser: Observable null
avatars: Observable [
"https://1.pixiecdn.com/sprites/148517/original.png"
"https://0.pixiecdn.com/sprites/148468/original.png"
"https://2.pixiecdn.com/sprites/137922/original.png"
"https://1.pixiecdn.com/sprites/147597/original.png"
"https://1.pixiecdn.com/sprites/151181/original.png"
"https://1.pixiecdn.com/sprites/150973/original.png"
"https://3.pixiecdn.com/sprites/151199/original.png"
"https://3.pixiecdn.com/sprites/151187/original.png"
"https://0.pixiecdn.com/sprites/151140/original.png"
"https://3.pixiecdn.com/sprites/150719/original.png"
"https://1.pixiecdn.com/sprites/151149/original.png"
"https://2.pixiecdn.com/sprites/151046/original.png"
].map (url) -> avatarURL: url
rooms: Observable []
props: Observable [
"https://1.pixiecdn.com/sprites/151213/original.png"
"https://3.pixiecdn.com/sprites/151203/original.png"
"https://0.pixiecdn.com/sprites/148204/original.png"
"https://1.pixiecdn.com/sprites/148365/original.png"
"https://2.pixiecdn.com/sprites/148330/original.png"
"https://1.pixiecdn.com/sprites/148333/original.png"
"https://1.pixiecdn.com/sprites/148329/original.png"
"https://1.pixiecdn.com/sprites/137441/original.png"
"https://0.pixiecdn.com/sprites/137380/original.png"
].map (url) -> imageURL: url
displayModalLoader: (message) ->
progressView = UI.Progress
message: message
Modal.show progressView.element,
cancellable: false
currentAccountId: ->
self.firebaseUser()?.uid
accountConnected: (firebaseUser) ->
unless firebaseUser
stats.increment "accountDisconnected"
return
stats.increment "accountConnected"
providerDisplayName = null
providerPhoto = null
# Get provider data
firebaseUser.providerData.forEach (profile) ->
providerDisplayName ?= profile.displayName
providerPhoto ?= profile.photoURL
user = Member.find(firebaseUser.uid)
self.currentUser user
self.displayModalLoader "Loading..."
user.connect().then ->
unless user.name()
user.name providerDisplayName
self.resetDisplayNameInput()
# TODO: Don't overwrite profile photo if the user already has one
user.updateProfilePhoto providerPhoto
user.updatePresence("")
Modal.hide()
# Display Avatar Drawer unless user has avatar
new Promise (resolve, reject) ->
if user.imageURL()
resolve()
else
self.element.querySelectorAll("tab-drawer > *").forEach (element) ->
element.classList.remove("show")
self.element.querySelector("avatar-control").classList.add("show")
checkForAvatarURL = (value) ->
if value
user.imageURL.stopObserving checkForAvatarURL
resolve()
user.imageURL.observe checkForAvatarURL
.then ->
# Connect to previously connected room
previousRoom = Room.find(user.roomId())
if previousRoom
# Need to force join because currentRoom will be equal to previousRoom
self.joinRoom previousRoom, true
else
# Display Rooms drawer
self.element.querySelectorAll("tab-drawer > *").forEach (element) ->
element.classList.remove("show")
self.element.querySelector("room-control").classList.add("show")
createRoom: ->
Modal.prompt("Room name", "a fun room")
.then (name) ->
if name
db.ref("rooms").push
name: name
clearAllProps: ->
self.currentRoom().clearAllProps()
addProp: (prop) ->
stats.increment "prop.add"
self.currentRoom().addProp prop
setBackgroundURL: (backgroundURL) ->
room = self.currentRoom()
room.imageURL(backgroundURL)
room.sync()
setAvatar: (avatarURL) ->
self.currentUser()
.update
imageURL: avatarURL
.sync()
loadFile: (file) ->
stats.increment "load-file"
# TODO: handle simultaneous uploads
shaUpload(self.currentUser().dataFolder(), file)
.then (downloadURL) ->
self.trigger "event", "file-upload"
Modal.form require("./templates/asset-form")()
.then (result) ->
switch result?.selection
when "avatar"
self.setAvatar downloadURL
self.trigger "event", "custom-avatar"
when "background"
self.setBackgroundURL downloadURL
self.trigger "event", "custom-background"
joinRoom: (room) ->
if room is self.currentRoom()
return
user = self.currentUser()
accountId = user?.key()
return unless accountId
stats.increment "rooms.join"
previousRoom = self.currentRoom()
previousRoom?.leave(accountId)
room.join(accountId)
user.roomId room.key()
user.sync()
self.currentRoom room
return
saySubmit: (e) ->
e.preventDefault()
input = e.currentTarget.querySelector('input')
words = input.value
if words
input.value = ""
self.currentRoom()?.addRoomEvent
source: self.currentAccountId()
type: "chat"
content:
sender: self.displayName()
message: words
self.currentUser().ref().child("text").onDisconnect().remove()
self.currentUser().update
text: words
.sync(db)
else
self.currentUser().ref().child("text").remove()
words: ->
self.currentRoom()?.members.map (member) ->
member.wordElement()
self.include Renderer
self.include Auth
self.initializeAuth()
do -> # General App Connectivity
# Populate Rooms list
db.ref("rooms").on "child_added", (room) ->
key = room.key
value = room.val()
delete value.props
stats.increment "rooms.child_added"
room = Room.find(key).update value
self.rooms.push room
self.firebaseUser.observe self.accountConnected
self.element = element = ChateauPresenter self
Drop element, (e) ->
files = e.dataTransfer.files
if files.length
# TODO: Process multiple files
# TODO: Process files based on type
# TODO: Process images based on size
file = files[0]
self.loadFile(file)
self.on "event", (name) ->
stats.increment "event.#{name}"
self.invokeRemote("event", arguments...)
return self