]> git.sesse.net Git - ultimatescore/commitdiff
Some refactorings to support merged schedules.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 20 Oct 2017 17:43:51 +0000 (19:43 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 20 Oct 2017 17:43:51 +0000 (19:43 +0200)
carousel.js

index 285947eb3cc264dcea645729445ad0d9e197d55c..240ad26e51c3f1f4f633af6e461ea338fc16d370 100644 (file)
@@ -1,4 +1,3 @@
-
 addheading = function(carousel, colspan, content)
 {
        var thead = document.createElement("thead");
@@ -250,7 +249,7 @@ parse_teams_from_spreadsheet = function(response) {
        return teams;
 };
 
-parse_games_from_spreadsheet = function(response, include_unplayed) {
+parse_games_from_spreadsheet = function(response, group_name, include_unplayed) {
        var games = [];
        for (var i = 12; response.values[i] !== undefined && response.values[i].length >= 1; ++i) {
                if ((response.values[i][2] && response.values[i][3]) || include_unplayed) {
@@ -259,18 +258,17 @@ parse_games_from_spreadsheet = function(response, include_unplayed) {
                                "name2": response.values[i][1],
                                "score1": parseInt(response.values[i][2]),
                                "score2": parseInt(response.values[i][3]),
-                               "streamtime": response.values[i][7]
+                               "streamtime": response.values[i][7],
+                               "group_name": group_name
                        });
                }
        }
        return games;
 };
 
-var req = new XMLHttpRequest();
-req.onload = function(e) {
-       var response = JSON.parse(req.responseText);
+var display_group = function(response, group_name) {
        var teams = parse_teams_from_spreadsheet(response);
-       var games = parse_games_from_spreadsheet(response, false);
+       var games = parse_games_from_spreadsheet(response, group_name, false);
 
        var teams_to_idx = make_teams_to_idx(teams);
        for (i = 0; i < games.length; ++i) {
@@ -301,7 +299,7 @@ req.onload = function(e) {
 
        var carousel = document.getElementById('carousel');
 
-       addheading(carousel, 5, "Current standings, TrønDisk 2017<br />Group A");
+       addheading(carousel, 5, "Current standings, TrønDisk 2017<br />" + group_name);
        var tr = document.createElement("tr");
        tr.className = "subfooter";
        addth(tr, "rank", "");
@@ -347,18 +345,16 @@ req.onload = function(e) {
 
        carousel.style.display = 'table';
 };
-req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/1CwRHQtpokVMGTPJu2FYYG-6rnG7OfISIcEHwBfXh-Y4/values/A1:H25?key=AIzaSyAuP9yQn8g0bSay6r_RpGtpFeIbwprH1TU');
-if (false) {
-       req.send();
-}
 
 // Stream schedule
-var req2 = new XMLHttpRequest();
-req2.onload = function(e) {
-       var response = JSON.parse(req.responseText);
+var display_stream_schedule = function(response, group_name) {
        var teams = parse_teams_from_spreadsheet(response);
+       var games = parse_games_from_spreadsheet(response, group_name, true);
+       display_stream_schedule_parsed(teams, games);
+};
+
+var display_stream_schedule_parsed = function(teams, games) {
        var teams_to_idx = make_teams_to_idx(teams);
-       var games = parse_games_from_spreadsheet(response, true);
        games = games.filter(function(game) { return game.streamtime !== undefined && game.streamtime.match(/[0-9]+:[0-9]+/) != null; });
        games.sort(function(a, b) {
                var m1 = a.streamtime.match(/([0-9]+):([0-9]+)/);
@@ -393,7 +389,7 @@ req2.onload = function(e) {
                var name2 = teams[teams_to_idx[games[i].name2]].mediumname;
 
                addtd(tr, "matchup", name1 + "–" + name2);
-               addtd(tr, "group", "Group A");
+               addtd(tr, "group", games[i].group_name);
 
                if (!isNaN(games[i].score1) && !isNaN(games[i].score2) &&
                    games[i].score1 !== games[i].score2) {
@@ -405,8 +401,30 @@ req2.onload = function(e) {
                tr.style = "-webkit-animation: fade-in 1.0s ease; -webkit-animation-delay: " + 0.25 * (row_num++) + "s; -webkit-animation-fill-mode: both;";
                carousel.appendChild(tr);
        }
+
+       carousel.style.display = 'table';
 };
-req2.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/1CwRHQtpokVMGTPJu2FYYG-6rnG7OfISIcEHwBfXh-Y4/values/A1:H25?key=AIzaSyAuP9yQn8g0bSay6r_RpGtpFeIbwprH1TU');
-if (false) {
-       req2.send();
-}
+
+var get_group = function(group_name, cb)
+{
+       var req = new XMLHttpRequest();
+       req.onload = function(e) {
+               cb(JSON.parse(req.responseText), group_name);
+       };
+       req.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/1CwRHQtpokVMGTPJu2FYYG-6rnG7OfISIcEHwBfXh-Y4/values/\'' + group_name + '\'!A1:H25?key=AIzaSyAuP9yQn8g0bSay6r_RpGtpFeIbwprH1TU');
+       req.send();
+};
+
+//get_group('Group A', display_group);
+//get_group('Group A', display_stream_schedule);
+//get_group('Group A', function(response_a) {
+//     get_group('Group B', function(response_b) {
+//             var teams = parse_teams_from_spreadsheet(response_a);
+//             var games = parse_games_from_spreadsheet(response_a, 'Group A', true);
+//             teams = teams.concat(parse_teams_from_spreadsheet(response_b));
+//             games = games.concat(parse_games_from_spreadsheet(response_b, 'Group B', true));
+//             console.log(teams);
+//             console.log(games);
+//             display_stream_schedule_parsed(teams, games);
+//     });
+//});