From 0a13c6ea725feead616a766bba443c2e90cb73ee Mon Sep 17 00:00:00 2001
From: LHTY03 <143739953+LHTY-RPI@users.noreply.github.com>
Date: Wed, 4 Dec 2024 18:50:58 -0500
Subject: [PATCH] added friend tag
---
friend/friend.php | 35 ++++++++
friend/src/friend.css | 124 ++++++++++++++++++++++++++++
friend/src/friend.js | 101 ++++++++++++++++++++++
friend/src/getFriendPredictions.php | 113 +++++++++++++++++++++++++
php/config.php | 2 +-
php/db_connect.php | 38 +++++++++
php/header.php | 5 +-
7 files changed, 416 insertions(+), 2 deletions(-)
create mode 100644 friend/friend.php
create mode 100644 friend/src/friend.css
create mode 100644 friend/src/friend.js
create mode 100644 friend/src/getFriendPredictions.php
create mode 100644 php/db_connect.php
diff --git a/friend/friend.php b/friend/friend.php
new file mode 100644
index 0000000..7d1e4b1
--- /dev/null
+++ b/friend/friend.php
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+
+ Friends & Predictions
+
+
+
+
+Friends & Recent Predictions
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/friend/src/friend.css b/friend/src/friend.css
new file mode 100644
index 0000000..41bef98
--- /dev/null
+++ b/friend/src/friend.css
@@ -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;
+}
\ No newline at end of file
diff --git a/friend/src/friend.js b/friend/src/friend.js
new file mode 100644
index 0000000..c608125
--- /dev/null
+++ b/friend/src/friend.js
@@ -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 =
+ 'Failed to load friends and predictions.
';
+ }
+}
+
+// Function to display login message
+function displayLoginMessage() {
+ const friendsList = document.getElementById('friends-list');
+ friendsList.innerHTML = `
+
+
Please log in to view friends and predictions.
+
Go to Login
+
+ `;
+}
+
+// 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 = 'No friends found
';
+ 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 = `
+ ${prediction.home_team}
+ vs
+ ${prediction.away_team}
+ `;
+
+ const pointsInfo = document.createElement('div');
+ pointsInfo.className = 'points-info';
+ pointsInfo.innerHTML = `
+ Wagered: ${prediction.points_wagered}
+
+ ${prediction.points > 0 ? '+' : ''}${prediction.points}
+
+ `;
+
+ predictionItem.appendChild(gameInfo);
+ predictionItem.appendChild(pointsInfo);
+ predictionsList.appendChild(predictionItem);
+ });
+ } else {
+ predictionsList.innerHTML = 'No recent predictions
';
+ }
+
+ friendsList.appendChild(friendSection);
+ });
+}
+
+// Initialize the page
+document.addEventListener('DOMContentLoaded', fetchFriendsAndPredictions);
\ No newline at end of file
diff --git a/friend/src/getFriendPredictions.php b/friend/src/getFriendPredictions.php
new file mode 100644
index 0000000..02cb8a2
--- /dev/null
+++ b/friend/src/getFriendPredictions.php
@@ -0,0 +1,113 @@
+ 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();
+ }
+}
+?>
\ No newline at end of file
diff --git a/php/config.php b/php/config.php
index bd67c96..161095d 100644
--- a/php/config.php
+++ b/php/config.php
@@ -1,3 +1,3 @@
\ No newline at end of file
diff --git a/php/db_connect.php b/php/db_connect.php
new file mode 100644
index 0000000..878d520
--- /dev/null
+++ b/php/db_connect.php
@@ -0,0 +1,38 @@
+connect_error) {
+ throw new Exception("Connection failed: " . $conn->connect_error);
+ }
+
+ // Set charset to handle special characters properly
+ $conn->set_charset("utf8mb4");
+
+} catch (Exception $e) {
+ // Return error as JSON
+ http_response_code(500);
+ echo json_encode(array(
+ 'success' => false,
+ 'error' => $e->getMessage()
+ ));
+ exit();
+}
+
+// Function to safely close connection
+function closeConnection($conn) {
+ if (isset($conn)) {
+ $conn->close();
+ }
+}
+?>
\ No newline at end of file
diff --git a/php/header.php b/php/header.php
index 5d92fa2..a86e521 100644
--- a/php/header.php
+++ b/php/header.php
@@ -31,7 +31,10 @@
Predictions
- Leaderboard
+ Leaderboard
+
+
+ Friends