-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
215 lines (186 loc) · 7.4 KB
/
index.php
File metadata and controls
215 lines (186 loc) · 7.4 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
// Error Handling
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Configuration
$CACHE_DURATION_MINUTES = 10; // How long to cache video view data (in minutes)
// Load Secrets
require_once("secrets.php");
###### Create Video Tree #####
require_once("video_data.php");
$first_vid = "PmWQmZXYd74";
###### Build Parents and Paths #####
build_parents_and_paths($first_vid);
###### Fetch Video Views ######
$view_result = fetch_video_views($CACHE_DURATION_MINUTES);
$curl_error = $view_result['curl_error'];
$curl_success_message = $view_result['curl_success_message'];
$date_last_query = $view_result['date_last_query'];
// Compute total video views
$total_views = 0;
foreach($GLOBALS["data"] as $vid){
$total_views += $vid->views;
}
// Compute total number of possible games (sum of paths from ending nodes)
$total_possible_games = 0;
foreach($GLOBALS["data"] as $vid){
if($vid->ending) {
$total_possible_games += count($vid->paths);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>CGP Grey: Rock Paper Scissors - A Statistical Analysis</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, viewport-fit=cover">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.2/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="css/cytoscape-styles.css?v=1">
<!-- Statsig -->
<script src="https://cdn.jsdelivr.net/npm/@statsig/js-client@3/build/statsig-js-client+session-replay+web-analytics.min.js?apikey=client-nf6ZYQVAcpf5GKM7Yx0JueLYAQGd0zXJC4ww2ElkpGc"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-J4H5B1ENEE"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-J4H5B1ENEE');
</script>
<script src="js/cytoscape.min.js"></script>
</head>
<body>
<div id="header">
<h1>CGP Grey: Rock Paper Scissors</h1>
<h2>Interactive Game Visualization</h2>
<div class="footer">
<p>Made by <a href="https://andreithuler.com" target="_blank">Andrei Thüler</a> |
<!-- <a href="https://github.com/athuler/CgpGreyRockPaperScissors/issues" target="_blank">Report Bug</a> | -->
<a href="https://github.com/athuler/CgpGreyRockPaperScissors" target="_blank">Source</a> |
<a href="https://github.com/sponsors/athuler" target="_blank">
<img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86" style="vertical-align: middle;"/>
</a>
</p>
</div>
</div>
<div id="breadcrumb">
<div>
<span><?=count(array_keys($GLOBALS["data"]))?></span> videos |
<span><?=number_format($total_views)?></span> views
<!-- | <span><?=number_format($total_possible_games)?></span> possible games -->
<span class="hide-on-small"> | Click any node to explore</span>
</div>
<div id="last-updated">
Updated <?php
$now = new DateTime();
$diff = $date_last_query->diff($now);
if ($diff->days > 0) {
echo $diff->days . ' day' . ($diff->days > 1 ? 's' : '') . ' ago';
} elseif ($diff->h > 0) {
echo $diff->h . ' hour' . ($diff->h > 1 ? 's' : '') . ' ago';
} elseif ($diff->i > 0) {
echo $diff->i . ' minute' . ($diff->i > 1 ? 's' : '') . ' ago';
} else {
echo 'now';
}
?>
</div>
</div>
<div id="controls">
<div class="btn-group">
<button class="control-btn" onclick="centerOnStart(window.firstVideoId, window.cy)">
<i class="bi bi-house"></i><span class="btn-text"> Go to Start</span>
</button>
<button class="control-btn" onclick="changeLayout('breadthfirst', window.cy, window.firstVideoId)">
<i class="bi bi-diagram-2"></i><span class="btn-text"> Tree Layout</span>
</button>
<button class="control-btn" onclick="changeLayout('cose', window.cy, window.firstVideoId)">
<i class="bi bi-node-plus"></i><span class="btn-text"> Bubble Layout</span>
</button>
<button class="control-btn" onclick="location.href='download.php'">
<i class="bi bi-download"></i><span class="btn-text"> Download</span>
</button>
<!-- <button class="control-btn" onclick="changeLayout('circle', window.cy, window.firstVideoId)">Circle Layout</button> -->
<!-- <button class="control-btn" onclick="resetView(window.cy, window.firstVideoId)"><i class="bi bi-arrows-angle-contract"></i> Reset View</button> -->
<!-- <button class="control-btn" onclick="fitToScreen(window.cy)"><i class="bi bi-fullscreen"></i> Fit All</button> -->
</div>
<div class="legend">
<div class="legend-item">
<div class="legend-color" style="background: #22c55e;"></div>
<span>Win</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #ef4444;"></div>
<span>Lose</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #666;"></div>
<span>Other</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #fbbf24;"></div>
<span>End</span>
</div>
</div>
</div>
<!-- Diagram -->
<div id="cy"></div>
<!-- Info Panel -->
<div id="info-panel">
<span class="close-btn" onclick="closeInfoPanel()">×</span>
<h3 id="info-video-id">Video Info</h3>
<div id="info-content"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>
<script src="js/cytoscape-graph.js?v=1"></script>
<?php if($curl_error !== null): ?>
<script>
console.warn('YouTube API fetch failed: <?=addslashes($curl_error)?>. Using cached data instead.');
</script>
<?php endif; ?>
<?php if($curl_success_message !== null): ?>
<script>
console.log('<?=addslashes($curl_success_message)?>');
</script>
<?php endif; ?>
<script>
// Prepare graph data from PHP
const videoData = <?php echo json_encode($GLOBALS["data"]); ?>;
const firstVideoId = "<?=$first_vid?>";
const firstVideoViews = <?=$GLOBALS["data"][$first_vid]->views?>;
// Make available globally for onclick handlers
window.firstVideoId = firstVideoId;
window.firstVideoViews = firstVideoViews;
// Initialize graph
const cy = initializeGraph(videoData, firstVideoId, firstVideoViews);
window.cy = cy;
// Store reference to the original navigateToNode function
const navigateToNodeOriginal = navigateToNode;
// Node click handler
cy.on('tap', 'node', function(evt) {
const node = evt.target;
showInfoPanel(node, firstVideoViews);
highlightNode(node, cy);
});
// Click on background to deselect
cy.on('tap', function(evt) {
if (evt.target === cy) {
closeInfoPanel();
clearHighlights(cy);
}
});
// Global function for navigating to nodes (called from info panel)
window.navigateToNode = function(nodeId) {
navigateToNodeOriginal(nodeId, cy);
};
// Initial center on start node
setTimeout(() => {
cy.zoom(0.5);
cy.center(cy.$(`#${firstVideoId}`));
}, 50);
</script>
</body>
</html>