CLEANUP: working on database now a config.php option
[e-DoKo.git] / functions.php
1 <?php
2
3 function config_check()
4 {
5   global $EmailName,$EMAIL_REPLY,$ADMIN_NAME,$ADMIN_EMAIL,$DB_work;
6
7   /* check if some variables are set in the config file, else set defaults */
8   if(!isset($EmailName))
9     $EmailName="[DoKo] ";
10   if(isset($EMAIL_REPLY))
11     {
12       ini_set("sendmail_from",$EMAIL_REPLY);
13     }
14   if(!isset($ADMIN_NAME))
15     {
16       output_header();
17       echo "<h1>Setup not completed</h1>";
18       echo "You need to set \$ADMIN_NAME in config.php.";
19       output_footer(); 
20       exit(); 
21     }    
22   if(!isset($ADMIN_EMAIL))
23     {
24       output_header();
25       echo "<h1>Setup not completed</h1>";
26       echo "You need to set \$ADMIN_EMAIL in config.php. ".
27         "If something goes wrong an email will be send to this address.";
28       output_footer(); 
29       exit(); 
30     }
31   if(!isset($DB_work))
32     {
33       output_header();
34       echo "<h1>Setup not completed</h1>";
35       echo "You need to set \$DB_work in config.php. ".
36         "If this is set to 1, the game will be suspended and one can work safely on the database.".
37         "The default should be 0 for the game to work.";
38       output_footer(); 
39       exit(); 
40     }
41   if($DB_work) 
42     {
43       output_header();
44       echo "Working on the database...please check back later."; 
45       output_footer(); 
46       exit(); 
47     }
48   
49   return;
50 }
51
52 function mymail($To,$Subject,$message,$header="")
53 {  
54   global $debug;
55
56   if($debug)
57     {
58       $message = str_replace("\n","<br />\n",$message);
59       $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
60                      "<a href=\"\\0\">\\0</a>", $message);
61       
62       echo "<br />To: $To<br />";
63       if($header != "") 
64         echo $header."<br />";
65       echo "Subject: $Subject <br />$message<br />\n";
66     }
67   else
68     if($header != "")
69       mail($To,$Subject,$message,$header);
70     else
71       mail($To,$Subject,$message);
72   return;
73 }
74
75 function myisset()
76 {
77   /* returns 1 if all names passed as args are defined by a GET or POST statement,
78    * else return 0
79    */
80   
81   $ok   = 1;
82   $args = func_get_args();
83   
84   foreach($args as $arg)
85     {
86       $ok = $ok * isset($_REQUEST[$arg]);
87       /*echo "$arg: ok = $ok <br />";
88        */ 
89     }
90   return $ok;
91 }
92
93 function myerror($message)
94 {
95   echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
96   mymail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
97   return;
98 }
99
100 function pos_array($c,$arr)
101 {
102   $ret = 0;
103   
104   $i   = 0;
105   foreach($arr as $a)
106     {
107       $i++;
108       if($a == $c)
109         {
110           $ret = $i;
111           break;
112         }
113     }
114   return $ret;
115 }
116
117 function is_trump($c) 
118
119   global $CARDS;
120
121   if(in_array($c,$CARDS["trump"]))
122     return 1;
123   else 
124     return 0;
125 }
126
127 function is_same_suite($c1,$c2) 
128 {
129   global $CARDS;
130   
131   if(in_array($c1,$CARDS["trump"]   ) && in_array($c2,$CARDS["trump"]   ) ) return 1;
132   if(in_array($c1,$CARDS["clubs"]   ) && in_array($c2,$CARDS["clubs"]   ) ) return 1;
133   if(in_array($c1,$CARDS["hearts"]  ) && in_array($c2,$CARDS["hearts"]  ) ) return 1;
134   if(in_array($c1,$CARDS["spades"]  ) && in_array($c2,$CARDS["spades"]  ) ) return 1;
135   if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
136   
137   return 0;
138 }
139
140 function compare_cards($a,$b,$game)
141 {
142   /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
143
144   global $CARDS;
145   global $RULES;
146   global $GAME;
147
148   /* first map all cards to the odd number, 
149    * this insure that the first card wins the trick 
150    * if they are the same card
151    */
152   if( $a/2 - (int)($a/2) != 0.5)
153     $a--;
154   if( $b/2 - (int)($b/2) != 0.5)
155     $b--;
156
157   /* some special cases */
158   switch($game)
159     {
160     case "normal":
161     case "silent":
162       if($RULES["schweinchen"]=="both" && $GAME["schweinchen"])
163         {
164           if($a == 19 || $a == 20 )
165             return 1;
166           if($b == 19 || $b == 20 )
167             return 0;
168         };
169       if($RULES["schweinchen"]=="second" && $GAME["schweinchen"]==3)
170         {
171           if($a == 19 || $a == 20 )
172             return 1;
173           if($b == 19 || $b == 20 )
174             return 0;
175         };
176     case "trump":
177     case "heart":
178     case "spade":
179     case "club":
180       if($RULES["dullen"]=="secondwins")
181         if($a==1 && $b==1) /* both 10 of hearts */
182           return 0;        /* second one wins.*/
183     }
184   
185   /* normal case */
186   if(is_trump($a) && is_trump($b) && $a<=$b)
187     return 1;
188   else if(is_trump($a) && is_trump($b) )
189     return 0;
190   else 
191     { /*$a is not a trump */
192       if(is_trump($b))
193         return 0;
194       else
195         { /* both no trump */
196
197           /* both clubs? */
198           $posA = pos_array($a,$CARDS["clubs"]);
199           $posB = pos_array($b,$CARDS["clubs"]);
200           if($posA && $posB)
201             if($posA <= $posB)
202               return 1;
203             else
204               return 0;
205
206           /* both spades? */
207           $posA = pos_array($a,$CARDS["spades"]);
208           $posB = pos_array($b,$CARDS["spades"]);
209           if($posA && $posB)
210             if($posA <= $posB)
211               return 1;
212             else
213               return 0;
214
215           /* both hearts? */
216           $posA = pos_array($a,$CARDS["hearts"]);
217           $posB = pos_array($b,$CARDS["hearts"]);
218           if($posA && $posB)
219             if($posA <= $posB)
220               return 1;
221             else
222               return 0;
223
224           /* both diamonds? */
225           $posA = pos_array($a,$CARDS["diamonds"]);
226           $posB = pos_array($b,$CARDS["diamonds"]);
227           if($posA && $posB)
228             if($posA <= $posB)
229               return 1;
230             else
231               return 0;
232           
233           /* not the same suit and no trump: a wins */
234           return 1;
235         }         
236     }
237
238
239 function get_winner($p,$mode)
240 {
241   /* get all 4 cards played in a trick, in the order they are played */
242   $tmp = $p[1];
243   $c1    = $tmp["card"];
244   $c1pos = $tmp["pos"]; 
245
246   $tmp = $p[2];
247   $c2    = $tmp["card"];
248   $c2pos = $tmp["pos"]; 
249
250   $tmp = $p[3];
251   $c3    = $tmp["card"];
252   $c3pos = $tmp["pos"]; 
253
254   $tmp = $p[4];
255   $c4    = $tmp["card"];
256   $c4pos = $tmp["pos"]; 
257
258   /* first card is better than all the rest */
259   if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
260     return $c1pos; 
261
262   /* second card is better than first and better than the rest */
263   if( !compare_cards($c1,$c2,$mode) &&  compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
264     return $c2pos;
265
266   /* third card is better than first card and better than last */
267   if( !compare_cards($c1,$c3,$mode) &&  compare_cards($c3,$c4,$mode) )
268     /* if second card is better than first, third card needs to be even better */
269     if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
270       return $c3pos;
271     /* second is worse than first, e.g. not following suite */
272     else if (compare_cards($c1,$c2,$mode) )
273       return $c3pos;
274
275   /* non of the above */
276   return $c4pos;
277 }
278
279 function count_nines($cards)
280 {
281   $nines = 0;
282
283   foreach($cards as $c)
284     {
285       if($c == "25" || $c == "26") $nines++;
286       else if($c == "33" || $c == "34") $nines++;
287       else if($c == "41" || $c == "42") $nines++;
288       else if($c == "47" || $c == "48") $nines++;
289     }
290   
291   return $nines;
292 }
293
294 function check_wedding($cards)
295 {
296
297   if( in_array("3",$cards) && in_array("4",$cards) )
298     return 1;
299
300   return 0;
301 }
302
303 function count_trump($cards)
304 {
305   global $RULES;
306
307   $trump = 0;
308
309   /* count each trump */
310   foreach($cards as $c)
311     if( (int)($c) <27) 
312       $trump++;
313
314   switch($RULES["schweinchen"])
315     {
316     case "none":
317       break;
318     case "second":
319     case "secondaftercall":
320       /* add one, in case the player has both foxes (schweinchen) */
321       if( in_array("19",$cards) && in_array("20",$cards) )
322         $trump++;
323     case "both":
324       /* subtract foxes */
325       if( in_array("19",$cards))
326         $trump--;
327       if( in_array("20",$cards) )
328         $trump--;
329       break;
330     }
331
332   return $trump;
333 }
334
335 function  create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
336 {
337   global $debug;
338
339   $r = array();
340   
341   if($debug)
342     {
343       $r[ 0]=1;     $r[12]=47;   $r[24]=13;       $r[36]=37;
344       $r[ 1]=2;     $r[13]=48;   $r[25]=14;       $r[37]=38;
345       $r[ 2]=3;     $r[14]=27;   $r[26]=15;       $r[38]=39;
346       $r[ 3]=4;     $r[15]=16;   $r[27]=28;       $r[39]=40;
347       $r[ 4]=5;     $r[16]=17;   $r[28]=29;       $r[40]=41;
348       $r[ 5]=18;    $r[17]=6;    $r[29]=30;       $r[41]=42;
349       $r[ 6]=19;    $r[18]=7;    $r[30]=31;       $r[42]=43;
350       $r[ 7]=20;    $r[19]=8;    $r[31]=32;       $r[43]=44;
351       $r[ 8]=45;    $r[20]=9;    $r[32]=21;       $r[44]=33;
352       $r[ 9]=46;    $r[21]=10;   $r[33]=22;       $r[45]=34;
353       $r[10]=35;    $r[22]=11;   $r[34]=23;       $r[46]=25;
354       $r[11]=36;    $r[23]=12;   $r[35]=24;       $r[47]=26;
355     }
356   else
357     {
358       /* check if we can find a game were non of the player was involved and return 
359        * cards insted 
360        */
361       $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
362       $randomnumbers = DB_get_unused_randomnumbers($userstr);
363       $randomnumbers = explode(":",$randomnumbers);
364       
365       if(sizeof($randomnumbers)==48)
366         return $randomnumbers;
367       
368       /* need to create new numbers */
369       for($i=0;$i<48;$i++)
370         $r[$i]=$i+1;
371       
372       /* shuffle using a better random generator than the standard one */
373       for ($i = 0; $i <48; $i++)
374         {
375           $j = @mt_rand(0, $i);
376           $tmp = $r[$i];
377           $r[$i] = $r[$j];
378           $r[$j] = $tmp;
379         }
380     };
381
382   return $r;
383 }
384
385 function display_cards($me,$myturn)
386 {
387   return;
388 }
389
390 function return_timezone($offset)
391 {
392   switch($offset)
393     {
394     case '1':
395       $zone = "Europe/Berlin";
396       break;
397     case '-8':
398       $zone = "America/Vancouver";
399       break;
400     case '13':
401       $zone = "Pacific/Auckland";
402       break;
403     default:
404       $zone = "Europe/London";
405     }
406   
407   return $zone;
408 }
409
410 function have_suit($cards,$c)
411 {
412   global $CARDS;
413   $suite = array();
414
415   if(in_array($c,$CARDS["trump"]))
416     $suite = $CARDS["trump"];
417   else if(in_array($c,$CARDS["clubs"]))
418     $suite = $CARDS["clubs"];
419   else if(in_array($c,$CARDS["spades"]))
420     $suite = $CARDS["spades"];
421   else if(in_array($c,$CARDS["hearts"]))
422     $suite = $CARDS["hearts"];
423   else if(in_array($c,$CARDS["diamonds"]))
424     $suite = $CARDS["diamonds"];
425
426   foreach($cards as $card)
427     {
428       if(in_array($card,$suite))
429         return 1;
430     }
431
432   return 0;
433 }
434
435 function same_type($card,$c)
436 {
437   global $CARDS;
438   $suite = "";
439
440   /* figure out what kind of card c is */
441   if(in_array($c,$CARDS["trump"]))
442     $suite = $CARDS["trump"];
443   else if(in_array($c,$CARDS["clubs"]))
444     $suite = $CARDS["clubs"];
445   else if(in_array($c,$CARDS["spades"]))
446     $suite = $CARDS["spades"];
447   else if(in_array($c,$CARDS["hearts"]))
448     $suite = $CARDS["hearts"];
449   else if(in_array($c,$CARDS["diamonds"]))
450     $suite = $CARDS["diamonds"];
451
452   /* card is the same suid return 1 */ 
453   if(in_array($card,$suite))
454     return 1;
455   
456   return 0;
457 }
458
459 function set_gametype($gametype)
460 {
461   global $CARDS;
462   global $RULES;
463
464   switch($gametype)
465     {
466     case "normal":
467     case "wedding":
468     case "poverty":
469     case "dpoverty":
470     case "trump":
471     case "silent":
472       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16', 
473                                  '17','18','19','20','21','22','23','24','25','26');
474       $CARDS["diamonds"] = array();
475       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
476       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
477       $CARDS["hearts"]   = array('43','44','45','46','47','48');
478       $CARDS["foxes"]    = array('19','20');
479       if($RULES["dullen"]=='none')
480         {
481           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16', 
482                                      '17','18','19','20','21','22','23','24','25','26');
483           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
484         }
485       break;
486     case "queen":
487       $CARDS["trump"]    = array('3','4','5','6','7','8','9','10');
488       $CARDS["clubs"]    = array('27','28','29','30','31','32','11','12','33','34');
489       $CARDS["spades"]   = array('35','36','37','38','39','40','13','14','41','42');
490       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','15','16','47','48');
491       $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
492       $CARDS["foxes"]    = array();
493       break;
494     case "jack":
495       $CARDS["trump"]    = array('11','12','13','14','15','16','17','18');
496       $CARDS["clubs"]    = array('27','28','29','30','31','32','3', '4','33','34');
497       $CARDS["spades"]   = array('35','36','37','38','39','40','5', '6','41','42');
498       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','7', '8','47','48');
499       $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
500       $CARDS["foxes"]    = array();
501       break;
502     case "trumpless":
503       $CARDS["trump"]    = array();
504       $CARDS["clubs"]    = array('27','28','29','30','31','32','3', '4','11','12','33','34');
505       $CARDS["spades"]   = array('35','36','37','38','39','40','5', '6','13','14','41','42');
506       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
507       $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
508       $CARDS["foxes"]    = array();
509       break;
510     case "club":
511       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16', 
512                                  '17','18','27','28','29','30','31','32','33','34');
513       $CARDS["clubs"]    = array();
514       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
515       $CARDS["hearts"]   = array('43','44','45','46','47','48');
516       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
517       $CARDS["foxes"]    = array();
518       if($RULES["dullen"]=='none')
519         {
520           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16', 
521                                      '17','18','27','28','29','30','31','32','33','34');
522           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
523         }
524       break;
525     case "spade":
526       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16', 
527                                  '17','18','35','36','37','38','39','40','41','42');
528       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
529       $CARDS["spades"]   = array();
530       $CARDS["hearts"]   = array('43','44','45','46','47','48');
531       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
532       $CARDS["foxes"]    = array();
533       if($RULES["dullen"]=='none')
534         {
535           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16', 
536                                      '17','18','35','36','37','38','39','40','41','42');
537           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
538         }
539       break;
540     case "heart":
541       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16', 
542                                  '17','18','43','44','45','46','47','48');
543       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
544       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
545       $CARDS["hearts"]   = array();
546       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
547       $CARDS["foxes"]    = array();
548       if($RULES["dullen"]=='none')
549         {
550           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16', 
551                             '17','18','43','44','1','2','45','46','47','48');
552         }
553       break;
554     }
555 }
556
557 function mysort($cards,$gametype)
558 {
559   usort ( $cards, "sort_comp" );
560   return $cards;
561 }
562
563 function sort_comp($a,$b)
564 {
565   global $CARDS;
566
567   $ALL = array();
568   $ALL = array_merge($CARDS["trump"],$CARDS["diamonds"],$CARDS["clubs"],
569                      $CARDS["hearts"],$CARDS["spades"],$CARDS["diamonds"]);
570
571   return pos_array($a,$ALL)-pos_array($b,$ALL);
572 }
573
574 function can_call($what,$hash)
575 {
576   global $RULES;
577
578   $gameid   = DB_get_gameid_by_hash($hash);
579   $gametype = DB_get_gametype_by_gameid($gameid);
580   $oldcall  = DB_get_call_by_hash($hash);
581   $pcall    = DB_get_partner_call_by_hash($hash);
582
583   if( ($pcall!=NULL && $what >= $pcall) || 
584       ($oldcall!=NULL && $what >=$oldcall) )
585     {
586       return 0;
587     }
588
589   $NRcards  = count(DB_get_hand($hash));
590   
591   $NRallcards = 0;
592   for ($i=1;$i<5;$i++)
593     {
594       $user         = DB_get_hash_from_game_and_pos($gameid,$i);
595       $NRallcards  += count(DB_get_hand($user));
596     };
597   
598   /* in case of a wedding, everything will be delayed by an offset */
599   $offset = 0;
600   if($gametype=="wedding")
601     {
602       $offset = DB_get_sickness_by_gameid($gameid); 
603       if ($offset <0) /* not resolved */
604         return 0;
605     };
606
607   switch ($RULES["call"])
608     {
609     case "1st-own-card":
610       if( 4-($what/30) >= 12 - ($NRcards + $offset))
611         return 1;
612       break;
613     case "5th-card":
614       if( 27+4*($what/30) <= $NRallcards + $offset*4)
615         return 1;
616       break;
617     case "9-cards":
618       
619       if($oldcall!=NULL && $pcall!=NULL)
620         $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
621       else if($oldcall!=NULL)
622         $mincall = $oldcall;
623       else if ($pcall!=NULL)
624         $mincall = $pcall;
625       else
626         $mincall = -1;
627
628       if( 12 <= ($NRcards + $offset))
629         {
630           return 1;
631         }
632       else if ( 9 <= ($NRcards + $offset))
633         {
634           if( ($mincall>=0 && $mincall==120) )
635             return 1;
636         }
637       else if ( 6 <= ($NRcards + $offset))
638         {
639           if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
640             return 1;
641         }
642       else if ( 3 <= ($NRcards + $offset))
643         {
644           if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
645             return 1;
646         }
647       else if ( 0 <= ($NRcards + $offset))
648         {
649           if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
650             return 1;
651         };
652       break;
653     }
654
655   return 0;
656 }
657
658 ?>