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