summaryrefslogtreecommitdiff
path: root/http
diff options
context:
space:
mode:
Diffstat (limited to 'http')
-rw-r--r--http/api-inc.php31
1 files changed, 23 insertions, 8 deletions
diff --git a/http/api-inc.php b/http/api-inc.php
index 9cac20e..fc0d48e 100644
--- a/http/api-inc.php
+++ b/http/api-inc.php
@@ -40,6 +40,8 @@ function run($parameters) {
$api = new $apiClass($parameters);
if ($parameters['tournament_rank'] == ApiPkl::RANK_KMP) {
$result = $api->calculate_kmp_points();
+ } else if ($parameters['tournament_rank'] == ApiPkl::RANK_BNET) {
+ $result = $api->calculate_bridgenet_points();
} else {
$result = $api->calculate_points();
}
@@ -49,6 +51,7 @@ function run($parameters) {
class ApiPkl {
const RANK_KMP = 101;
+ const RANK_BNET = 102;
protected $parameters;
@@ -92,7 +95,7 @@ class ApiPkl {
if (!isset($this->parameters['manual']) || !isset($this->parameters['manual']['min_points']) || !isset($this->parameters['manual']['tournament_weight'])) {
$this->ensure_parameters(array('tournament_rank', 'over39_boards'));
$this->check_values($this->parameters, array(
- 'tournament_rank' => function($r) { return ctype_digit($r) && ((intval($r) >= 0 && intval($r) <= 7) || intval($r) == ApiPkl::RANK_KMP); },
+ 'tournament_rank' => function($r) { return ctype_digit($r) && ((intval($r) >= 0 && intval($r) <= 7) || in_array(intval($r), array(ApiPkl::RANK_KMP, ApiPkl::RANK_BNET))); },
'over39_boards' => function($r) { return ctype_digit($r) && intval($r) >= 0 && intval($r) <= 1; }
));
} else {
@@ -121,13 +124,13 @@ class ApiPkl {
$return['players'] = isset($this->parameters['players']) ? intval($this->parameters['players']) : intval($this->parameters['contestants']) * $return['type'];
$return['title_sum'] = floatval($this->parameters['title_sum']);
$weights = array(
- array(1, 2, 4, 5, 7, 10, 15, 25, ApiPkl::RANK_KMP => 0),
- array(2, 3, 5, 7, 10, 15, 25, 40, ApiPkl::RANK_KMP => 0)
+ array(1, 2, 4, 5, 7, 10, 15, 25, ApiPkl::RANK_KMP => 0, ApiPkl::RANK_BNET => 1),
+ array(2, 3, 5, 7, 10, 15, 25, 40, ApiPkl::RANK_KMP => 0, ApiPkl::RANK_BNET => 1) // not 2, according to MarcinW
);
$return['tournament_weight'] = (isset($this->parameters['manual']) && isset($this->parameters['manual']['tournament_weight'])) ? intval($this->parameters['manual']['tournament_weight']) : $weights[intval($this->parameters['over39_boards'])][intval($this->parameters['tournament_rank'])];
$min_points = array(
- array(0, 0, 0, 0, 50, 75, 150, 200, ApiPkl::RANK_KMP => 0),
- array(0, 0, 0, 0, 70, 100, 200, 300, ApiPkl::RANK_KMP => 0)
+ array(0, 0, 0, 0, 50, 75, 150, 200, ApiPkl::RANK_KMP => 0, ApiPkl::RANK_BNET => 0),
+ array(0, 0, 0, 0, 70, 100, 200, 300, ApiPkl::RANK_KMP => 0, ApiPkl::RANK_BNET => 0)
);
$return['min_points'] = (isset($this->parameters['manual']) && isset($this->parameters['manual']['min_points'])) ? intval($this->parameters['manual']['min_points']) : $min_points[intval($this->parameters['over39_boards'])][intval($this->parameters['tournament_rank'])];
$return['players_coefficient'] = (isset($this->parameters['manual']) && isset($this->parameters['manual']['players_coefficient'])) ? floatval($this->parameters['manual']['players_coefficient']) : 0.05;
@@ -185,16 +188,15 @@ class ApiPkl {
return 0.0;
}
- function calculate_points() {
+ function calculate_points($min_points=1, $scale_factor=1.0) {
$max_points = safe_ceil(max(
$this->parameters['min_points'],
(1 + 0.25 * ($this->parameters['type'] > 2)) * (max(0.15, $this->parameters['title_sum'] / $this->parameters['players']) * $this->parameters['tournament_weight'] + $this->parameters['players_coefficient'] * $this->parameters['contestants'] * $this->parameters['type'])
));
- $min_points = 1;
$result = array("sum" => 0, "points" => array());
for ($place = 1; $place <= $this->parameters['contestants']; $place++) {
$percentage = $this->get_percentage_from_position($place, $this->parameters['contestants'], $this->parameters['points_cutoffs']);
- $points = safe_ceil(floatval($max_points) * $percentage);
+ $points = safe_ceil(floatval($max_points) * $percentage * $scale_factor);
$result['points'][$place] = max($min_points, intval($points));
$result['sum'] += $this->parameters['type'] * $result['points'][$place];
}
@@ -217,6 +219,10 @@ class ApiPkl {
}
return $result;
}
+
+ function calculate_bridgenet_points() {
+ throw new ParametersException('BridgeNET points not supported in this API version');
+ }
}
class ApiPklV1 extends ApiPkl {}
@@ -239,6 +245,10 @@ class ApiPklV3 extends ApiPklV2 {
function check_parameters() {
if (!isset($this->parameters['manual']) || !isset($this->parameters['manual']['min_points']) || !isset($this->parameters['manual']['tournament_weight'])) {
+ $this->ensure_parameters(array('tournament_rank'));
+ if ($this->parameters['tournament_rank'] == ApiPkl::RANK_BNET) {
+ $this->ensure_parameters(array('boards'));
+ }
if (isset($this->parameters['boards'])) {
$this->check_values($this->parameters, array(
'boards' => function($r) { return ctype_digit($r); }
@@ -249,6 +259,11 @@ class ApiPklV3 extends ApiPklV2 {
return parent::check_parameters();
}
+ function calculate_bridgenet_points() {
+ $factor = min($this->parameters['boards'] / 27.0, 1.0);
+ return $this->calculate_points(0, $factor);
+ }
+
}
?>