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