diff options
-rw-r--r-- | API.md | 4 | ||||
-rw-r--r-- | http/api.php | 40 |
2 files changed, 36 insertions, 8 deletions
@@ -11,7 +11,7 @@ Parametry (standardowe) | type | typ turnieju | `1` = indywiduel, `2` = pary, `4` = teamy | | contestants | liczba uczestników | liczba całkowita dodatnia | | title_sum | suma WK | liczba nieujemna | -| tournament_rank | ranga turnieju | `0` = klubowy, `1` = okręgowy, `2` = regionalny, `3` = OTX, `4` = OTX\*, `5` = OTX\*\*, `6` = OTX\*\*\*, `7` = OTX\*\*\*\* | +| tournament_rank | ranga turnieju | `0` = klubowy, `1` = okręgowy, `2` = regionalny, `3` = OTX, `4` = OTX\*, `5` = OTX\*\*, `6` = OTX\*\*\*, `7` = OTX\*\*\*\*, `101` = KMP | | over39_boards | liczba rozdań | `0` = mniejsza niż 40, `1` = co najmniej 40 | W standardowym trybie użycia API wszystkie powyższe parametry są obowiązkowe. @@ -42,6 +42,8 @@ W parametrze `manual[points_cutoffs]` poprzedniki każdej pary (odsetek najwyżs W przypadku podania zarówno parametru `manual[min_points]`, jak i `manual[tournament_weight]`, parametry `tournament_rank` i `over39_boards` przestają być wymagane. +Dla turniejów o randze KMP powyższe parametry, jak i parametr liczby rozdań, są ignorowane (ale API wciąż wymaga podania parametrów `over39_boards`). + Przykładowe zapytania do API ---------------------------- diff --git a/http/api.php b/http/api.php index 29c56ec..3ab7f3d 100644 --- a/http/api.php +++ b/http/api.php @@ -90,7 +90,7 @@ function check_parameters($parameters) { if (!isset($parameters['manual']) || !isset($parameters['manual']['min_points']) || !isset($parameters['manual']['tournament_weight'])) { ensure_parameters($parameters, array('tournament_rank', 'over39_boards')); check_values($parameters, array( - 'tournament_rank' => function($r) { return ctype_digit($r) && intval($r) >= 0 && intval($r) <= 7; }, + 'tournament_rank' => function($r) { return ctype_digit($r) && ((intval($r) >= 0 && intval($r) <= 7) || intval($r) == RANK_KMP); }, 'over39_boards' => function($r) { return ctype_digit($r) && intval($r) >= 0 && intval($r) <= 1; } )); } else { @@ -106,23 +106,28 @@ function check_parameters($parameters) { } } +const RANK_KMP = 101; + function parse_parameters($parameters) { $return = array(); $return['type'] = intval($parameters['type']); if ($return['type'] == 3 || $return['type'] > 4) { throw new ParametersException('Parameter: type has incorrect value (' . $return['type'] . ')'); } + if ($return['type'] != 2 && $parameters['tournament_rank'] == RANK_KMP) { + throw new ParametersException('Parameter: type has incorrect value (' . $return['type'] . ') for KMP tournament'); + } $return['contestants'] = intval($parameters['contestants']); $return['players'] = isset($parameters['players']) ? intval($parameters['players']) : intval($parameters['contestants']) * $return['type']; $return['title_sum'] = floatval($parameters['title_sum']); $weights = array( - array(1, 2, 4, 5, 7, 10, 15, 25), - array(2, 3, 5, 7, 10, 15, 25, 40) + array(1, 2, 4, 5, 7, 10, 15, 25, RANK_KMP => 0), + array(2, 3, 5, 7, 10, 15, 25, 40, RANK_KMP => 0) ); $return['tournament_weight'] = (isset($parameters['manual']) && isset($parameters['manual']['tournament_weight'])) ? intval($parameters['manual']['tournament_weight']) : $weights[intval($parameters['over39_boards'])][intval($parameters['tournament_rank'])]; $min_points = array( - array(0, 0, 0, 0, 50, 75, 150, 200), - array(0, 0, 0, 0, 70, 100, 200, 300) + array(0, 0, 0, 0, 50, 75, 150, 200, RANK_KMP => 0), + array(0, 0, 0, 0, 70, 100, 200, 300, RANK_KMP => 0) ); $return['min_points'] = (isset($parameters['manual']) && isset($parameters['manual']['min_points'])) ? intval($parameters['manual']['min_points']) : $min_points[intval($parameters['over39_boards'])][intval($parameters['tournament_rank'])]; $return['players_coefficient'] = (isset($parameters['manual']) && isset($parameters['manual']['players_coefficient'])) ? floatval($parameters['manual']['players_coefficient']) : 0.05; @@ -196,11 +201,32 @@ function calculate_points($parameters) { return $result; } +function calculate_kmp_points($parameters) { + $max_points = safe_ceil($parameters['contestants'] * 0.5); + $min_points = 1; + $result = array("sum" => 0, "points" => array(1 => $max_points)); + for ($place = 2; $place <= $parameters['contestants']; $place++) { + $result['points'][$place] = max($min_points, $result['points'][$place-1]-1); + $result['sum'] += $parameters['type'] * $result['points'][$place]; + } + if ($parameters['title_sum'] / $parameters['players'] >= 2.5) { + $result['points'][1] += 5; + $result['points'][2] += 3; + $result['points'][3] += 1; + $result['sum'] += $parameters['type'] * 8; + } + return $result; +} + function run($parameters) { try { check_parameters($parameters); - $parameters = parse_parameters($parameters); - $result = calculate_points($parameters); + $params = parse_parameters($parameters); + if ($parameters['tournament_rank'] == RANK_KMP) { + $result = calculate_kmp_points($params); + } else { + $result = calculate_points($params); + } return $result; } catch (ParametersException $e) { |