CLEANUP: rewrote html output for the rules section for new games
[e-DoKo.git] / include / output.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 /* functions which only ouput html  */
9
10 function output_ask_for_new_game($playerA,$playerB,$playerC,$playerD,$oldgameid)
11 {
12   global $RULES;
13
14   echo "<div class=\"message\">\n<form action=\"index.php?action=new\" method=\"post\">\n";
15   echo "Do you want to continue playing?(This will start a new game, with $playerD as dealer.)\n";
16   echo "  <input type=\"hidden\" name=\"PlayerA\" value=\"$playerA\" />\n";
17   echo "  <input type=\"hidden\" name=\"PlayerB\" value=\"$playerB\" />\n";
18   echo "  <input type=\"hidden\" name=\"PlayerC\" value=\"$playerC\" />\n";
19   echo "  <input type=\"hidden\" name=\"PlayerD\" value=\"$playerD\" />\n";
20   echo "  <input type=\"hidden\" name=\"dullen\"  value=\"".$RULES["dullen"]."\" />\n";
21   echo "  <input type=\"hidden\" name=\"schweinchen\" value=\"".$RULES["schweinchen"]."\" />\n";
22   echo "  <input type=\"hidden\" name=\"callrule\" value=\"".$RULES["call"]."\" />\n";
23   echo "  <input type=\"hidden\" name=\"followup\" value=\"$oldgameid\" />\n";
24   echo "  <input type=\"submit\" value=\"keep playing\" />\n";
25   echo "</form>\n</div>";
26   return;
27 }
28
29 function output_form_for_new_game($names)
30 {
31   $copy_names = $names; /* local copy, so that we can delete names from it
32                          * after we selected them to make sure that each name
33                          * only shows up once
34                          */
35 ?>
36   <form action="index.php?action=new" method="post">
37     <h2> Select players (Remember: you need to be one of the players) </h2>
38
39    <div class="table">
40      <img class="table" src="pics/table.png" alt="table" />
41 <?php
42
43   /* ask player for names */
44
45   $i=0;
46
47   /* delete players name, since he will be on position D anyway */
48   unset($copy_names[array_search($_SESSION["name"],$copy_names)]);
49
50   srand((float) microtime() * 10000000);
51   foreach( array("PlayerA","PlayerB","PlayerC","PlayerD") as $player)
52     {
53       /* pick 3 names at random and put the players name on position D*/
54       if($i<3)
55         {
56           $randkey = array_rand($copy_names);
57           $rand = $copy_names[$randkey];
58           /* delete this name from the list of possible names */
59           unset($copy_names[$randkey]);
60         }
61       else
62         {
63           $rand = $_SESSION["name"];
64         }
65
66       echo  "     <div class=\"table".$i."\">\n";
67       $i++;
68       echo "       <select name=\"$player\" size=\"1\">  \n";
69       foreach($names as $name)
70         {
71           if($name==$rand)
72             {
73               echo "         <option selected=\"selected\">$name</option>\n";
74             }
75           else
76             echo "         <option>$name</option>\n";
77         }
78       echo "       </select>\n     </div>\n";
79     }
80 ?>
81     </div>
82
83      <h2 class="rules">Rules</h2>
84      <h3>Gameplay-related</h3>
85      <h4>Ten of hearts:</h4>
86      <p>
87        <select name="dullen">
88          <option value="none"> just normal non-trump  </option>
89          <option value="firstwins"> first ten of hearts wins the trick </option>
90          <option value="secondwins" selected="selected"> second ten of hearts wins the trick </option>
91        </select>
92      </p>
93      <h4>Schweinchen (both foxes), only in normal games or silent solos:</h4>
94      <p>
95        <select name="schweinchen">
96          <option value="none" selected="selected"> none </option>
97          <option value="both"> both become highest trump (automatic call at beginning of the game)   </option>
98          <option value="second"> first one normal, second one becomes highest (call during the game) </option>
99          <option value="secondaftercall">  second one become highest only in case re/contra was announced
100          </option>
101        </select>
102      </p>
103      <h4>Call Re/Contra, etc.:</h4>
104      <p>
105        <select name="callrule">
106          <option value="1st-own-card" selected="selected">  Can call re/contra on the first <strong>own</strong> card played, 90 on the second, etc.</option>
107          <option value="5th-card">  Can call re/contra until 5th card is played, 90 until 9th card is played, etc.</option>
108          <option value="9-cards" > Can call re/contra until 5th card is played, 90 if player still has 9 cards, etc.</option>
109        </select>
110      </p>
111      <h3>Scoring-related</h3>
112      <h4>(not yet implemented)</h4>
113      <p><input type="submit" value="start game"></p>
114      </form>
115 <?php
116 }
117
118 function output_table($data,$caption="",$class="",$id="")
119 {
120
121   $HTML  = "\n<table";
122
123   if($class!="")
124     $HTML.= " class=\"$class\"";
125   if($id!="")
126     $HTML.= " id=\"$id\"";
127
128   $HTML.=">\n";
129
130   $i=0;
131
132   if($caption!="")
133     $HTML .= "  <caption> $caption </caption>\n";
134
135   foreach($data as $record)
136     {
137       if(!$i)
138         $HTML .= "  <thead>\n  <tr>\n";
139       else
140         {
141           if($i==1) $HTML .= "  <tbody>\n";
142           $HTML .= "  <tr>  ";
143         }
144       foreach($record as $point)
145         {
146           if($i)
147             $HTML .= "    <td>$point</td> ";
148           else
149             $HTML .= "    <th>$point</th> ";
150         }
151
152       if(!$i)
153         $HTML .= "  </tr>\n  </thead>\n";
154       else
155         {
156           $HTML .= "  </tr>\n";
157         }
158       $i++;
159     }
160   $HTML .= "  </tbody>\n</table>\n";
161
162   return $HTML;
163 }
164
165 function display_card($card,$dir="english")
166 {
167   /* cards are only availabl for the odd values, e.g. 1.png, 3.png, ...
168    * convert even cards to the matching odd value */
169
170   if( $card/2 - (int)($card/2) == 0.5 || $card == 0)
171     echo "<img src=\"cards/".$dir."/".$card.".png\"  alt=\"".DB_get_card_name($card)."\" />\n";
172   else
173     echo "<img src=\"cards/".$dir."/".($card-1).".png\"  alt=\"".DB_get_card_name($card-1)."\" />\n";
174
175   return;
176 }
177
178 function display_link_card($card,$dir="english",$type="card")
179 {
180   if( $card/2 - (int)($card/2) == 0.5)
181     echo "<div class=\"cardinput\"><input type=\"radio\" name=\"".$type."\" value=\"".$card."\" /><img src=\"cards/".$dir."/".$card.".png\" alt=\"".DB_get_card_name($card)."\" /></div>\n";
182   else
183     echo "<div class=\"cardinput\" ><input type=\"radio\" name=\"".$type."\" value=\"".$card."\" /><img src=\"cards/".$dir."/".($card-1).".png\" alt=\"".DB_get_card_name($card-1)."\" /></div>\n";
184   return;
185 }
186
187 function output_check_for_sickness($me,$mycards)
188 {
189  ?>
190   <div class="sickness"> Thanks for joining the game...<br />
191
192     do you want to play solo?
193     <select name="solo" size="1">
194       <option selected="selected">No</option>
195       <option>trumpless</option>
196       <option>trump</option>
197       <option>queen</option>
198       <option>jack</option>
199       <option>club</option>
200       <option>spade</option>
201       <option>heart</option>
202     </select>
203     <br />
204
205  <?php
206
207   echo "Wedding?";
208   if(check_wedding($mycards))
209      {
210        echo " yes<input type=\"radio\" name=\"wedding\" value=\"yes\" checked=\"checked\" />";
211        echo " no <input type=\"radio\" name=\"wedding\" value=\"no\" /> <br />\n";
212      }
213    else
214      {
215        echo " no <input type=\"hidden\" name=\"wedding\" value=\"no\" /> <br />\n";
216      };
217
218   echo "Do you have poverty?";
219   if(count_trump($mycards)<4)
220     {
221       echo " yes<input type=\"radio\" name=\"poverty\" value=\"yes\" checked=\"checked\" />";
222       echo " no <input type=\"radio\" name=\"poverty\" value=\"no\" /> <br />\n";
223     }
224   else
225     {
226       echo " no <input type=\"hidden\" name=\"poverty\" value=\"no\" /> <br />\n";
227     };
228
229    echo "Do you have too many nines?";
230   if(count_nines($mycards)>4)
231      {
232        echo " yes<input type=\"radio\" name=\"nines\" value=\"yes\" checked=\"checked\" />";
233        echo " no <input type=\"radio\" name=\"nines\" value=\"no\" /> <br />\n";
234      }
235    else
236      {
237        echo " no <input type=\"hidden\" name=\"nines\" value=\"no\" /> <br />\n";
238      };
239
240    echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
241    echo "<input type=\"submit\" value=\"count me in\" />\n";
242
243    echo "</div>\n";
244
245   return;
246 }
247
248 function output_form_calls($me,$myparty)
249 {
250   if( can_call(120,$me) )
251     {
252     if($myparty=='re')
253       echo "re (120):";
254     else if ($myparty=='contra')
255       echo "contra (120):";
256     else
257       echo " re/contra (120):";
258     echo " <input type=\"radio\" name=\"call\" value=\"120\" /> <br />";
259     }
260   if( can_call(90,$me) )
261     echo " 90:".
262       " <input type=\"radio\" name=\"call\" value=\"90\" /> <br />";
263   if( can_call(60,$me) )
264     echo " 60:".
265       " <input type=\"radio\" name=\"call\" value=\"60\" /> <br />";
266   if( can_call(30,$me) )
267     echo " 30:".
268       " <input type=\"radio\" name=\"call\" value=\"30\" /> <br />";
269   if( can_call(0,$me) )
270     echo " 0:".
271       " <input type=\"radio\" name=\"call\" value=\"0\" /> <br />".
272       " no call:".
273       " <input type=\"radio\" name=\"call\" value=\"no\" /> <br />";
274 }
275
276 function output_check_want_to_play($me)
277 {
278    ?>
279  <div class="joingame">
280    Do you want to play a game of DoKo? <br />
281    yes<input type="radio" name="in" value="yes" />
282    no<input type="radio" name="in" value="no" /> <br />
283 <?php
284   echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
285   echo "\n";
286   echo "<input type=\"submit\" value=\"submit\" />\n";
287   echo " </div>\n";
288
289   return;
290 }
291
292 function output_header()
293 {
294    global $REV;
295 ?>
296 <!DOCTYPE html PUBLIC
297     "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
298     "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
299 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
300   <head>
301      <title>e-Doko</title>
302      <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" />
303      <link rel="shortcut icon" type="image/x-icon" href="pics/edoko-favicon.png" />
304      <link rel="stylesheet" type="text/css" href="css/standard021.css" />
305      <script type="text/javascript" src="include/game.js"> </script>
306      <script type="text/javascript" src="include/jquery.js"> </script>
307      <script type="text/javascript" src="include/jquery.tablesorter.js"></script>
308      <script type="text/javascript">
309         $(document).ready(function()
310            {
311               $("#ScoreTable").tablesorter({ widgets: ['zebra']});
312            });
313      </script>
314   </head>
315 <body onload="high_last();">
316 <div class="header">
317 <h1> Welcome to E-Doko </h1>
318 </div>
319 <?php
320
321   echo "<div class=\"main\">";
322   return;
323 }
324
325 function output_footer()
326 {
327   global $REV,$PREF;
328
329   echo "</div>\n\n";
330   echo "<div class=\"footer\">\n";
331   echo "  <p class=\"left\"> copyright 2006-2009 Arun Persaud, Lance Thornton(graphics), Jeff Zerger(database support) <br />\n".
332     "  Verwendung der [deutschen] Kartenbilder mit Genehmigung <br />der Spielkartenfabrik Altenburg GmbH,(c) ASS Altenburger <br />\n".
333     "  - ASS Altenburger Spielkarten - Spielkartenfabrik Altenburg GmbH <br />\n".
334     "  a Carta Mundi Company Email: info@spielkarten.com Internet: www.spielkarten.com</p>\n";
335  echo " <p class=\"right\"> See the latest changes <a href=\"http://nubati.net/cgi-bin/gitweb.cgi?p=e-DoKo.git;a=summary\">\n".
336     "  via git </a> <br />or download the source via <br />\n'git clone http://nubati.net/git/e-DoKo.git' <br />\n".
337     "  <a href=\"http://www.dreamhost.com/green.cgi\">\n".
338     "  <img  border=\"0\" alt=\"Green Web Hosting! This site hosted by DreamHost.\"".
339     "src=\"https://secure.newdream.net/green1.gif\" height=\"32\" width=\"100\" /></a>\n".
340     "  </p> \n";
341   echo "\n";
342   echo "</div>\n";
343
344   echo "</body>\n";
345   echo "</html>\n";
346
347   return;
348 }
349
350 function output_status()
351 {
352   global $defaulttimezone,$INDEX,$WIKI;
353    if(isset($_SESSION["name"]))
354      {
355        $name = $_SESSION["name"];
356
357        /* logout info */
358        echo "\n<div class=\"status\">\n";
359        echo $name,"\n";
360        echo " | <a href=\"".$INDEX."\"> mypage </a>\n";
361        echo " | <a href=\"".$INDEX."?action=prefs\">settings</a>\n";
362        echo " | <a href=\"".$INDEX."?action=new\">new game</a>\n";
363        echo " | <a href=\"".$INDEX."?action=stats\">statistics</a>\n";
364        echo " | <a href=\"".$WIKI."\">wiki</a>\n";
365        echo " |&nbsp;&nbsp;&nbsp; <a href=\"".$INDEX."?action=logout\">logout</a>\n";
366        echo "</div>\n";
367
368        /* last logon time */
369        $myid  = DB_get_userid("name",$name);
370        $zone  = DB_get_user_timezone($myid);
371
372        $time     = DB_get_user_timestamp($myid);
373        date_default_timezone_set($defaulttimezone);
374        $unixtime = strtotime($time);
375        date_default_timezone_set($zone);
376
377        echo "<div class=\"lastlogin\"><span>last login: ".date("r",$unixtime)."</span></div>\n";
378      }
379    else
380      {
381        echo "\n<div class=\"status\">\n";
382        echo "<a href=\"".$INDEX."\">login</a>\n";
383        echo "</div>\n";
384      }
385   return;
386 }
387
388 function output_select_timezone($name,$timezone="")
389 {
390   $Tzone = array ("Pacific/Apia"         => "Apia",                /*UTC-11*/
391                   "Pacific/Honolulu"     => "Honolulu",            /*UTC-10*/
392                   "America/Anchorage"    => "Anchorage",           /*UTC-9*/
393                   "America/Vancouver"    => "Berkeley",            /*UTC-8*/
394                   "America/Phoenix"      => "Phoenix",             /*UTC-7*/
395                   "America/Chicago"      => "Chicago",             /*UTC-6*/
396                   "America/New_York"     => "New York",            /*UTC-5*/
397                   "America/Santiago"     => "Santiago",            /*UTC-4*/
398                   "America/Buenos_Aires" => "Buenos Aires",        /*UTC-3*/
399                   "Atlantic/South_Georgia" => "Fernando de Noronha", /*UTC-2*/
400                   "Atlantic/Azores"       => "Azores",             /*UTC-1"*/
401                   "Europe/London"         => "London",             /*UTC*/
402                   "Europe/Berlin"         => "Berlin",             /*UTC+1*/
403                   "Africa/Cairo"          => "Cairo",              /*UTC+2*/
404                   "Europe/Moscow"         => "Moscow",             /*UTC+3*/
405                   "Asia/Tehran"           => "Tehran",             /*UTC+3:30*/
406                   "Asia/Dubai"            => "Dubai",              /*UTC+4*/
407                   "Asia/Karachi"          => "Karachi",            /*UTC+5*/
408                   "Asia/Calcutta"         => "Delhi",              /*UTC+5:30*/
409                   "Asia/Kathmandu"        => "Kathmandu",          /*UTC+5:45*/
410                   "Asia/Dhaka"            => "Dhaka",              /*UTC+6*/
411                   "Asia/Rangoon"          => "Yangon",             /*UTC+6:30*/
412                   "Asia/Bangkok"          => "Bangkok",            /*UTC+7*/
413                   "Asia/Hong_Kong"        => "Beijing",            /*UTC+8*/
414                   "Asia/Tokyo"            => "Tokyo",              /*UTC+9*/
415                   "Australia/Darwin"      => "Darwin",             /*UTC+9:30*/
416                   "Australia/Sydney"      => "Sydney",             /*UTC+10*/
417                   "Asia/Magadan"          => "Magadan",            /*UTC+11*/
418                   "Pacific/Auckland"      => "Wellington" );       /*UTC+12*/
419
420   echo "  <select id=\"$name\" name=\"$name\" size=\"1\">\n";
421
422   foreach($Tzone as $zone=>$city)
423     {
424       if($timezone==$zone)
425         echo "   <option value=\"$zone\" selected=\"selected\">$city</option>\n";
426       else
427         echo "   <option value=\"$zone\">$city</option>\n";
428     }
429   echo "  </select>\n";
430
431   return;
432 }
433
434 function output_password_recovery($email,$password)
435 {
436 ?>
437    <form action="index.php" method="post">
438 <?php
439   echo "  <input type=\"hidden\" name=\"email\" value=\"".$email."\" />\n";
440   echo "  <input type=\"hidden\" name=\"password\" value=\"".$password."\" />\n";
441   echo "  <input type=\"hidden\" name=\"passwd\"  value=\"set\" />\n";
442 ?>
443      <fieldset>
444        <legend>Password recovery</legend>
445         <table>
446          <tr>
447             <td><label for="email">Old password:</label></td>
448             <td><input type="password" id="password0" name="password0" size="20" maxlength="30" /> </td>
449          </tr><tr>
450             <td><label for="password">New password:</label></td>
451             <td><input type="password" id="password1" name="password1" size="20" maxlength="30" /></td>
452          </tr><tr>
453             <td><label for="password">Retype:</label></td>
454             <td><input type="password" id="password2" name="password2" size="20" maxlength="30" /></td>
455          </tr><tr>
456            <td></td>
457            <td> <input type="submit" class="submitbutton" name="passwd" value="set" /></td>
458          </tr>
459         </table>
460      </fieldset>
461    </form>
462
463 <?php
464 }
465
466 function output_user_notes($userid,$gameid,$userstatus)
467 {
468   echo "<div class=\"notes\"> Personal notes: <br />\n";
469   $notes = DB_get_notes_by_userid_and_gameid($userid,$gameid);
470   foreach($notes as $note)
471     echo "$note <hr />\n";
472   if($userstatus!='gameover')
473     echo "<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
474   echo "</div> \n";
475
476   return;
477 }
478
479 function output_robotproof($i)
480 {
481   switch($i)
482     {
483     case 0:
484       return "6*7=";
485     case 1:
486       return "5*7=";
487     case 2:
488       return "4*7=";
489     case 3:
490       return "3*7=";
491     case 4:
492       return "2*7=";
493     }
494 }
495
496 function output_exchanged_cards()
497 {
498   /* in a poverty game this function will output the exchanged cards
499    * players in the team will see the cards, the other team will see
500    * the backside of cards
501    */
502
503   /* need some information about the game */
504   global $gameid,$mygametype, $PREF,$me,$mystatus;
505
506   /* some variables to track where the people with poverty are sitting */
507   $partnerpos1 = 0;
508   $povertypos1 = 0;
509   $partnerpos2 = 0;
510   $povertypos2 = 0;
511
512   /* only need to do it in a poverty game, this might not be needed, but
513    * just to make sure everything is ok
514    */
515   if($mygametype == 'poverty' || $mygametype=='dpoverty')
516     {
517       /* find out who has poverty */
518       for($mypos=1;$mypos<5;$mypos++)
519         {
520           $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
521           if($usersick == 'poverty')
522             if($povertypos1)
523               $povertypos2 = $mypos;
524             else
525               $povertypos1 = $mypos;
526         }
527       /* get hash and exchanged cards for all involved */
528       $povertyhash1 = DB_get_hash_from_game_and_pos($gameid,$povertypos1);
529       $partnerhash1 = DB_get_partner_hash_by_hash($povertyhash1);
530
531       $povertycards1 = DB_get_exchanged_cards($povertyhash1);
532       $partnercards1 = DB_get_exchanged_cards($partnerhash1);
533
534       $partnerpos1 = DB_get_pos_by_hash($partnerhash1);
535       if($povertypos2)
536         {
537           $povertyhash2 = DB_get_hash_from_game_and_pos($gameid,$povertypos2);
538           $partnerhash2 = DB_get_partner_hash_by_hash($povertyhash2);
539
540           $povertycards2 = DB_get_exchanged_cards($povertyhash2);
541           $partnercards2 = DB_get_exchanged_cards($partnerhash2);
542
543           $partnerpos2 = DB_get_pos_by_hash($partnerhash2);
544         }
545     }
546
547   /* output the cards
548    * go through all positions, check that position has cards that need to be shown and
549    * show those cards
550    */
551   $show=1;
552   for($mypos=1;$mypos<5;$mypos++)
553     {
554       $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
555       if($usersick!=NULL ||
556          $mypos==$povertypos1 || $mypos==$partnerpos1 ||
557          $mypos==$povertypos2 || $mypos==$partnerpos2 )
558         {
559           /* figure out if we gave trump back */
560           $trump_back1=0;
561           if($povertypos2)
562             foreach($povertycards1 as $card)
563               {
564                 if(is_trump($card))
565                   {
566                     $trump_back1=1;
567                     break;
568                   }
569               }
570           $trump_back2=0;
571           if($povertypos2)
572             foreach($povertycards2 as $card)
573               {
574                 if(is_trump($card))
575                   {
576                     $trump_back2=1;
577                     break;
578                   }
579               }
580
581           /* output vorbehalt  */
582           echo "      <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />\n";
583           if($show)
584             echo "       $usersick <br />\n";
585
586           /* output cards */
587           if($mypos==$partnerpos1)
588             {
589               foreach($partnercards1 as $card)
590                 {
591                   echo '        ';
592                   if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
593                     display_card($card,$PREF['cardset']);
594                   else
595                     display_card(0,$PREF['cardset']);
596                 }
597               if($trump_back1) echo "        Trump back";
598             }
599           else if($mypos==$povertypos1)
600             {
601               foreach($povertycards1 as $card)
602                 {
603                   echo '        ';
604                   if($povertyhash1 == $me || $partnerhash1 == $me || $mystatus=='gameover')
605                     display_card($card,$PREF['cardset']);
606                   else
607                     display_card(0,$PREF['cardset']);
608               }
609               if($trump_back1) echo "        Trump back";
610             }
611           else if($mypos==$povertypos2)
612             {
613               foreach($povertycards2 as $card)
614                 {
615                   echo '        ';
616                   if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
617                     display_card($card,$PREF['cardset']);
618                   else
619                     display_card(0,$PREF['cardset']);
620                 }
621               if($trump_back2) echo "        Trump back";
622             }
623           else if($mypos==$partnerpos2)
624             {
625               foreach($partnercards2 as $card)
626                 {
627                   if(is_trump($card)) $trump_back=1;
628                   echo '        ';
629                   if($povertyhash2 == $me || $partnerhash2 == $me || $mystatus=='gameover')
630                     display_card($card,$PREF['cardset']);
631                   else
632                     display_card(0,$PREF['cardset']);
633                 }
634               if($trump_back2) echo "        Trump back";
635             }
636           echo  "      </div>\n";
637         }
638       if($mygametype == $usersick)
639         $show = 0;
640     }
641 }
642
643
644 ?>