16ca508bb70d8dfca1d2452713f53db4df030294
[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 return_timezone($offset)
394 {
395   switch($offset)
396     {
397     case '1':
398       $zone = "Europe/Berlin";
399       break;
400     case '-8':
401       $zone = "America/Vancouver";
402       break;
403     case '13':
404       $zone = "Pacific/Auckland";
405       break;
406     default:
407       $zone = "Europe/London";
408     }
409
410   return $zone;
411 }
412
413 function have_suit($cards,$c)
414 {
415   global $CARDS;
416   $suite = array();
417
418   if(in_array($c,$CARDS["trump"]))
419     $suite = $CARDS["trump"];
420   else if(in_array($c,$CARDS["clubs"]))
421     $suite = $CARDS["clubs"];
422   else if(in_array($c,$CARDS["spades"]))
423     $suite = $CARDS["spades"];
424   else if(in_array($c,$CARDS["hearts"]))
425     $suite = $CARDS["hearts"];
426   else if(in_array($c,$CARDS["diamonds"]))
427     $suite = $CARDS["diamonds"];
428
429   foreach($cards as $card)
430     {
431       if(in_array($card,$suite))
432         return 1;
433     }
434
435   return 0;
436 }
437
438 function same_type($card,$c)
439 {
440   global $CARDS;
441   $suite = "";
442
443   /* figure out what kind of card c is */
444   if(in_array($c,$CARDS["trump"]))
445     $suite = $CARDS["trump"];
446   else if(in_array($c,$CARDS["clubs"]))
447     $suite = $CARDS["clubs"];
448   else if(in_array($c,$CARDS["spades"]))
449     $suite = $CARDS["spades"];
450   else if(in_array($c,$CARDS["hearts"]))
451     $suite = $CARDS["hearts"];
452   else if(in_array($c,$CARDS["diamonds"]))
453     $suite = $CARDS["diamonds"];
454
455   /* card is the same suid return 1 */
456   if(in_array($card,$suite))
457     return 1;
458
459   return 0;
460 }
461
462 function set_gametype($gametype)
463 {
464   global $CARDS;
465   global $RULES;
466
467   switch($gametype)
468     {
469     case "normal":
470     case "wedding":
471     case "poverty":
472     case "dpoverty":
473     case "trump":
474     case "silent":
475       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
476                                  '17','18','19','20','21','22','23','24','25','26');
477       $CARDS["diamonds"] = array();
478       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
479       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
480       $CARDS["hearts"]   = array('43','44','45','46','47','48');
481       $CARDS["foxes"]    = array('19','20');
482       if($RULES["dullen"]=='none')
483         {
484           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
485                                      '17','18','19','20','21','22','23','24','25','26');
486           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
487         }
488       break;
489     case "queen":
490       $CARDS["trump"]    = array('3','4','5','6','7','8','9','10');
491       $CARDS["clubs"]    = array('27','28','29','30','31','32','11','12','33','34');
492       $CARDS["spades"]   = array('35','36','37','38','39','40','13','14','41','42');
493       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','15','16','47','48');
494       $CARDS["diamonds"] = array('19','20','21','22','23','24','17','18','25','26');
495       $CARDS["foxes"]    = array();
496       break;
497     case "jack":
498       $CARDS["trump"]    = array('11','12','13','14','15','16','17','18');
499       $CARDS["clubs"]    = array('27','28','29','30','31','32','3', '4','33','34');
500       $CARDS["spades"]   = array('35','36','37','38','39','40','5', '6','41','42');
501       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','7', '8','47','48');
502       $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','25','26');
503       $CARDS["foxes"]    = array();
504       break;
505     case "trumpless":
506       $CARDS["trump"]    = array();
507       $CARDS["clubs"]    = array('27','28','29','30','31','32','3', '4','11','12','33','34');
508       $CARDS["spades"]   = array('35','36','37','38','39','40','5', '6','13','14','41','42');
509       $CARDS["hearts"]   = array('43','44', '1', '2','45','46','7', '8','15','16','47','48');
510       $CARDS["diamonds"] = array('19','20','21','22','23','24','9','10','17','18','25','26');
511       $CARDS["foxes"]    = array();
512       break;
513     case "club":
514       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
515                                  '17','18','27','28','29','30','31','32','33','34');
516       $CARDS["clubs"]    = array();
517       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
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','27','28','29','30','31','32','33','34');
525           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
526         }
527       break;
528     case "spade":
529       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
530                                  '17','18','35','36','37','38','39','40','41','42');
531       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
532       $CARDS["spades"]   = array();
533       $CARDS["hearts"]   = array('43','44','45','46','47','48');
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','35','36','37','38','39','40','41','42');
540           $CARDS["hearts"]   = array('43','44','1','2','45','46','47','48');
541         }
542       break;
543     case "heart":
544       $CARDS["trump"]    = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16',
545                                  '17','18','43','44','45','46','47','48');
546       $CARDS["clubs"]    = array('27','28','29','30','31','32','33','34');
547       $CARDS["spades"]   = array('35','36','37','38','39','40','41','42');
548       $CARDS["hearts"]   = array();
549       $CARDS["diamonds"] = array('19','20','21','22','23','24','25','26');
550       $CARDS["foxes"]    = array();
551       if($RULES["dullen"]=='none')
552         {
553           $CARDS["trump"]    = array('3','4','5','6','7','8','9','10','11','12','13','14','15','16',
554                             '17','18','43','44','1','2','45','46','47','48');
555         }
556       break;
557     }
558 }
559
560 function mysort($cards,$gametype)
561 {
562   usort ( $cards, "sort_comp" );
563   return $cards;
564 }
565
566 function sort_comp($a,$b)
567 {
568   global $CARDS;
569
570   $ALL = array();
571   $ALL = array_merge($CARDS["trump"],$CARDS["diamonds"],$CARDS["clubs"],
572                      $CARDS["hearts"],$CARDS["spades"],$CARDS["diamonds"]);
573
574   return pos_array($a,$ALL)-pos_array($b,$ALL);
575 }
576
577 function can_call($what,$hash)
578 {
579   global $RULES;
580
581   $gameid   = DB_get_gameid_by_hash($hash);
582   $gametype = DB_get_gametype_by_gameid($gameid);
583   $oldcall  = DB_get_call_by_hash($hash);
584   $pcall    = DB_get_partner_call_by_hash($hash);
585
586   if( ($pcall!=NULL && $what >= $pcall) ||
587       ($oldcall!=NULL && $what >=$oldcall) )
588     {
589       return 0;
590     }
591
592   $NRcards  = count(DB_get_hand($hash));
593
594   $NRallcards = 0;
595   for ($i=1;$i<5;$i++)
596     {
597       $user         = DB_get_hash_from_game_and_pos($gameid,$i);
598       $NRallcards  += count(DB_get_hand($user));
599     };
600
601   /* in case of a wedding, everything will be delayed by an offset */
602   $offset = 0;
603   if($gametype=="wedding")
604     {
605       $offset = DB_get_sickness_by_gameid($gameid);
606       if ($offset <0) /* not resolved */
607         return 0;
608     };
609
610   switch ($RULES["call"])
611     {
612     case "1st-own-card":
613       if( 4-($what/30) >= 12 - ($NRcards + $offset))
614         return 1;
615       break;
616     case "5th-card":
617       if( 27+4*($what/30) <= $NRallcards + $offset*4)
618         return 1;
619       break;
620     case "9-cards":
621
622       if($oldcall!=NULL && $pcall!=NULL)
623         $mincall = ($oldcall>$pcall) ? $pcall : $oldcall;
624       else if($oldcall!=NULL)
625         $mincall = $oldcall;
626       else if ($pcall!=NULL)
627         $mincall = $pcall;
628       else
629         $mincall = -1;
630
631       if( 12 <= ($NRcards + $offset))
632         {
633           return 1;
634         }
635       else if ( 9 <= ($NRcards + $offset))
636         {
637           if( ($mincall>=0 && $mincall==120) )
638             return 1;
639         }
640       else if ( 6 <= ($NRcards + $offset))
641         {
642           if( ($mincall>=0 && $mincall<=90 && $what<=60 ) )
643             return 1;
644         }
645       else if ( 3 <= ($NRcards + $offset))
646         {
647           if( ($mincall>=0 && $mincall<=60 && $what<=30 ) )
648             return 1;
649         }
650       else if ( 0 <= ($NRcards + $offset))
651         {
652           if( ($mincall>=0 && $mincall<=30 && $what==0 ) )
653             return 1;
654         };
655       break;
656     }
657
658   return 0;
659 }
660
661 function display_table ()
662 {
663   global $gameid, $GT, $debug,$host;
664
665   $result = mysql_query("SELECT  User.fullname as name,".
666                         "        Hand.position as position, ".
667                         "        User.id, ".
668                         "        Hand.party as party, ".
669                         "        Hand.sickness as sickness, ".
670                         "        Hand.point_call, ".
671                         "        User.last_login, ".
672                         "        Hand.hash        ".
673                         "FROM Hand ".
674                         "LEFT JOIN User ON User.id=Hand.user_id ".
675                         "WHERE Hand.game_id='".$gameid."' ".
676                         "ORDER BY position ASC");
677
678   echo "<div class=\"table\">\n".
679     "  <img class=\"table\" src=\"pics/table.png\" alt=\"table\" />\n";
680   while($r = mysql_fetch_array($result,MYSQL_NUM))
681     {
682       $name  = $r[0];
683       $pos   = $r[1];
684       $user  = $r[2];
685       $party = $r[3];
686       $sickness  = $r[4];
687       $call      = $r[5];
688       $lastlogin = strtotime($r[6]);
689       $hash      = $r[7];
690
691       $offset = DB_get_user_timezone($user);
692       $zone   = return_timezone($offset);
693       date_default_timezone_set($zone);
694
695       echo "  <div class=\"table".($pos-1)."\">\n";
696       if(!$debug)
697         echo "   $name \n";
698       else
699         echo "   <a href=\"".$host."?me=".$hash."\">$name</a>\n";
700
701       /* add hints for poverty, wedding, solo, etc */
702       if($GT=="poverty" && $party=="re")
703         if($sickness=="poverty")
704           {
705             $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
706             $cards    = DB_get_all_hand($userhash);
707             $trumpNR  = count_trump($cards);
708             if($trumpNR)
709               echo "   <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
710             else
711               echo "   <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
712           }
713         else
714           echo "   <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
715
716       if($GT=="dpoverty")
717         if($party=="re")
718           if($sickness=="poverty")
719             {
720               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
721               $cards    = DB_get_all_hand($userhash);
722               $trumpNR  = count_trump($cards);
723               if($trumpNR)
724                 echo "   <img src=\"pics/button/poverty_trump_button.png\" class=\"button\" alt=\"poverty < trump back\" />";
725               else
726                 echo "   <img src=\"pics/button/poverty_notrump_button.png\" class=\"button\" alt=\"poverty <\" />";
727             }
728           else
729             echo "   <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" />";
730         else
731           if($sickness=="poverty")
732             {
733               $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
734               $cards    = DB_get_all_hand($userhash);
735               $trumpNR  = count_trump($cards);
736               if($trumpNR)
737                 echo "   <img src=\"pics/button/poverty2_trump_button.png\" class=\"button\" alt=\"poverty2 < trump back\" />";
738               else
739                 echo "   <img src=\"pics/button/poverty2_notrump_button.png\" class=\"button\" alt=\"poverty2 <\" />";
740             }
741           else
742             echo "   <img src=\"pics/button/poverty2_partner_button.png\" class=\"button\" alt=\"poverty2 >\" />";
743
744       if($GT=="wedding" && $party=="re")
745         if($sickness=="wedding")
746           echo "   <img src=\"pics/button/wedding_button.png\" class=\"button\" alt=\"wedding\" />";
747         else
748           echo "   <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" />";
749
750       if(ereg("solo",$GT) && $party=="re")
751         {
752           if(ereg("queen",$GT))
753             echo "   <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" />";
754           else if(ereg("jack",$GT))
755             echo "   <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" />";
756           else if(ereg("club",$GT))
757             echo "   <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" />";
758           else if(ereg("spade",$GT))
759             echo "   <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" />";
760           else if(ereg("heart",$GT))
761             echo "   <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" />";
762           else if(ereg("trumpless",$GT))
763             echo "   <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
764           else if(ereg("trump",$GT))
765             echo "   <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" />";
766         }
767
768       /* add point calls */
769       if($call!=NULL)
770         {
771           if($party=="re")
772             echo "  <img src=\"pics/button/re_button.png\" class=\"button\" alt=\"re\" />";
773           else
774             echo "  <img src=\"pics/button/contra_button.png\" class=\"button\" alt=\"contra\" />";
775           switch($call)
776             {
777             case "0":
778               echo "   <img src=\"pics/button/0_button.png\" class=\"button\" alt=\"0\" />";
779               break;
780             case "30":
781               echo "   <img src=\"pics/button/30_button.png\" class=\"button\" alt=\"30\" />";
782               break;
783             case "60":
784               echo "   <img src=\"pics/button/60_button.png\" class=\"button\" alt=\"60\" />";
785               break;
786             case "90":
787               echo "   <img src=\"pics/button/90_button.png\" class=\"button\" alt=\"90\" />";
788               break;
789             }
790         }
791
792       echo "    <br />\n";
793       echo "    <span title=\"".date("Y-m-d H:i:s")."\">local time</span>\n";
794       echo "    <span title=\"".date("Y-m-d H:i:s",$lastlogin)."\">last login</span>\n";
795       echo "   </div>\n";
796
797     }
798   echo  "</div>\n"; /* end output table */
799
800
801   return;
802 }
803
804
805 function display_user_menu()
806 {
807   global $wiki,$myid,$host;
808   echo "<div class=\"usermenu\">\n".
809     "<a href=\"index.php\"> Go to my user page </a>";
810
811   $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.player from Hand".
812                         " LEFT JOIN Game On Hand.game_id=Game.id".
813                         " WHERE Hand.user_id='$myid'".
814                         " AND Game.player='$myid'".
815                         " AND Game.status<>'gameover'".
816                         " ORDER BY Game.session" );
817   if(mysql_num_rows($result))
818       echo "<hr />It's your turn in these games:<br />\n";
819
820   while( $r = mysql_fetch_array($result,MYSQL_NUM))
821     {
822       echo "<a href=\"".$host."?me=".$r[0]."\">game ".DB_format_gameid($r[1])." </a><br />\n";
823     }
824
825   echo "<hr /> <a href=\"".$host."?new\">Start a new game</a>\n";
826
827   echo "<hr /> <a href=\"".substr($host,0,-9)."stats.php\">Statistics</a>\n";
828
829   echo
830     "<hr />Report bugs in the <a href=\"". $wiki."\">wiki</a>\n";
831   echo  "</div>\n";
832   return;
833 }
834
835 function generate_score_table($session)
836 {
837
838   /* get all ids */
839   $gameids = DB_get_gameids_of_finished_games_by_session($session);
840
841   if($gameids == NULL)
842     return "";
843
844   $output = "<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n";
845
846
847   /* get player id, names... from the first game */
848   $player = array();
849   $result = mysql_query("SELECT User.id, User.fullname from Hand".
850                         " LEFT JOIN User On Hand.user_id=User.id".
851                         " WHERE Hand.game_id=".$gameids[0]);
852   while( $r = mysql_fetch_array($result,MYSQL_NUM))
853     {
854       $player[] = array( 'id' => $r[0], 'points' => 0 );
855       $output.= "  <td> ".substr($r[1],0,2)." </td>\n";
856     }
857   $output.="  <td>P</td>\n </tr>\n";
858
859   /* get points and generate table */
860   foreach($gameids as $gameid)
861     {
862       $output.=" <tr>\n";
863
864       $re_score = DB_get_score_by_gameid($gameid);
865       foreach($player as $key=>$pl)
866         {
867           $party = DB_get_party_by_gameid_and_userid($gameid,$pl['id']);
868           if($party == "re")
869             if(DB_get_gametype_by_gameid($gameid)=="solo")
870               $player[$key]['points'] += 3*$re_score;
871             else
872               $player[$key]['points'] += $re_score;
873           else if ($party == "contra")
874             $player[$key]['points'] -= $re_score;
875
876           $output.="  <td>".$player[$key]['points']."</td>\n";
877         }
878       $output.="  <td>".abs($re_score);
879
880       /* check for solo */
881       if(DB_get_gametype_by_gameid($gameid)=="solo")
882         $output.= " S";
883       $output.="</td>\n </tr>\n";
884     }
885
886   $output.="</table></div>\n";
887
888   return $output;
889 }
890
891 ?>