-
Notifications
You must be signed in to change notification settings - Fork 1
/
start-the-game.js
135 lines (110 loc) · 4.89 KB
/
start-the-game.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
let twitch_client_id = 'j4zhgepzz3onr4pn0e9a4iumcrxqjq'
document.getElementById('twitch-auth-link').setAttribute('href', `https://id.twitch.tv/oauth2/authorize?response_type=token&client_id=${twitch_client_id}&redirect_uri=${window.location.href.replace(/\/+$/, '')}&scope=viewing_activity_read`);
let db = firebase.firestore();
function enableClickToEnter() {
document.getElementById('enter-to-enter').addEventListener('click', (e) => {
document.getElementById('prof-bar').classList = 'profile-bar clicked';
document.getElementById('filler-bar').classList = 'filler-bar clicked';
document.getElementById('enter-to-enter').classList = 'enter-to-enter clicked';
document.getElementById('enter-steps').classList = 'enter-steps clicked';
document.getElementById('addOption').classList += ' clicked'
document.getElementById('scoreboard-list-div').classList += ' clicked'
getAllScoreboardsAndShow();
});
}
async function checkIfAuth() {
var url = window.location.hash;
let access_token = url.match(/\#(?:access_token)\=([\S\s]*?)\&/)[1];
console.log(`Do we have access token: ${!!access_token}`);
return !!access_token; // TODO: better check. Hit API endpoint and confirm access token is Gucci
}
async function getAuthToken() {
var url = window.location.hash;
let access_token = url.match(/\#(?:access_token)\=([\S\s]*?)\&/)[1];
return access_token;
}
async function getTwitchBroadcasterId(access_token) {
let twitch_url = new URL(`https://api.twitch.tv/helix/users`);
// twitch_url.search = new URLSearchParams({
// game_id: this.data.game
// });
// Default options are marked with *
const response = await fetch(twitch_url, {
method: 'GET', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
// credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Client-ID': twitch_client_id,
'Authorization': 'Bearer ' + access_token
},
});
return response.json(); // parses JSON response into native JavaScript objects
}
async function init() {
if (await checkIfAuth()) {
console.log('logged In');
document.getElementById('login-msg').innerHTML = 'Success';
document.getElementById('twitch-login').classList.add('logged-in');
enableClickToEnter();
} else {
}
}
init();
async function getAllScoreboardsAndShow() {
let access_token = await getAuthToken();
let broadcasterData = await getTwitchBroadcasterId(access_token);
console.log(broadcasterData);
let broadcasterId = broadcasterData.data[0].id;
console.log(`BroadcasterID = ${broadcasterId}`);
db.collection(`/scoreboards/${broadcasterId}/allboards`).onSnapshot((querySnapshot) => {
console.log('got something');
document.getElementById('scoreboard-list-div').innerHTML = '';
querySnapshot.forEach(scoreboard => {
addScoreboardToDOM(scoreboard.data(), broadcasterId, scoreboard.id);
})
});
}
async function addScoreBoard() {
let access_token = await getAuthToken();
let broadcasterData = await getTwitchBroadcasterId(access_token);
console.log(broadcasterData);
let broadcasterId = broadcasterData.data[0].id;
let roomID = document.getElementById("scoreboardName").value;
db.collection(`/scoreboards/${broadcasterId}/allboards`)
.add({
room: roomID,
p1Score: 0,
p2Score: 0,
timestamp: new Date(),
})
.then(function (docRef) {
console.log("Document written with ID: ", docRef);
})
.catch(function (error) {
console.error("Error adding document: ", error);
});
}
function addScoreboardToDOM(scoreboard, bid, sid) {
console.log('SCOREBOARD');
console.log(scoreboard);
let scoreboardEl = document.createElement('kaaro-scoreboard-card');
scoreboardEl.setAttribute('p1Score', scoreboard.p1Score);
scoreboardEl.setAttribute('p2Score', scoreboard.p2Score);
scoreboardEl.setAttribute('room', scoreboard.room);
scoreboardEl.setAttribute('bid', bid);
scoreboardEl.setAttribute('sid', sid);
// let eachScoreboard = `
// <div class="eachScoreboard">
// <div class="scoreDiv">
// ${scoreboard.p1Score} - ${scoreboard.p2Score}
// </div>
// <div class="scoreboardTitle">
// <a href="./board?bid=${bid}&sid=${sid}" class="">
// ${scoreboard.room}
// </a>
// </div>
// </div>
// `;
document.getElementById('scoreboard-list-div').append(scoreboardEl);
}