1
|
|
|
<?php |
2
|
|
|
namespace AL\Common\DAO; |
3
|
|
|
|
4
|
|
|
require_once __DIR__."/../../lib/Db.php" ; |
5
|
|
|
require_once __DIR__."/../Model/Game/Control.php" ; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* DAO for game controls |
9
|
|
|
*/ |
10
|
|
|
class ControlDAO { |
11
|
|
|
private $mysqli; |
12
|
|
|
|
13
|
|
|
public function __construct($mysqli) { |
14
|
|
|
$this->mysqli = $mysqli; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get all controls |
19
|
|
|
* |
20
|
|
|
* @return \AL\Common\Model\Game\GameControls[] An array of controls |
21
|
|
|
*/ |
22
|
|
|
public function getAllControls() { |
23
|
|
|
$stmt = \AL\Db\execute_query( |
24
|
|
|
"ControlDAO: getAllControls", |
25
|
|
|
$this->mysqli, |
26
|
|
|
"SELECT id, name FROM control ORDER BY name", |
27
|
|
|
null, null |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
\AL\Db\bind_result( |
31
|
|
|
"ControlDAO: getAllControls", |
32
|
|
|
$stmt, |
33
|
|
|
$id, $name |
|
|
|
|
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$game_controls = []; |
37
|
|
|
while ($stmt->fetch()) { |
38
|
|
|
$game_controls[] = new \AL\Common\Model\Game\Control( |
39
|
|
|
$id, $name |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$stmt->close(); |
44
|
|
|
|
45
|
|
|
return $game_controls; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get list of game_controls for a game |
50
|
|
|
* |
51
|
|
|
* @param integer Game ID |
52
|
|
|
*/ |
53
|
|
|
public function getGameControlsForGame($game_id) { |
54
|
|
|
$stmt = \AL\Db\execute_query( |
55
|
|
|
"ControlDAO: getGameControlsForGame", |
56
|
|
|
$this->mysqli, |
57
|
|
|
"SELECT control_id, name |
58
|
|
|
FROM game_control LEFT JOIN control ON (game_control.control_id = control.id) |
59
|
|
|
WHERE game_id = ?", |
60
|
|
|
"i", $game_id |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
\AL\Db\bind_result( |
64
|
|
|
"ControlDAO: getGameControlsForGame", |
65
|
|
|
$stmt, |
66
|
|
|
$engine_id, $name |
|
|
|
|
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$game_controls = []; |
70
|
|
|
while ($stmt->fetch()) { |
71
|
|
|
$game_controls[] = new \AL\Common\Model\Game\Control( |
72
|
|
|
$engine_id, $name |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$stmt->close(); |
77
|
|
|
|
78
|
|
|
return $game_controls; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the list of game controls for this game |
83
|
|
|
* |
84
|
|
|
* @param integer Game ID |
85
|
|
|
* @param integer[] List of game control IDs |
|
|
|
|
86
|
|
|
*/ |
87
|
|
|
public function setGameControlForGame($game_id, $game_control_ids) { |
88
|
|
|
$stmt = \AL\Db\execute_query( |
89
|
|
|
"ControlDAO: setGameControlForGame", |
90
|
|
|
$this->mysqli, |
91
|
|
|
"DELETE FROM game_control WHERE game_id = ?", |
92
|
|
|
"i", $game_id |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
foreach ($game_control_ids as $id) { |
96
|
|
|
$stmt = \AL\Db\execute_query( |
97
|
|
|
"ControlDAO: setGameControlForGame", |
98
|
|
|
$this->mysqli, |
99
|
|
|
"INSERT INTO game_control (game_id, control_id) VALUES (?, ?)", |
100
|
|
|
"ii", $game_id, $id |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$stmt->close(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* add a control to the database |
109
|
|
|
* |
110
|
|
|
* @param varchar control |
111
|
|
|
*/ |
112
|
|
|
public function addControl($control) { |
113
|
|
|
$stmt = \AL\Db\execute_query( |
114
|
|
|
"ControlDAO: addControl", |
115
|
|
|
$this->mysqli, |
116
|
|
|
"INSERT INTO control (`name`) VALUES (?)", |
117
|
|
|
"s", $control |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$stmt->close(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* delete a control |
125
|
|
|
* |
126
|
|
|
* @param int control_id |
|
|
|
|
127
|
|
|
*/ |
128
|
|
|
public function deleteControl($control_id) { |
129
|
|
|
$stmt = \AL\Db\execute_query( |
130
|
|
|
"ControlDAO: deleteControl", |
131
|
|
|
$this->mysqli, |
132
|
|
|
"DELETE FROM control WHERE id = ?", |
133
|
|
|
"i", $control_id |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$stmt->close(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* update a control |
141
|
|
|
* |
142
|
|
|
* @param int control_id |
143
|
|
|
* @param varchar control_name |
|
|
|
|
144
|
|
|
*/ |
145
|
|
|
public function updateControl($control_id, $control_name) { |
146
|
|
|
$stmt = \AL\Db\execute_query( |
147
|
|
|
"ControlDAO: updateControl", |
148
|
|
|
$this->mysqli, |
149
|
|
|
"UPDATE control SET name = ? WHERE id = ?", |
150
|
|
|
"si", $control_name, $control_id |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$stmt->close(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|