Skip to content

Commit

Permalink
Recent masteries (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsearo authored Apr 13, 2022
1 parent 1f32c4c commit ead7478
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/database/mastery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Gets completed and mastery award information.
* This includes User, Game and Completed or Mastered Date.
*
* Results are configurable based on input parameters allowing returning data for a specific users friends
* and selecting a specific date
*
* @param string $date Date to grab information from
* @param string $friendsOf User to get friends data for
* @param int $offset starting point to return rows
* @param int $count number of rows to return
* @return array Leaderboard data to display
*/
function getRecentMasteryData($date, $friendsOf = null, $offset = 0, $count = 50)
{
// Determine the friends condition
$friendCondAward = "";
if ($friendsOf !== null) {
$friendCondAward = "AND (saw.User IN (SELECT Friend FROM Friends WHERE User LIKE '$friendsOf' AND Friendship = 1) OR saw.User LIKE '$friendsOf')";
}

$retVal = [];
$query = "SELECT saw.User, saw.AwardDate as AwardedAt, UNIX_TIMESTAMP( saw.AwardDate ) as AwardedAtUnix, saw.AwardType, saw.AwardData, saw.AwardDataExtra, gd.Title AS GameTitle, gd.ID AS GameID, c.Name AS ConsoleName, gd.ImageIcon AS GameIcon
FROM SiteAwards AS saw
LEFT JOIN GameData AS gd ON gd.ID = saw.AwardData
LEFT JOIN Console AS c ON c.ID = gd.ConsoleID
WHERE saw.AwardType = 1 AND AwardData > 0 AND AwardDataExtra IS NOT NULL $friendCondAward
AND saw.AwardDate BETWEEN TIMESTAMP('$date') AND DATE_ADD('$date', INTERVAL 24 * 60 * 60 - 1 SECOND)
ORDER BY AwardedAt DESC
LIMIT $offset, $count";

$dbResult = s_mysql_query($query);
if ($dbResult !== false) {
while ($db_entry = mysqli_fetch_assoc($dbResult)) {
$retVal[] = $db_entry;
}
}
return $retVal;
}
1 change: 1 addition & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_once __DIR__ . '/database/game.php';
require_once __DIR__ . '/database/history.php';
require_once __DIR__ . '/database/leaderboard.php';
require_once __DIR__ . '/database/mastery.php';
require_once __DIR__ . '/database/message.php';
require_once __DIR__ . '/database/news.php';
require_once __DIR__ . '/database/playlist.php';
Expand Down
1 change: 1 addition & 0 deletions lib/render/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ function RenderToolbar($user, $permissions = 0)
echo "<li><a href='/developerstats.php'>Developers</a></li>";
// echo "<li><a href='/leaderboardList.php'>Leaderboards</a></li>";
echo "<li><a href='/globalRanking.php'>Global Ranking</a></li>";
echo "<li><a href='/recentMastery.php'>Recent Masteries</a></li>";
echo "<li class='divider'></li>";
echo "<li><a href='https://docs.retroachievements.org/'>User Documentation</a></li>";
echo "<li><a href='https://docs.retroachievements.org/Developer-docs/'>Developer Documentation</a></li>";
Expand Down
166 changes: 166 additions & 0 deletions public/recentMastery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../lib/bootstrap.php';

RA_ReadCookieCredentials($user, $points, $truePoints, $unreadMessageCount, $permissions);

$maxCount = 25;
// First day with Game Awards
$minDate = '2014-09-29';

$errorCode = requestInputSanitized('e');
$offset = requestInputSanitized('o', 0, 'integer');
$offset = max($offset, 0);
$friends = requestInputSanitized('f', 0, 'integer');
$date = requestInputSanitized('d', date("Y-m-d"));

switch ($friends) {
case 0: // Global
$lbUsers = "";
break;
case 1: // Friends
$lbUsers = "Friends";
break;
default:
$lbUsers = "";
break;
}

if ($friends == 1) {
$data = getRecentMasteryData($date, $user, $offset, $maxCount + 1);
} else {
$data = getRecentMasteryData($date, null, $offset, $maxCount + 1);
}

RenderHtmlStart();
RenderHtmlHead("Recent " . $lbUsers . " Masteries");
?>
<body>
<?php
RenderTitleBar($user, $points, $truePoints, $unreadMessageCount, $errorCode, $permissions);
RenderToolbar($user, $permissions);
?>
<div id='mainpage'>
<div id='fullcontainer'>
<?php
RenderErrorCodeWarning($errorCode);
echo "<h2 class='longheader'>Recent " . $lbUsers . " Masteries</h2>";

// Add the leaderboard filters
echo "<div class='embedded mb-1'>";

// Create the Users filters only if a user is logged in
if ($user !== null) {
echo "<div>";
echo "<b>Users:</b> ";
if ($friends == 0) {
echo "<b><a href='/recentMastery.php?d=$date&f=0'>*All Users</a></b> | ";
} else {
echo "<a href='/recentMastery.php?d=$date&f=0'>All Users</a> | ";
}
if ($friends == 1) {
echo "<b><a href='/recentMastery.php?d=$date&f=1'>*Friends Only</a></b>";
} else {
echo "<a href='/recentMastery.php?d=$date&f=1'>Friends Only</a>";
}
echo "</div>";
}

// Create the custom date filter
echo "<form action='/recentMastery.php' method='get'>";
echo "<label for='d'><b>Jump to Date: </b></label>";
echo "<input type='hidden' name='t' value=" . 0 . ">";
echo "<input type='date' name='d' value=" . $date . " min=$minDate max=" . date("Y-m-d") . "> ";
echo "<input type='hidden' name='f' value=" . $friends . ">";
echo "<input type='submit' value='Goto Date' />";
echo "</form>";

// Clear filter
if ($date != date("Y-m-d") || $friends != 0) {
echo "<div>";
echo "<a href='/recentMastery.php'>Clear Filter</a>";
echo "</div>";
}
echo "</div>";

echo "<table><tbody>";

// Headers
echo "<tr>";
echo "<th>User</th>";
echo "<th>Type</th>";
echo "<th>Game</th>";
echo "<th>Date</th>";
echo "</tr>";

$userCount = 0;
$skip = false;
// Create the table rows
foreach ($data as $dataPoint) {
// Break if we have hit the maxCount + 1 user
if ($userCount == $maxCount) {
$userCount++;
$skip = true;
}

if (!$skip) {
echo "<tr>";

echo "<td>";
echo GetUserAndTooltipDiv($dataPoint['User'], true);
echo GetUserAndTooltipDiv($dataPoint['User'], false);
echo "</td>";

echo "<td>";
if ($dataPoint['AwardDataExtra'] == 1) {
echo "Mastered";
} else {
echo "Completed";
}
echo "</td>";

echo "<td>";
echo GetGameAndTooltipDiv($dataPoint['GameID'], $dataPoint['GameTitle'], $dataPoint['GameIcon'], $dataPoint['ConsoleName']);
echo "</td>";

echo "<td>";
echo $dataPoint['AwardedAt'];
echo "</td>";

echo "</tr>";
$userCount++;
}
}
echo "</tbody></table>";

// Add page traversal
echo "<div class='rightalign row'>";
if ($date > $minDate) {
$prevDate = date('Y-m-d', strtotime($date . "-1 days"));
echo "<a href='/recentMastery.php?d=$prevDate&f=$friends&o=0'>&lt; Prev Day </a>";
if ($date < date("Y-m-d")) {
echo " | ";
}
}
if ($offset > 0) {
$prevOffset = $offset - $maxCount;
echo "<a href='/recentMastery.php?d=$date&f=$friends&o=$prevOffset'>&lt; Prev $maxCount </a>";
}
if ($userCount > $maxCount) {
if ($offset > 0) {
echo " - ";
}
$nextOffset = $offset + $maxCount;
echo "<a href='/recentMastery.php?d=$date&f=$friends&o=$nextOffset'>Next $maxCount &gt;</a>";
}
if ($date < date("Y-m-d")) {
$nextDate = date('Y-m-d', strtotime($date . "+1 days"));
echo " | <a href='/recentMastery.php?d=$nextDate&f=$friends&o=0'>Next Day &gt;</a>";
}
echo "</div>";
?>
</div>
</div>
<?php RenderFooter(); ?>
</body>
<?php RenderHtmlEnd(); ?>

0 comments on commit ead7478

Please sign in to comment.