Skip to content

Commit

Permalink
Better breeding page cache control
Browse files Browse the repository at this point in the history
  • Loading branch information
thommcgrath committed Dec 31, 2024
1 parent ac21739 commit d7eef10
Showing 1 changed file with 82 additions and 61 deletions.
143 changes: 82 additions & 61 deletions Website/www/games/arksa/breeding.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
$ism = isset($_GET['ism']) ? floatval($_GET['ism']) : 1.0;
$iam = isset($_GET['iam']) ? floatval($_GET['iam']) : 1.0;

$steamIds = [];
$contentPackIds = [];
$curseForgeIds = [];
$m = isset($_GET['m']) ? explode(',', $_GET['m']) : [];
for ($idx = 0; $idx < count($m); $idx++) {
$id = filter_var($m[$idx], FILTER_VALIDATE_INT);
if ($id !== false) {
$steamIds[] = $id;
$curseForgeIds[] = $id;
} elseif (BeaconCommon::IsUUID($m[$idx])) {
$contentPackIds[] = $m[$idx];
}
}

Expand All @@ -25,16 +28,55 @@
$iam = ($iam > 0) ? $iam : 1.0;

$database = BeaconCommon::Database();
$results = $database->Query('SELECT MAX(build_number) AS newest_build FROM updates;');
$minVersion = $results->Field('newest_build');

$officialPacks = ContentPack::Search(['minVersion' => $minVersion, 'isOfficial' => true], true);
foreach ($officialPacks as $officialPack) {
$contentPackIds[] = $officialPack->ContentPackId();
}

$marketplacePacks = ContentPack::Search(['minVersion' => $minVersion, 'marketplace' => 'CurseForge', 'marketplaceId' => implode(',', $curseForgeIds)], true);
foreach ($marketplacePacks as $marketplacePack) {
$contentPackIds[] = $marketplacePack->ContentPackId();
}

$results = $database->Query('SELECT MAX(EXTRACT(EPOCH FROM last_update)) AS last_update FROM public.content_update_times WHERE min_version <= $1 AND content_pack_id = ANY($2);', $minVersion, '{' . implode(',', $contentPackIds) . '}');
$lastUpdate = $results->Field('last_update');

$cacheKey = md5('arksa.breeding:' . $lastUpdate . ';msm=' . number_format($msm, 8) . ';ipm=' . number_format($ipm, 8) . ';ism=' . number_format($ism, 8) . ';iam=' . number_format($iam, 8) . ';m=' . implode(',', $contentPackIds));
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
$tags = explode(',', stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
$validMatches = ['"' . $cacheKey . '"', 'W/"' . $cacheKey . '"'];
foreach ($tags as $tag) {
$tag = trim($tag);
if (in_array($tag, $validMatches)) {
http_response_code(304);
header('Content-Type: text/plain');
exit;
}
}
}

header('ETag: "' . $cacheKey . '"');
BeaconTemplate::AddStylesheet(BeaconCommon::AssetURI('breeding.css'));

$cached = BeaconCache::Get($cacheKey);
if (is_null($cached) === false) {
echo $cached;
exit;
}

$results = $database->Query('SELECT value::INTEGER FROM arksa.game_variables WHERE key = $1;', 'Cuddle Period');
if ($results->RecordCount() != 1) {
http_response_code(500);
echo "There was a problem loading the default imprint interval.";
exit;
}
$official_cuddle_period = $results->Field('value');
$computed_cuddle_period = round($official_cuddle_period * $ipm);
$officialCuddlePeriod = $results->Field('value');
$computedCuddlePeriod = round($officialCuddlePeriod * $ipm);

BeaconTemplate::AddStylesheet(BeaconCommon::AssetURI('breeding.css'));
ob_start();

?><div id="breeding-stats">
<div id="breeding-stats-msm" class="breeding-stats-column">
Expand All @@ -55,7 +97,7 @@
</div>
<div id="breeding-stats-if" class="breeding-stats-column">
<div class="breeding-stats-label">Imprint Frequency:</div>
<div class="breeding-stats-value"><?php echo htmlentities(BeaconCommon::SecondsToEnglish($computed_cuddle_period, true)); ?></div>
<div class="breeding-stats-value"><?php echo htmlentities(BeaconCommon::SecondsToEnglish($computedCuddlePeriod, true)); ?></div>
</div>
</div>
<table id="creature-chart" class="generic">
Expand All @@ -70,71 +112,50 @@
<tbody>
<?php

$cache_key = 'arksa.breeding:msm=' . number_format($msm, 8) . ';ipm=' . number_format($ipm, 8) . ';ism=' . number_format($ism, 8) . ';iam=' . number_format($iam, 8) . ';m=' . implode(',', $steamIds);
$cached = BeaconCache::Get($cache_key);

