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