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
|
|
|
$team_a_query = "FIND_IN_SET(?, team_a_players)"; |
35
|
|
|
$team_b_query = "FIND_IN_SET(?, team_b_players)"; |
36
|
|
|
} else { |
37
|
|
|
throw new InvalidArgumentException("Invalid model provided"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
switch ($result) { |
41
|
2 |
|
case "wins": |
42
|
2 |
|
case "win": |
43
|
2 |
|
case "victory": |
44
|
2 |
|
case "victories": |
45
|
1 |
|
$query = "($team_a_query AND team_a_points > team_b_points) OR ($team_b_query AND team_b_points > team_a_points)"; |
46
|
1 |
|
break; |
47
|
2 |
|
case "loss": |
48
|
2 |
|
case "lose": |
49
|
2 |
|
case "losses": |
50
|
2 |
|
case "defeat": |
51
|
2 |
|
case "defeats": |
52
|
|
|
$query = "($team_a_query AND team_b_points > team_a_points) OR ($team_b_query AND team_a_points > team_b_points)"; |
53
|
|
|
break; |
54
|
2 |
|
case "draw": |
55
|
2 |
|
case "draws": |
56
|
2 |
|
case "tie": |
57
|
2 |
|
case "ties": |
58
|
|
|
$query = "($team_a_query OR $team_b_query) AND team_a_points = team_b_points"; |
59
|
|
|
break; |
60
|
|
|
default: |
61
|
2 |
|
$query = "$team_a_query OR $team_b_query"; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$this->conditions[] = $query; |
65
|
2 |
|
$this->parameters[] = $participant->getId(); |
66
|
2 |
|
$this->parameters[] = $participant->getId(); |
67
|
|
|
|
68
|
2 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Group results by day |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
1 |
|
public function groupByMonth() |
77
|
|
|
{ |
78
|
1 |
|
$this->groupQuery .= "GROUP BY YEAR(timestamp), MONTH(timestamp)"; |
79
|
|
|
|
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get a count for each month's matches |
85
|
|
|
* |
86
|
|
|
* @param Team $team The team in question |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
1 |
|
public function getSummary(Team $team) |
90
|
|
|
{ |
91
|
1 |
|
$this->groupByMonth(); |
92
|
|
|
|
93
|
1 |
|
$query = $this->createQuery("YEAR(timestamp) as y, MONTH(timestamp) as m, COUNT(*) as count"); |
94
|
|
|
|
95
|
1 |
|
$matches = array(); |
96
|
1 |
|
$results = Database::getInstance()->query($query, $this->parameters); |
97
|
|
|
|
98
|
1 |
|
foreach ($results as $match) { |
99
|
1 |
|
$matches[$match['y'] . '-' . sprintf('%02d', $match['m'])] = $match['count']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Add entries for dates with 0 matches |
103
|
1 |
|
$timestamp = $team->getCreationDate()->setTimezone('UTC')->startOfMonth(); |
104
|
1 |
|
while ($timestamp->lte(TimeDate::now())) { |
105
|
1 |
|
$key = $timestamp->format('Y-m'); |
106
|
1 |
|
if (!isset($matches[$key])) { |
107
|
1 |
|
$matches[$key] = 0; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$timestamp->addMonth(); |
111
|
|
|
} |
112
|
1 |
|
ksort($matches); |
113
|
|
|
|
114
|
1 |
|
return $matches; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|