forked from clarkerubber/engine-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine-evaluator.php
More file actions
163 lines (132 loc) · 5.12 KB
/
engine-evaluator.php
File metadata and controls
163 lines (132 loc) · 5.12 KB
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
<?php
include("keys.php"); //include keys to lichess API
include("config.php"); //Include settings
//include functions
include("functions/global.php");
include("functions/moveTimes.php");
include("functions/blur.php");
include("functions/computerAnalysis.php");
include("functions/ratingIncrease.php");
include("functions/knownEngineIP.php");
function cheatIndex ( $username, $forceDeep = FALSE, $token = NULL, $target = "http://en.lichess.org/api/" ) {
//Input: A players name
//Output: A players cheat index
global $SAMPLE_SIZE, $lichessApiToken;
global $POINTS_TOTAL, $DEEP_SEARCH_THRESHOLD, $DEEP_SAMPLE_SIZE, $DEEP_SELECTION_SIZE, $DEEP_MOVE_THRESHOLD;
global $REPORT_THRESHOLD, $MARK_THRESHOLD;
if( $token == NULL ){
$token = $lichessApiToken;
}
$gameReq = $target."game?username=$username&rated=1&nb=$SAMPLE_SIZE&token=$token"; //api request for player game data
$playerReq = $target."user/$username?token=$lichessApiToken"; //api request for player information
if ( ( $gameJson = file_get_contents( $gameReq ) ) != FALSE
&& ( $playerJson = file_get_contents( $playerReq ) ) != FALSE ){
$games = json_decode( $gameJson, TRUE )['list']; //decode game data
$player = json_decode( $playerJson, TRUE ); //decode player data
$deepIndex = null;
if ( !empty( $games ) && !empty( $player ) ){
$action = "NOTHING";
$points = array();
$reportGames = array();
//-----Game Functions-------
$points['SD'] = SDpoints( $games, $username );
$points['BL'] = BLpoints( $games, $username );
$points['CA'] = CApoints( $games, $username );
//-----Player Functions-----
$points['RI'] = RIpoints( $player['progress'] );
$points['IP'] = IPpoints( $player['knownEnginesSharingIp'] );
arsort( $points );
$availableAlloc = 100;
$cheatIndex = 0;
foreach ($points as $key => $value) {
if( $availableAlloc - $POINTS_TOTAL[$key] >= 0 ) {
$availableAlloc -= $POINTS_TOTAL[$key];
$cheatIndex += $value;
}
}
if ( $cheatIndex >= $DEEP_SEARCH_THRESHOLD || $forceDeep == TRUE ) {
$gameReq = $target."game?username=$username&rated=1&nb=$DEEP_SAMPLE_SIZE&token=$token";
$games = json_decode( file_get_contents( $gameReq ), TRUE )['list'];
$gameIndexes = array();
//Process each of the games individually with individual indexes.
foreach ( $games as $key => $unused ) {
//this is a bit of a hack so I don't have to make a new set of functions.
$game = array();
$game[] = $games[$key];
$deepPoints['SD'] = SDpoints( $game, $username, $DEEP_MOVE_THRESHOLD );
$deepPoints['BL'] = BLpoints( $game, $username, $DEEP_MOVE_THRESHOLD );
$deepPoints['CA'] = CApoints( $game, $username, $DEEP_MOVE_THRESHOLD );
arsort( $deepPoints );
$availableAlloc = 100;
$gameIndex = 0;
foreach ($deepPoints as $deepPointsKey => $value) {
if( $availableAlloc - $POINTS_TOTAL[$deepPointsKey] >= 0 ) {
$availableAlloc -= $POINTS_TOTAL[$deepPointsKey];
$gameIndex += $value;
}
}
$summaries[] = array( "url" => str_replace( "lichess.org/", "lichess.org/", $games[$key]['url'] ),
"moveTime" => floor( 2 * $deepPoints['SD'] ),
"blur" => floor( 2 * $deepPoints['BL'] ),
"error" => floor( 2 * $deepPoints['CA'] )
);
$gameIndexes[] = $gameIndex;
}
//Sort games by indexes
array_multisort( $gameIndexes, SORT_DESC, SORT_NUMERIC, $summaries );
$returnedSampleSize = count( $games );
$y = 0;
//Calculate mean
$sum = 0;
for($x = 0; $x < $DEEP_SELECTION_SIZE && $x < $returnedSampleSize; $x++ ){
if($gameIndexes[$x] > 0){ //i.e. there is enough moves played
$reportGames[] = $summaries[$x];
$sum += $gameIndexes[$x];
$y++;
}
}
$deepIndex = $sum / $y;
if ( $deepIndex >= $REPORT_THRESHOLD ) {
$action = "REPORT";
}
if ( $deepIndex >= $MARK_THRESHOLD ) {
$action = "MARK";
}
}
//-----Report Outputs-------
$outputArray = array(
"userId" => $username,
"cheatIndex" => floor($cheatIndex),
"deepIndex" => $deepIndex ? floor($deepIndex) : null,
"action" => $action,
"games" => $reportGames,
"moveTime" => floor( 100 * $points['SD'] / $POINTS_TOTAL['SD'] ),
"blur" => floor( 100 * $points['BL'] / $POINTS_TOTAL['BL'] ),
"computerAnalysis" => floor( 100 * $points['CA'] / $POINTS_TOTAL['CA'] ),
"progress" => floor( 100 * $points['RI'] / $POINTS_TOTAL['RI'] ),
"knownEngineIP" => floor( 100 * $points['IP'] / $POINTS_TOTAL['IP'] )
);
$output = json_encode($outputArray);
}else{
exit(2);
}
} else {
exit(1);
}
return $output;
}
$output = "";
if( isset( $argv[4] ) ) {
$output = cheatIndex( strtolower( $argv[1] ), $argv[2], $argv[3], $argv[4] );
} else if ( isset( $argv[3] ) ) {
$output = cheatIndex( strtolower( $argv[1] ), $argv[2], $argv[3] );
} else if ( isset( $argv[2] ) ) {
$output = cheatIndex( strtolower( $argv[1] ), $argv[2] );
} else if ( isset( $argv[1] ) ) {
$output = cheatIndex( strtolower( $argv[1] ) );
} else {
error_log("Missing username parameter");
exit(3);
}
echo $output;
exit(0);