summaryrefslogtreecommitdiff
path: root/http/api-inc.php
blob: c75ed2e951afdda57c580f05594c7e5f39f263b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php

if (!function_exists('http_response_code')) {
    function http_response_code($code = NULL) {
        $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');
        header($protocol . ' ' . $code . ' ' . $codes[$code]);
    }
}

function safe_ceil($value, $precision = 1e-6) {
    $ceilValue = ceil($value);
    return (abs($value - $ceilValue) < (1-$precision)) ? $ceilValue : round($value);
}

function recursive_ksort(&$array, $flags = SORT_REGULAR) {
    if (!is_array($array)) return false;
    ksort($array, $flags);
    foreach ($array as &$arr) {
        recursive_ksort($arr, $flags);
    }
    return true;
}

class ParametersException extends Exception {};

function run($parameters) {
    $versionClasses = array(
        '1' => 'ApiPklV1',       // RegKlas 2018.11.01
        '2' => 'ApiPklV2',       // RegKMP  2020.01.01
        '3' => 'ApiPklV3',       // local BNET 2020.05.01
        '_default' => 'ApiPklV3'
    );
    $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) {
        $result = $api->calculate_kmp_points();
    } else if (isset($parameters['tournament_rank']) && $parameters['tournament_rank'] == ApiPkl::RANK_BNET) {
        $result = $api->calculate_bridgenet_points();
    } else {
        $result = $api->calculate_points();
    }
    return $result;
}

class ApiPkl {

    const RANK_KMP = 101;
    const RANK_BNET = 102;

    protected $parameters;

    function __construct($parameters) {
        $this->parameters = $parameters;
        $this->check_parameters($parameters);
        $this->parameters = $this->parse_parameters($parameters);
    }

    protected function is_integer($value) {
        return ctype_digit($value) || is_int($value);
    }

    function ensure_parameters($params) {
        foreach ($params as $param) {
            if (!isset($this->parameters[$param])) {
                throw new ParametersException('Missing parameter: ' . $param);
            }
            if (!is_numeric($this->parameters[$param])) {
                throw new ParametersException('Parameter: ' . $param . ' is not a numeric value (' . $this->parameters[$param] . ')');
            }
        }
    }

    function check_values($parameters, $params) {
        foreach ($params as $param => $test) {
            if (!$test($parameters[$param])) {
                throw new ParametersException('Parameter: ' . $param . ' has incorrect value (' . $parameters[$param] . ')');
            }
        }
    }

    function check_parameters() {
        $this->ensure_parameters(array('type', 'contestants', 'title_sum'));
        $this->check_values($this->parameters, array(
            'type' => function($r) { return ctype_digit($r) && intval($r) > 0; },
            'contestants' => function($r) { return ctype_digit($r) && intval($r) > 0; },
            'title_sum' => function($r) { return floatval($r) >= 0; }
        ));
        if (isset($this->parameters['players'])) {
            $this->check_values($this->parameters, array(
                'players' => function($r) { return ctype_digit($r) && intval($r) > 0; }
            ));
        }
        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) || 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 {
            $this->check_values($this->parameters['manual'], array(
                'min_points' => function($r) { return ctype_digit($r) && intval($r) >= 0; },
                'tournament_weight' => function($r) { return ctype_digit($r) && intval($r) > 0; }
            ));
        }
        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; }
            ));
        }
    }

    function parse_parameters() {
        $return = array();
        $return['type'] = intval($this->parameters['type']);
        if ($return['type'] == 3 || $return['type'] > 4) {
            throw new ParametersException('Parameter: type has incorrect value (' . $return['type'] . ')');
        }
        if ($return['type'] != 2 && 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 (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'] = floatval($this->parameters['title_sum']);
        $weights = array(
            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, 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;
        $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(
            array(0.0, 1.0),
            array(0.02, 0.9),
            array(0.2, 0.2),
            array(0.5, 0.0)
        );
        recursive_ksort($return['points_cutoffs']);
        if ($return['points_cutoffs'][0][0] != 0.0) {
            array_unshift($return['points_cutoffs'], array(0.0, 1.0));
        }
        if ($return['points_cutoffs'][count($return['points_cutoffs'])-1][1] != 0.0) {
            array_push($return['points_cutoffs'], array(1.0, 0.0));
        }
        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]);
            }
            $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]);
            }
            $cutoff[1] = floatval($cutoff[1]);
        }
        unset($cutoff);
        for ($prev = 0; $prev < count($return['points_cutoffs']) - 1; $prev++) {
            $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]);
            }
            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]);
            }
        }
        return $return;
    }

    function get_position_percentage_from_position($position, $contestants) {
        return ($position - 1) / $contestants;
    }

    function get_percentage_from_position($position, $contestants, $cutoffs) {
        $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)) {
                $result = ($position_percentage - $cutoffs[$prev][0]) * ($cutoffs[$prev][1] - $cutoffs[$next][1]) / ($cutoffs[$prev][0] - $cutoffs[$next][0]) + $cutoffs[$prev][1];
                return $result;
            }
        }
        return 0.0;
    }

    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'])
        ));
        $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);
            $points = max($min_points, intval($points));
            if ($points > 0) {
                $result['points'][$place] = $points;
                $result['sum'] += $this->parameters['type'] * $result['points'][$place];
            }
        }
        return $result;
    }

    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));
        for ($place = 2; $place <= $this->parameters['contestants']; $place++) {
            $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'] >= 2.5) {
            $result['points'][1] += 5;
            $result['points'][2] += 3;
            $result['points'][3] += 1;
            $result['sum'] += $this->parameters['type'] * 8;
        }
        return $result;
    }

    function calculate_bridgenet_points() {
        throw new ParametersException('BridgeNET points not supported in this API version');
    }
}

class ApiPklV1 extends ApiPkl {}

class ApiPklV2 extends ApiPklV1 {

    function calculate_kmp_points() {
        // the line below works only because you can't play > 39 boards in KMP
        // please kill me if it ever changes
        $this->parameters['tournament_weight'] = 4;
        $this->parameters['min_points'] = 0;
        $intermediate = $this->calculate_points();
        $this->parameters['min_points'] = $intermediate['points'][1] + 10;
        return $this->calculate_points();
    }

}

class ApiPklV3 extends ApiPklV2 {

    function check_parameters() {
        if (isset($this->parameters['tournament_rank']) && $this->parameters['tournament_rank'] == ApiPkl::RANK_BNET) {
            $this->ensure_parameters(array('boards', 'type'));
            if ($this->parameters['type'] == 4) {
                throw new ParametersException('Parameter: type has incorrect value (' . $this->parameters['type'] . ') for BridgeNET tournament');
            }
            if (isset($this->parameters['manual'])) {
                unset($this->parameters['manual']);
            }
        }
        if (isset($this->parameters['boards'])) {
            $this->check_values($this->parameters, array(
                'boards' => function($r) { return $this->is_integer($r); }
            ));
            $this->parameters['over39_boards'] = strval(intval($this->parameters['boards'] > 39));
        }
        return parent::check_parameters();
    }

    function calculate_bridgenet_points() {
        $factor = min($this->parameters['boards'] / 27.0, 1.0);
        return $this->calculate_points(0, $factor);
    }

}

?>