7a23fb172c3808f393ce347fd03f78154a3632c2
[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,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 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);
166                                 
167                                 if ($stmt->fetch()) {
168                                         $result = new Plant($id, $theuserid, $thename, $code, $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                         die($plant);
192
193                         $stmt = $this->m_Connection->prepare("UPDATE plant SET Code=?, Valid=? WHERE ID=?");
194
195                         if ($stmt) {
196                                 $stmt->bind_param("sid", $plant->Code, $plant->IsValid(), $plant->ID);
197                                 $stmt->execute();
198
199                                 if ($stmt->affected_rows == 1) {
200                                         $result = true;
201                                 }
202                                 $stmt->close();
203                         } else {
204                                 die("Look there, behind you. A three-headed monkey!");
205                         }
206                         
207                         return $result;
208                 }
209                 public function DropPlant($plant) {
210                         $result = false;
211                         
212                         $stmt = $this->m_Connection->prepare("DELETE FROM plant WHERE ID=?");
213                         if ($stmt) {
214                                 $stmt->bind_param("d", $plant->ID);
215                                 $stmt->execute();
216
217                                 if ($stmt->affected_rows == 1) {
218                                         $result = true;
219                                 }
220                                 $stmt->close();
221                         } else {
222                                 die("Au man, that hurt.");
223                         }
224
225                         return $result;
226                 }
227                 public function GetPlantsForUser($userid) {
228                         $result = array();
229                         $plantids = array();
230                         
231                         $stmt = $this->m_Connection->prepare("SELECT ID FROM plant WHERE UserID=?");
232                         if ($stmt) {
233                                 $stmt->bind_param("d", $userid);
234                                 $stmt->execute();
235                                 $stmt->bind_result($id);
236
237                                 while ($stmt->fetch()) {
238                                         $plantids[] = $id;
239                                 }
240                                 $stmt->close();
241                         } else {
242                                 die("None of that young lady!");
243                         }
244                         
245                         foreach ($plantids as $id) {
246                                 $result[] = $this->GetPlantByID($userid, $id);
247                         }
248                         
249                         return $result;
250                 }
251                 public function GetCurrentSeasonScore($userid) {
252                         $result = null;
253                         
254                         $stmt = $this->m_Connection->prepare("SELECT ID FROM seasonscore WHERE UserID=?");
255                         if ($stmt) {
256                                 $stmt->bind_param("d", $userid);
257                                 $stmt->execute();
258                                 $stmt->bind_result($id);
259
260                                 while ($stmt->fetch()) {
261                                         $result = $this->GetSeasonScoreByID($id);
262                                 }
263                                 $stmt->close();
264                         } else {
265                                 die("WHY YOU LITTLE...!");
266                         }
267                         
268                         return $result;
269                 }
270                 public function GetSeasonScoreByID($ssid) {
271                         $result = null;
272                         
273                         $stmt = $this->m_Connection->prepare("SELECT ID, UserID, SeasonID, Score FROM seasonscore WHERE ID=?");
274                         if ($stmt) {
275                                 $stmt->bind_param("d", $ssid);
276                                 $stmt->execute();
277                                 $stmt->bind_result( $id, $userid, $seasonid, $score);
278
279                                 while ($stmt->fetch()) {
280                                         $result = new SeasonScore($id, $userid, $seasonid, $score, $this);
281                                 }
282                                 $stmt->close();
283                         } else {
284                                 die("WHY YOU LITTLE...!");
285                         }
286                         
287                         if ($result != null) {
288                                 $result->Season = $this->GetSeasonForSeasonScore($result);
289                         }
290                         
291                         return $result;
292                 }
293                 
294                 public function GetSeasonForSeasonScore($score) {
295                         $season = null;
296                         
297                         $stmt = $this->m_Connection->prepare("SELECT ID, IsRunning FROM season WHERE ID=?");
298                         if ($stmt) {
299                                 $stmt->bind_param("d", $score->SeasonID);
300                                 $stmt->execute();
301                                 $stmt->bind_result( $id, $isrunnning);
302
303                                 while ($stmt->fetch()) {
304                                         $season = new Season($id, $isrunnning, $this);
305                                 }
306                                 $stmt->close();
307                         } else {
308                                 die("You Crack me up little buddy.");
309                         }
310                         
311                         return $season;
312                 }
313                 
314                 public function GetAllSeasonScores() {
315                         $result = array();
316                         $idlist = array();
317                         
318                         $stmt = $this->m_Connection->query("SELECT ID FROM seasonscore");
319                 
320                         if ($stmt) {
321                                 while ($obj = $stmt->fetch_object()) {
322                                         $idlist[] = $obj->ID;
323                                 }
324                                 $stmt->close();
325                         } else {
326                                 die("WHY YOU LITTLE...!");
327                         }
328                         
329                         foreach ($idlist as $id) {
330                                 $result[] = $this->GetSeasonScoreByID($id);
331                         }
332                         
333                         return $result;
334                 }
335         }
336 ?>