started on adding support for different solo games (modified get_winner a tiny bit...
[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,$mode)
23 {
24   /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
25
26   /* don't think we need this */
27   if( $a/2 - (int)($a/2) != 0.5)
28     $a--;
29   if( $b/2 - (int)($b/2) != 0.5)
30     $b--;
31   
32   switch($mode)
33     {
34     case "trumpless":
35       break;
36     case "jack":
37       break;
38     case "queen":
39       break;
40     case "trump":
41       break;
42     case "club":
43       break;
44     case "spade":
45       break;
46     case "heart":
47       break;
48     case "normal":
49       /* first map all cards to the odd number */
50       if(is_trump($a) && $a<=$b)
51         return 1;
52       else if(is_trump($a) && $a>$b)
53         return 0;
54       else 
55         { /*$a is not a trump */
56           if(is_trump($b))
57             return 0;
58           else
59             {
60               /* both clubs? */
61               if( is_club($a) && is_club($b))
62                 if($a<=$b)
63                   return 1;
64                 else
65                   return 0;
66               /* both spade? */
67               if( is_spade($a) && is_spade($b))
68                 if($a<=$b)
69                   return 1;
70                 else
71                   return 0;
72               /* both heart? */
73               if( is_heart($a) && is_heart($b))
74                 if($a<=$b)
75                   return 1;
76                 else
77                   return 0;
78               return 1;
79             }     
80         }
81     }
82
83
84 function get_winner($p,$mode)
85 {
86   /* get all 4 cards played in a trick */
87   $c1 = $p[1];
88   $c2 = $p[2];
89   $c3 = $p[3];
90   $c4 = $p[4];
91
92   /* find out who won */
93   if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
94     return 1;
95   if( compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
96     return 2;
97   if( compare_cards($c3,$c4,$mode) )
98     return 3;
99   return 4;
100 }
101
102 function count_nines($cards)
103 {
104   $nines = 0;
105
106   foreach($cards as $c)
107     {
108       if($c == "25" || $c == "26") $nines++;
109       else if($c == "33" || $c == "34") $nines++;
110       else if($c == "41" || $c == "42") $nines++;
111       else if($c == "47" || $c == "48") $nines++;
112     }
113   
114   return $nines;
115 }
116
117 function check_wedding($cards)
118 {
119
120   if( in_array("3",$cards) && in_array("2",$cards) )
121     return 1;
122
123   return 0;
124 }
125
126 function count_trump($cards)
127 {
128   $trump = 0;
129
130   /* count each trump */
131   foreach($cards as $c)
132     if( (int)($c) <27) 
133       $trump++;
134
135   /* subtract foxes */
136   if( in_array("19",$cards))
137     $trump--;
138   if( in_array("20",$cards) )
139     $trump--;
140   /* add one, in case the player has both foxes (schweinchen) */
141   if( in_array("19",$cards) && in_array("20",$cards) )
142     $trump++;
143
144   return $trump;
145 }
146
147 function card_to_name($card)
148 {
149   switch($card)
150     {
151       case 1:
152       case 2:
153         return "ten of hearts";
154       case 3:
155       case 4:
156       return "queen of clubs";
157       case 5:
158       case 6:
159       return "queen of spades";
160       case 7:
161       case 8:
162       return "queen of hearts";
163       case 9:
164       case 10:
165       return "queen of diamonds";
166       case 11:
167       case 12:
168       return "jack of clubs";
169       case 13:
170       case 14:
171       return "jack of spades";
172       case 15:
173       case 16:
174       return "jack of hearts";
175       case 17:
176       case 18:
177       return "jack of diamonds";
178       case 19:
179       case 20:
180       return "ace of diamonds";
181       case 21:
182       case 22:
183       return "ten of diamonds";
184       case 23:
185       case 24:
186       return "king of diamonds";
187       case 25:
188       case 26:
189       return "nine of diamonds";;
190       case 27:
191       case 28:
192       return "ace of clubs";
193       case 29:
194       case 30:
195       return "ten of clubs";
196       case 31:
197       case 32:
198       return "king of clubs";
199       case 33:
200       case 34:
201       return "nine of clubs";
202       case 35:
203       case 36:
204       return "ace of spades";
205       case 37:
206       case 38:
207       return "ten of spades";
208       case 39:
209       case 40:
210       return "king of spades";
211       case 41:
212       case 42:
213       return "nine of spades";
214       case 43:
215       case 44:
216       return "ace of hearts";
217       case 45:
218       case 46:
219       return "king of hearts";
220       case 47:
221       case 48:
222       return "nine of hearts";
223       default:
224       return "something went wrong, please contact the admin. Error: code1.";
225     }
226 }
227
228 function card_value($card)
229 {
230   switch($card)
231     {
232     case 1:      /* heart */
233     case 2:
234       return 10;
235     case 3:     /* clubes */     
236     case 4:                      
237     case 5:     /* spades */     
238     case 6:                      
239     case 7:     /* hearts */     
240     case 8:                      
241     case 9:     /* diamonds */   
242     case 10:                     
243       return 3;
244     case 11:    /* clubes */     
245     case 12:                     
246     case 13:    /* spades */     
247     case 14:                     
248     case 15:    /* hearts */     
249     case 16:                     
250     case 17:    /* diamonds */   
251     case 18:
252       return 2;                  
253     case 19:    /* diamonds */ 
254     case 20:                   
255     case 27:    /* clubs */    
256     case 28:                   
257     case 35:    /* spades */   
258     case 36:                   
259     case 43:    /* hearts */   
260     case 44:                   
261       return 11;
262     case 21:    /* diamonds */    
263     case 22:
264     case 29:    /* clubs */
265     case 30:
266     case 37:    /* spades */
267     case 38:
268       return 10;
269     case 23:    /* diamonds */ 
270     case 24:                   
271     case 31:    /* clubs */    
272     case 32:                   
273     case 39:    /* spades */   
274     case 40:                   
275     case 45:    /* hearts */   
276     case 46:                   
277       return 4;
278     case 25:    /* diamonds */   
279     case 26:                   
280     case 33:    /* clubs */    
281     case 34:                   
282     case 41:    /* spades */   
283     case 42:                   
284     case 47:    /* hearts */   
285     case 48:                   
286       return 0;
287     default:
288       echo "something went wrong, please contact the admin. ErrorCode: 2<br>";
289       return 0;
290     }
291 }
292
293
294 function  create_array_of_random_numbers()
295 {
296   $r = array();
297   $a = array();
298   
299   for($i=1;$i<49;$i++)
300     $a[$i]=$i;
301   
302   $r = array_rand($a,48);
303    
304   return $r;
305 }
306
307
308
309
310 function display_cards($me,$myturn)
311 {
312   return;
313 }
314
315 function return_timezone($offset)
316 {
317   switch($offset)
318     {
319     case '1':
320       $zone = "Europe/Berlin";
321       break;
322     case '-8':
323       $zone = "America/Vancouver";
324       break;
325     case '13':
326       $zone = "Pacific/Auckland";
327       break;
328     default:
329       $zone = "Europe/London";
330     }
331   
332   return $zone;
333 }
334
335 ?>