-
Notifications
You must be signed in to change notification settings - Fork 11
/
calcratings.php
142 lines (130 loc) · 4.79 KB
/
calcratings.php
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
<?php
include 'lib.php';
$db = Database::getConnection();
$db->query("DELETE FROM ratings") or die($db->error);
$compQuery = "SELECT name, start FROM events ORDER BY start";
$futexQuery = "SELECT name, start FROM events WHERE format=\"Future Extended\" ORDER BY start";
$classicQuery = "SELECT name, start FROM events WHERE format=\"Classic\" ORDER BY start";
$stdQuery= "SELECT name, start FROM events WHERE format=\"Standard\" ORDER BY start";
$otherQuery= "SELECT name, start FROM events WHERE format!=\"Standard\" AND format!=\"Future Extended\" AND format!=\"Classic\" ORDER BY start";
$modernQuery = "SELECT name, start FROM events WHERE START >='2007-10-29' ORDER BY start";
$xpdcQuery= "SELECT name, start FROM events WHERE season=1 and series=\"XPDC\" ORDER BY start";
calcRating("Composite", $compQuery);
calcRating("Classic", $classicQuery);
calcRating("Future Extended", $futexQuery);
calcRating("Standard", $stdQuery);
calcRating("Other Formats", $otherQuery);
calcRating("Modern", $modernQuery);
calcRating("XPDC Season 1", $xpdcQuery);
function calcRating($format, $query) {
global $db;
$db->query($query) or die($db->error);
$result = mysql_query($query, $db) or die(mysql_error());
while($row = $result->fetch_assoc()) {
$event = $row['name'];
$players = calcPostEventRatings($event, $format);
insertRatings($players, $format, $row['start']);
}
mysql_free_result($result);
}
function insertRatings($players, $format, $date) {
global $db;
foreach($players as $player=>$data) {
$rating = $data['rating'];
$wins = $data['wins']; $losses = $data['losses'];
$stmt = $db->prepare("INSERT INTO ratings VALUES(?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sdssdd", $player, $rating, $format, $date, $wins, $losses);
$stmt->execute() or die($stmt->error);
$stmt->close();
}
}
function calcPostEventRatings($event, $format) {
global $db;
$players = getEntryRatings($event, $format);
$matches = getMatches($event);
foreach ($matches as $match) {
$aPts = 0.5; $bPts = 0.5;
if(strcmp($match['result'], 'A') == 0) {
$aPts = 1.0; $bPts = 0.0;
$players[$match['playera']]['wins']++;
$players[$match['playerb']]['losses']++;
}
elseif(strcmp($match['result'], 'B') == 0) {
$aPts = 0.0; $bPts = 1.0;
$players[$match['playerb']]['wins']++;
$players[$match['playera']]['losses']++;
}
$newA = newRating($players[$match['playera']]['rating'],
$players[$match['playerb']]['rating'],
$aPts, $match['kvalue']);
$newB = newRating($players[$match['playerb']]['rating'],
$players[$match['playera']]['rating'],
$bPts, $match['kvalue']);
$players[$match['playera']]['rating'] = $newA;
$players[$match['playerb']]['rating'] = $newB;
}
return $players;
}
function newRating($old, $opp, $pts, $k) {
$new = $old + ($k * ($pts - winProb($old, $opp)));
if($old < $new) {$new = ceil($new);}
elseif($old > $new) {$new = floor($new);}
return $new;
}
function winProb($rating, $oppRating) {
return 1/(pow(10, ($oppRating - $rating)/400) + 1);
}
function getMatches($event) {
global $db;
$stmt = $db->prepare("SELECT LCASE(m.playera) AS playera, LCASE(m.playerb) AS playerb, m.result, e.kvalue
FROM matches AS m, subevents AS s, events AS e
WHERE m.subevent=s.id AND s.parent=e.name AND e.name = ?
ORDER BY s.timing, m.round");
$stmt->bind_param("s", $event);
$stmt->execute();
$stmt->bind_result($playera, $playerb, $result, $kvalue);
$data = array();
while ($stmt->fetch()) {
$data[] = array('playera' => $playera,
'playerb' => $playerb,
'result' => $result,
'kvalue' => $kvalue);
}
$stmt->close();
return $data;
}
function getEntryRatings($event, $format) {
global $db;
$stmt = $db->prepare("SELECT LCASE(n.player) AS player, r.rating, q.qmax, r.wins, r.losses
FROM entries AS n
LEFT OUTER JOIN ratings AS r ON r.player = n.player
LEFT OUTER JOIN
(SELECT qr.player AS qplayer, MAX(qr.updated) AS qmax
FROM ratings AS qr, events AS qe
WHERE qr.updated<qe.start AND qe.name = ? AND qr.format = ?
GROUP BY qr.player) AS q
ON q.qplayer=r.player
WHERE n.event = ? AND ((q.qmax=r.updated AND q.qplayer=r.player AND r.format = ?)
OR q.qmax IS NULL)
GROUP BY n.player ORDER BY n.player");
$stmt->bind_param("ssss", $event, $format, $event, $format);
$stmt->execute();
$stmt->bind_result($player, $rating, $qmax, $wins, $losses);
$data = array();
while ($stmt->fetch()) {
$datum = array();
if(!is_null($qmax)) {
$datum['rating'] = $rating;
$datum['wins'] = $wins;
$datum['losses'] = $losses;
} else {
$datum['rating'] = 1600;
$datum['wins'] = 0;
$datum['losses'] = 0;
}
$data[$player] = $datum;
}
$stmt->close();
return $data;
}
?>