function render($twig, $sdata = array()) { // time or score leaderboards if (isset($_GET['times'])) { $times = true; } else { $times = false; } // page defaults to 0 if (isset($_GET['page']) && (int) $_GET['page'] > 0) { $page = $_GET['page']; } else { $page = 0; } // get a leaderboard object $leaderboards_today = new Leaderboard(); // and populate it with either today's entries or entries from a given day if (isset($_GET['date'])) { $today = $leaderboards_today->createGlobal($_GET['date'], 'rank', 'ASC', $page * 30, 30, $times); } else { $today = $leaderboards_today->createGlobal(0, 'rank', 'ASC', $page * 30, 30, $times); } // it might happen that some leaderboards aren't ready yet // for the days to come if ($today) { $today = $today->toArray(); } else { echo $twig->render('index.php', $sdata); return false; } // get yesterday's leaderboards $leaderboards_yesterday = new Leaderboard(); $yesterday = $leaderboards_yesterday->createGlobal(1, 'rank', 'ASC', 0, 5, $times)->toArray(); // get global stats $global = $leaderboards_today->globalStats; // prev and next dates $first_daily = new DateTime('2015-10-30'); $date = new DateTime($leaderboards_today->date); $date->sub(new DateInterval('P1D')); $prevdate = $date >= $first_daily ? $date->format('Y-m-d') : $leaderboards_today->date; // $date->add(new DateInterval('P2D')); $nextdate = $date->format('Y-m-d'); // get user's friends ranks if available if (isset($_SESSION['steamid']) && $sdata['friends']) { $f = new Friends(); $friends = $f->getFriendsRanks($_SESSION['steamid'], $leaderboards_today->dayid); // if no friends have already played today's daily // return a full list of friends if (!$friends) { $friends = $sdata['friends']; } } if ($_SESSION['admin'] > 0) { unset($_SESSION['date']); } // where the wild things are $kids = ceil($global['cleanruns'] / 30) - 1; // get today's challenge from db $challenge = $leaderboards_today->challenge; // create the css class used for both the char and objective icons switch ($challenge['char_id']) { case 0: $daily_char = 'isaac'; break; case 1: $daily_char = 'maggie'; break; case 2: $daily_char = 'cain'; break; case 3: $daily_char = 'judas'; break; case 4: $daily_char = 'xxx'; break; case 5: $daily_char = 'eve'; break; case 6: $daily_char = 'samson'; break; case 7: $daily_char = 'azazel'; break; case 8: $daily_char = 'lazarus'; break; case 9: $daily_char = 'eden'; break; case 10: $daily_char = 'thelost'; break; case 11: $daily_char = 'lazarus2'; break; case 12: $daily_char = 'judas2'; break; case 13: $daily_char = 'lilith'; break; case 14: $daily_char = 'keeper'; break; default: $daily_char = 'question-mark'; break; } // endfloor switch ($challenge['endlevel']) { case 6: $daily_floor = 'mom'; break; case 8: $daily_floor = 'womb'; break; case 101: $daily_floor = 'cathedral'; break; case 111: $daily_floor = 'chest'; break; case 100: $daily_floor = 'sheol'; break; case 110: $daily_floor = 'darkroom'; break; case 257: $daily_floor = 'greed'; break; default: $daily_floor = 'question-mark'; break; } // mega satan flag if ($challenge['megasatan']) { $daily_floor = 'mega-satan'; } // hard mode flag if ($challenge['hard']) { $daily_floor .= '-hard'; } // challenge array $challenge = array('char' => $daily_char, 'floor' => $daily_floor); // send out to render $data = array('location' => $times ? 'times' : 'scores', 'date' => $leaderboards_today->date, 'challenge' => $challenge, 'prevdate' => $prevdate, 'nextdate' => $nextdate, 'scores' => $today, 'scores_yesterday' => $yesterday, 'kids' => $kids, 'friends' => $friends, 'global' => $global, 'page' => $page + 1); if (isset($_GET['json'])) { print json_encode($today); } else { echo $twig->render('index.php', array_merge($sdata, $data)); } }