forgot to add the new table pic
[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 myisset()
18 {
19   /* returns 1 if all names passed as args are defined by a GET or POST statement,
20    * else return 0
21    */
22
23   $ok   = 1;
24   $args = func_get_args();
25   
26   foreach($args as $arg)
27     {
28       $ok = $ok * isset($_REQUEST[$arg]);
29       /* echo "$arg: ok = $ok <br />";
30        */
31     }
32   return $ok;
33 }
34
35 function pos_array($c,$arr)
36 {
37   $ret = 0;
38
39   $i   = 0;
40   foreach($arr as $a)
41     {
42       $i++;
43       if($a == $c)
44         {
45           $ret = $i;
46           break;
47         }
48     }
49   return $ret;
50 }
51
52 function is_trump($c,$game) 
53
54   switch($game)
55     {
56     case "queen":
57       if(in_array($c,array('3','4','5','6','7','8','9','10')))
58         return 1;
59       else 
60         return 0;
61       break;
62     case "normal":
63       return (($c<27) ? 1:0);
64       break;
65     }
66 }
67
68 function is_same_suite($c1,$c2,$game) 
69 {
70   switch($game)
71     {
72     case "queen":
73       /* clubs */
74       if(in_array($c1,array('11','12','27','28','29','30','31','32','33','34')) &&
75          in_array($c2,array('11','12','27','28','29','30','31','32','33','34')) )
76         return 1;
77       /* spade */
78       if(in_array($c1,array('13','14','35','36','37','38','39','40','41','42')) &&
79          in_array($c2,array('13','14','35','36','37','38','39','40','41','42')) )
80         return 1;
81       /* heart */
82       if(in_array($c1,array( '1', '2','15','16','43','44','45','46','47','48')) &&
83          in_array($c2,array( '1', '2','15','16','43','44','45','46','47','48')) )
84         return 1;
85       /* diamonds */
86       if(in_array($c1,array('17','18','19','20','21','22','23','24','25','26')) &&
87          in_array($c2,array('17','18','19','20','21','22','23','24','25','26')) )
88         return 1;
89       
90       return 0;
91       break;
92     case "normal":
93       /* clubs */
94       if(in_array($c1,array('27','28','29','30','31','32','33','34')) &&
95          in_array($c2,array('27','28','29','30','31','32','33','34')) )
96         return 1;
97       /* spade */
98       if(in_array($c1,array('35','36','37','38','39','40','41','42')) &&
99          in_array($c2,array('35','36','37','38','39','40','41','42')) )
100         return 1;
101       /* heart */
102       if(in_array($c1,array('43','44','45','46','47','48')) &&
103          in_array($c2,array('43','44','45','46','47','48')) )
104         return 1;
105       
106       return 0;
107       break;
108     }
109 }
110
111 function compare_cards($a,$b,$game)
112 {
113   /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
114
115   /* first map all cards to the odd number, 
116    * this insure that the first card wins the trick 
117    * if they are the same card
118    */
119   if( $a/2 - (int)($a/2) != 0.5)
120     $a--;
121   if( $b/2 - (int)($b/2) != 0.5)
122     $b--;
123   
124   switch($game)
125     {
126     case "trumpless":
127       break;
128     case "jack":
129       break;
130     case "queen":
131       if(is_trump($a,$game) && is_trump($b,$game) && $a<=$b)
132         return 1;
133       else if(is_trump($a,$game) && is_trump($b,$game) )
134         return 0;
135       else 
136         { /*$a is not a trump */
137           if(is_trump($b,$game))
138             return 0;
139           else
140             { /* both no trump */
141               /* both clubs? */
142               $posA = pos_array($a,array('27','28','29','30','31','32','11','12','33','34'));
143               $posB = pos_array($b,array('27','28','29','30','31','32','11','12','33','34'));
144               if($posA && $posB)
145                 if($posA <= $posB)
146                   return 1;
147                 else
148                   return 0;
149               /* both spades? */
150               $posA = pos_array($a,array('35','36','37','38','39','40','13','14','41','42'));
151               $posB = pos_array($b,array('35','36','37','38','39','40','13','14','41','42'));
152               if($posA && $posB)
153                 if($posA <= $posB)
154                   return 1;
155                 else
156                   return 0;
157               /* both hearts? */
158               $posA = pos_array($a,array('43','44','15','16','45','46', '1', '2','47','48'));
159               $posB = pos_array($b,array('43','44','15','16','45','46', '1', '2','47','48'));
160               if($posA && $posB)
161                 if($posA <= $posB)
162                   return 1;
163                 else
164                   return 0;
165               /* both diamonds? */
166               $posA = pos_array($a,array('19','20','21','22','23','24','17','18','25','26'));
167               $posB = pos_array($b,array('19','20','21','22','23','24','17','18','25','26'));
168               if($posA && $posB)
169                 if($posA <= $posB)
170                   return 1;
171                 else
172                   return 0;
173
174               /* not the same suit and no trump: a wins */
175               return 1;
176             }     
177         }
178       break;
179     case "trump":
180       break;
181     case "club":
182       break;
183     case "spade":
184       break;
185     case "heart":
186       break;
187     case "normal":
188       if($a==1 && $b==1) /* both 10 of hearts */
189         return 0;        /* second one wins. TODO should be able to set this at the start of a new game */
190       if(is_trump($a,$game) && $a<=$b)
191         return 1;
192       else if(is_trump($a,$game))
193         return 0;
194       else 
195         { /*$a is not a trump */
196           if(is_trump($b,$game))
197             return 0;
198           else
199             {
200               if(is_same_suite($a,$b,$game))
201                 if($a<=$b)
202                   return 1;
203                 else
204                   return 0;
205               
206               /* not the same suit and no trump: a wins */
207               return 1;
208             }     
209         }
210     }
211
212
213 function get_winner($p,$mode)
214 {
215   /* get all 4 cards played in a trick */
216   $c1 = $p[1];
217   $c2 = $p[2];
218   $c3 = $p[3];
219   $c4 = $p[4];
220
221   /* find out who won */
222   if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
223     return 1;
224   if( !compare_cards($c1,$c2,$mode) && compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
225     return 2;
226   if( !compare_cards($c1,$c3,$mode) && !compare_cards($c2,$c3,$mode) && compare_cards($c3,$c4,$mode) )
227     return 3;
228   return 4;
229 }
230
231 function count_nines($cards)
232 {
233   $nines = 0;
234
235   foreach($cards as $c)
236     {
237       if($c == "25" || $c == "26") $nines++;
238       else if($c == "33" || $c == "34") $nines++;
239       else if($c == "41" || $c == "42") $nines++;
240       else if($c == "47" || $c == "48") $nines++;
241     }
242   
243   return $nines;
244 }
245
246 function check_wedding($cards)
247 {
248
249   if( in_array("3",$cards) && in_array("2",$cards) )
250     return 1;
251
252   return 0;
253 }
254
255 function count_trump($cards)
256 {
257   $trump = 0;
258
259   /* count each trump */
260   foreach($cards as $c)
261     if( (int)($c) <27) 
262       $trump++;
263
264   /* subtract foxes */
265   if( in_array("19",$cards))
266     $trump--;
267   if( in_array("20",$cards) )
268     $trump--;
269   /* add one, in case the player has both foxes (schweinchen) */
270   if( in_array("19",$cards) && in_array("20",$cards) )
271     $trump++;
272
273   return $trump;
274 }
275
276 function card_to_name($card)
277 {
278   switch($card)
279     {
280       case 1:
281       case 2:
282         return "ten of hearts";
283       case 3:
284       case 4:
285       return "queen of clubs";
286       case 5:
287       case 6:
288       return "queen of spades";
289       case 7:
290       case 8:
291       return "queen of hearts";
292       case 9:
293       case 10:
294       return "queen of diamonds";
295       case 11:
296       case 12:
297       return "jack of clubs";
298       case 13:
299       case 14:
300       return "jack of spades";
301       case 15:
302       case 16:
303       return "jack of hearts";
304       case 17:
305       case 18:
306       return "jack of diamonds";
307       case 19:
308       case 20:
309       return "ace of diamonds";
310       case 21:
311       case 22:
312       return "ten of diamonds";
313       case 23:
314       case 24:
315       return "king of diamonds";
316       case 25:
317       case 26:
318       return "nine of diamonds";;
319       case 27:
320       case 28:
321       return "ace of clubs";
322       case 29:
323       case 30:
324       return "ten of clubs";
325       case 31:
326       case 32:
327       return "king of clubs";
328       case 33:
329       case 34:
330       return "nine of clubs";
331       case 35:
332       case 36:
333       return "ace of spades";
334       case 37:
335       case 38:
336       return "ten of spades";
337       case 39:
338       case 40:
339       return "king of spades";
340       case 41:
341       case 42:
342       return "nine of spades";
343       case 43:
344       case 44:
345       return "ace of hearts";
346       case 45:
347       case 46:
348       return "king of hearts";
349       case 47:
350       case 48:
351       return "nine of hearts";
352       default:
353       return "something went wrong, please contact the admin. Error: code1.";
354     }
355 }
356
357 function card_value($card)
358 {
359   switch($card)
360     {
361     case 1:      /* heart */
362     case 2:
363       return 10;
364     case 3:     /* clubes */     
365     case 4:                      
366     case 5:     /* spades */     
367     case 6:                      
368     case 7:     /* hearts */     
369     case 8:                      
370     case 9:     /* diamonds */   
371     case 10:                     
372       return 3;
373     case 11:    /* clubes */     
374     case 12:                     
375     case 13:    /* spades */     
376     case 14:                     
377     case 15:    /* hearts */     
378     case 16:                     
379     case 17:    /* diamonds */   
380     case 18:
381       return 2;                  
382     case 19:    /* diamonds */ 
383     case 20:                   
384     case 27:    /* clubs */    
385     case 28:                   
386     case 35:    /* spades */   
387     case 36:                   
388     case 43:    /* hearts */   
389     case 44:                   
390       return 11;
391     case 21:    /* diamonds */    
392     case 22:
393     case 29:    /* clubs */
394     case 30:
395     case 37:    /* spades */
396     case 38:
397       return 10;
398     case 23:    /* diamonds */ 
399     case 24:                   
400     case 31:    /* clubs */    
401     case 32:                   
402     case 39:    /* spades */   
403     case 40:                   
404     case 45:    /* hearts */   
405     case 46:                   
406       return 4;
407     case 25:    /* diamonds */   
408     case 26:                   
409     case 33:    /* clubs */    
410     case 34:                   
411     case 41:    /* spades */   
412     case 42:                   
413     case 47:    /* hearts */   
414     case 48:                   
415       return 0;
416     default:
417       echo "something went wrong, please contact the admin. ErrorCode: 2<br>";
418       return 0;
419     }
420 }
421
422
423 function  create_array_of_random_numbers()
424 {
425   $r = array();
426   $a = array();
427   
428   for($i=1;$i<49;$i++)
429     $a[$i]=$i;
430   
431   $r = array_rand($a,48);
432    
433   return $r;
434 }
435
436
437
438
439 function display_cards($me,$myturn)
440 {
441   return;
442 }
443
444 function return_timezone($offset)
445 {
446   switch($offset)
447     {
448     case '1':
449       $zone = "Europe/Berlin";
450       break;
451     case '-8':
452       $zone = "America/Vancouver";
453       break;
454     case '13':
455       $zone = "Pacific/Auckland";
456       break;
457     default:
458       $zone = "Europe/London";
459     }
460   
461   return $zone;
462 }
463
464 ?>