1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains a class to quickly generate database queries for matches |
4
|
|
|
* |
5
|
|
|
* @package BZiON\Models\QueryBuilder |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class can be used to search for matches with specific characteristics in |
11
|
|
|
* the database. |
12
|
|
|
* |
13
|
|
|
* @package BZiON\Models\QueryBuilder |
14
|
|
|
*/ |
15
|
|
|
class MatchQueryBuilder extends QueryBuilder |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Only include matches where a specific team/player played |
19
|
|
|
* |
20
|
|
|
* @param Team|Player $participant The team/player which played the matches |
21
|
|
|
* @param string $result The outcome of the matches (win, draw or loss) |
22
|
|
|
* @return self |
23
|
|
|
*/ |
24
|
2 |
|
public function with($participant, $result = null) |
25
|
|
|
{ |
26
|
2 |
|
if (!$participant || !$participant->isValid()) { |
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
|
if ($participant instanceof Team) { |
31
|
2 |
|
$team_a_query = "team_a = ?"; |
32
|
2 |
|
$team_b_query = "team_b = ?"; |
33
|
|
|
} elseif ($participant instanceof Player) { |
34
|
|
|
$this->extras = 'INNER JOIN match_participation mp ON mp.match_id = matches.id'; |
35
|
|
|
$team_a_query = '? = mp.user_id'; |
36
|
|
|
$team_b_query = '? = mp.user_id'; |
37
|
|
|
} else { |
38
|
|
|
throw new InvalidArgumentException("Invalid model provided"); |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
$team_query_or = ($team_b_query === null) ? $team_a_query : "$team_a_query OR $team_b_query"; |
42
|
2 |
|
|
43
|
2 |
|
switch ($result) { |
44
|
2 |
|
case "wins": |
45
|
1 |
|
case "win": |
46
|
1 |
|
case "victory": |
47
|
2 |
|
case "victories": |
48
|
2 |
|
$query = "($team_a_query AND team_a_points > team_b_points) OR ($team_b_query AND team_b_points > team_a_points)"; |
49
|
2 |
|
break; |
50
|
2 |
|
case "loss": |
51
|
2 |
|
case "lose": |
52
|
|
|
case "losses": |
53
|
|
|
case "defeat": |
54
|
2 |
|
case "defeats": |
55
|
2 |
|
$query = "($team_a_query AND team_b_points > team_a_points) OR ($team_b_query AND team_a_points > team_b_points)"; |
56
|
2 |
|
break; |
57
|
2 |
|
case "draw": |
58
|
|
|
case "draws": |
59
|
|
|
case "tie": |
60
|
|
|
case "ties": |
61
|
2 |
|
$query = "($team_query_or) AND team_a_points = team_b_points"; |
62
|
|
|
break; |
63
|
|
|
default: |
64
|
2 |
|
$query = "$team_query_or"; |
65
|
2 |
|
} |
66
|
2 |
|
|
67
|
|
|
$this->whereConditions[] = $query; |
68
|
2 |
|
$this->parameters[] = $participant->getId(); |
69
|
|
|
$this->parameters[] = $participant->getId(); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Group results by day |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function groupByMonth() |
80
|
|
|
{ |
81
|
|
|
$this->groupQuery .= "GROUP BY YEAR(timestamp), MONTH(timestamp)"; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get a count for each month's matches |
88
|
|
|
* |
89
|
1 |
|
* @param TimeDate $timeDate The team in question |
90
|
|
|
* |
91
|
1 |
|
* @return array |
92
|
|
|
*/ |
93
|
1 |
|
public function getSummary(TimeDate $timeDate) |
94
|
|
|
{ |
95
|
1 |
|
$this->groupQuery = 'GROUP BY match_date'; |
96
|
1 |
|
|
97
|
|
|
$query = $this->createQuery("DATE_FORMAT(timestamp, '%Y-%m') AS match_date, COUNT(*) as match_count"); |
98
|
1 |
|
|
99
|
1 |
|
$matches = []; |
100
|
|
|
$results = Database::getInstance()->query($query, $this->parameters); |
101
|
|
|
|
102
|
1 |
|
foreach ($results as $match) { |
103
|
1 |
|
$matches[$match['match_date']] = $match['match_count']; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
$interval = new DateInterval('P1M'); |
107
|
1 |
|
$dateRange = new DatePeriod($timeDate, $interval, TimeDate::now()); |
108
|
|
|
|
109
|
1 |
|
/** @var DateTime $month */ |
110
|
1 |
|
foreach($dateRange as $month) { |
111
|
|
|
$key = $month->format('Y-m'); |
112
|
|
|
|
113
|
|
|
if (!isset($matches[$key])) { |
114
|
1 |
|
$matches[$key] = 0; |
115
|
|
|
} |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
ksort($matches); |
119
|
|
|
|
120
|
|
|
return $matches; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|