summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2025-02-13 03:00:45 +0100
committeremkael <emkael@tlen.pl>2025-02-13 03:00:45 +0100
commit479ca8d010303ce51439b3dd387c64c2fa6688a8 (patch)
treec25f60441553fa0d5fe706e9b55d45d786109f76
parent113c0b2976e845e401f0b115b87d8114fbb8b8a1 (diff)
Anxious, yet liberating steps towards code readability at 3 A.M.
-rw-r--r--http/api-inc.php233
1 files changed, 177 insertions, 56 deletions
diff --git a/http/api-inc.php b/http/api-inc.php
index 449874c..1f90c07 100644
--- a/http/api-inc.php
+++ b/http/api-inc.php
@@ -2,19 +2,25 @@
if (!function_exists('http_response_code')) {
function http_response_code($code = NULL) {
- $codes = array(400 => "Bad Request",
- 500 => "Internal Server Error");
+ $codes = array(
+ 400 => "Bad Request",
+ 500 => "Internal Server Error"
+ );
if (!isset($codes[$code])) {
return;
}
- $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
+ $protocol = isset($_SERVER['SERVER_PROTOCOL'])
+ ? $_SERVER['SERVER_PROTOCOL']
+ : 'HTTP/1.0';
header($protocol . ' ' . $code . ' ' . $codes[$code]);
}
}
function safe_ceil($value, $precision = 1e-6) {
$ceilValue = ceil($value);
- return (abs($value - $ceilValue) < (1-$precision)) ? $ceilValue : round($value);
+ return (abs($value - $ceilValue) < (1-$precision))
+ ? $ceilValue
+ : round($value);
}
function recursive_ksort(&$array, $flags = SORT_REGULAR) {
@@ -41,12 +47,18 @@ function run($parameters) {
'4.0' => 'ApiPklV4', // RegKlas 2025.01.01 (2025.02.12)
'_default' => 'ApiPklV4'
);
- $version = isset($parameters['version']) ? $parameters['version'] : '_default';
- $apiClass = isset($versionClasses[$version]) ? $versionClasses[$version] : $versionClasses['_default'];
+ $version = isset($parameters['version'])
+ ? $parameters['version']
+ : '_default';
+ $apiClass = isset($versionClasses[$version])
+ ? $versionClasses[$version]
+ : $versionClasses['_default'];
$api = new $apiClass($parameters);
- if (isset($parameters['tournament_rank']) && $parameters['tournament_rank'] == ApiPkl::RANK_KMP) {
+ if (isset($parameters['tournament_rank'])
+ && $parameters['tournament_rank'] == ApiPkl::RANK_KMP) {
$result = $api->calculate_kmp_points();
- } else if (isset($parameters['tournament_rank']) && $parameters['tournament_rank'] == ApiPkl::RANK_BNET) {
+ } else if (isset($parameters['tournament_rank'])
+ && $parameters['tournament_rank'] == ApiPkl::RANK_BNET) {
$result = $api->calculate_bridgenet_points();
} else {
$result = $api->calculate_points();
@@ -151,10 +163,15 @@ class ApiPkl {
function ensure_parameters($params) {
foreach ($params as $param) {
if (!isset($this->parameters[$param])) {
- throw new ParametersException('Missing parameter: ' . $param);
+ throw new ParametersException(
+ sprintf('Missing parameter: %s', $param)
+ );
}
if (!is_numeric($this->parameters[$param])) {
- throw new ParametersException('Parameter: ' . $param . ' is not a numeric value (' . $this->parameters[$param] . ')');
+ throw new ParametersException(
+ sprintf('Parameter: %s is not a numeric value (%s)',
+ $param, $this->parameters[$param])
+ );
}
}
}
@@ -162,7 +179,10 @@ class ApiPkl {
function check_values($parameters, $params) {
foreach ($params as $param => $test) {
if (!$test($parameters[$param])) {
- throw new ParametersException('Parameter: ' . $param . ' has incorrect value (' . $parameters[$param] . ')');
+ throw new ParametersException(
+ sprintf('Parameter: %s has incorrect value (%s)',
+ $param, $parameters[$param])
+ );
}
}
}
@@ -170,21 +190,33 @@ class ApiPkl {
function check_parameters() {
$this->ensure_parameters(array('type', 'contestants'));
$this->check_values($this->parameters, array(
- 'type' => function($r) { return is_integer_like($r) && intval($r) > 0; },
- 'contestants' => function($r) { return is_integer_like($r) && intval($r) > 0; }
+ 'type' => function($r) {
+ return is_integer_like($r) && intval($r) > 0;
+ },
+ 'contestants' => function($r) {
+ return is_integer_like($r) && intval($r) > 0;
+ }
));
if (isset($this->parameters['players'])) {
$this->check_values($this->parameters, array(
- 'players' => function($r) { return is_integer_like($r) && intval($r) > 0; }
+ 'players' => function($r) {
+ return is_integer_like($r) && intval($r) > 0;
+ }
));
}
- if (!isset($this->parameters['manual']) || !isset($this->parameters['manual']['min_points'])) {
+ if (!isset($this->parameters['manual'])
+ || !isset($this->parameters['manual']['min_points'])) {
$this->ensure_parameters(array('title_sum'));
$this->check_values($this->parameters, array(
- 'title_sum' => function($r) { return floatval($r) >= 0; }
+ 'title_sum' => function($r) {
+ return floatval($r) >= 0;
+ }
));
- if (!isset($this->parameters['manual']) || !isset($this->parameters['manual']['tournament_weight'])) {
- $this->ensure_parameters(array('tournament_rank', 'over39_boards'));
+ if (!isset($this->parameters['manual'])
+ || !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 is_integer_like($r) &&
@@ -201,21 +233,30 @@ class ApiPkl {
ApiPkl::RANK_BNET
));
},
- 'over39_boards' => function($r) { return is_integer_like($r) && intval($r) >= 0 && intval($r) <= 1; }
+ 'over39_boards' => function($r) {
+ return is_integer_like($r) && intval($r) >= 0 && intval($r) <= 1;
+ }
));
} else {
$this->check_values($this->parameters['manual'], array(
- 'tournament_weight' => function($r) { return is_integer_like($r) && intval($r) > 0; }
+ 'tournament_weight' => function($r) {
+ return is_integer_like($r) && intval($r) > 0;
+ }
));
}
} else {
$this->check_values($this->parameters['manual'], array(
- 'min_points' => function($r) { return is_integer_like($r) && intval($r) >= 0; }
+ 'min_points' => function($r) {
+ return is_integer_like($r) && intval($r) >= 0;
+ }
));
}
- if (isset($this->parameters['manual']) && isset($this->parameters['manual']['players_coefficient'])) {
+ if (isset($this->parameters['manual'])
+ && isset($this->parameters['manual']['players_coefficient'])) {
$this->check_values($this->parameters['manual'], array(
- 'players_coefficient' => function($r) { return floatval($r) >= 0; }
+ 'players_coefficient' => function($r) {
+ return floatval($r) >= 0;
+ }
));
}
}
@@ -226,17 +267,29 @@ class ApiPkl {
if (!in_array($return['type'], array(
ApiPkl::TYPE_INDIVIDUAL, ApiPkl::TYPE_PAIRS, ApiPkl::TYPE_TEAMS
))) {
- throw new ParametersException('Parameter: type has incorrect value (' . $return['type'] . ')');
+ throw new ParametersException(
+ sprintf('Parameter: type has incorrect value (%s)',
+ $return['type'])
+ );
}
- if ($return['type'] != ApiPkl::TYPE_PAIRS && isset($this->parameters['tournament_rank']) && $this->parameters['tournament_rank'] == ApiPkl::RANK_KMP) {
- throw new ParametersException('Parameter: type has incorrect value (' . $return['type'] . ') for KMP tournament');
+ if (($return['type'] != ApiPkl::TYPE_PAIRS)
+ && isset($this->parameters['tournament_rank'])
+ && $this->parameters['tournament_rank'] == ApiPkl::RANK_KMP) {
+ throw new ParametersException(
+ sprintf('Parameter: type has incorrect value (%s) for KMP tournament',
+ $return['type'])
+ );
}
if (isset($this->parameters['boards'])) {
$return['boards'] = intval($this->parameters['boards']);
}
$return['contestants'] = intval($this->parameters['contestants']);
- $return['players'] = isset($this->parameters['players']) ? intval($this->parameters['players']) : intval($this->parameters['contestants']) * $return['type'];
- $return['title_sum'] = isset($this->parameters['title_sum']) ? floatval($this->parameters['title_sum']) : 0.0;
+ $return['players'] = isset($this->parameters['players'])
+ ? intval($this->parameters['players'])
+ : intval($this->parameters['contestants']) * $return['type'];
+ $return['title_sum'] = isset($this->parameters['title_sum'])
+ ? floatval($this->parameters['title_sum'])
+ : 0.0;
$weights = array(
array(
ApiPkl::RANK_OTXxxxx => static::WEIGHT_OTXxxxx_UNDER40,
@@ -264,10 +317,12 @@ class ApiPkl {
)
);
$return['tournament_weight'] = 0;
- if (isset($this->parameters['manual']) && isset($this->parameters['manual']['tournament_weight'])) {
+ if (isset($this->parameters['manual'])
+ && isset($this->parameters['manual']['tournament_weight'])) {
$return['tournament_weight'] = intval($this->parameters['manual']['tournament_weight']);
} else {
- if (isset($this->parameters['over39_boards']) && isset($this->parameters['tournament_rank'])) {
+ if (isset($this->parameters['over39_boards'])
+ && isset($this->parameters['tournament_rank'])) {
$return['tournament_weight'] = $weights[intval($this->parameters['over39_boards'])][intval($this->parameters['tournament_rank'])];
}
}
@@ -297,14 +352,27 @@ class ApiPkl {
ApiPkl::RANK_BNET => static::POINTS_BNET_OVER40
)
);
- $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']) : static::PLAYERS_COEFFICIENT;
- $return['points_cutoffs'] = (isset($this->parameters['manual']) && isset($this->parameters['manual']['points_cutoffs']) && is_array($this->parameters['manual']['points_cutoffs'])) ? $this->parameters['manual']['points_cutoffs'] : array(
+ $return['min_points'] = $min_points[intval($this->parameters['over39_boards'])][intval($this->parameters['tournament_rank'])];
+ if (isset($this->parameters['manual'])
+ && isset($this->parameters['manual']['min_points'])) {
+ $return['min_points'] = intval($this->parameters['manual']['min_points']);
+ }
+ $return['players_coefficient'] = static::PLAYERS_COEFFICIENT;
+ if (isset($this->parameters['manual'])
+ && isset($this->parameters['manual']['players_coefficient'])) {
+ $return['players_coefficient'] = floatval($this->parameters['manual']['players_coefficient']);
+ }
+ $return['points_cutoffs'] = array(
array(0.0, 1.0),
array(static::POINTS_CUTOFF_1, static::POINTS_CUTOFF_1_VALUE),
array(static::POINTS_CUTOFF_2, static::POINTS_CUTOFF_2_VALUE),
array(static::POINTS_CUTOFF_3, static::POINTS_CUTOFF_3_VALUE)
);
+ if (isset($this->parameters['manual'])
+ && isset($this->parameters['manual']['points_cutoffs'])
+ && is_array($this->parameters['manual']['points_cutoffs'])) {
+ $return['points_cutoffs'] = $this->parameters['manual']['points_cutoffs'];
+ }
recursive_ksort($return['points_cutoffs']);
if ($return['points_cutoffs'][0][0] != 0.0) {
array_unshift($return['points_cutoffs'], array(0.0, 1.0));
@@ -314,11 +382,17 @@ class ApiPkl {
}
foreach ($return['points_cutoffs'] as &$cutoff) {
if (($cutoff[0] < 0.0) || ($cutoff[0] > 1.0)) {
- throw new ParametersException('Cutoff points need to be between 0.0 and 1.0: ' . $cutoff[0]);
+ throw new ParametersException(
+ sprintf('Cutoff points need to be between 0.0 and 1.0: %f',
+ $cutoff[0])
+ );
}
$cutoff[0] = floatval($cutoff[0]);
if (($cutoff[1] < 0.0) || ($cutoff[1] > 1.0)) {
- throw new ParametersException('Cutoff values need to be between 1.0 and 0.0: ' . $cutoff[1]);
+ throw new ParametersException(
+ sprintf('Cutoff values need to be between 1.0 and 0.0: %f',
+ $cutoff[1])
+ );
}
$cutoff[1] = floatval($cutoff[1]);
}
@@ -327,11 +401,17 @@ class ApiPkl {
$next = $prev + 1;
if ($return['points_cutoffs'][$prev][0] >= $return['points_cutoffs'][$next][0]) {
throw new ParametersException(
- 'Cutoff points need to be ascending: ' . $return['points_cutoffs'][$prev][0] . ', ' . $return['points_cutoffs'][$next][0]);
+ sprintf('Cutoff points need to be ascending: %f, %f',
+ $return['points_cutoffs'][$prev][0],
+ $return['points_cutoffs'][$next][0])
+ );
}
if ($return['points_cutoffs'][$prev][1] < $return['points_cutoffs'][$next][1]) {
throw new ParametersException(
- 'Cutoff values need to be non-ascending: ' . $return['points_cutoffs'][$prev][1] . ', ' . $return['points_cutoffs'][$next][1]);
+ sprintf('Cutoff values need to be non-ascending: %f, %f',
+ $return['points_cutoffs'][$prev][1],
+ $return['points_cutoffs'][$next][1])
+ );
}
}
return $return;
@@ -342,10 +422,13 @@ class ApiPkl {
}
function get_percentage_from_position($position, $contestants, $cutoffs) {
- $position_percentage = $this->get_position_percentage_from_position($position, $contestants);
+ $position_percentage = $this->get_position_percentage_from_position(
+ $position, $contestants
+ );
for ($prev = 0; $prev < count($cutoffs) - 1; $prev++) {
$next = $prev + 1;
- if (($cutoffs[$prev][0] <= $position_percentage) && ($cutoffs[$next][0] >= $position_percentage)) {
+ if (($cutoffs[$prev][0] <= $position_percentage)
+ && ($cutoffs[$next][0] >= $position_percentage)) {
$result = ($position_percentage - $cutoffs[$prev][0]) * ($cutoffs[$prev][1] - $cutoffs[$next][1]) / ($cutoffs[$prev][0] - $cutoffs[$next][0]) + $cutoffs[$prev][1];
return $result;
}
@@ -358,18 +441,33 @@ class ApiPkl {
}
protected function _get_max_points() {
- return safe_ceil(max(
- $this->parameters['min_points'],
- $this->_get_type_multiplier() * (max(static::MINIMUM_AVG_TITLE, $this->parameters['title_sum'] / $this->parameters['players']) * $this->parameters['tournament_weight'] + $this->parameters['players_coefficient'] * $this->parameters['contestants'] * $this->parameters['type'])
- ));
+ return safe_ceil(
+ max(
+ $this->parameters['min_points'],
+ $this->_get_type_multiplier() * (
+ max(
+ static::MINIMUM_AVG_TITLE,
+ $this->parameters['title_sum'] / $this->parameters['players']) * $this->parameters['tournament_weight'] + $this->parameters['players_coefficient'] * $this->parameters['contestants'] * $this->parameters['type']
+ )
+ )
+ );
}
function calculate_points($min_points=1, $scale_factor=1.0) {
$max_points = $this->_get_max_points();
- $result = array("sum" => 0, "points" => array());
+ $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 * $scale_factor);
+ $percentage = $this->get_percentage_from_position(
+ $place,
+ $this->parameters['contestants'],
+ $this->parameters['points_cutoffs']
+ );
+ $points = safe_ceil(
+ floatval($max_points) * $percentage * $scale_factor
+ );
$points = max($min_points, intval($points));
if ($points > 0) {
$result['points'][$place] = $points;
@@ -382,9 +480,15 @@ class ApiPkl {
function calculate_kmp_points() {
$max_points = safe_ceil($this->parameters['contestants'] * 0.5);
$min_points = 1;
- $result = array("sum" => 0, "points" => array(1 => $max_points));
+ $result = array(
+ "sum" => 0,
+ "points" => array(1 => $max_points)
+ );
for ($place = 2; $place <= $this->parameters['contestants']; $place++) {
- $result['points'][$place] = max($min_points, $result['points'][$place-1]-1);
+ $result['points'][$place] = max(
+ $min_points,
+ $result['points'][$place-1] - 1
+ );
$result['sum'] += $this->parameters['type'] * $result['points'][$place];
}
if ($this->parameters['title_sum'] / $this->parameters['players'] >= static::KMP_BONUS_MINIMUM_AVG_TITLE) {
@@ -400,15 +504,21 @@ class ApiPkl {
}
function calculate_bridgenet_points() {
- throw new ParametersException('BridgeNET points not supported in this API version');
+ throw new ParametersException(
+ 'BridgeNET points not supported in this API version'
+ );
}
protected function _set_board_count() {
if (isset($this->parameters['boards'])) {
$this->check_values($this->parameters, array(
- 'boards' => function($r) { return is_integer_like($r); }
+ 'boards' => function($r) {
+ return is_integer_like($r);
+ }
));
- $this->parameters['over39_boards'] = strval(intval($this->parameters['boards'] > 39));
+ $this->parameters['over39_boards'] = strval(
+ intval($this->parameters['boards'] > 39)
+ );
}
}
@@ -448,12 +558,16 @@ class ApiPklV3 extends ApiPklV2 {
protected function _bridgenet_parameters() {
$this->ensure_parameters(array('boards', 'type'));
if ($this->parameters['type'] == ApiPkl::TYPE_TEAMS) {
- throw new ParametersException('Parameter: type has incorrect value (' . $this->parameters['type'] . ') for BridgeNET tournament');
+ throw new ParametersException(
+ sprintf('Parameter: type has incorrect value (%s) for BridgeNET tournament',
+ $this->parameters['type'])
+ );
}
}
function check_parameters() {
- if (isset($this->parameters['tournament_rank']) && $this->parameters['tournament_rank'] == ApiPkl::RANK_BNET) {
+ if (isset($this->parameters['tournament_rank'])
+ && $this->parameters['tournament_rank'] == ApiPkl::RANK_BNET) {
$this->_bridgenet_parameters();
if (isset($this->parameters['manual'])) {
unset($this->parameters['manual']);
@@ -464,7 +578,10 @@ class ApiPklV3 extends ApiPklV2 {
}
function calculate_bridgenet_points() {
- $factor = min($this->parameters['boards'] / static::BNET_POINTS_FACTOR_QUOTIENT, static::BNET_POINTS_FACTOR_CAP);
+ $factor = min(
+ $this->parameters['boards'] / static::BNET_POINTS_FACTOR_QUOTIENT,
+ static::BNET_POINTS_FACTOR_CAP
+ );
return $this->calculate_points(0, $factor);
}
@@ -478,10 +595,14 @@ class ApiPklV3_1 extends ApiPklV3 {
protected function _bridgenet_parameters() {
$this->ensure_parameters(array('boards'));
$this->check_values($this->parameters, array(
- 'boards' => function($r) { return is_integer_like($r); }
+ 'boards' => function($r) {
+ return is_integer_like($r);
+ }
));
if (intval($this->parameters['boards']) < static::BNET_MINIMUM_BOARD_COUNT) {
- throw new ParametersException('At least 20 boards must be played in BridgeNET tournaments');
+ throw new ParametersException(
+ 'At least 20 boards must be played in BridgeNET tournaments'
+ );
}
}