-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
176 lines (154 loc) · 5.29 KB
/
app.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
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('./service-worker.js')
.then(function() { console.log('Service Worker Registered'); });
}
let isDM = false;
const database = firebase.database();
let rollsRef;
let rolls = {};
let roomCode;
let firebaseAuthContainer = $('#firebaseui-auth-container');
let signInStatus = $('#sign-in-status');
let signOutButton = $('#sign-out');
let dmQuestionWrapper = $('#dm_q_wrapper');
let submissionWrapper = $('#submissions');
// FirebaseUI config.
let uiConfig = {
signInSuccessUrl: 'lanaholics.servegame.com:4818',
signInOptions: [
// Leave the lines as is for the providers you want to offer your users.
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
firebase.auth.EmailAuthProvider.PROVIDER_ID
]
};
// Initialize the FirebaseUI Widget using Firebase.
let ui = new firebaseui.auth.AuthUI(firebase.auth());
// The start method will wait until the DOM is loaded.
ui.start('#firebaseui-auth-container', uiConfig);
initApp = function () {
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
let displayName = user.displayName;
// let email = user.email;
// let emailVerified = user.emailVerified;
// let photoURL = user.photoURL;
// let phoneNumber = user.phoneNumber;
// let providerData = user.providerData;
user.getToken().then(function (accessToken) {
signInStatus.html(displayName);
signOutButton.html('Sign out');
});
signOutButton.css('display', 'block');
getUserInfo(user.uid);
askIfDM();
} else {
// User is signed out.
signInStatus.html('Signed out');
signOutButton.css('display', 'none');
}
}, function (error) {
console.log(error);
});
};
window.addEventListener('load', function () {
initApp()
});
function getUserInfo(userID) {
}
function signOut() {
firebase.auth().signOut().then(function () {
openSignIn();
}).catch(function (error) {
console.error(error);
});
}
function askIfDM() {
firebaseAuthContainer.css('display', 'none');
dmQuestionWrapper.css('display', 'block');
}
function openSignIn() {
submissionWrapper.css('display', 'none');
dmQuestionWrapper.css('display', 'none');
firebaseAuthContainer.css('display', 'block');
}
function switchViews(tf) {
isDM = tf;
if (isDM) {
$('#room_code_input').val(Math.floor(1000 + Math.random() * 9000));
enterRoom();
} else {
document.getElementById('room_code_input_wrapper').style.display = 'block';
}
dmQuestionWrapper.css('display', 'none');
}
function enterRoom() {
const roomCodeInput = $('#room_code_input');
roomCode = roomCodeInput.val();
if (roomCode % 1 === 0) {
rollsRef = database.ref('rolls/' + roomCode);
if (isDM) {
rollsRef.on('child_added', function (data) {
rolls[data.key] = [data.val().name, data.val().roll];
updateRolls();
});
rollsRef.on('child_changed', function (data) {
rolls[data.key] = [data.val().name, data.val().roll];
updateRolls();
});
rollsRef.on('child_removed', function (data) {
delete rolls[data.key];
updateRolls();
});
document.getElementById('roll_values').style.display = 'block';
}
$('#room_code').html("Rm: " + roomCode);
$('#room_code_input_wrapper').css('display', 'none');
submissionWrapper.css('display', 'block');
} else {
alert("Room number must be an integer (e.g. 1234)")
}
}
function updateRolls() {
// Create items array
let items = Object.keys(rolls).map(function (key) {
return [key, rolls[key]];
});
// Sort the array based on the second element
items.sort(function (a, b) {
return b[1][1] - a[1][1];
});
document.getElementById('roll_values').innerHTML = "";
let ul = document.getElementById('roll_values');
for (let i = 0; i < items.length; i++) {
let li = document.createElement('li');
li.innerHTML = '<li class="mdl-list__item mdl-list__item--two-line">' +
'<span class="mdl-list__item-primary-content" style="text-align: left">' +
'<i class="material-icons mdl-list__item-avatar">person</i>' +
'<span>' + items[i][1][0] + '</span>' +
'<br>' +
'<span style="font-size: 0.8em">' + items[i][1][1] + '</span>' +
'</span>' +
'<span class="mdl-list__item-secondary-content">' +
'<button class="mdl-button mdl-js-button mdl-button--icon mdl-js-ripple-effect" ' +
'onclick="removeValue(\'' + items[i][0] + '\')" ' +
'style="float: right;">' +
'<i class="material-icons">remove_circle</i>' +
'</button>' +
'</span>' +
'</li>';
ul.appendChild(li);
}
}
function writeValue() {
let name = $('#name').val();
let roll = $('#roll').val();
rollsRef.push().set({
name: name,
roll: roll
});
}
function removeValue(key) {
rollsRef.child(key).remove();
}