NEW FEATURE: new settings page, user can now change timezone
[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]=48;   $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]=19;    $r[18]=7;    $r[30]=31;       $r[42]=43;
376       $r[ 7]=20;    $r[19]=8;    $r[31]=32;       $r[43]=44;
377       $r[ 8]=45;    $r[20]=9;    $r[32]=21;       $r[44]=33;
378       $r[ 9]=46;    $r[21]=10;   $r[33]=22;       $r[45]=34;
379       $r[10]=35;    $r[22]=11;   $r[34]=23;       $r[46]=25;
380       $r[11]=36;    $r[23]=12;   $r[35]=24;       $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
668   $result = DB_query("SELECT  User.fullname as name,".
669                      "        Hand.position as position, ".
670                      "        User.id, ".
671                      "        Hand.party as party, ".
672                      "        Hand.sickness as sickness, ".
673                      "        Hand.point_call, ".
674                      "        User.last_login, ".
675                      "        Hand.hash,       ".
676                      "        User.timezone    ".
677                      "FROM Hand ".
678                      "LEFT JOIN User ON User.id=Hand.user_id ".
679                      "WHERE Hand.game_id='".$gameid."' ".
680                      "ORDER BY position ASC");
681
682   echo "<div class=\"table\">\n".
683     "  <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
684   while($r = DB_fetch_array($result))
685     {
686       $name  = $r[0];
687       $pos   = $r[1];
688       $user  = $r[2];
689       $party = $r[3];
690       $sickness  = $r[4];
691       $call      = $r[5];
692       $hash      = $r[7];
693       $timezone  = $r[8];
694       date_default_timezone_set($defaulttimezone);
695       $lastlogin = strtotime($r[6]);
696       date_default_timezone_set($timezone);
697       $timenow   = strtotime(date("Y-m-d H:i:s"));
698
699       echo "  <div class=\"table".($pos-1)."\">\n";
700       if(!$debug)
701         echo "   $name \n";
702       else
703         echo "   <a href=\"".$INDEX."?action=game&me=".$hash."\">$name</a>\n";
704
705       /* add hints for poverty, wedding, solo, etc */
706       if($GT=="poverty" && $party=="re")
707         if($sickness=="poverty")
708           {
709             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
710             $cards    = DB_get_all_hand($userhash);
711             $trumpNR  = count_trump($cards);
712             if($trumpNR)
713               echo "   <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
714             else
715               echo "   <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
716           }
717         else
718           echo "   <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
719
720       if($GT=="dpoverty")
721         if($party=="re")
722           if($sickness=="poverty")
723             {
724               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
725               $cards    = DB_get_all_hand($userhash);
726               $trumpNR  = count_trump($cards);
727               if($trumpNR)
728                 echo "   <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
729               else
730                 echo "   <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
731             }
732           else
733             echo "   <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
734         else
735           if($sickness=="poverty")
736             {
737               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
738               $cards    = DB_get_all_hand($userhash);
739               $trumpNR  = count_trump($cards);
740               if($trumpNR)
741                 echo "   <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
742               else
743                 echo "   <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
744             }
745           else
746             echo "   <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
747
748       if($GT=="wedding" && $party=="re")
749         if($sickness=="wedding")
750           echo "   <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
751         else
752           echo "   <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
753
754       if(ereg("solo",$GT) && $party=="re")
755         {
756           if(ereg("queen",$GT))
757             echo "   <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
758           else if(ereg("jack",$GT))
759             echo "   <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
760           else if(ereg("club",$GT))
761             echo "   <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
762           else if(ereg("spade",$GT))
763             echo "   <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
764           else if(ereg("heart",$GT))
765             echo "   <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
766           else if(ereg("trumpless",$GT))
767             echo "   <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
768           else if(ereg("trump",$GT))
769             echo "   <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
770         }
771
772       /* add point calls */
773       if($call!=NULL)
774         {
775           if($party=="re")
776             echo "  <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
777           else
778             echo "  <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
779           switch($call)
780             {
781             case "0":
782               echo "   <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
783               break;
784             case "30":
785               echo "   <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
786               break;
787             case "60":
788               echo "   <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
789               break;
790             case "90":
791               echo "   <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
792               break;
793             }
794         }
795
796       echo "    <br />\n";
797       echo "    <span title=\"".date("Y-m-d H:i:s",$timenow).  "\">local time</span>\n";
798       echo "    <span title=\"".date("Y-m-d H:i:s",$lastlogin)."\">last login</span>\n";
799       echo "   </div>\n";
800
801     }
802   echo  "</div>\n"; /* end output table */
803
804
805   return;
806 }
807
808
809 function display_user_menu()
810 {
811   global $WIKI,$myid,$INDEX;
812   echo "<div class=\"usermenu\">\n".
813     "<a href=\"".$INDEX."\"> Go to my user page </a>";
814
815   $result = DB_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
816                      " LEFT JOIN Game On Hand.game_id=Game.id".
817                      " WHERE Hand.user_id='$myid'".
818                      " AND Game.player='$myid'".
819                      " AND Game.status<>'gameover'".
820                      " ORDER BY Game.session" );
821   if(DB_num_rows($result))
822       echo "<hr />It's your turn in these games:<br />\n";
823
824   while( $r = DB_fetch_array($result))
825     {
826       echo "<a href=\"".$INDEX."?action=game&me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a><br />\n";
827     }
828
829   echo "<hr /> <a href=\"".$INDEX."?action=new\">Start a new game</a>\n";
830
831   echo "<hr /> <a href=\"".$INDEX."?action=prefs\">Change settings</a>\n";
832
833   echo "<hr /> <a href=\"".$INDEX."?action=stats\">Statistics</a>\n";
834
835   echo
836     "<hr />Report bugs in the <a href=\"".$WIKI."\">wiki</a>\n";
837   echo  "</div>\n";
838   return;
839 }
840
841 function generate_score_table($session)
842 {
843   /* get all ids */
844   $gameids = DB_get_gameids_of_finished_games_by_session($session);
845
846   if($gameids == NULL)
847     return "";
848
849   $output = "<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n";
850
851
852   /* get player id, names... from the first game */
853   $player = array();
854   $result = DB_query("SELECT User.id, User.fullname from Hand".
855                      " LEFT JOIN User On Hand.user_id=User.id".
856                      " WHERE Hand.game_id=".$gameids[0]);
857   while( $r = DB_fetch_array($result))
858     {
859       $player[] = array( 'id' => $r[0], 'points' => 0 );
860       $output.= "  <td> ".substr($r[1],0,2)." </td>\n";
861     }
862   $output.="  <td>P</td>\n </tr>\n";
863
864   /* get points and generate table */
865   foreach($gameids as $gameid)
866     {
867       $output.=" <tr>\n";
868
869       $re_score = DB_get_score_by_gameid($gameid);
870       foreach($player as $key=>$pl)
871         {
872           $party = DB_get_party_by_gameid_and_userid($gameid,$pl['id']);
873           if($party == "re")
874             if(DB_get_gametype_by_gameid($gameid)=="solo")
875               $player[$key]['points'] += 3*$re_score;
876             else
877               $player[$key]['points'] += $re_score;
878           else if ($party == "contra")
879             $player[$key]['points'] -= $re_score;
880
881           $output.="  <td>".$player[$key]['points']."</td>\n";
882         }
883       $output.="  <td>".abs($re_score);
884
885       /* check for solo */
886       if(DB_get_gametype_by_gameid($gameid)=="solo")
887         $output.= " S";
888       $output.="</td>\n </tr>\n";
889     }
890
891   $output.="</table></div>\n";
892
893   return $output;
894 }
895
896 function generate_global_score_table()
897 {
898   /* get all ids */
899   $gameids = DB_get_gameids_of_finished_games_by_session(0);
900
901   if($gameids == NULL)
902     return "";
903
904   /* get player id, names... from the User table */
905   $player = array();
906   $result = DB_query("SELECT User.id, User.fullname FROM User");
907
908   while( $r = DB_fetch_array($result))
909     $player[] = array( 'id' => $r[0], 'name'=> $r[1], 'points' => 0 ,'nr' => 0);
910
911   /* get points and generate table */
912   foreach($gameids as $gameid)
913     {
914       $re_score = DB_get_score_by_gameid($gameid);
915       /* TODO: this shouldn't loop over all players, just the 4 players that are in the game */
916       foreach($player as $key=>$pl)
917         {
918           $party = DB_get_party_by_gameid_and_userid($gameid,$pl['id']);
919           if($party == "re")
920             if(DB_get_gametype_by_gameid($gameid)=="solo")
921               $player[$key]['points'] += 3*$re_score;
922             else
923               $player[$key]['points'] += $re_score;
924           else if ($party == "contra")
925             $player[$key]['points'] -= $re_score;
926           if($party)
927             $player[$key]['nr']+=1;
928         }
929     }
930   
931   echo "<table>\n <tr>\n";
932   function cmp($a,$b)
933   {
934     if($a['nr']==0 ) return 1;
935     if($b['nr']==0) return 1;
936
937     $a=$a['points']/$a['nr'];
938     $b=$b['points']/$b['nr'];
939
940     if ($a == $b) 
941       return 0;
942     return ($a > $b) ? -1 : 1;
943   }
944   usort($player,"cmp");
945   foreach($player as $pl)
946     {
947       if($pl['nr']>10)
948         echo "  <tr><td>",$pl['name'],"</td><td>",round($pl['points']/$pl['nr'],3),"</td></tr>\n";
949     }
950   echo "</table>\n";
951     
952   return;
953 }
954
955
956
957 ?>