forked from koolsb/fpp-nfl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.php
More file actions
395 lines (348 loc) · 14.5 KB
/
Copy pathcontent.php
File metadata and controls
395 lines (348 loc) · 14.5 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/*
* fpp-gameday - Pro Sports Scoring Plugin
* Settings / configuration page
*/
function fetchJson($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'fpp-gameday/1.0');
$body = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err || $body === false) return null;
return json_decode($body, true);
}
function getTeams($sport, $league) {
$url = "https://site.api.espn.com/apis/site/v2/sports/{$sport}/{$league}/teams?limit=200";
$data = fetchJson($url);
if (!$data) return [];
$teams = [];
foreach ($data['sports'] ?? [] as $s) {
foreach ($s['leagues'] ?? [] as $l) {
foreach ($l['teams'] ?? [] as $t) {
$team = $t['team'] ?? [];
if (isset($team['id'], $team['displayName']))
$teams[] = ['id' => $team['id'], 'name' => $team['displayName']];
}
}
}
usort($teams, fn($a, $b) => strcmp($a['name'], $b['name']));
return $teams;
}
function getNCAATeams() {
$url = "https://site.api.espn.com/apis/v2/sports/football/college-football/standings?limit=500";
$data = fetchJson($url);
if (!$data) return [];
$teams = [];
foreach ($data['children'] ?? [] as $conf) {
foreach ($conf['standings']['entries'] ?? [] as $entry) {
$team = $entry['team'] ?? [];
if (isset($team['id'], $team['displayName']))
$teams[] = ['id' => $team['id'], 'name' => $team['displayName']];
}
}
usort($teams, fn($a, $b) => strcmp($a['name'], $b['name']));
return $teams;
}
function getSequences() {
$data = fetchJson('http://127.0.0.1/api/sequence');
if (!$data) return [];
$seqs = isset($data['Sequences']) ? $data['Sequences'] : (array_values($data) === $data ? $data : []);
sort($seqs);
return array_values(array_map(fn($s) => preg_replace('/\.fseq$/i', '', $s), $seqs));
}
function getPlaylists() {
$data = fetchJson('http://127.0.0.1/api/playlists');
if (!$data || !is_array($data)) return [];
sort($data);
return $data;
}
function getAudio() {
$data = fetchJson('http://127.0.0.1/api/media');
if (!$data || !is_array($data)) return [];
sort($data);
return $data;
}
$playlists = getPlaylists();
$audio = getAudio();
$teamsData = [
'nfl' => getTeams('football', 'nfl'),
'ncaa' => getNCAATeams(),
'nhl' => getTeams('hockey', 'nhl'),
'mlb' => getTeams('baseball', 'mlb'),
'afl' => getTeams('australian-football', 'afl'),
];
$sequences = getSequences();
?>
<!DOCTYPE html>
<html>
<head>
<title>GameDay</title>
<!-- Bootstrap is already loaded by FPP's shell page -->
</head>
<body class="p-3">
<h2 class="mb-4">GameDay</h2>
<div class="mb-3 d-flex align-items-center gap-3">
<label for="enabled">Enable Plugin: <input type="checkbox" id="enabled" class="enableCheckbox"></label>
<input type="button" class="buttons" value="Save Settings" onclick="saveConfig()">
<span id="save-status" class="text-muted small"></span>
</div>
<ul class="nav nav-tabs mb-4" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" data-bs-toggle="tab" data-bs-target="#pane-nfl" type="button">NFL</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#pane-ncaa" type="button">NCAA</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#pane-nhl" type="button">NHL</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#pane-mlb" type="button">MLB</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#pane-afl" type="button">AFL</button>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="pane-nfl" role="tabpanel">
<div id="teams-nfl"></div>
<button class="btn btn-outline-secondary btn-sm mt-2" onclick="addTeamRow('nfl')">+ Add NFL Team</button>
</div>
<div class="tab-pane fade" id="pane-ncaa" role="tabpanel">
<div id="teams-ncaa"></div>
<button class="btn btn-outline-secondary btn-sm mt-2" onclick="addTeamRow('ncaa')">+ Add NCAA Team</button>
</div>
<div class="tab-pane fade" id="pane-nhl" role="tabpanel">
<div id="teams-nhl"></div>
<button class="btn btn-outline-secondary btn-sm mt-2" onclick="addTeamRow('nhl')">+ Add NHL Team</button>
</div>
<div class="tab-pane fade" id="pane-mlb" role="tabpanel">
<div id="teams-mlb"></div>
<button class="btn btn-outline-secondary btn-sm mt-2" onclick="addTeamRow('mlb')">+ Add MLB Team</button>
</div>
<div class="tab-pane fade" id="pane-afl" role="tabpanel">
<div id="teams-afl"></div>
<button class="btn btn-outline-secondary btn-sm mt-2" onclick="addTeamRow('afl')">+ Add AFL Team</button>
</div>
</div>
<script>
const TEAMS_DATA = <?= json_encode($teamsData) ?>;
const SEQUENCES = <?= json_encode($sequences) ?>;
const PLAYLISTS = <?= json_encode($playlists) ?>;
const AUDIO = <?= json_encode($audio) ?>;
const LEAGUES = ['nfl', 'ncaa', 'nhl', 'mlb', 'afl'];
const LEAGUE_LABELS = { nfl: 'NFL Football', ncaa: 'NCAA Football', nhl: 'NHL Hockey', mlb: 'MLB Baseball', afl: 'AFL' };
function buildTeamSelect(lg, selectedID) {
const sel = document.createElement('select');
sel.className = 'form-select team-select';
sel.appendChild(new Option('-- None --', ''));
for (const t of (TEAMS_DATA[lg] || [])) {
const opt = new Option(t.name, t.id);
if (String(t.id) === String(selectedID)) opt.selected = true;
sel.appendChild(opt);
}
return sel;
}
const ACTION_LISTS = { sequence: SEQUENCES, playlist: PLAYLISTS, audio: AUDIO };
function buildActionSelects(typeVal, valueVal, typeClass, valueClass) {
const wrap = document.createElement('div');
wrap.className = 'd-flex gap-2';
const typeSel = document.createElement('select');
typeSel.className = 'form-select form-select-sm ' + (typeClass || '');
typeSel.style.maxWidth = '110px';
for (const [v, l] of [['', 'None'], ['sequence', 'Sequence'], ['playlist', 'Playlist'], ['audio', 'Audio']]) {
const o = new Option(l, v);
if (v === typeVal) o.selected = true;
typeSel.appendChild(o);
}
const valueSel = document.createElement('select');
valueSel.className = 'form-select form-select-sm ' + (valueClass || '');
function populateValues() {
const t = typeSel.value;
valueSel.innerHTML = '';
valueSel.disabled = !t;
if (!t) { valueSel.appendChild(new Option('—', '')); return; }
valueSel.appendChild(new Option('-- None --', ''));
for (const s of (ACTION_LISTS[t] || [])) {
const o = new Option(s, s);
if (s === valueVal) o.selected = true;
valueSel.appendChild(o);
}
}
typeSel.onchange = () => { valueVal = ''; populateValues(); };
populateValues();
wrap.append(typeSel, valueSel);
return wrap;
}
function addTeamRow(lg, data) {
data = data || {};
const isFootball = (lg === 'nfl' || lg === 'ncaa');
const container = document.getElementById('teams-' + lg);
const card = document.createElement('div');
card.className = 'card mb-3 team-card';
// Header
const header = document.createElement('div');
header.className = 'card-header d-flex align-items-center gap-2';
const logo = document.createElement('img');
logo.className = 'team-logo rounded';
logo.style.cssText = 'height:40px;display:none;';
const title = document.createElement('strong');
title.className = 'team-title';
title.textContent = data.teamName || LEAGUE_LABELS[lg];
const badge = document.createElement('span');
badge.className = 'badge bg-secondary ms-auto team-badge';
badge.textContent = '-';
const removeBtn = document.createElement('button');
removeBtn.className = 'btn btn-outline-danger btn-sm ms-2';
removeBtn.textContent = 'Remove';
removeBtn.onclick = () => card.remove();
header.append(logo, title, badge, removeBtn);
// Body
const body = document.createElement('div');
body.className = 'card-body';
// Team row
const teamRow = document.createElement('div');
teamRow.className = 'row g-3 mb-3';
const teamCol = document.createElement('div');
teamCol.className = 'col-md-6';
const teamLabel = document.createElement('label');
teamLabel.className = 'form-label';
teamLabel.textContent = 'Team';
const teamSel = buildTeamSelect(lg, data.teamID || '');
teamSel.onchange = () => {
const opt = teamSel.options[teamSel.selectedIndex];
title.textContent = opt.value ? opt.text : LEAGUE_LABELS[lg];
logo.style.display = 'none';
};
teamCol.append(teamLabel, teamSel);
const refreshCol = document.createElement('div');
refreshCol.className = 'col-md-6 d-flex align-items-end';
const refreshBtn = document.createElement('button');
refreshBtn.className = 'btn btn-outline-secondary btn-sm';
refreshBtn.textContent = 'Refresh Team Info';
refreshBtn.onclick = () => {
const cards = Array.from(container.children);
refreshLeague(lg, cards.indexOf(card));
};
refreshCol.append(refreshBtn);
teamRow.append(teamCol, refreshCol);
// Sequence row
const seqRow = document.createElement('div');
seqRow.className = 'row g-3';
function actionCol(label, typeClass, valueClass, typeVal, valueVal) {
const col = document.createElement('div');
col.className = 'col-md-6';
const lbl = document.createElement('label');
lbl.className = 'form-label';
lbl.textContent = label;
col.append(lbl, buildActionSelects(typeVal, valueVal, typeClass, valueClass));
return col;
}
seqRow.append(actionCol('Win', 'win-type', 'win-val',
data.winActionType || '', data.winActionValue || ''));
if (isFootball) {
seqRow.append(actionCol('Touchdown', 'td-type', 'td-val',
data.touchdownActionType || '', data.touchdownActionValue || ''));
seqRow.append(actionCol('Field Goal', 'fg-type', 'fg-val',
data.fieldgoalActionType || '', data.fieldgoalActionValue || ''));
} else {
seqRow.append(actionCol('Score', 'score-type', 'score-val',
data.scoreActionType || '', data.scoreActionValue || ''));
}
body.append(teamRow, seqRow);
card.append(header, body);
container.append(card);
// Apply logo/badge if we have live data
if (data.teamLogo) { logo.src = data.teamLogo; logo.style.display = ''; }
updateCardBadge(card, data.gameStatus || '');
}
function updateCardBadge(card, gameStatus) {
const badge = card.querySelector('.team-badge');
if (!badge) return;
const classes = { pre: 'badge bg-info text-dark ms-auto team-badge', in: 'badge bg-success ms-auto team-badge', post: 'badge bg-secondary ms-auto team-badge' };
badge.className = classes[gameStatus] || 'badge bg-secondary ms-auto team-badge';
badge.textContent = gameStatus ? gameStatus.toUpperCase() : '-';
}
async function loadConfig() {
try {
const resp = await fetch('/api/plugin-apis/ProSportsScoring/config');
if (!resp.ok) return;
const cfg = await resp.json();
document.getElementById('enabled').checked = !!cfg.enabled;
for (const lg of LEAGUES) {
const teams = (cfg.leagues || {})[lg] || [];
const container = document.getElementById('teams-' + lg);
container.innerHTML = '';
for (const t of teams) {
if (t.teamID) addTeamRow(lg, t);
}
}
} catch (e) {
console.error('loadConfig error:', e);
}
}
function buildConfigPayload() {
const cfg = { enabled: document.getElementById('enabled').checked, leagues: {} };
for (const lg of LEAGUES) {
const isFootball = (lg === 'nfl' || lg === 'ncaa');
const teams = [];
for (const card of document.getElementById('teams-' + lg).children) {
const t = {
teamID: (card.querySelector('.team-select') || {}).value || '',
winActionType: (card.querySelector('.win-type') || {}).value || '',
winActionValue: (card.querySelector('.win-val') || {}).value || ''
};
if (isFootball) {
t.touchdownActionType = (card.querySelector('.td-type') || {}).value || '';
t.touchdownActionValue = (card.querySelector('.td-val') || {}).value || '';
t.fieldgoalActionType = (card.querySelector('.fg-type') || {}).value || '';
t.fieldgoalActionValue = (card.querySelector('.fg-val') || {}).value || '';
} else {
t.scoreActionType = (card.querySelector('.score-type') || {}).value || '';
t.scoreActionValue = (card.querySelector('.score-val') || {}).value || '';
}
teams.push(t);
}
cfg.leagues[lg] = teams;
}
return cfg;
}
async function saveConfig() {
const statusEl = document.getElementById('save-status');
statusEl.textContent = 'Saving...';
try {
const resp = await fetch('/api/plugin-apis/ProSportsScoring/config', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(buildConfigPayload())
});
statusEl.textContent = resp.ok ? 'Saved.' : 'Save failed (' + resp.status + ')';
if (resp.ok) setTimeout(() => { statusEl.textContent = ''; }, 2000);
} catch (e) {
statusEl.textContent = 'Save error: ' + e;
}
}
async function refreshLeague(lg, idx) {
const statusEl = document.getElementById('save-status');
statusEl.textContent = 'Refreshing...';
await fetch('/api/plugin-apis/ProSportsScoring/config', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(buildConfigPayload())
});
try {
const resp = await fetch(`/api/plugin-apis/ProSportsScoring/refresh/${lg}/${idx}`, { method: 'POST' });
statusEl.textContent = '';
if (resp.ok) loadConfig();
else statusEl.textContent = 'Refresh failed (' + resp.status + ')';
} catch (e) {
statusEl.textContent = 'Refresh error: ' + e;
}
}
loadConfig();
</script>
</body>
</html>