Skip to content

Commit

Permalink
Merge pull request #106 from RPI-ITWS/friend_tag
Browse files Browse the repository at this point in the history
added friend tag
  • Loading branch information
LHTY03 authored Dec 4, 2024
2 parents d67dd17 + 0a13c6e commit 39bfc67
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 2 deletions.
35 changes: 35 additions & 0 deletions friend/friend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./src/friend.css">

<?php include "../php/include_header.php"?>
<title>Friends & Predictions</title>
</head>
<body>
<?php include "../php/header.php" ?>

<h1>Friends & Recent Predictions</h1>

<div class="friends-container">
<div class="friends-list" id="friends-list">

</div>
</div>

<template id="friend-prediction-template">
<div class="friend-section">
<div class="friend-header">
<h2 class="friend-name"></h2>
<span class="total-points"></span>
</div>
<div class="predictions-list">

</div>
</div>
</template>
<script src="./src/friend.js" defer></script>
</body>
</html>
124 changes: 124 additions & 0 deletions friend/src/friend.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
body {
font-family: Arial, sans-serif;
background-color: #f8f8f8;
color: #333;
margin: 0;
}

h1 {
text-align: left;
font-size: 24px;
font-weight: bold;
padding: 30px 0px 20px 20px;
color: #000;
}

.friends-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}

.login-message {
background-color: #ffffff;
border: 1px solid #ddd;
border-radius: 8px;
padding: 30px;
text-align: center;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.login-message p {
margin-bottom: 20px;
font-size: 18px;
color: #666;
}

.login-button {
display: inline-block;
padding: 10px 20px;
background-color: #007bff;
color: white;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
transition: background-color 0.2s ease;
}

.login-button:hover {
background-color: #0056b3;
}

.friend-section {
background-color: #ffffff;
border: 1px solid #ddd;
border-radius: 8px;
padding: 15px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}

.friend-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 1px solid #ddd;
}

.friend-name {
font-size: 18px;
font-weight: bold;
color: #333;
margin: 0;
transition: color 0.2s ease;
cursor: pointer;
}

.friend-name:hover {
color: #007bff;
text-decoration: underline;
}

.total-points {
font-weight: bold;
padding: 5px 10px;
border-radius: 4px;
background-color: #f0f0f0;
}

.predictions-list {
display: flex;
flex-direction: column;
gap: 10px;
}

.prediction-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
background-color: #f8f8f8;
border-radius: 4px;
}

.game-info {
flex: 1;
}

.prediction-points {
padding: 3px 8px;
border-radius: 3px;
font-weight: bold;
}

.points-gained {
background-color: #d4edda;
color: #155724;
}

.points-lost {
background-color: #f8d7da;
color: #721c24;
}
101 changes: 101 additions & 0 deletions friend/src/friend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Function to fetch friends and their recent predictions
async function fetchFriendsAndPredictions() {
try {
const response = await fetch('./src/getFriendPredictions.php');
if (!response.ok) {
const data = await response.json();
if (response.status === 401) {
displayLoginMessage();
return;
}
throw new Error(data.message || 'Unknown error occurred');
}
const data = await response.json();
if (data.success) {
displayFriendsAndPredictions(data.friends);
} else {
throw new Error(data.error || 'Unknown error occurred');
}
} catch (error) {
console.error('Error fetching friends and predictions:', error);
document.getElementById('friends-list').innerHTML =
'<div class="error-message">Failed to load friends and predictions.</div>';
}
}

// Function to display login message
function displayLoginMessage() {
const friendsList = document.getElementById('friends-list');
friendsList.innerHTML = `
<div class="login-message">
<p>Please log in to view friends and predictions.</p>
<a href="../Auth/Login/login.php" class="login-button">Go to Login</a>
</div>
`;
}

