-
Notifications
You must be signed in to change notification settings - Fork 0
/
trueskill.php
102 lines (89 loc) · 2.48 KB
/
trueskill.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
<?php
define('RATING_OFFSET', 200.0);
function pipeExec($command, $stdin)
{
$fdSpec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$dir = getcwd();
chdir(dirname(__FILE__));
$process = proc_open($command, $fdSpec, $pipes);
chdir($dir);
if ($process === false) {
return null;
}
while (strlen($stdin) > 0) {
$size = fwrite($pipes[0], $stdin);
if ($size === false) {
return null;
}
$stdin = substr($stdin, $size);
}
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return $stdout;
}
function tsDefaultScore()
{
return array("mu"=>1000.0, "sigma"=>(1000.0/3.0), "rating"=>0.0 + RATING_OFFSET);
}
function tsMatchMatrix($players)
{
$playerList = array();
foreach ($players as $player) {
$playerList[] = array("id"=>(int)$player["userID"], "mu"=>(float)$player["mu"], "sigma"=>(float)$player["sigma"]);
}
$input = array("players"=>$playerList);
$jsonInput = json_encode($input);
$jsonOutput = pipeExec("python trueskill/matchmatrix.py", $jsonInput);
$output = json_decode($jsonOutput);
$matrix = array();
foreach ($players as $userID => $player) {
$matrix[$userID] = array();
}
foreach ($output as $match) {
$matrix[$match->id1][$match->id2] = $match->quality;
$matrix[$match->id2][$match->id1] = $match->quality;
}
return $matrix;
}
function tsProcessMatchResults($players, $results)
{
$playerList = array();
foreach ($players as $player) {
$playerList[] = array("id"=>(int)$player["userID"], "mu"=>(float)$player["mu"], "sigma"=>(float)$player["sigma"]);
}
$matchList = array();
foreach ($results as $result) {
$match = array();
$match["teams"] = array();
$match["rankings"] = $result["rankings"];
foreach ($result["teams"] as $team) {
$newTeam = array();
foreach($team as $userID) {
$newTeam[] = (int)$userID;
}
$match["teams"][] = $newTeam;
}
$matchList[] = $match;
}
$input = array("players"=>$playerList, "matches"=>$matchList);
$jsonInput = json_encode($input);
$jsonOutput = pipeExec("python trueskill/processresults.py", $jsonInput);
$output = json_decode($jsonOutput);
$newScores = array();
$rank = 1;
foreach($output as $score) {
$newScores[$score->id] = array();
$newScores[$score->id]["mu"] = $score->mu;
$newScores[$score->id]["sigma"] = $score->sigma;
$newScores[$score->id]["rating"] = $score->rating + RATING_OFFSET;
$newScores[$score->id]["rank"] = $rank++;
}
return $newScores;
}