forgotten changes
[L-seed.git] / web / php / Database.php
1 <?php
2         
3         class DataBase
4         {
5                 public $m_Connection;
6
7                 public function __construct() {
8                         $this->m_Connection = @new mysqli( 'localhost', 'lseed', 'GCp:rtAaN8nwTFMP', 'lseed' );
9                         if (mysqli_connect_errno() != 0) {
10                                 die( "Argh what did you do?->SERVERERR=0, ".mysqli_connect_error());
11                         }
12                 }
13                 
14                 public function Clear() {
15                         $res1 = $this->m_Connection->query("DELETE FROM user");
16                         $res2 = $this->m_Connection->query("DELETE FROM plant");
17                         $res3 = $this->m_Connection->query("DELETE FROM season");
18                         $res4 = $this->m_Connection->query("DELETE FROM seasonscore");
19                         return $res1 && $res2 && $res3 && $res4;
20                 }
21                 public function Close() {
22                         $this->m_Connection->close();
23                 }
24
25                 public function CreateUser($user, $md5pw) {
26                         $result = false;
27
28                         $stmt = $this->m_Connection->prepare("INSERT INTO user (Name, Password, IsAdmin) VALUES (?, ?, ?)");
29                         
30                         if ($stmt) {
31                                 $isadmin = false;
32                                 $stmt->bind_param("ssb", $user, $md5pw, $isadmin);
33                                 $stmt->execute();
34
35                                 if ($stmt->affected_rows == 1) {
36                                         $result = true;
37                                 }
38                                 
39                                 $stmt->close();
40                         } else {
41                                 die("You little Nerd. How could you?");
42                         }
43
44                         return $result;
45                 }
46                 public function GetUser($username) {
47                         $result = null;
48                         $userid = null;
49                         
50                         $stmt = $this->m_Connection->prepare("SELECT ID FROM user WHERE Name=?");
51                         
52                         if ($stmt) {
53                                 $stmt->bind_param("s", $username);
54                                 $stmt->execute();
55                                 $stmt->bind_result( $userid);
56
57                                 $stmt->fetch();
58                                 $stmt->close();
59                         } else {
60                                 die("You silly bastard. Not again!!!!");
61                         }
62                         
63                         if ($userid != null) {
64                                 $result = $this->GetUserByID($userid);
65                         }
66
67                         return $result;
68                 }
69                 public function GetUserByID($userid) {
70                         $result = null;
71                         
72                         $stmt = $this->m_Connection->prepare("SELECT ID, Name, Password, IsAdmin, NextSeed FROM user WHERE ID=?");
73                         
74                         if ($stmt) {
75                                 $stmt->bind_param("d", $userid);
76                                 $stmt->execute();
77                                 $stmt->bind_result( $id, $name, $pw, $isadmin, $nextseedid);
78
79                                 if ($stmt->fetch()) {
80                                         $result = new User($id, $name, $pw, $isadmin, $nextseedid, $this);
81                                 }
82                                 $stmt->close();
83                         } else {
84                                 die("AAARRRRRRR!!!!");
85                         }
86                         
87                         if ($result != null) {
88                                 $result->SeasonScore = $this->GetCurrentSeasonScore($result);
89                         }
90
91                         return $result;
92                 }
93                 public function SetUsersNextSeed($userid, $plantid) {
94                         $result = false;
95                         
96                         $stmt = $this->m_Connection->prepare("UPDATE user SET NextSeed=? WHERE ID=?");
97                         
98                         if ($stmt) {
99                                 $stmt->bind_param("dd", $plantid, $userid);
100                                 $stmt->execute();
101
102                                 if ($stmt->affected_rows == 1) {
103                                         $result = true;
104                                 }
105                                 $stmt->close();
106                         } else {
107                                 die("Look there, behind you. A three-headed monkey!");
108                         }
109                         
110                         return $result;
111                 }
112                 public function InsertNewPlant($userid, $name, $code) {
113                         $result = false;
114                         
115                         $stmt = $this->m_Connection->prepare("INSERT INTO plant (UserID, Name, Code) VALUES (?, ?, ?)");
116                         
117                         if ($stmt) {
118                                 $stmt->bind_param("dss", $userid, $name, $code);
119                                 $stmt->execute();
120                                 
121                                 if ($stmt->affected_rows == 1) {
122                                         $result = true;
123                                 }
124                                 $stmt->close();
125                         } else {
126                                 die("DAAAAAAMN.");
127                         }
128                         
129                         return $result;
130                 }
131                 public function GetPlant($userid, $name) {
132                         $result = null;
133                         $plantid = null;
134
135                         $stmt = $this->m_Connection->prepare("SELECT ID FROM plant WHERE UserID=? AND Name=?");
136                         
137                         if ($stmt) {
138                                 $stmt->bind_param("ds", $userid, $name);
139                                 $stmt->execute();
140                                 $stmt->bind_result( $plantid);
141                                 
142                                 $stmt->fetch();
143                                 $stmt->close();
144                         } else {
145                                 die("The server you requested is currently unavailable. Please engage in nosepicking...");
146                         }
147                         
148                         if ($plantid != null) {
149                                 $result = $this->GetPlantByID($userid, $plantid);
150                         }
151                         
152                         return $result;
153                 }
154                 public function GetPlantByID($userid, $plantid) {
155                         $result = null;
156
157                         $stmt = $this->m_Connection->prepare("SELECT ID, UserID, Name, Code FROM plant WHERE UserID=? AND ID=?");
158                         
159                         if ($stmt) {
160                                 $stmt->bind_param("ds", $userid, $plantid);
161                                 $stmt->execute();
162                                 $stmt->bind_result( $id, $theuserid, $thename, $code);
163                                 
164                                 if ($stmt->fetch()) {
165                                         $result = new Plant($id, $theuserid, $thename, $code, $this);
166                                 } else {
167                                         //echo "nope no plant like that found.";
168                                 }
169                                 $stmt->close();
170                         } else {
171                                 die("WTF?");
172                         }
173                         
174                         if ($result != null) {
175                                 $user = $this->GetUserByID($userid);
176                                 if ($user != null) {
177                                         $result->IsActive = $user->NextSeedID == $result->ID;
178                                 } else {
179                                         die("Invisible? No waaaay.");
180                                 }
181                         }
182                         
183                         return $result;
184                 }
185                 public function UpdatePlant($plant) {
186                         $result = false;
187                         
188                         $stmt = $this->m_Connection->prepare("UPDATE plant SET Code=? WHERE ID=?");
189                         
190                         if ($stmt) {
191                                 $stmt->bind_param("sd", $plant->Code, $plant->ID);
192                                 $stmt->execute();
193
194                                 if ($stmt->affected_rows == 1) {
195                                         $result = true;
196                                 }
197                                 $stmt->close();
198                         } else {
199                                 die("Look there, behind you. A three-headed monkey!");
200                         }
201                         
202                         return $result;
203                 }
204                 public function DropPlant($plant) {
205                         $result = false;
206                         
207                         $stmt = $this->m_Connection->prepare("DELETE FROM plant WHERE ID=?");
208                         if ($stmt) {
209                                 $stmt->bind_param("d", $plant->ID);
210                                 $stmt->execute();
211
212                                 if ($stmt->affected_rows == 1) {
213                                         $result = true;
214                                 }
215                                 $stmt->close();
216                         } else {
217                                 die("Au man, that hurt.");
218                         }
219
220                         return $result;
221                 }
222                 public function GetPlantsForUser($userid) {
223                         $result = array();
224                         $plantids = array();
225                         
226                         $stmt = $this->m_Connection->prepare("SELECT ID FROM plant WHERE UserID=?");
227                         if ($stmt) {
228                                 $stmt->bind_param("d", $userid);
229                                 $stmt->execute();
230                                 $stmt->bind_result($id);
231
232                                 while ($stmt->fetch()) {
233                                         $plantids[] = $id;
234                                 }
235                                 $stmt->close();
236                         } else {
237                                 die("None of that young lady!");
238                         }
239                         
240                         foreach ($plantids as $id) {
241                                 $result[] = $this->GetPlantByID($userid, $id);
242                         }
243                         
244                         return $result;
245                 }
246                 public function GetCurrentSeasonScore($userid) {
247                         $result = null;
248                         
249                         $stmt = $this->m_Connection->prepare("SELECT ID FROM seasonscore WHERE UserID=?");
250                         if ($stmt) {
251                                 $stmt->bind_param("d", $userid);
252                                 $stmt->execute();
253                                 $stmt->bind_result($id);
254
255                                 while ($stmt->fetch()) {
256                                         $result = $this->GetSeasonScoreByID($id);
257                                 }
258                                 $stmt->close();
259                         } else {
260                                 die("WHY YOU LITTLE...!");
261                         }
262                         
263                         return $result;
264                 }
265                 public function GetSeasonScoreByID($ssid) {
266                         $result = null;
267                         
268                         $stmt = $this->m_Connection->prepare("SELECT ID, UserID, SeasonID, Score FROM seasonscore WHERE ID=?");
269                         if ($stmt) {
270                                 $stmt->bind_param("d", $ssid);
271                                 $stmt->execute();
272                                 $stmt->bind_result( $id, $userid, $seasonid, $score);
273
274                                 while ($stmt->fetch()) {
275                                         $result = new SeasonScore($id, $userid, $seasonid, $score, $this);
276                                 }
277                                 $stmt->close();
278                         } else {
279                                 die("WHY YOU LITTLE...!");
280                         }
281                         
282                         if ($result != null) {
283                                 $result->Season = $this->GetSeasonForSeasonScore($result);
284                         }
285                         
286                         return $result;
287                 }
288                 
289                 public function GetSeasonForSeasonScore($score) {
290                         $season = null;
291                         
292                         $stmt = $this->m_Connection->prepare("SELECT ID, IsRunning FROM season WHERE ID=?");
293                         if ($stmt) {
294                                 $stmt->bind_param("d", $score->SeasonID);
295                                 $stmt->execute();
296                                 $stmt->bind_result( $id, $isrunnning);
297
298                                 while ($stmt->fetch()) {
299                                         $season = new Season($id, $isrunnning, $this);
300                                 }
301                                 $stmt->close();
302                         } else {
303                                 die("You Crack me up little buddy.");
304                         }
305                         
306                         return $season;
307                 }
308                 
309                 public function GetAllSeasonScores() {
310                         $result = array();
311                         $idlist = array();
312                         
313                         $stmt = $this->m_Connection->query("SELECT ID FROM seasonscore");
314                 
315                         if ($stmt) {
316                                 while ($obj = $stmt->fetch_object()) {
317                                         $idlist[] = $obj->ID;
318                                 }
319                                 $stmt->close();
320                         } else {
321                                 die("WHY YOU LITTLE...!");
322                         }
323                         
324                         foreach ($idlist as $id) {
325                                 $result[] = $this->GetSeasonScoreByID($id);
326                         }
327                         
328                         return $result;
329                 }
330         }
331 ?>