| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | class Rankings { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	public static function collectRaceRankings(Smr\Database $db, AbstractSmrPlayer $player) : array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 		$rankings = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 		$rank = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 		while ($db->nextRecord()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 			// increase rank counter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 			$rank++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 			$race_id = $db->getInt('race_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 			if ($player->getRaceID() == $race_id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 				$style = ' class="bold"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 				$style = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 			$rankings[$rank] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 				'style' => $style, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 				'race_id' => $db->getInt('race_id'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 				'amount' => $db->getInt('amount'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 				'amount_avg' => IRound($db->getInt('amount') / $db->getInt('num_players')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 				'num_players' => $db->getInt('num_players'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		return $rankings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	public static function collectAllianceRankings(Smr\Database $db, AbstractSmrPlayer $player, int $rank) : array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		$rankings = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		while ($db->nextRecord()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 			// increase rank counter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			$rank++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			$currentAlliance = SmrAlliance::getAlliance($db->getInt('alliance_id'), $player->getGameID()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			$class = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			if ($player->getAllianceID() == $currentAlliance->getAllianceID()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 				$class = ' class="bold"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			} elseif ($currentAlliance->hasDisbanded()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 				$class = ' class="red"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			$rankings[$rank] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 				'Rank' => $rank, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 				'Alliance' => $currentAlliance, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				'Class' => $class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 				'Value' => $db->getInt('amount') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		return $rankings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 	public static function collectRankings(Smr\Database $db, AbstractSmrPlayer $player, int $rank) : array { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 		$rankings = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 		while ($db->nextRecord()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 			// increase rank counter | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 			$rank++; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 			$currentPlayer = SmrPlayer::getPlayer($db->getInt('account_id'), $player->getGameID(), false, $db); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 			$class = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 			if ($player->equals($currentPlayer)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 				$class .= 'bold'; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 			if ($currentPlayer->hasNewbieStatus()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 				$class .= ' newbie'; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 			if ($class != '') { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 				$class = ' class="' . trim($class) . '"'; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 			$rankings[$rank] = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 				'Rank' => $rank, | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 				'Player' => $currentPlayer, | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 				'Class' => $class, | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 				'Value' => $db->getInt('amount') | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 			); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 		return $rankings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 * Get a subset of rankings from the player table sorted by $stat. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	public static function playerRanks(string $stat, int $minRank = 1, int $maxRank = 10) : array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$session = Smr\Session::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$db = Smr\Database::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$offset = $minRank - 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$limit = $maxRank - $offset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$db->query('SELECT *, ' . $stat . ' AS amount FROM player WHERE game_id = ' . $db->escapeNumber($session->getGameID()) . ' ORDER BY amount DESC, player_name LIMIT ' . $offset . ', ' . $limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		return self::collectRankings($db, $session->getPlayer(), $offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * Get a subset of rankings from the alliance table sorted by $stat. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	public static function allianceRanks(string $stat, int $minRank = 1, int $maxRank = 10) : array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$session = Smr\Session::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$db = Smr\Database::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		$offset = $minRank - 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$limit = $maxRank - $offset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$db->query('SELECT alliance_id, alliance_' . $stat . ' AS amount FROM alliance WHERE game_id = ' . $db->escapeNumber($session->getGameID()) . ' ORDER BY amount DESC, alliance_name LIMIT ' . $offset . ', ' . $limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		return self::collectAllianceRankings($db, $session->getPlayer(), $offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	public static function calculateMinMaxRanks(int $ourRank, int $totalRanks) : array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		$session = Smr\Session::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		$minRank = $session->getRequestVarInt('min_rank', $ourRank - 5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$maxRank = $session->getRequestVarInt('max_rank', $ourRank + 5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		if ($minRank <= 0 || $minRank > $totalRanks) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			$minRank = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		$maxRank = min($maxRank, $totalRanks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$template = Smr\Template::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		$template->assign('MinRank', $minRank); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		$template->assign('MaxRank', $maxRank); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$template->assign('TotalRanks', $totalRanks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		return [$minRank, $maxRank]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  |