e56a5b956c0d788bc69b1c4bb0ef0be7069b130a
[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                         $plant = new Plant(0,$userid,$plantname,$code,0,this);
116                         $valid = $plant->IsValid();
117                         
118                         $stmt = $this->m_Connection->prepare("INSERT INTO plant (UserID, Name, Code, Valid) VALUES (?, ?, ?, ?)");
119                         
120                         if ($stmt) {
121                                 $stmt->bind_param("dssi", $userid, $name, $code, $valid);
122                                 $stmt->execute();
123                                 
124                                 if ($stmt->affected_rows == 1) {
125                                         $result = true;
126                                 }
127                                 $stmt->close();
128                         } else {
129                                 die("DAAAAAAMN.");
130                         }
131                         
132                         return $result;
133                 }
134                 public function GetPlant($userid, $name) {
135                         $result = null;
136                         $plantid = null;
137
138                         $stmt = $this->m_Connection->prepare("SELECT ID FROM plant WHERE UserID=? AND Name=?");
139                         
140                         if ($stmt) {
141                                 $stmt->bind_param("ds", $userid, $name);
142                                 $stmt->execute();
143                                 $stmt->bind_result( $plantid);
144                                 
145                                 $stmt->fetch();
146                                 $stmt->close();
147                         } else {
148                                 die("The server you requested is currently unavailable. Please engage in nosepicking...");
149                         }
150                         
151                         if ($plantid != null) {
152                                 $result = $this->GetPlantByID($userid, $plantid);
153                         }
154                         
155                         return $result;
156                 }
157                 public function GetPlantByID($userid, $plantid) {
158                         $result = null;
159
160                         $stmt = $this->m_Connection->prepare("SELECT ID, UserID, Name, Code, Valid FROM plant WHERE UserID=? AND ID=?");
161                         
162                         if ($stmt) {
163                                 $stmt->bind_param("ds", $userid, $plantid);
164                                 $stmt->execute();
165                                 $stmt->bind_result( $id, $theuserid, $thename, $code, $isvalid);
166                                 
167                                 if ($stmt->fetch()) {
168                                         $result = new Plant($id, $theuserid, $thename, $code, $isvalid, $this);
169                                 } else {
170                                         //echo "nope no plant like that found.";
171                                 }
172                                 $stmt->close();
173                         } else {
174                                 die("WTF?");
175                         }
176                         
177                         if ($result != null) {
178                                 $user = $this->GetUserByID($userid);
179                                 if ($user != null) {
180                                         $result->IsActive = $user->NextSeedID == $result->ID;
181                                 } else {
182                                         die("Invisible? No waaaay.");
183                                 }
184                         }
185                         
186                         return $result;
187                 }
188                 public function UpdatePlant($plant) {
189                         $result = false;
190                         
191                         $stmt = $this->m_Connection->prepare("UPDATE plant SET Code=?, Valid=? WHERE ID=?");
192
193                         if ($stmt) {
194                                 $stmt->bind_param("sid", $plant->Code, $plant->IsValid(), $plant->ID);
195                                 $stmt->execute();
196
197                                 if ($stmt->affected_rows == 1) {
198                                         $result = true;
199                                 }
200                                 $stmt->close();
201                         } else {
202                                 die("Look there, behind you. A three-headed monkey!");
203                         }
204                         
205                         return $result;
206                 }
207                 public function DropPlant($plant) {
208                         $result = false;
209                         
210                         $stmt = $this->m_Connection->prepare("DELETE FROM plant WHERE ID=?");
211                         if ($stmt) {
212                                 $stmt->bind_param("d", $plant->ID);
213                                 $stmt->execute();
214
215                                 if ($stmt->affected_rows == 1) {
216                                         $result = true;
217                                 }
218                                 $stmt->close();
219                         } else {
220                                 die("Au man, that hurt.");
221                         }
222
223                         return $result;
224                 }
225                 public function GetPlantsForUser($userid) {
226                         $result = array();
227                         $plantids = array();
228                         
229                         $stmt = $this->m_Connection->prepare("SELECT ID FROM plant WHERE UserID=?");
230                         if ($stmt) {
231                                 $stmt->bind_param("d", $userid);
232                                 $stmt->execute();
233                                 $stmt->bind_result($id);
234
235                                 while ($stmt->fetch()) {
236                                         $plantids[] = $id;
237                                 }
238                                 $stmt->close();
239                         } else {
240                                 die("None of that young lady!");
241                         }
242                         
243                         foreach ($plantids as $id) {
244                                 $result[] = $this->GetPlantByID($userid, $id);
245                         }
246                         
247                         return $result;
248                 }
249                 public function GetCurrentSeasonScore($userid) {
250                         $result = null;
251                         
252                         $stmt = $this->m_Connection->prepare("SELECT ID FROM seasonscore WHERE UserID=?");
253                         if ($stmt) {
254                                 $stmt->bind_param("d", $userid);
255                                 $stmt->execute();
256                                 $stmt->bind_result($id);
257
258                                 while ($stmt->fetch()) {
259                                         $result = $this->GetSeasonScoreByID($id);
260                                 }
261                                 $stmt->close();
262                         } else {
263                                 die("WHY YOU LITTLE...!");
264                         }
265                         
266                         return $result;
267                 }
268                 public function GetSeasonScoreByID($ssid) {
269                         $result = null;
270                         
271                         $stmt = $this->m_Connection->prepare("SELECT ID, UserID, SeasonID, Score FROM seasonscore WHERE ID=?");
272                         if ($stmt) {
273                                 $stmt->bind_param("d", $ssid);
274                                 $stmt->execute();
275                                 $stmt->bind_result( $id, $userid, $seasonid, $score);
276
277                                 while ($stmt->fetch()) {
278                                         $result = new SeasonScore($id, $userid, $seasonid, $score, $this);
279                                 }
280                                 $stmt->close();
281                         } else {
282                                 die("What the heck?");
283                         }
284                         
285                         if ($result != null) {
286                                 $result->Season = $this->GetSeasonForSeasonScore($result);
287                         }
288                         
289                         return $result;
290                 }
291                 
292                 public function GetSeasonForSeasonScore($score) {
293                         $season = null;
294                         
295                         $stmt = $this->m_Connection->prepare("SELECT ID, IsRunning FROM season WHERE ID=?");
296                         if ($stmt) {
297                                 $stmt->bind_param("d", $score->SeasonID);
298                                 $stmt->execute();
299                                 $stmt->bind_result( $id, $isrunnning);
300
301                                 while ($stmt->fetch()) {
302                                         $season = new Season($id, $isrunnning, $this);
303                                 }
304                                 $stmt->close();
305                         } else {
306                                 die("You Crack me up little buddy.");
307                         }
308                         
309                         return $season;
310                 }
311                 
312                 public function GetAllSeasonScores() {
313                         $result = array();
314                         $idlist = array();
315                         
316                         $stmt = $this->m_Connection->query("SELECT ID FROM seasonscore");
317                 
318                         if ($stmt) {
319                                 while ($obj = $stmt->fetch_object()) {
320                                         $idlist[] = $obj->ID;
321                                 }
322                                 $stmt->close();
323                         } else {
324                                 die("WHY YOU LITTLE...!");
325                         }
326                         
327                         foreach ($idlist as $id) {
328                                 $result[] = $this->GetSeasonScoreByID($id);
329                         }
330                         
331                         return $result;
332                 }
333         }
334 ?>