chess_ratings_adj_cxr()   F
last analyzed

Complexity

Conditions 24
Paths 1728

Size

Total Lines 82
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 24
eloc 39
nc 1728
nop 5
dl 0
loc 82
rs 0
c 2
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// ------------------------------------------------------------------------- //
4
//  This program is free software; you can redistribute it and/or modify     //
5
//  it under the terms of the GNU General Public License as published by     //
6
//  the Free Software Foundation; either version 2 of the License, or        //
7
//  (at your option) any later version.                                      //
8
//                                                                           //
9
//  You may not change or alter any portion of this comment or credits       //
10
//  of supporting developers from this source code or any supporting         //
11
//  source code which is considered copyrighted (c) material of the          //
12
//  original comment or credit authors.                                      //
13
//                                                                           //
14
//  This program is distributed in the hope that it will be useful,          //
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
//  GNU General Public License for more details.                             //
18
//                                                                           //
19
//  You should have received a copy of the GNU General Public License        //
20
//  along with this program; if not, write to the Free Software              //
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
//  ------------------------------------------------------------------------ //
23
//  The CXR Rating System algorithm is used by permission of Chess Express   //
24
//  Ratings, Inc. <http://chess-express.com>.                               //
25
//  ------------------------------------------------------------------------ //
26
27
/**
28
 * Ratings functions specific to CXR rating system.
29
 *
30
 * @param mixed $white_rating
31
 * @param mixed $white_games
32
 * @param mixed $black_rating
33
 * @param mixed $black_games
34
 * @param mixed $pgn_result
35
 * @subpackage ratings
36
 * @package    chess
37
 */
38
39
/**
40
 * Update the players' ratings for an individual game, using the CXR rating system.
41
 *
42
 * @param int    $white_rating White's current rating
43
 * @param int    $white_games  Number of rated games that white has played
44
 * @param int    $black_rating Black's current rating
45
 * @param int    $black_games  Number of rated games that black has played
46
 * @param string $pgn_result   Game result: '1-0' (white won), '0-1' (black won) or '1/2-1/2' (draw)
47
 * @return array  Array with two elements:
48
 *                             - $white_rating_new - white's new rating
49
 *                             - $black_rating_new - black's new rating
50
 */
51
function chess_ratings_adj_cxr($white_rating, $white_games, $black_rating, $black_games, $pgn_result)
52
{
53
    // compute score: +1 for win, 0 for draw, -1 for loss
54
55
    switch ($pgn_result) {
56
        case '1-0':
57
            $S = 1;
58
            break;
59
        case '1/2-1/2':
60
        default: // should not occur
61
            $S = 0;
62
            break;
63
        case '0-1':
64
            $S = -1;
65
            break;
66
    }
67
68
    if (($white_games < 5 && $black_games < 5) || ($white_games > 5 && $black_games > 5)) {
69
        // Formula 1: Rnew = Rold + (S x 21) + (Ropponent - Rold) / 25
70
71
        $w_new = ($S * 21) + ($black_rating - $white_rating) / 25;
72
73
        $b_new = (-$S * 21) + ($white_rating - $black_rating) / 25;
74
    } elseif ($white_games > 5 && $black_games < 5) {
75
        // Formula 2: Rnew = Rold + (S x 6) + (Ropponent - Rold) / 100
76
77
        $w_new = ($S * 6) + ($black_rating - $white_rating) / 100;
78
79
        // Formula 3: Rnew = (4/5) x Rold + (1/5) x Ropponent + (S x 80)
80
81
        $b_new = ($white_rating / 5) + ($S * -80) - ($black_rating / 5);
82
    } else {
83
        // Formula 2: Rnew = Rold + (S x 6) + (Ropponent - Rold) / 100
84
85
        $b_new = ($S * 6) + ($white_rating - $black_rating) / 100;
86
87
        // Formula 3: Rnew = (4/5) x Rold + (1/5) x Ropponent + (S x 80)
88
89
        $w_new = ($black_rating / 5) + ($S * -80) - ($white_rating / 5);
90
    }
91
92
    // Rule R1: The winning rated player must gain at least two points.
93
94
    // Rule R2: The losing rated player must lose at least two points.
95
96
    if (abs($w_new) < 2) {
97
        $w_new = $S * 2;
98
    }
99
100
    if (abs($b_new) < 2) {
101
        $b_new = $S * -2;
102
    }
103
104
    // Rule R3: The rated player must not gain nor lose more than 41 points.
105
106
    if (abs($w_new) > 41) {
107
        $w_new = $S * 41;
108
    }
109
110
    if (abs($b_new) > 41) {
111
        $b_new = $S * -41;
112
    }
113
114
    if (1 == $S) {
115
        if ($white_games < 5 && $w_new < 0) {
116
            $w_new = 2;
117
        }
118
119
        if ($black_games < 5 && $b_new > 0) {
120
            $b_new = -2;
121
        }
122
    } elseif (-1 == $S) {
123
        if ($white_games < 5 && $w_new > 0) {
124
            $w_new = -2;
125
        }
126
127
        if ($black_games < 5 && $b_new < 0) {
128
            $b_new = 2;
129
        }
130
    }
131
132
    return [$white_rating + $w_new, $black_rating + $b_new];
133
}
134
135
/**
136
 * Get the number of provisional games used in the CXR rating system.
137
 *
138
 * @return int  Number of provisional games
139
 */
140
function chess_ratings_num_provisional_games_cxr()
141
{
142
    return 5;
143
}
144