390f6c9dde0cb8a05ca234296843a2e002752911
[e-DoKo.git] / functions.php
1 <?php
2
3 function mymail($To,$Subject,$message)
4 {  
5   global $debug;
6
7   if($debug)
8     {
9       $message = str_replace("\n","<br />",$message);
10       echo "<br />To: $To<br />Subject: $Subject <br />$message<br />\n";
11     }
12   else
13     mail($To,$Subject,$message);
14   return;
15 }
16
17 function is_trump($c) { return (($c<27) ? 1:0);}
18 function is_club($c)  { return (in_array($c,array('27','28','29','30','31','32','33','34')));}
19 function is_spade($c) { return (in_array($c,array('35','36','37','38','39','40','41','42')));}
20 function is_heart($c) { return (in_array($c,array('43','44','45','46','47','48')));}
21
22 function compare_cards($a,$b)
23 {
24   /* if a is higher than b return 1, else 0, a being the card first played */
25   
26   /* first map all cards to the odd number */
27   if( $a/2 - (int)($a/2) != 0.5)
28     $a--;
29   if( $b/2 - (int)($b/2) != 0.5)
30     $b--;
31   
32   if(is_trump($a) && $a<=$b)
33     return 1;
34   else if(is_trump($a) && $a>$b)
35     return 0;
36   else 
37     { /*$a is not a trump */
38       if(is_trump($b))
39         return 0;
40       else
41         {
42           /* both clubs? */
43           if( is_club($a) && is_club($b))
44             if($a<=$b)
45               return 1;
46             else
47               return 0;
48           /* both spade? */
49           if( is_spade($a) && is_spade($b))
50             if($a<=$b)
51               return 1;
52             else
53               return 0;
54           /* both heart? */
55           if( is_heart($a) && is_heart($b))
56             if($a<=$b)
57               return 1;
58             else
59               return 0;
60       return 1;
61         }         
62     }
63       
64
65
66 function get_winner($p)
67 {
68   /* get all 4 cards played in a trick */
69   $c1 = $p[1];
70   $c2 = $p[2];
71   $c3 = $p[3];
72   $c4 = $p[4];
73
74   /* find out who won */
75   if( compare_cards($c1,$c2) && compare_cards($c1,$c3) && compare_cards($c1,$c4) )
76     return 1;
77   if( compare_cards($c2,$c3) && compare_cards($c2,$c4) )
78     return 2;
79   if( compare_cards($c3,$c4) )
80     return 3;
81   return 4;
82 }
83
84 function count_nines($cards)
85 {
86   $nines = 0;
87
88   foreach($cards as $c)
89     {
90       if($c == "25" || $c == "26") $nines++;
91       else if($c == "33" || $c == "34") $nines++;
92       else if($c == "41" || $c == "42") $nines++;
93       else if($c == "47" || $c == "48") $nines++;
94     }
95   
96   return $nines;
97 }
98
99 function check_wedding($cards)
100 {
101
102   if( in_array("3",$cards) && in_array("2",$cards) )
103     return 1;
104
105   return 0;
106 }
107
108 function count_trump($cards)
109 {
110   $trump = 0;
111
112   /* count each trump */
113   foreach($cards as $c)
114     if( (int)($c) <27) 
115       $trump++;
116
117   /* subtract foxes */
118   if( in_array("19",$cards))
119     $trump--;
120   if( in_array("20",$cards) )
121     $trump--;
122   /* add one, in case the player has both foxes (schweinchen) */
123   if( in_array("19",$cards) && in_array("20",$cards) )
124     $trump++;
125
126   return $trump;
127 }
128
129 function card_to_name($card)
130 {
131   switch($card)
132     {
133       case 1:
134       case 2:
135         return "ten of hearts";
136       case 3:
137       case 4:
138       return "queen of clubs";
139       case 5:
140       case 6:
141       return "queen of spades";
142       case 7:
143       case 8:
144       return "queen of hearts";
145       case 9:
146       case 10:
147       return "queen of diamonds";
148       case 11:
149       case 12:
150       return "jack of clubs";
151       case 13:
152       case 14:
153       return "jack of spades";
154       case 15:
155       case 16:
156       return "jack of hearts";
157       case 17:
158       case 18:
159       return "jack of diamonds";
160       case 19:
161       case 20:
162       return "ace of diamonds";
163       case 21:
164       case 22:
165       return "ten of diamonds";
166       case 23:
167       case 24:
168       return "king of diamonds";
169       case 25:
170       case 26:
171       return "nine of diamonds";;
172       case 27:
173       case 28:
174       return "ace of clubs";
175       case 29:
176       case 30:
177       return "ten of clubs";
178       case 31:
179       case 32:
180       return "king of clubs";
181       case 33:
182       case 34:
183       return "nine of clubs";
184       case 35:
185       case 36:
186       return "ace of spades";
187       case 37:
188       case 38:
189       return "ten of spades";
190       case 39:
191       case 40:
192       return "king of spades";
193       case 41:
194       case 42:
195       return "nine of spades";
196       case 43:
197       case 44:
198       return "ace of hearts";
199       case 45:
200       case 46:
201       return "king of hearts";
202       case 47:
203       case 48:
204       return "nine of hearts";
205       default:
206       return "something went wrong, please contact the admin. Error: code1.";
207     }
208 }
209
210 function card_value($card)
211 {
212   switch($card)
213     {
214     case 1:      /* heart */
215     case 2:
216       return 10;
217     case 3:     /* clubes */     
218     case 4:                      
219     case 5:     /* spades */     
220     case 6:                      
221     case 7:     /* hearts */     
222     case 8:                      
223     case 9:     /* diamonds */   
224     case 10:                     
225       return 3;
226     case 11:    /* clubes */     
227     case 12:                     
228     case 13:    /* spades */     
229     case 14:                     
230     case 15:    /* hearts */     
231     case 16:                     
232     case 17:    /* diamonds */   
233     case 18:
234       return 2;                  
235     case 19:    /* diamonds */ 
236     case 20:                   
237     case 27:    /* clubs */    
238     case 28:                   
239     case 35:    /* spades */   
240     case 36:                   
241     case 43:    /* hearts */   
242     case 44:                   
243       return 11;
244     case 21:    /* diamonds */    
245     case 22:
246     case 29:    /* clubs */
247     case 30:
248     case 37:    /* spades */
249     case 38:
250       return 10;
251     case 23:    /* diamonds */ 
252     case 24:                   
253     case 31:    /* clubs */    
254     case 32:                   
255     case 39:    /* spades */   
256     case 40:                   
257     case 45:    /* hearts */   
258     case 46:                   
259       return 4;
260     case 25:    /* diamonds */   
261     case 26:                   
262     case 33:    /* clubs */    
263     case 34:                   
264     case 41:    /* spades */   
265     case 42:                   
266     case 47:    /* hearts */   
267     case 48:                   
268       return 0;
269     default:
270       echo "something went wrong, please contact the admin. ErrorCode: 2<br>";
271       return 0;
272     }
273 }
274
275 function display_card($card)
276 {
277   /* cards are only availabl for the odd values, e.g. 1.png, 3.png, ... 
278    * convert even cards to the matching odd value */
279
280   if( $card/2 - (int)($card/2) == 0.5)
281     echo "<img src=\"cards/".$card.".png\"  alt=\"".card_to_name($card)."\" />\n";
282   else
283     echo "<img src=\"cards/".($card-1).".png\"  alt=\"".card_to_name($card-1)."\" />\n";
284
285   return;
286 }
287
288 function display_link_card($card)
289 {
290   if( $card/2 - (int)($card/2) == 0.5)
291     echo "<input type=\"radio\" name=\"card\" value=\"".$card."\" /><img src=\"cards/".$card.".png\" alt=\"\" />\n";
292   else
293     echo "<input type=\"radio\" name=\"card\" value=\"".$card."\" /><img src=\"cards/".($card-1).".png\" alt=\"\" />\n";
294   return;
295 }
296
297 function  create_array_of_random_numbers()
298 {
299   $r = array();
300   $a = array();
301   
302   for($i=1;$i<49;$i++)
303     $a[$i]=$i;
304   
305   $r = array_rand($a,48);
306    
307   return $r;
308 }
309
310 function check_want_to_play($me)
311 {
312    ?>
313  <form action="index.php" method="post">
314    Do you want to play a game of DoKo?
315    yes<input type="radio" name="in" value="yes" />
316    no<input type="radio" name="in" value="no" /> <br />
317
318    Do you want to get an email for every card played or only if it your move?
319    every card<input type="radio" name="update" value="card" />
320    only on my turn<input type="radio" name="update" value="turn" /> <br />
321 <?php   
322   echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
323   echo "\n";
324   echo "<input type=\"submit\" value=\"count me in\" />\n";
325   echo " </form>\n";
326
327   return;
328 }
329
330 function check_for_sickness($me,$mycards)
331 {
332  ?>
333   <p> nothing implemented so far, but give it a try anyway ;) </p>                
334
335   <form action="index.php" method="post">
336
337     do you want to play solo? 
338     <select name="solo" size="1">
339       <option>No</option>
340       <option>trumpless</option>
341       <option>trump</option>
342       <option>queen</option>
343       <option>jack</option>
344       <option>club</option>
345       <option>spade</option>
346       <option>heart</option>
347     </select>     
348     <br />
349
350  <?php   
351       
352    echo "wedding?";
353   if(check_wedding($mycards))
354      {
355        echo " yes<input type=\"radio\" name=\"wedding\" value=\"yes\" />";
356        echo " no <input type=\"radio\" name=\"wedding\" value=\"no\" /> <br />\n";
357      }
358    else
359      {
360        echo " no <input type=\"hidden\" name=\"wedding\" value=\"no\" /> <br />\n";
361      };
362
363   echo "do you have poverty?";
364   if(count_trump($mycards)<4)
365     {
366       echo " yes<input type=\"radio\" name=\"poverty\" value=\"yes\" />";
367       echo " no <input type=\"radio\" name=\"poverty\" value=\"no\" /> <br />\n";
368     }
369   else
370     {
371       echo " no <input type=\"hidden\" name=\"poverty\" value=\"no\" /> <br />\n";
372     };
373
374    echo "do you have too many nines?";
375   if(count_nines($mycards)>4)
376      {
377        echo " yes<input type=\"radio\" name=\"nines\" value=\"yes\" />";
378        echo " no <input type=\"radio\" name=\"nines\" value=\"no\" /> <br />\n";
379      }
380    else
381      {
382        echo " no <input type=\"hidden\" name=\"nines\" value=\"no\" /> <br />\n";
383      };
384
385    echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
386    echo "<input type=\"submit\" value=\"count me in\" />\n";
387
388    echo "</form>\n";
389
390   return;
391 }
392
393 function display_status()
394 {
395   echo "<div class=\"info\">";
396   echo " is someone playing solo, etc?";
397   echo "</div>";
398   
399   return;
400 }
401
402 function display_news()
403 {
404   global $wiki;
405   echo "<div class=\"bug\"> ".
406     "Please hit shift+reload.<br /><hr />".
407     "Fixed a few bugs, comments might be working again <br /><hr />".
408     "If you find more bugs, please list them in the <a href=\"".$wiki.
409     "\">wiki</a>.</div>\n";
410   return;
411 }
412
413 function display_cards($me,$myturn)
414 {
415   return;
416 }
417
418 function return_timezone($offset)
419 {
420   switch($offset)
421     {
422     case '1':
423       $zone = "Europe/Berlin";
424       break;
425     case '-8':
426       $zone = "America/Vancouver";
427       break;
428     case '13':
429       $zone = "Pacific/Auckland";
430       break;
431     default:
432       $zone = "Europe/London";
433     }
434   
435   return $zone;
436 }
437
438 function output_form_for_new_game()
439 {
440 ?>
441     <p>Please add 4 names, please make sure that the names are correct! </p>
442        <form action="index.php" method="post">
443    Name:  <input name="PlayerA" type="text" size="10" maxlength="20" /> 
444    Name:  <input name="PlayerB" type="text" size="10" maxlength="20" /> 
445    Name:  <input name="PlayerC" type="text" size="10" maxlength="20" /> 
446    Name:  <input name="PlayerD" type="text" size="10" maxlength="20" /> 
447
448    <input type="submit" value="start game" />
449  </form>
450 <?php
451 }
452
453 ?>