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