BUGFIX: some variables where not defined after moving some code around
[e-DoKo.git] / include / functions.php
1 <?php
2 /* make sure that we are not called from outside the scripts, 
3  * use a variable defined in config.php to check this
4  */
5 if(!isset($HOST))
6   exit;
7
8 function config_check()
9 {
10   global $EmailName,$EMAIL_REPLY,$ADMIN_NAME,$ADMIN_EMAIL,$DB_work;
11
12   /* check if some variables are set in the config file, else set defaults */
13   if(!isset($EmailName))
14     $EmailName="[DoKo] ";
15   if(isset($EMAIL_REPLY))
16     {
17       ini_set("sendmail_from",$EMAIL_REPLY);
18     }
19   if(!isset($ADMIN_NAME))
20     {
21       output_header();
22       echo "<h1>Setup not completed</h1>";
23       echo "You need to set \$ADMIN_NAME in config.php.";
24       output_footer();
25       exit();
26     }
27   if(!isset($ADMIN_EMAIL))
28     {
29       output_header();
30       echo "<h1>Setup not completed</h1>";
31       echo "You need to set \$ADMIN_EMAIL in config.php. ".
32         "If something goes wrong an email will be send to this address.";
33       output_footer();
34       exit();
35     }
36   if(!isset($DB_work))
37     {
38       output_header();
39       echo "<h1>Setup not completed</h1>";
40       echo "You need to set \$DB_work in config.php. ".
41         "If this is set to 1, the game will be suspended and one can work safely on the database.".
42         "The default should be 0 for the game to work.";
43       output_footer();
44       exit();
45     }
46   if($DB_work)
47     {
48       output_header();
49       echo "Working on the database...please check back later.";
50       output_footer();
51       exit();
52     }
53
54   return;
55 }
56
57 function mymail($To,$Subject,$message,$header="")
58 {
59   global $debug,$EMAIL_REPLY;
60
61   if(isset($EMAIL_REPLY))
62     $header .= "From: e-DoKo daemon <$EMAIL_REPLY>\r\n";
63
64   if($debug)
65     {
66       /* display email on screen,
67        * change txt -> html
68        */
69       $message = str_replace("\n","<br />\n",$message);
70       $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
71                      "<a href=\"\\0\">\\0</a>", $message);
72
73       echo "<br />To: $To<br />";
74       if($header != "")
75         echo $header."<br />";
76       echo "Subject: $Subject <br />$message<br />\n";
77     }
78   else
79     if($header != "")
80       mail($To,$Subject,$message,$header);
81     else
82       mail($To,$Subject,$message);
83   return;
84 }
85
86 function myisset()
87 {
88   /* returns 1 if all names passed as args are defined by a GET or POST statement,
89    * else return 0
90    */
91
92   $ok   = 1;
93   $args = func_get_args();
94
95   foreach($args as $arg)
96     {
97       $ok = $ok * isset($_REQUEST[$arg]);
98       /*echo "$arg: ok = $ok <br />";
99        */
100     }
101   return $ok;
102 }
103
104 function myerror($message)
105 {
106   echo "<span class=\"error\">".htmlspecialchars($message)."</span>\n";
107   mymail($ADMIN_EMAIL,$EmailName." Error in Code",$message);
108   return;
109 }
110
111 function pos_array($c,$arr)
112 {
113   $ret = 0;
114
115   $i   = 0;
116   foreach($arr as $a)
117     {
118       $i++;
119       if($a == $c)
120         {
121           $ret = $i;
122           break;
123         }
124     }
125   return $ret;
126 }
127
128 function is_trump($c)
129 {
130   global $CARDS;
131
132   if(in_array($c,$CARDS["trump"]))
133     return 1;
134   else
135     return 0;
136 }
137
138 function is_same_suite($c1,$c2)
139 {
140   global $CARDS;
141
142   if(in_array($c1,$CARDS["trump"]   ) && in_array($c2,$CARDS["trump"]   ) ) return 1;
143   if(in_array($c1,$CARDS["clubs"]   ) && in_array($c2,$CARDS["clubs"]   ) ) return 1;
144   if(in_array($c1,$CARDS["hearts"]  ) && in_array($c2,$CARDS["hearts"]  ) ) return 1;
145   if(in_array($c1,$CARDS["spades"]  ) && in_array($c2,$CARDS["spades"]  ) ) return 1;
146   if(in_array($c1,$CARDS["diamonds"]) && in_array($c2,$CARDS["diamonds"]) ) return 1;
147
148   return 0;
149 }
150
151 function compare_cards($a,$b,$game)
152 {
153   /* if "a" is higher than "b" return 1, else 0, "a" being the card first played */
154
155   global $CARDS;
156   global $RULES;
157   global $GAME;
158
159   /* first map all cards to the odd number,
160    * this insure that the first card wins the trick
161    * if they are the same card
162    */
163   if( $a/2 - (int)($a/2) != 0.5)
164     $a--;
165   if( $b/2 - (int)($b/2) != 0.5)
166     $b--;
167
168   /* check for schweinchen and ten of hearts*/
169   switch($game)
170     {
171     case "normal":
172     case "silent":
173     case "trump":
174       if($RULES["schweinchen"]=="both" && $GAME["schweinchen"])
175         {
176           if($a == 19 || $a == 20 )
177             return 1;
178           if($b == 19 || $b == 20 )
179             return 0;
180         };
181       if($RULES["schweinchen"]=="second" && $GAME["schweinchen"]==3)
182         {
183           if($a == 19 || $a == 20 )
184             return 1;
185           if($b == 19 || $b == 20 )
186             return 0;
187         };
188     case "heart":
189     case "spade":
190     case "club":
191       /* check for ten of hearts rule */
192       if($RULES["dullen"]=="secondwins")
193         if($a==1 && $b==1) /* both 10 of hearts */
194           return 0;        /* second one wins.*/
195     case "trumpless":
196     case "jack":
197     case "queen":
198       /* no special cases here */
199     }
200
201   /* normal case */
202   if(is_trump($a) && is_trump($b) && $a<=$b)
203     return 1;
204   else if(is_trump($a) && is_trump($b) )
205     return 0;
206   else
207     { /*$a is not a trump */
208       if(is_trump($b))
209         return 0;
210       else
211         { /* both no trump */
212
213           /* both clubs? */
214           $posA = pos_array($a,$CARDS["clubs"]);
215           $posB = pos_array($b,$CARDS["clubs"]);
216           if($posA && $posB)
217             if($posA <= $posB)
218               return 1;
219             else
220               return 0;
221
222           /* both spades? */
223           $posA = pos_array($a,$CARDS["spades"]);
224           $posB = pos_array($b,$CARDS["spades"]);
225           if($posA && $posB)
226             if($posA <= $posB)
227               return 1;
228             else
229               return 0;
230
231           /* both hearts? */
232           $posA = pos_array($a,$CARDS["hearts"]);
233           $posB = pos_array($b,$CARDS["hearts"]);
234           if($posA && $posB)
235             if($posA <= $posB)
236               return 1;
237             else
238               return 0;
239
240           /* both diamonds? */
241           $posA = pos_array($a,$CARDS["diamonds"]);
242           $posB = pos_array($b,$CARDS["diamonds"]);
243           if($posA && $posB)
244             if($posA <= $posB)
245               return 1;
246             else
247               return 0;
248
249           /* not the same suit and no trump: a wins */
250           return 1;
251         }
252     }
253 }
254
255 function get_winner($p,$mode)
256 {
257   /* get all 4 cards played in a trick, in the order they are played */
258   $tmp = $p[1];
259   $c1    = $tmp["card"];
260   $c1pos = $tmp["pos"];
261
262   $tmp = $p[2];
263   $c2    = $tmp["card"];
264   $c2pos = $tmp["pos"];
265
266   $tmp = $p[3];
267   $c3    = $tmp["card"];
268   $c3pos = $tmp["pos"];
269
270   $tmp = $p[4];
271   $c4    = $tmp["card"];
272   $c4pos = $tmp["pos"];
273
274   /* first card is better than all the rest */
275   if( compare_cards($c1,$c2,$mode) && compare_cards($c1,$c3,$mode) && compare_cards($c1,$c4,$mode) )
276     return $c1pos;
277
278   /* second card is better than first and better than the rest */
279   if( !compare_cards($c1,$c2,$mode) &&  compare_cards($c2,$c3,$mode) && compare_cards($c2,$c4,$mode) )
280     return $c2pos;
281
282   /* third card is better than first card and better than last */
283   if( !compare_cards($c1,$c3,$mode) &&  compare_cards($c3,$c4,$mode) )
284     /* if second card is better than first, third card needs to be even better */
285     if( !compare_cards($c1,$c2,$mode) && !compare_cards($c2,$c3,$mode) )
286       return $c3pos;
287     /* second is worse than first, e.g. not following suite */
288     else if (compare_cards($c1,$c2,$mode) )
289       return $c3pos;
290
291   /* non of the above */
292   return $c4pos;
293 }
294
295 function count_nines($cards)
296 {
297   $nines = 0;
298
299   foreach($cards as $c)
300     {
301       if($c == "25" || $c == "26") $nines++;
302       else if($c == "33" || $c == "34") $nines++;
303       else if($c == "41" || $c == "42") $nines++;
304       else if($c == "47" || $c == "48") $nines++;
305     }
306
307   return $nines;
308 }
309
310 function check_wedding($cards)
311 {
312
313   if( in_array("3",$cards) && in_array("4",$cards) )
314     return 1;
315
316   return 0;
317 }
318
319 function count_trump($cards)
320 {
321   global $RULES;
322
323   $trump = 0;
324
325   /* count each trump, including the foxes */
326   foreach($cards as $c)
327     if( (int)($c) <27)
328       $trump++;
329
330   /* normally foxes don't count as trump, so we substract them here
331    * in case someone has schweinchen, one or two of them should count as trump 
332    * though, so we need to add one trump for those cases */
333   
334   /* subtract foxes */
335   if( in_array("19",$cards))
336     $trump--;
337   if( in_array("20",$cards) )
338     $trump--;
339
340   /* handle case where player has schweinchen */
341   if( in_array("19",$cards) && in_array("20",$cards) )
342     switch($RULES["schweinchen"])
343       {
344       case "both":
345         /* add two, in case the player has both foxes (schweinchen) */
346         $trump++;
347         $trump++;
348         break;
349       case "second":
350       case "secondaftercall":
351         /* add one, in case the player has both foxes (schweinchen) */
352         $trump++;
353         break;
354       case "none":
355         break;
356       }
357
358   return $trump;
359 }
360
361 function  create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
362 {
363   global $debug;
364
365   $r = array();
366
367   if($debug)
368     {
369       $r[ 0]=1;     $r[12]=47;   $r[24]=13;       $r[36]=37;
370       $r[ 1]=2;     $r[13]=23;   $r[25]=14;       $r[37]=38;
371       $r[ 2]=3;     $r[14]=27;   $r[26]=15;       $r[38]=39;
372       $r[ 3]=4;     $r[15]=16;   $r[27]=28;       $r[39]=40;
373       $r[ 4]=5;     $r[16]=17;   $r[28]=29;       $r[40]=41;
374       $r[ 5]=18;    $r[17]=6;    $r[29]=30;       $r[41]=42;
375       $r[ 6]=21;    $r[18]=7;    $r[30]=31;       $r[42]=43;
376       $r[ 7]=22;    $r[19]=8;    $r[31]=32;       $r[43]=44;
377       $r[ 8]=45;    $r[20]=9;    $r[32]=19;       $r[44]=33;
378       $r[ 9]=46;    $r[21]=10;   $r[33]=20;       $r[45]=24;
379       $r[10]=35;    $r[22]=11;   $r[34]=48;       $r[46]=25;
380       $r[11]=36;    $r[23]=12;   $r[35]=34;       $r[47]=26;
381     }
382   else
383     {
384       /* check if we can find a game were non of the player was involved and return
385        * cards insted
386        */
387       $userstr = "'".implode("','",array($useridA,$useridB,$useridC,$useridD))."'";
388       $randomnumbers = DB_get_unused_randomnumbers($userstr);
389       $randomnumbers = explode(":",$randomnumbers);
390
391       if(sizeof($randomnumbers)==48)
392         return $randomnumbers;
393
394       /* need to create new numbers */
395       for($i=0;$i<48;$i++)
396         $r[$i]=$i+1;
397
398       /* shuffle using a better random generator than the standard one */
399       for ($i = 0; $i <48; $i++)
400         {
401           $j = @mt_rand(0, $i);
402           $tmp = $r[$i];
403           $r[$i] = $r[$j];
404           $r[$j] = $tmp;
405         }
406     };
407
408   return $r;
409 }
410
411 function display_cards($me,$myturn)
412 {
413   return;
414 }
415
416 function have_suit($cards,$c)
417 {
418   global $CARDS;
419   $suite = array();
420
421   if(in_array($c,$CARDS["trump"]))
422     $suite = $CARDS["trump"];
423   else if(in_array($c,$CARDS["clubs"]))
424     $suite = $CARDS["clubs"];
425   else if(in_array($c,$CARDS["spades"]))
426     $suite = $CARDS["spades"];
427   else if(in_array($c,$CARDS["hearts"]))
428     $suite = $CARDS["hearts"];
429   else if(in_array($c,$CARDS["diamonds"]))
430     $suite = $CARDS["diamonds"];
431
432   foreach($cards as $card)
433     {
434       if(in_array($card,$suite))
435         return 1;
436     }
437
438   return 0;
439 }
440
441 function same_type($card,$c)
442 {
443   global $CARDS;
444   $suite = "";
445
446   /* figure out what kind of card c is */
447   if(in_array($c,$CARDS["trump"]))
448     $suite = $CARDS["trump"];
449   else if(in_array($c,$CARDS["clubs"]))
450     $suite = $CARDS["clubs"];
451   else if(in_array($c,$CARDS["spades"]))
452     $suite = $CARDS["spades"];
453   else if(in_array($c,$CARDS["hearts"]))
454     $suite = $CARDS["hearts"];
455   else if(in_array($c,$CARDS["diamonds"]))
456     $suite = $CARDS["diamonds"];
457
458   /* card is the same suid return 1 */
459   if(in_array($card,$suite))
460     return 1;
461
462   return 0;
463 }
464
465 function set_gametype($gametype)
466 {
467   global $CARDS;
468   global $RULES;
469
470   switch($gametype)
471     {
472     case "normal":
473     case "wedding":
474     case "poverty":
475     case "dpoverty":
476     case "trump":
477     case "silent":
478       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
479                                  '17','18','19','20','21','22','23','24','25','26');
480       $CARDS["diamonds"] = array();
481       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
482       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
483       $CARDS["hearts"]   = array('43','44','45','46','47','48');
484       $CARDS["foxes"]    = array('19','20');
485       if($RULES["dullen"]=='none')
486         {
487           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
488                                      '17','18','19','20','21','22','23','24','25','26');
489           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
490         }
491       break;
492     case "queen":
493       $CARDS["trump"]    = array('3','4','5','6','7','8','9','10');
494       $CARDS["clubs"]    = array('27','28','29','30','31','32','11','12','33','34');
495       $CARDS["spades"]   = array('35','36','37','38','39','40','13','14','41','42');
496       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','15','16','47','48');
497       $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
498       $CARDS["foxes"]    = array();
499       break;
500     case "jack":
501       $CARDS["trump"]    = array('11','12','13','14','15','16','17','18');
502       $CARDS["clubs"]    = array('27','28','29','30','31','32','3', '4','33','34');
503       $CARDS["spades"]   = array('35','36','37','38','39','40','5', '6','41','42');
504       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','7', '8','47','48');
505       $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
506       $CARDS["foxes"]    = array();
507       break;
508     case "trumpless":
509       $CARDS["trump"]    = array();
510       $CARDS["clubs"]    = array('27','28','29','30','31','32','3', '4','11','12','33','34');
511       $CARDS["spades"]   = array('35','36','37','38','39','40','5', '6','13','14','41','42');
512       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
513       $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
514       $CARDS["foxes"]    = array();
515       break;
516     case "club":
517       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
518                                  '17','18','27','28','29','30','31','32','33','34');
519       $CARDS["clubs"]    = array();
520       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
521       $CARDS["hearts"]   = array('43','44','45','46','47','48');
522       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
523       $CARDS["foxes"]    = array();
524       if($RULES["dullen"]=='none')
525         {
526           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
527                                      '17','18','27','28','29','30','31','32','33','34');
528           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
529         }
530       break;
531     case "spade":
532       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
533                                  '17','18','35','36','37','38','39','40','41','42');
534       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
535       $CARDS["spades"]   = array();
536       $CARDS["hearts"]   = array('43','44','45','46','47','48');
537       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
538       $CARDS["foxes"]    = array();
539       if($RULES["dullen"]=='none')
540         {
541           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
542                                      '17','18','35','36','37','38','39','40','41','42');
543           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
544         }
545       break;
546     case "heart":
547       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
548                                  '17','18','43','44','45','46','47','48');
549       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
550       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
551       $CARDS["hearts"]   = array();
552       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
553       $CARDS["foxes"]    = array();
554       if($RULES["dullen"]=='none')
555         {
556           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
557                             '17','18','43','44','1','2','45','46','47','48');
558         }
559       break;
560     }
561 }
562
563 function mysort($cards,$gametype)
564 {
565   usort ( $cards, "sort_comp" );
566   return $cards;
567 }
568
569 function sort_comp($a,$b)
570 {
571   global $CARDS;
572
573   $ALL = array();
574   $ALL = array_merge($CARDS["trump"],$CARDS["diamonds"],$CARDS["clubs"],
575                      $CARDS["hearts"],$CARDS["spades"],$CARDS["diamonds"]);
576
577   return pos_array($a,$ALL)-pos_array($b,$ALL);
578 }
579
580 function can_call($what,$hash)
581 {
582   global $RULES;
583
584   $gameid   = DB_get_gameid_by_hash($hash);
585   $gametype = DB_get_gametype_by_gameid($gameid);
586   $oldcall  = DB_get_call_by_hash($hash);
587   $pcall    = DB_get_partner_call_by_hash($hash);
588
589   if( ($pcall!=NULL && $what >= $pcall) ||
590       ($oldcall!=NULL && $what >=$oldcall) )
591     {
592       return 0;
593     }
594
595   $NRcards  = count(DB_get_hand($hash));
596
597   $NRallcards = 0;
598   for ($i=1;$i<5;$i++)
599     {
600       $user         = DB_get_hash_from_game_and_pos($gameid,$i);
601       $NRallcards  += count(DB_get_hand($user));
602     };
603
604   /* in case of a wedding, everything will be delayed by an offset */
605   $offset = 0;
606   if($gametype=="wedding")
607     {
608       $offset = DB_get_sickness_by_gameid($gameid);
609       if ($offset <0) /* not resolved */
610         return 0;
611     };
612
613   switch ($RULES["call"])
614     {
615     case "1st-own-card":
616       if( 4-($what/30) >= 12 - ($NRcards + $offset))
617         return 1;
618       break;
619     case "5th-card":
620       if( 27+4*($what/30) <= $NRallcards + $offset*4)
621         return 1;
622       break;
623     case "9-cards":
624
625       if($oldcall!=NULL && $pcall!=NULL)
626         $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
627       else if($oldcall!=NULL)
628         $mincall = $oldcall;
629       else if ($pcall!=NULL)
630         $mincall = $pcall;
631       else
632         $mincall = -1;
633
634       if( 12 <= ($NRcards + $offset))
635         {
636           return 1;
637         }
638       else if ( 9 <= ($NRcards + $offset))
639         {
640           if( ($mincall>=0 && $mincall==120) )
641             return 1;
642         }
643       else if ( 6 <= ($NRcards + $offset))
644         {
645           if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
646             return 1;
647         }
648       else if ( 3 <= ($NRcards + $offset))
649         {
650           if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
651             return 1;
652         }
653       else if ( 0 <= ($NRcards + $offset))
654         {
655           if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
656             return 1;
657         };
658       break;
659     }
660
661   return 0;
662 }
663
664 function display_table ()
665 {
666   global $gameid, $GT, $debug,$INDEX,$defaulttimezone;
667   global $RULES,$GAME,$gametype;
668
669   $result = DB_query("SELECT  User.fullname as name,".
670                      "        Hand.position as position, ".
671                      "        User.id, ".
672                      "        Hand.party as party, ".
673                      "        Hand.sickness as sickness, ".
674                      "        Hand.point_call, ".
675                      "        User.last_login, ".
676                      "        Hand.hash,       ".
677                      "        User.timezone    ".
678                      "FROM Hand ".
679                      "LEFT JOIN User ON User.id=Hand.user_id ".
680                      "WHERE Hand.game_id='".$gameid."' ".
681                      "ORDER BY position ASC");
682
683   echo "<div class=\"table\">\n".
684     "  <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
685   while($r = DB_fetch_array($result))
686     {
687       $name  = $r[0];
688       $pos   = $r[1];
689       $user  = $r[2];
690       $party = $r[3];
691       $sickness  = $r[4];
692       $call      = $r[5];
693       $hash      = $r[7];
694       $timezone  = $r[8];
695       date_default_timezone_set($defaulttimezone);
696       $lastlogin = strtotime($r[6]);
697       date_default_timezone_set($timezone);
698       $timenow   = strtotime(date("Y-m-d H:i:s"));
699
700       echo "  <div class=\"table".($pos-1)."\">\n";
701       if(!$debug)
702         echo "   $name \n";
703       else
704         echo "   <a href=\"".$INDEX."?action=game&me=".$hash."\">$name</a>\n";
705
706       /* add hints for poverty, wedding, solo, etc */
707       if( $gametype != "solo")
708         if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" && $GAME["schweinchen-who"]==$hash)
709           echo " Schweinchen. <br />";
710
711       if($GT=="poverty" && $party=="re")
712         if($sickness=="poverty")
713           {
714             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
715             $cards    = DB_get_all_hand($userhash);
716             $trumpNR  = count_trump($cards);
717             if($trumpNR)
718               echo "   <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
719             else
720               echo "   <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
721           }
722         else
723           echo "   <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
724
725       if($GT=="dpoverty")
726         if($party=="re")
727           if($sickness=="poverty")
728             {
729               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
730               $cards    = DB_get_all_hand($userhash);
731               $trumpNR  = count_trump($cards);
732               if($trumpNR)
733                 echo "   <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
734               else
735                 echo "   <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
736             }
737           else
738             echo "   <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
739         else
740           if($sickness=="poverty")
741             {
742               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
743               $cards    = DB_get_all_hand($userhash);
744               $trumpNR  = count_trump($cards);
745               if($trumpNR)
746                 echo "   <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
747               else
748                 echo "   <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
749             }
750           else
751             echo "   <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
752
753       if($GT=="wedding" && $party=="re")
754         if($sickness=="wedding")
755           echo "   <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
756         else
757           echo "   <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
758
759       if(ereg("solo",$GT) && $party=="re")
760         {
761           if(ereg("queen",$GT))
762             echo "   <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
763           else if(ereg("jack",$GT))
764             echo "   <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
765           else if(ereg("club",$GT))
766             echo "   <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
767           else if(ereg("spade",$GT))
768             echo "   <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
769           else if(ereg("heart",$GT))
770             echo "   <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
771           else if(ereg("trumpless",$GT))
772             echo "   <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
773           else if(ereg("trump",$GT))
774             echo "   <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
775         }
776
777       /* add point calls */
778       if($call!=NULL)
779         {
780           if($party=="re")
781             echo "  <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
782           else
783             echo "  <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
784           switch($call)
785             {
786             case "0":
787               echo "   <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
788               break;
789             case "30":
790               echo "   <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
791               break;
792             case "60":
793               echo "   <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
794               break;
795             case "90":
796               echo "   <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
797               break;
798             }
799         }
800
801       echo "    <br />\n";
802       echo "    <span title=\"".date("Y-m-d H:i:s",$timenow).  "\">local time</span>\n";
803       echo "    <span title=\"".date("Y-m-d H:i:s",$lastlogin)."\">last login</span>\n";
804       echo "   </div>\n";
805
806     }
807   echo  "</div>\n"; /* end output table */
808
809
810   return;
811 }
812
813
814 function display_user_menu()
815 {
816   global $WIKI,$myid,$INDEX;
817   echo "<div class=\"usermenu\">\n".
818     "<a href=\"".$INDEX."\"> Go to my user page </a>";
819
820   $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
821                      " LEFT JOIN Game On Hand.game_id=Game.id".
822                      " WHERE Hand.user_id='$myid'".
823                      " AND Game.player='$myid'".
824                      " AND Game.status<>'gameover'".
825                      " ORDER BY Game.session" );
826   if(DB_num_rows($result))
827       echo "<hr />It's your turn in these games:<br />\n";
828
829   while( $r = DB_fetch_array($result))
830     {
831       echo "<a href=\"".$INDEX."?action=game&me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a><br />\n";
832     }
833
834   echo "<hr /> <a href=\"".$INDEX."?action=new\">Start a new game</a>\n";
835
836   echo "<hr /> <a href=\"".$INDEX."?action=prefs\">Change settings</a>\n";
837
838   echo "<hr /> <a href=\"".$INDEX."?action=stats\">Statistics</a>\n";
839
840   echo
841     "<hr />Report bugs in the <a href=\"".$WIKI."\">wiki</a>\n";
842   echo  "</div>\n";
843   return;
844 }
845
846 function generate_score_table($session)
847 {
848   /* get all ids */
849   $gameids = DB_get_gameids_of_finished_games_by_session($session);
850
851   if($gameids == NULL)
852     return "";
853
854   $output = "<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n";
855
856
857   /* get player id, names... from the first game */
858   $player = array();
859   $result = DB_query("SELECT User.id, User.fullname from Hand".
860                      " LEFT JOIN User On Hand.user_id=User.id".
861                      " WHERE Hand.game_id=".$gameids[0]);
862   while( $r = DB_fetch_array($result))
863     {
864       $player[] = array( 'id' => $r[0], 'points' => 0 );
865       $output.= "  <td> ".substr($r[1],0,2)." </td>\n";
866     }
867   $output.="  <td>P</td>\n </tr>\n";
868
869   /* get points and generate table */
870   foreach($gameids as $gameid)
871     {
872       $output.=" <tr>\n";
873
874       $re_score = DB_get_score_by_gameid($gameid);
875       foreach($player as $key=>$pl)
876         {
877           $party = DB_get_party_by_gameid_and_userid($gameid,$pl['id']);
878           if($party == "re")
879             if(DB_get_gametype_by_gameid($gameid)=="solo")
880               $player[$key]['points'] += 3*$re_score;
881             else
882               $player[$key]['points'] += $re_score;
883           else if ($party == "contra")
884             $player[$key]['points'] -= $re_score;
885
886           $output.="  <td>".$player[$key]['points']."</td>\n";
887         }
888       $output.="  <td>".abs($re_score);
889
890       /* check for solo */
891       if(DB_get_gametype_by_gameid($gameid)=="solo")
892         $output.= " S";
893       $output.="</td>\n </tr>\n";
894     }
895
896   $output.="</table></div>\n";
897
898   return $output;
899 }
900
901 function generate_global_score_table()
902 {
903   /* get all ids */
904   $gameids = DB_get_gameids_of_finished_games_by_session(0);
905
906   if($gameids == NULL)
907     return "";
908
909   /* get player id, names... from the User table */
910   $player = array();
911   $result = DB_query("SELECT User.id, User.fullname FROM User");
912
913   while( $r = DB_fetch_array($result))
914     $player[] = array( 'id' => $r[0], 'name'=> $r[1], 'points' => 0 ,'nr' => 0);
915
916   /* get points and generate table */
917   foreach($gameids as $gameid)
918     {
919       $re_score = DB_get_score_by_gameid($gameid);
920       /* TODO: this shouldn't loop over all players, just the 4 players that are in the game */
921       foreach($player as $key=>$pl)
922         {
923           $party = DB_get_party_by_gameid_and_userid($gameid,$pl['id']);
924           if($party == "re")
925             if(DB_get_gametype_by_gameid($gameid)=="solo")
926               $player[$key]['points'] += 3*$re_score;
927             else
928               $player[$key]['points'] += $re_score;
929           else if ($party == "contra")
930             $player[$key]['points'] -= $re_score;
931           if($party)
932             $player[$key]['nr']+=1;
933         }
934     }
935   
936   echo "<table>\n <tr>\n";
937   function cmp($a,$b)
938   {
939     if($a['nr']==0 ) return 1;
940     if($b['nr']==0) return 1;
941
942     $a=$a['points']/$a['nr'];
943     $b=$b['points']/$b['nr'];
944
945     if ($a == $b) 
946       return 0;
947     return ($a > $b) ? -1 : 1;
948   }
949   usort($player,"cmp");
950   foreach($player as $pl)
951     {
952       if($pl['nr']>10)
953         echo "  <tr><td>",$pl['name'],"</td><td>",round($pl['points']/$pl['nr'],3),"</td></tr>\n";
954     }
955   echo "</table>\n";
956     
957   return;
958 }
959
960
961
962 ?>