-
Notifications
You must be signed in to change notification settings - Fork 79
/
scene-live.js
273 lines (242 loc) · 9.19 KB
/
scene-live.js
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
272
273
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import AuthenticatedController from 'ares-webportal/mixins/authenticated-controller';
export default Controller.extend(AuthenticatedController, {
gameApi: service(),
flashMessages: service(),
gameSocket: service(),
favicon: service(),
scenePose: '',
rollString: null,
confirmDeleteScenePose: false,
selectSkillRoll: false,
selectLocation: false,
newLocation: null,
scrollPaused: false,
onSceneActivity: function(msg /* , timestamp */) {
let splitMsg = msg.split('|');
let sceneId = splitMsg[0];
let char = splitMsg[1];
let poseData = splitMsg[2];
// For poses we can just add it to the display. Other events require a reload.
if (sceneId === this.get('model.scene.id')) {
if (poseData) {
poseData = JSON.parse(poseData);
let poses = this.get('model.scene.poses');
poses.pushObject(poseData);
this.get('gameSocket').notify('New scene activity!');
this.scrollSceneWindow();
} else {
this.get('gameApi').requestOne('liveScene', { id: this.get('model.scene.id') }).then( response => {
this.set(`model.scene`, response)
this.get('gameSocket').notify('New scene activity!');
this.scrollSceneWindow();
});
}
}
else {
this.get('model.my_scenes').forEach(s => {
if (s.id === sceneId) {
s.set('is_unread', true);
this.get('gameSocket').notify('New activity in one of your other scenes!');
}
});
}
},
rollEnabled: function() {
return this.get('model.abilities').length > 0;
}.property('model.abilities'),
pageTitle: function() {
return 'Scene ' + this.get('model.scene.id');
}.property('model.scene.id'),
resetOnExit: function() {
this.set('scenePose', '');
this.set('rollString', null);
this.set('confirmDeleteScenePose', false);
this.set('selectSkillRoll', false)
},
setupCallback: function() {
let self = this;
this.get('gameSocket').set('sceneCallback', function(data) {
self.onSceneActivity(data) } );
},
showSceneSelection: function() {
return this.get('model.my_scenes').length > 0;
}.property('model.my_scenes.@each.id'),
scrollSceneWindow: function() {
// Unless scrolling paused
if (this.get('scrollPaused')) {
return;
}
try {
$('#live-scene-log').stop().animate({
scrollTop: $('#live-scene-log')[0].scrollHeight
}, 800);
}
catch(error) {
// This happens sometimes when transitioning away from screen.
}
},
scenePoses: function() {
return this.get('model.scene.poses').map(p => Ember.Object.create(p));
}.property('model.scene.poses.@each.id'),
actions: {
locationSelected(loc) {
this.set('newLocation', loc);
},
changeLocation() {
let api = this.get('gameApi');
let newLoc = this.get('newLocation');
if (!newLoc) {
this.get('flashMessages').danger("You haven't selected a location.");
return;
}
this.set('selectLocation', false);
this.set('newLocation', null);
api.requestOne('changeSceneLocation', { scene_id: this.get('model.scene.id'),
location: newLoc })
.then( (response) => {
if (response.error) {
return;
}
});
},
cookies() {
let api = this.get('gameApi');
api.requestOne('sceneCookies', { id: this.get('model.scene.id') }, null)
.then( (response) => {
if (response.error) {
return;
}
this.get('flashMessages').success('You give cookies to the scene participants.');
});
},
editScenePose(scenePose) {
scenePose.set('editActive', true);
},
cancelScenePoseEdit(scenePose) {
scenePose.set('editActive', false);
},
deleteScenePose() {
let api = this.get('gameApi');
let poseId = this.get('confirmDeleteScenePose.id');
this.set('confirmDeleteScenePose', false);
let scenePose = this.get('model.scene.poses').find(p => p.id === poseId);
this.get('model.scene.poses').removeObject(scenePose);
api.requestOne('deleteScenePose', { scene_id: this.get('model.scene.id'),
pose_id: poseId })
.then( (response) => {
if (response.error) {
return;
}
});
},
saveScenePose(scenePose, notify) {
let pose = scenePose.get('raw_pose');
if (pose.length === 0) {
this.get('flashMessages').danger("You haven't entered anything.");
return;
}
scenePose.set('editActive', false);
scenePose.set('pose', pose);
let api = this.get('gameApi');
api.requestOne('editScenePose', { scene_id: this.get('model.scene.id'),
pose_id: scenePose.id, pose: pose, notify: notify })
.then( (response) => {
if (response.error) {
return;
}
scenePose.set('pose', response.pose);
});
this.set('scenePose', '');
},
addPose(poseType) {
let pose = this.get('scenePose');
if (pose.length === 0) {
this.get('flashMessages').danger("You haven't entered anything.");
return;
}
let api = this.get('gameApi');
this.set('scenePose', '');
api.requestOne('addScenePose', { id: this.get('model.scene.id'),
pose: pose, pose_type: poseType })
.then( (response) => {
if (response.error) {
return;
}
this.scrollSceneWindow();
});
},
addSceneRoll() {
let api = this.get('gameApi');
// Needed because the onChange event doesn't get triggered when the list is
// first loaded, so the roll string is empty.
let rollString = this.get('rollString') || this.get('model.abilities')[0];
if (!rollString) {
this.get('flashMessages').danger("You haven't selected an ability to roll.");
return;
}
this.set('selectSkillRoll', false);
this.set('rollString', null);
api.requestOne('addSceneRoll', { scene_id: this.get('model.scene.id'),
roll_string: rollString })
.then( (response) => {
if (response.error) {
return;
}
});
},
changeSceneStatus(status) {
let api = this.get('gameApi');
if (status === 'share') {
this.get('gameSocket').set('sceneCallback', null);
}
api.requestOne('changeSceneStatus', { id: this.get('model.scene.id'),
status: status }, null)
.then( (response) => {
if (response.error) {
return;
}
if (status === 'share') {
this.get('flashMessages').success('The scene has been shared.');
this.transitionToRoute('scene', this.get('model.scene.id'));
}
else if (status === 'stop') {
this.get('flashMessages').success('The scene has been stopped.');
this.send('reloadModel');
}
else if (status === 'restart') {
this.get('flashMessages').success('The scene has been restarted.');
this.send('reloadModel');
}
});
},
muteScene(option) {
let api = this.get('gameApi');
api.requestOne('muteScene', { id: this.get('model.scene.id'),
muted: option }, null)
.then( (response) => {
if (response.error) {
return;
}
let status = option ? 'muted' : 'unmuted';
this.get('flashMessages').success(`The scene has been ${status}.`);
this.send('reloadModel');
});
},
scrollDown() {
this.scrollSceneWindow();
},
refresh() {
this.resetOnExit();
this.send('reloadModel');
},
pauseScroll() {
this.set('scrollPaused', true);
},
unpauseScroll() {
this.set('scrollPaused', false);
this.scrollSceneWindow();
}
}
});