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