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