Below is a simple web app to display your college baseball game predictions. The app assumes you have a JSON file (e.g., data.json) containing the matchups and simulation data. Here's a quick overview of the setup:
JSON Structure
Your JSON file should look something like this:
{
"matchups": [
{
"team1": "Team A",
"team2": "Team B",
"simulations": [
{"winner": "Team A"},
{"winner": "Team B"},
{"winner": "Team A"}
// More simulation objects...
]
},
{
"team1": "Team C",
"team2": "Team D",
"simulations": [
{"winner": "Team C"},
{"winner": "Team D"}
// More simulation objects...
]
}
// More matchups...
]
}
- Each matchup has two teams (
team1 and team2).
- The
simulations array contains objects, each indicating the winner of a simulation run.
- The app will calculate win probabilities based on the simulations (e.g., percentage of simulations where a team wins).
Web App Code
The web app uses plain HTML, CSS, and JavaScript. It fetches the JSON file, processes the data, and displays the predictions in a simple list.
- index.html: The main HTML file.
- style.css: Basic styling for readability.
- script.js: JavaScript to fetch and process the JSON data.
Save these files in the same directory as your JSON file (e.g., data.json). You can open index.html in a web browser to run it. If you're serving this on a web server, ensure the JSON file is in the same accessible path.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>College Baseball Predictions</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>College Baseball Game Predictions</h1>
<div id="predictions-container"></div>
<script src="script.js"></script>
</body>
</html>
style.css
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f4f4f4;
}
h1 {
color: #333;
text-align: center;
}
.matchup {
border: 1px solid #ddd;
padding: 15px;
margin-bottom: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.matchup h2 {
color: #007bff;
}
.matchup p {
font-size: 16px;
margin: 5px 0;
}
script.js
document.addEventListener('DOMContentLoaded', () => {
const predictionsContainer = document.getElementById('predictions-container');
// Fetch the JSON file
fetch('data.json')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
const matchups = data.matchups; // Array of matchups
if (!matchups || matchups.length === 0) {
predictionsContainer.innerHTML = '<p>No matchups found in the data.</p>';
return;
}
matchups.forEach(matchup => {
const team1 = matchup.team1;
const team2 = matchup.team2;
const simulations = matchup.simulations || []; // Array of simulation results
if (simulations.length === 0) {
predictionsContainer.innerHTML += `<div class="matchup"><h2>${team1} vs ${team2}</h2><p>No simulations available.</p></div>`;
return;
}
// Calculate wins for each team
let team1Wins = 0;
simulations.forEach(sim => {
if (sim.winner === team1) team1Wins++;
});
const totalSimulations = simulations.length;
const team1WinProbability = ((team1Wins / totalSimulations) * 100).toFixed(2); // Percentage
const team2Wins = totalSimulations - team1Wins; // Assuming no ties
const team2WinProbability = ((team2Wins / totalSimulations) * 100).toFixed(2);
// Generate HTML for this matchup
const html = `
<div class="matchup">
<h2>${team1} vs ${team2}</h2>
<p>${team1} win probability: ${team1WinProbability}%</p>
<p>${team2} win probability: ${team2WinProbability}%</p>
</div>
`;
predictionsContainer.innerHTML += html;
});
})
.catch(error => {
console.error('Error fetching or processing data:', error);
predictionsContainer.innerHTML = '<p>Error loading predictions. Please check the JSON file.</p>';
});
});
How It Works
- Loading Data: When the page loads,
script.js uses the Fetch API to load data.json. Make sure this file is in the same directory or provide the correct path (e.g., if it's on a server, use a full URL).
- Processing Data: For each matchup, the script counts the number of simulations where
team1 won and calculates the win probabilities as percentages.
- Displaying Data: The results are dynamically added to the page in a simple card-like format for each matchup.
- Error Handling: If the JSON file is missing, malformed, or empty, the app displays a user-friendly error message.
Running the App
- Locally: Save the files and open
index.html in a browser. If you're using a browser's security features (like CORS), you might need a local server (e.g., via Python's http.server or Node.js with Express).
- On a Server: Upload the files to a web server. Place
data.json in the same directory as index.html.
- Assumptions:
- Simulations always result in a winner (no ties are handled).
- If you have a large JSON file, this simple app should still work, but for very large datasets, you might want to optimize it.
If you need enhancements (e.g., real-time streaming of simulations, more advanced styling, or sorting), let me know!