| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  | <?php namespace Moserware\Skills; | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2010-09-25 10:15:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  |  * Base class for all skill calculator implementations. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | abstract class SkillCalculator | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     private $_supportedOptions; | 
					
						
							|  |  |  |     private $_playersPerTeamAllowed; | 
					
						
							|  |  |  |     private $_totalTeamsAllowed; | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-03 20:17:34 -04:00
										 |  |  |     protected function __construct($supportedOptions, TeamsRange $totalTeamsAllowed, PlayersRange $playerPerTeamAllowed) | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->_supportedOptions = $supportedOptions; | 
					
						
							|  |  |  |         $this->_totalTeamsAllowed = $totalTeamsAllowed; | 
					
						
							|  |  |  |         $this->_playersPerTeamAllowed = $playerPerTeamAllowed; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-08 21:44:36 -04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Calculates new ratings based on the prior ratings and team ranks. | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |      * @param GameInfo $gameInfo Parameters for the game. | 
					
						
							|  |  |  |      * @param array $teamsOfPlayerToRatings A mapping of team players and their ratings. | 
					
						
							|  |  |  |      * @param array $teamRanks The ranks of the teams where 1 is first place. For a tie, repeat the number (e.g. 1, 2, 2). | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2010-10-08 21:44:36 -04:00
										 |  |  |      * @return All the players and their new ratings. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-09-25 15:46:23 -04:00
										 |  |  |     public abstract function calculateNewRatings(GameInfo &$gameInfo, | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  |                                                  array $teamsOfPlayerToRatings, | 
					
						
							|  |  |  |                                                  array $teamRanks); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-08 21:44:36 -04:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Calculates the match quality as the likelihood of all teams drawing. | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param GameInfo $gameInfo Parameters for the game. | 
					
						
							|  |  |  |      * @param array $teamsOfPlayerToRatings A mapping of team players and their ratings. | 
					
						
							| 
									
										
										
										
											2010-10-08 21:44:36 -04:00
										 |  |  |      * @return The quality of the match between the teams as a percentage (0% = bad, 100% = well matched). | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-09-25 15:46:23 -04:00
										 |  |  |     public abstract function calculateMatchQuality(GameInfo &$gameInfo, | 
					
						
							|  |  |  |                                                    array &$teamsOfPlayerToRatings); | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function isSupported($option) | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return ($this->_supportedOptions & $option) == $option; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-25 15:46:23 -04:00
										 |  |  |     protected function validateTeamCountAndPlayersCountPerTeam(array &$teamsOfPlayerToRatings) | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         self::validateTeamCountAndPlayersCountPerTeamWithRanges($teamsOfPlayerToRatings, $this->_totalTeamsAllowed, $this->_playersPerTeamAllowed); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static function validateTeamCountAndPlayersCountPerTeamWithRanges( | 
					
						
							| 
									
										
										
										
											2010-09-25 15:46:23 -04:00
										 |  |  |         array &$teams, | 
					
						
							|  |  |  |         TeamsRange &$totalTeams, | 
					
						
							|  |  |  |         PlayersRange &$playersPerTeam) | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  |         $countOfTeams = 0; | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($teams as $currentTeam) { | 
					
						
							|  |  |  |             if (!$playersPerTeam->isInRange($currentTeam->count())) { | 
					
						
							|  |  |  |                 throw new Exception("Player count is not in range"); | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |             $countOfTeams++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |         if (!$totalTeams->isInRange($countOfTeams)) { | 
					
						
							| 
									
										
										
										
											2010-08-28 22:05:41 -04:00
										 |  |  |             throw new Exception("Team range is not in range"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SkillCalculatorSupportedOptions | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const NONE = 0x00; | 
					
						
							|  |  |  |     const PARTIAL_PLAY = 0x01; | 
					
						
							|  |  |  |     const PARTIAL_UPDATE = 0x02; | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  | } |