+
+ if ($cfgEnableQr && $game->website) {
+ $qrfileName = preg_replace('#[^\\w\\d._-]#', '_', $game->website) . '.png';
+ $qrfilePath = $GLOBALS['qrDir'] . $qrfileName;
+ if (!file_exists($qrfilePath)) {
+ $cmd = __DIR__ . '/create-qr.sh'
+ . ' ' . escapeshellarg($game->website)
+ . ' ' . escapeshellarg($qrfilePath);
+ passthru($cmd, $retval);
+ if ($retval != 0) {
+ exit(20);
+ }
+ }
+ $qrUrlPath = $GLOBALS['baseUrl'] . 'gen-qr/' . $qrfileName;
+ $game->media[] = (object) [
+ 'type' => 'image',
+ 'url' => $qrUrlPath,
+ ];
+ }
+
+ //rewrite urls from Internet Archive to our servers
+ $game->discover = rewriteUrl($game->discover);
+ foreach ($game->media as $medium) {
+ $medium->url = rewriteUrl($medium->url);
+ }
+ foreach ($game->releases as $release) {
+ $release->url = rewriteUrl($release->url);
+ }
+}
+
+/**
+ * Implements a sensible ranking system described in
+ * https://stackoverflow.com/a/1411268/2826013
+ */
+function calculateRank(array $games)
+{
+ $averageRatings = array_map(
+ function ($game) {
+ return $game->rating->average;
+ },
+ $games
+ );
+ $average = array_sum($averageRatings) / count($averageRatings);
+ $C = $average;
+ $m = 500;
+
+ foreach ($games as $game) {
+ $R = $game->rating->average;
+ $v = $game->rating->count;
+ $game->rating->rank = ($R * $v + $C * $m) / ($v + $m);
+ }
+}
+
+function getFirstVideoUrl($media)
+{
+ foreach ($media as $medium) {
+ if ($medium->type == 'video') {
+ return $medium->url;
+ }
+ }
+ return null;
+}
+
+function getAllImageUrls($media)
+{
+ $imageUrls = [];
+ foreach ($media as $medium) {
+ if ($medium->type == 'image') {
+ $imageUrls[] = $medium->url;
+ }
+ }
+ return $imageUrls;
+}
+
+function getPromotedProduct($game)
+{
+ if (!isset($game->products) || !count($game->products)) {
+ return null;
+ }
+ foreach ($game->products as $gameProd) {
+ if ($gameProd->promoted) {
+ return $gameProd;
+ }
+ }
+ return null;
+}
+
+/**
+ * vimeo only work with HTTPS now,
+ * and the OUYA does not support SNI.
+ * We get SSL errors and no video for them :/
+ */
+function isUnsupportedVideoUrl($url)
+{
+ return strpos($url, '://vimeo.com/') !== false;
+}
+
+function removeMakeGames(array $games)
+{
+ return filterByGenre($games, 'Tutorials', true);
+}
+
+function removeMakeGenres($genres)
+{
+ $filtered = [];
+ foreach ($genres as $genre) {
+ if ($genre != 'Tutorials' && $genre != 'Builds') {
+ $filtered[] = $genre;
+ }
+ }
+ return $filtered;
+}
+
+function rewriteUrl($url)
+{
+ foreach ($GLOBALS['urlRewrites'] as $pattern => $replacement) {
+ $url = preg_replace($pattern, $replacement, $url);
+ }
+ return $url;