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