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