Last active
March 28, 2017 23:42
-
-
Save XVicarious/45f87b15bcd3be41829afe59b7d6f163 to your computer and use it in GitHub Desktop.
Get the top cards of the format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* @param int $formatId id of the format to fetch cards for | |
* @param string[] $types what types of cards you want | |
* @param int $limit how many of the top cards you want | |
* @return [] array of size $limit of cards | |
*/ | |
public static function getTopCards($formatId, $types = [], $limit = 10) | |
{ | |
try { | |
$dbh = self::getInstance(); | |
$query = self::GET_TOP_CARDS_FORMAT; | |
if (!empty($types)) { | |
$query .= ' AND ('; | |
$totalTypes = count($types); | |
for ($i = 0; $i < $totalTypes; $i++) { | |
$query .= "(type LIKE '$types[$i]')"; | |
if ($i + 1 !== $totalTypes) { | |
$query .= ' OR '; | |
} | |
} | |
$query .= ')'; | |
} | |
$stmt = $dbh->prepare($query); | |
$stmt->bindParam(':formatId', $formatId, PDO::PARAM_INT); | |
$stmt->execute(); | |
$cards = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
$newCards = []; | |
$newerCards = []; | |
$quantityCards = []; | |
foreach ($cards as $card) { | |
$index = self::cardExists($newCards, intval($card['id'])); | |
if ($index > -1) { | |
$newCards[$index]['numberOf'] += intval($card['numberOf']); | |
$quantityCards[$index] += intval($card['numberOf']); | |
} else { | |
array_push($newCards, ['id'=>intval($card['id']), | |
'numberOf'=>intval($card['numberOf']), | |
'cardName'=>$card['cardName']]); | |
array_push($quantityCards, intval($card['numberOf'])); | |
} | |
} | |
arsort($quantityCards); | |
foreach ($quantityCards as $key => $value) { | |
array_push($newerCards, $newCards[$key]); | |
if (sizeof($newerCards) == $limit) { | |
break; | |
} | |
} | |
return $newerCards; | |
} catch (PDOException $pdoe) { | |
error_log($pdoe->getMessage(), 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment