-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsessions.js
61 lines (54 loc) · 1.63 KB
/
sessions.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
function begin_session() {
var task_order = firebase.database().ref('task_order/');
task_order.once('value').then(function(snapshot) {
i = snapshot.val().current_index;
o = eval(snapshot.val().orders);
order = o[i];
uid = firebase.auth().currentUser.uid;
users = firebase.database().ref('/users/' + uid + '/');
updates = {};
task = order.pop()
updates['current_task'] = task;
updates['tasks_to_play'] = order;
promise = users.update(updates).then();
window.game.state.start('Boot', true,false, task);
//increment the index
i = i + 1;
if (i >= o.length) {
i = 0;
}
//save it
var updates = {};
updates['current_index'] = i;
firebase.database().ref('task_order/').update(updates);
});
}
function next_task() {
uid = firebase.auth().currentUser.uid;
users = firebase.database().ref('/users/' + uid + '/');
users.once('value').then(function(snapshot) {
tasks_to_play = snapshot.val().tasks_to_play;
updates = {};
if (tasks_to_play != null) {
task = tasks_to_play.pop();
updates['tasks_to_play'] = tasks_to_play;
users.update(updates);
window.game.state.start(task);
} else {
end_session();
}
});
}
function end_session() {
uid = firebase.auth().currentUser.uid;
users = firebase.database().ref('/users/' + uid + '/');
updates = {}
updates['current_task'] = null;
users.update(updates);
//increment session counter
var p = firebase.database().ref('/users/' + uid + '/session_count').transaction(function(c) {
return c + 1;
});
//after increment, sign the user out - TODO - send them to a thank you page as well
p.then(function () {firebase.auth().signOut();})
}