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