if (is_null($cached)) {
ob_start();

$results = $database->Query('SELECT MAX(build_number) AS newest_build FROM updates;');
$min_version = $results->Field('newest_build');

$officialPacks = ContentPack::Search(['minVersion' => $min_version, 'isOfficial' => true], true);
$officialPackIds = [];
foreach ($officialPacks as $officialPack) {
$officialPackIds[] = $officialPack->ContentPackId();
$showModNames = count($curseForgeIds) > 0;
$creatures = Creature::Search(['minVersion' => $minVersion, 'contentPackId' => $contentPackIds], true);
foreach ($creatures as $creature) {
if (is_null($creature->IncubationTimeSeconds()) || is_null($creature->MatureTimeSeconds())) {
continue;
}

$marketplacePacks = ContentPack::Search(['minVersion' => $min_version, 'marketplace' => 'Steam Workshop', 'marketplaceId' => implode(',', $steamIds)], true);
$marketplacePackIds = [];
foreach ($marketplacePacks as $marketplacePack) {
$marketplacePackIds[] = $marketplacePack->ContentPackId();
}

$showModNames = count($steamIds) > 0;
$combinedPackIds = array_merge($officialPackIds, $marketplacePackIds);
$creatures = Creature::Search(['minVersion' => $min_version, 'contentPackId' => $combinedPackIds], true);
foreach ($creatures as $creature) {
if (is_null($creature->IncubationTimeSeconds()) || is_null($creature->MatureTimeSeconds())) {
continue;
}

$incubation_seconds = $creature->IncubationTimeSeconds() / $ism;
$mature_seconds = $creature->MatureTimeSeconds() / $msm;
$incubationSeconds = $creature->IncubationTimeSeconds() / $ism;
$matureSeconds = $creature->MatureTimeSeconds() / $msm;

$max_cuddles = 0;
$per_cuddle = 0;
if ($iam > 0) {
$max_cuddles = floor($mature_seconds / $computed_cuddle_period);
$maxCuddles = 0;
$perCuddle = 0;
if ($iam > 0) {
$maxCuddles = floor($matureSeconds / $computedCuddlePeriod);

if ($max_cuddles > 0) {
$per_cuddle = min((1 / $max_cuddles) * $iam, 1.0);
$max_cuddles = ceil(1.0 / $per_cuddle);
}
}
if ($max_cuddles == 0) {
$cuddle_text = 'Can\'t Imprint';
} else {
$cuddle_text = number_format($per_cuddle * 100, 0) . '% ea / ' . $max_cuddles . ' total';
}

$label = htmlentities($creature->Label());
if ($showModNames) {
$label .= '<span class="beacon-engram-mod-name"><br>' . htmlentities($creature->ContentPackName()) . '</span>';
if ($maxCuddles > 0) {
$perCuddle = min((1 / $maxCuddles) * $iam, 1.0);
$maxCuddles = ceil(1.0 / $perCuddle);
}
}
if ($maxCuddles == 0) {
$cuddleText = 'Can\'t Imprint';
} else {
$cuddleText = number_format($perCuddle * 100, 0) . '% ea / ' . $maxCuddles . ' total';
}

$incubation_text = BeaconCommon::SecondsToEnglish(round($incubation_seconds), true);
$mature_text = BeaconCommon::SecondsToEnglish(round($mature_seconds), true);
echo '<tr><td>' . $label . '<span class="narrow-only text-lighter"><br><strong>Incubation Time:</strong> ' . htmlentities($incubation_text) . '<br><strong>Mature Time:</strong> ' . htmlentities($mature_text) . '<br><strong>Imprinting:</strong> ' . htmlentities($cuddle_text) . '</span></td><td class="wide-only">' . htmlentities($incubation_text) . '</td><td class="wide-only">' . htmlentities($mature_text) . '</td><td class="wide-only">' . htmlentities($cuddle_text) . '</td></tr>';
$label = htmlentities($creature->Label());
if ($showModNames) {
$label .= '<span class="beacon-engram-mod-name"><br>' . htmlentities($creature->ContentPackName()) . '</span>';
}

$cached = ob_get_contents();
ob_end_clean();
BeaconCache::Set($cache_key, $cached);
$incubationText = BeaconCommon::SecondsToEnglish(round($incubationSeconds), true);
$matureText = BeaconCommon::SecondsToEnglish(round($matureSeconds), true);
echo '<tr><td>' . $label . '<span class="narrow-only text-lighter"><br><strong>Incubation Time:</strong> ' . htmlentities($incubationText) . '<br><strong>Mature Time:</strong> ' . htmlentities($matureText) . '<br><strong>Imprinting:</strong> ' . htmlentities($cuddleText) . '</span></td><td class="wide-only">' . htmlentities($incubationText) . '</td><td class="wide-only">' . htmlentities($matureText) . '</td><td class="wide-only">' . htmlentities($cuddleText) . '</td></tr>';
}

echo $cached;
?>
</tbody>
</table>
<p class="smaller text-center">Any creature that can be imprinted can be imprinted to 100%</p>
<p class="smaller text-center">Any creature that can be imprinted can be imprinted to 100%</p><?php

$cached = ob_get_contents();
ob_end_clean();
BeaconCache::Set($cacheKey, $cached);
echo $cached;

?>

0 comments on commit d7eef10

Please sign in to comment.