]> git.sesse.net Git - pkanalytics/commitdiff
Add some rudimentary main menu support to the viewer.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 May 2023 16:17:50 +0000 (18:17 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 May 2023 16:17:50 +0000 (18:17 +0200)
ultimate.js
viewer.html

index b65a2b37392210c84faa265d870827097b4019d4..7de103e86b385c6243508b307f1b4277182a95a1 100644 (file)
@@ -2,11 +2,12 @@
 
 // No frameworks, no compilers, no npm, just JavaScript. :-)
 
-let json;
+let global_json;
 
+addEventListener('hashchange', () => { console.log('heei'); process_matches(global_json); });
 fetch('ultimate.json')
    .then(response => response.json())
-   .then(response => { process_matches(response); });
+   .then(response => { global_json = response; process_matches(global_json); });
 
 function attribute_player_time(player, to, from) {
        if (player.on_field_since > from) {
@@ -175,7 +176,6 @@ function process_matches(json) {
                        } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') {
                                ++p[type];
                        } else if (type !== 'in' && type !== 'out' && type !== 'pull' &&
-                                  type !== 'their_pull' && type !== 'restart' && type !== 'goal' &&
                                   type !== 'their_goal' && type !== 'stoppage' && type !== 'unknown' &&
                                   type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' &&
                                   type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' &&
@@ -186,8 +186,65 @@ function process_matches(json) {
                }
        }
 
+       let chosen_category = get_chosen_category();
+       write_main_menu(chosen_category);
+
        let rows = [];
+       if (chosen_category === 'general') {
+               rows = make_table_general(players);
+       } else if (chosen_category === 'pulls') {
+               console.log("PULL STATS");
+               for (const [q,p] of Object.entries(players)) {
+                       if (p.pulls === 0) {
+                               continue;
+                       }
+                       let sum_time = 0;
+                       for (const t of p.pull_times) {
+                               sum_time += t;
+                       }
+                       let avg_time = 1e-3 * sum_time / p.pulls;
+                       let msg = p.name + ' did ' + p.pulls + ' pull(s), ' + p.oob_pulls + ' OOB';
+                       if (p.oob_pulls < p.pulls) {
+                               msg += ', avg. hangtime ' + avg_time.toFixed(1) + ' sec for others';
+                       }
+                       console.log(msg, p.pull_times);
+               }
+       }
+       document.getElementById('stats').replaceChildren(...rows);
+}
 
+function get_chosen_category() {
+       if (window.location.hash === '#pulls') {
+               return 'pulls';
+       } else {
+               return 'general';
+       }
+}
+
+function write_main_menu(chosen_category) {
+       let elems = [];
+       if (chosen_category === 'general') {
+               elems.push(document.createTextNode('General'));
+       } else {
+               let a = document.createElement('a');
+               a.appendChild(document.createTextNode('General'));
+               a.setAttribute('href', '#general');
+               elems.push(a);
+       }
+       elems.push(document.createTextNode(' | '));
+       if (chosen_category === 'pulls') {
+               elems.push(document.createTextNode('Pulls'));
+       } else {
+               let a = document.createElement('a');
+               a.appendChild(document.createTextNode('Pulls'));
+               a.setAttribute('href', '#pulls');
+               elems.push(a);
+       }
+       document.getElementById('mainmenu').replaceChildren(...elems);
+}
+
+function make_table_general(players) {
+       let rows = [];
        {
                let header = document.createElement('tr');
                add_cell(header, 'th', 'Player');
@@ -210,22 +267,5 @@ function process_matches(json) {
                rows.push(row);
                console.log(p.name + " played " + p.points_played + " points (" + Math.floor(p.playing_time_ms / 60000) + " min), " + p.goals + " goals, " + p.assists + " assists, plus/minus: " + pm);
        }
-       document.getElementById('stats').replaceChildren(...rows);
-
-       console.log("PULL STATS");
-       for (const [q,p] of Object.entries(players)) {
-               if (p.pulls === 0) {
-                       continue;
-               }
-               let sum_time = 0;
-               for (const t of p.pull_times) {
-                       sum_time += t;
-               }
-               let avg_time = 1e-3 * sum_time / p.pulls;
-               let msg = p.name + ' did ' + p.pulls + ' pull(s), ' + p.oob_pulls + ' OOB';
-               if (p.oob_pulls < p.pulls) {
-                       msg += ', avg. hangtime ' + avg_time.toFixed(1) + ' sec for others';
-               }
-               console.log(msg, p.pull_times);
-       }
+       return rows;
 }
index 569f69682672dbb66fcdd4ab86a91e7dc02cc5b9..0a20d883d5331f7e5648868c161278b2a25f6b08 100644 (file)
@@ -7,6 +7,8 @@
     <script src="ultimate.js"></script>
   </head>
   <body>
+    <p id="mainmenu">
+    </p>
     <table id="stats">
     </table>
   </body>