-
Notifications
You must be signed in to change notification settings - Fork 0
/
milestone-generator.php
297 lines (252 loc) · 10.8 KB
/
milestone-generator.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace plough\stats;
use plough\log;
require_once(__DIR__ . "/../logger.php");
require_once("db.php");
// Constants
// Milestone states
const MS_STATE_ACHIEVED = "Achieved";
const MS_STATE_NEXT = "Next";
// Milestone types
const MS_TYPE_GENERAL = "General";
const MS_TYPE_BATTING = "Batting";
const MS_TYPE_BOWLING = "Bowling";
const MS_TYPE_FIELDING = "Fielding";
const MS_TYPE_KEEPING = "Keeping";
// Milestone gaps to next - only record "next" milestone if within this gap
const MS_GAP_MATCHES = 3;
const MS_GAP_RUNS = 50;
const MS_GAP_WICKETS = 5;
const MS_GAP_CATCHES = 3;
const MS_GAP_KEEPING_CATCHES = 3;
// Helpers
class MilestoneResult
{
public $achieved = array();
public $next;
}
function calculate_milestones($start_value, $current_value, $milestone_values)
{
$result = new MilestoneResult();
foreach ($milestone_values as $idx => $milestone)
{
// Find first potential milestone after the start value
if ($milestone > $start_value)
{
if ($milestone <= $current_value)
{
// Milestone achieved this season
array_push($result->achieved, $milestone);
}
else
{
// Found next milestone - stop here
$result->next = $milestone;
break;
}
}
}
return $result;
}
function get_milestone_description($value, $name, $gap = null)
{
$result = $value . " " . strtolower($name);
if ($gap)
$result = $result . "\n($gap needed)";
return $result;
}
class MilestoneGenerator
{
// Properties
private $_config;
private $_db;
// Milestone values
// General milestones
private $_msValuesMatches;
// Batting
private $_msValuesRuns;
// Bowling
private $_msValuesWickets;
// Fielding
private $_msValuesCatches;
// Keeping
private $_msValuesKeepingCatches;
// Public methods
public function __construct($config, \SQLite3 $db)
{
$this->_config = $config;
$this->_db = $db;
// Build milestone value lists
$this->_msValuesMatches = range(50, 10000, 50);
$this->_msValuesRuns = range(1000, 50000, 1000);
$this->_msValuesWickets = range(50, 5000, 50);
$this->_msValuesCatches = range(25, 500, 25);
$this->_msValuesKeepingCatches = range(25, 500, 25);
}
public function clear_milestones()
{
$db = $this->_db;
db_truncate_table($db, "Milestone");
}
public function generate_milestones($season)
{
$db = $this->_db;
$inserter = db_create_insert_milestone($db);
$players = get_players_by_name($db);
// Calculate relevant milestones
foreach ($players as $player_name => $player)
{
// Players without a Play-Cricket ID haven't played since 2017 (at the latest)
// and definitely don't have any milestone data. We skip them here for efficiency.
if ($player["PcPlayerId"] != NO_PC_PLAYER_ID)
{
// Fetch start / season data for fields of interest
// Assumes that this function is called immediately after the season
// summary is calculated for $season but before it is added to the
// career summary.
$start_data = $this->get_milestone_data($player, PERIOD_CAREER, $season - 1);
$season_data = $this->get_milestone_data($player, PERIOD_SEASON, $season);
// General
$this->calculate_and_store(
$inserter, $player, $season, $start_data, $season_data,
MS_TYPE_GENERAL, "Matches", $this->_msValuesMatches, MS_GAP_MATCHES
);
// Batting
$this->calculate_and_store(
$inserter, $player, $season, $start_data, $season_data,
MS_TYPE_BATTING, "Runs", $this->_msValuesRuns, MS_GAP_RUNS
);
// Bowling
$this->calculate_and_store(
$inserter, $player, $season, $start_data, $season_data,
MS_TYPE_BOWLING, "Wickets", $this->_msValuesWickets, MS_GAP_WICKETS
);
// Fielding
$this->calculate_and_store(
$inserter, $player, $season, $start_data, $season_data,
MS_TYPE_FIELDING, "Catches", $this->_msValuesCatches, MS_GAP_CATCHES
);
// Keeping
$this->calculate_and_store(
$inserter, $player, $season, $start_data, $season_data,
MS_TYPE_KEEPING, "KeepingCatches", $this->_msValuesKeepingCatches, MS_GAP_KEEPING_CATCHES, "keeping catches"
);
}
}
}
// Assumes the player name is in the first column of each row
public function join_milestones_to_player_rows($season, $rows, $milestone_types)
{
$db = $this->_db;
// Build map from player name to milestone text
$milestone_types_str = "'" . implode("', '", $milestone_types) . "'";
$statement = $db->prepare(
'SELECT
p.Name
,m.State
,m.Description
FROM Player p
INNER JOIN Milestone m on m.PlayerId = p.PlayerId
WHERE
m.Type in (' . $milestone_types_str . ')
and m.Season = :Season
ORDER BY p.Name, m.State, m.Description
');
$statement->bindValue(":Season", $season);
$query_result = $statement->execute();
$name_to_milestone_text = array();
while ($row = $query_result->fetchArray(SQLITE3_ASSOC))
{
$name = $row["Name"];
$state = $row["State"];
if ($state == MS_STATE_ACHIEVED)
$css_class = "achieved-milestone";
else
$css_class = "next-milestone";
$text_for_this_milestone =
"<span class='$css_class'>" . $row["Description"] . "</span>";
if (!array_key_exists($name, $name_to_milestone_text))
$name_to_milestone_text[$name] = "";
$current_text = $name_to_milestone_text[$name];
if (strlen($current_text) > 0)
$current_text = $current_text . PHP_EOL;
$current_text = $current_text . $text_for_this_milestone;
$name_to_milestone_text[$name] = $current_text;
}
$rows_with_milestones = array();
foreach ($rows as $row)
{
$row_with_milestones = $row;
$name = $row_with_milestones[0];
$milestone_text = "";
if (array_key_exists($name, $name_to_milestone_text))
$milestone_text = $name_to_milestone_text[$name];
array_push($row_with_milestones, $milestone_text);
array_push($rows_with_milestones, $row_with_milestones);
}
return $rows_with_milestones;
}
private function get_milestone_data($player, $period_type, $season)
{
$db = $this->_db;
if ($period_type == PERIOD_CAREER)
$table_prefix = "Career";
else if ($period_type == PERIOD_SEASON)
$table_prefix = "Season";
$statement = $db->prepare(
'SELECT
m.Matches as Matches
,ba.Runs as Runs
,bo.Wickets as Wickets
,f.CatchesFielding as Catches
,f.CatchesKeeping as KeepingCatches
FROM Player p
LEFT JOIN ' . $table_prefix . 'MatchesSummary m ON m.PlayerId = p.PlayerId
LEFT JOIN ' . $table_prefix . 'BattingSummary ba ON ba.PlayerId = p.PlayerId
LEFT JOIN ' . $table_prefix . 'BowlingSummary bo ON bo.PlayerId = p.PlayerId
LEFT JOIN ' . $table_prefix . 'FieldingSummary f ON f.PlayerId = p.PlayerId
WHERE
p.PlayerId = :PlayerId
AND m.Season = :Season AND m.MatchType = \'Regular\'
AND ba.Season = :Season AND ba.MatchType = \'Regular\'
AND bo.Season = :Season AND bo.MatchType = \'Regular\'
AND f.Season = :Season AND f.MatchType = \'Regular\'
ORDER BY p.PlayerId
');
$statement->bindValue(":PlayerId", $player["PlayerId"]);
$statement->bindValue(":Season", $season);
return $statement->execute()->fetchArray(SQLITE3_ASSOC);
}
private function calculate_and_store(
$inserter, $player, $season, $start_data, $season_data, $type, $name, $value_list, $max_gap_to_next, $name_for_desc = null
)
{
// Calculate
$start_value = $start_data[$name] ?? 0;
$season_value = $season_data[$name] ?? 0;
$current_value = $start_value + $season_value;
$result = calculate_milestones($start_value, $current_value, $value_list);
// Store
$inserter->bindValue(":PlayerId", $player["PlayerId"]);
$inserter->bindValue(":Season", $season);
$inserter->bindValue(":Type", $type);
$name_to_use = (is_null($name_for_desc) ? $name : $name_for_desc);
foreach ($result->achieved as $milestone)
{
$inserter->bindValue(":State", MS_STATE_ACHIEVED);
$inserter->bindValue(":Description", get_milestone_description($milestone, $name_to_use));
$inserter->execute();
}
if ($season == $this->_config->getCurrentSeason() && $player["Active"])
{
$gap = $result->next - $current_value;
if ($gap <= $max_gap_to_next)
{
$inserter->bindValue(":State", MS_STATE_NEXT);
$inserter->bindValue(":Description", get_milestone_description($result->next, $name_to_use, $gap));
$inserter->execute();
}
}
}
}
?>