// Function to display friends and their predictions
function displayFriendsAndPredictions(friends) {
const friendsList = document.getElementById('friends-list');
const template = document.getElementById('friend-prediction-template');
friendsList.innerHTML = '';

if (!friends || friends.length === 0) {
friendsList.innerHTML = '<div class="no-friends">No friends found</div>';
return;
}

friends.forEach(friend => {
const friendSection = template.content.cloneNode(true);

// Set friend name and total points
const friendNameElement = friendSection.querySelector('.friend-name');
friendNameElement.textContent = friend.player;

// Add click event and cursor style to friend name
friendNameElement.style.cursor = 'pointer';
friendNameElement.addEventListener('click', () => {
window.location.href = `../profile/profile.php?id=${friend.id}`;
});

friendSection.querySelector('.total-points').textContent =
`Total Points: ${friend.total_points}`;

// Add predictions
const predictionsList = friendSection.querySelector('.predictions-list');
if (friend.predictions && friend.predictions.length > 0) {
friend.predictions.forEach(prediction => {
const predictionItem = document.createElement('div');
predictionItem.className = 'prediction-item';

const gameInfo = document.createElement('div');
gameInfo.className = 'game-info';
gameInfo.innerHTML = `
<span class="team ${prediction.prediction === prediction.home_team ? 'predicted' : ''}">${prediction.home_team}</span>
vs
<span class="team ${prediction.prediction === prediction.away_team ? 'predicted' : ''}">${prediction.away_team}</span>
`;

const pointsInfo = document.createElement('div');
pointsInfo.className = 'points-info';
pointsInfo.innerHTML = `
<span class="points-wagered">Wagered: ${prediction.points_wagered}</span>
<span class="prediction-points ${prediction.points > 0 ? 'points-gained' : 'points-lost'}">
${prediction.points > 0 ? '+' : ''}${prediction.points}
</span>
`;

predictionItem.appendChild(gameInfo);
predictionItem.appendChild(pointsInfo);
predictionsList.appendChild(predictionItem);
});
} else {
predictionsList.innerHTML = '<div class="no-predictions">No recent predictions</div>';
}

friendsList.appendChild(friendSection);
});
}

// Initialize the page
document.addEventListener('DOMContentLoaded', fetchFriendsAndPredictions);
113 changes: 113 additions & 0 deletions friend/src/getFriendPredictions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
http_response_code(401);
echo json_encode(array(
'success' => false,
'error' => 'not_logged_in',
'message' => 'Please log in to view friends and predictions'
));
exit;
}

try {
if (!isset($_SESSION['user_id'])) {
throw new Exception("User not logged in");
}

$servername = "localhost";
$username = "superuser";
$password_db = "superuser";
$dbname = "WinShare";

$conn = new mysqli($servername, $username, $password_db, $dbname);

if ($conn->connect_error) {
throw new Exception("Connection failed: " . $conn->connect_error);
}

$userId = $_SESSION['user_id'];

$sql = "SELECT
u.id,
u.username as player,
u.profilePic as profilePic,
gp.home_team,
gp.away_team,
gp.winner_name as prediction,
gp.points_earned as points,
gp.points_wagered,
gp.prediction_time
FROM users u
INNER JOIN friendships f ON (f.friend_id = u.id OR f.user_id = u.id)
LEFT JOIN game_predictions gp ON u.id = gp.user_id
WHERE (f.user_id = ? OR f.friend_id = ?)
AND f.status = 'accepted'
AND u.id != ?
AND (gp.prediction_time >= NOW() - INTERVAL 2 DAY OR gp.prediction_time IS NULL)
ORDER BY u.username, gp.prediction_time DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("iii", $userId, $userId, $userId);
$stmt->execute();
$result = $stmt->get_result();

$friends = array();
$currentFriend = null;

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($currentFriend === null || $currentFriend['id'] !== $row['id']) {
if ($currentFriend !== null) {
$friends[] = $currentFriend;
}
$currentFriend = [
'id' => $row['id'],
'player' => htmlspecialchars($row['player']),
'profilePic' => htmlspecialchars($row['profilePic']),
'total_points' => 0,
'predictions' => []
];
}

if ($row['prediction'] !== null) {
$currentFriend['predictions'][] = [
'home_team' => htmlspecialchars($row['home_team']),
'away_team' => htmlspecialchars($row['away_team']),
'prediction' => htmlspecialchars($row['prediction']),
'points_wagered' => (int)$row['points_wagered'],
'points' => (int)$row['points'],
'prediction_date' => $row['prediction_time']
];
$currentFriend['total_points'] += (int)$row['points'];
}
}

if ($currentFriend !== null) {
$friends[] = $currentFriend;
}
}

$response = array(
'success' => true,
'friends' => $friends
);

echo json_encode($response);

} catch (Exception $e) {
http_response_code(500);
echo json_encode(array(
'success' => false,
'error' => $e->getMessage()
));
} finally {
if (isset($conn)) {
$conn->close();
}
}
?>
2 changes: 1 addition & 1 deletion php/config.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
define('SPORTS_RADAR_API_KEY', 'tO63xruLG9m40nNmCJPrBh4XP81AlMhVfcIOTO6y');
define('SPORTS_RADAR_API_KEY', 'qdMaqOsygYU4p6F3nzIe6VhujLjhsTMvRX1WPjbK');
?>
Loading

0 comments on commit 39bfc67

Please sign in to comment.