NEW FEATURE: statistics of most solos per game
[e-DoKo.git] / include / game.php
index 668aa1fe06096a59d2e2512116fe0e135a74b3b0..4f506f5b5b26eb5a19c557c12fe3935912769fa7 100644 (file)
@@ -26,6 +26,8 @@ if(!$myid)
     exit();
   }
 
+global $GAME,$RULES,$CARDS;
+
 /* user might get here by clicking on the link in an email, so session might not be set */
 if(isset($_SESSION["name"]))
   output_status($_SESSION["name"]);
@@ -76,6 +78,11 @@ else
        $ok=1;
   }
 
+/* these are the defaults */
+$GAME['schweinchen-who']    = NULL;
+$GAME['schweinchen-first']  = NULL;
+$GAME['schweinchen-second'] = NULL;
+
 if($ok)
 {
   /* need to check for Schweinchen */
@@ -89,33 +96,44 @@ if($ok)
   $GAME['schweinchen-first']  = 0; /* to keep track if they have been played already */
   $GAME['schweinchen-second'] = 0;
 }
-else
-{
-  /* no need to check for Schweinchen */
-  $GAME['schweinchen-who']=NULL;
-}
 /* end check for Schweinchen */
 
+/* set the $CARDS variable, needed for sorting the cards
+ * we set it to normal so that the pre-game phase is handled ok
+ * and later set it to the correct game type that is played
+ */
+set_gametype('normal');
+
 /* put everyting in a form */
-echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
+echo "<form action=\"index.php?action=game&amp;me=$me\" method=\"post\">\n";
 
 /* output game */
 
 /* output extra division in case this game is part of a session */
 if($session)
   {
-    echo "<div class=\"session\">\n".
-      "This game is part of session $session: \n";
+    echo "<div class=\"session\">\n";
+    echo "  <div class=\"sessionrules\">Rules (+icons fur rules) \n";
+    echo "    <div>\n";
+    echo "       10ofhearts : ".$RULES["dullen"]      ."<br />\n";
+    echo "       schweinchen: ".$RULES["schweinchen"] ."<br />\n";
+    echo "       call:        ".$RULES["call"]        ."<br />\n";
+    echo "    </div>\n  </div>\n";
+    echo "  <div class=\"sessionscore\">Score \n";
+    $score   = generate_score_table($session);
+    echo format_score_table_html($score,$myid);
+    echo "  </div>\n";
     $hashes = DB_get_hashes_by_session($session,$myid);
     $i = 1;
     foreach($hashes as $hash)
       {
-       if($hash == $me)
-         echo "$i \n";
-       else
-         echo "<a href=\"".$INDEX."?action=game&me=".$hash."\">$i</a> \n";
-       $i++;
+        if($hash == $me)
+         $j=$i;
+        $i++;
+       $lasthash=$hash;
       }
+    $i--;
+    echo "This is game number $j of <a href=\"".$INDEX."?action=game&amp;me=$lasthash\">$i</a> in session $session.";
     echo "</div>\n";
   }
 
@@ -134,7 +152,11 @@ display_table();
 switch($mystatus)
   {
   case 'start':
-    if( !myisset("in") )
+    /* don't ask if user has autosetup set to yest */
+    $skip = 0;
+    if($PREF['autosetup']=='yes') $skip = 1;
+
+    if( !myisset("in") && !$skip)
       {
        /* asks the player, if he wants to join the game */
        output_check_want_to_play($me);
@@ -143,7 +165,7 @@ switch($mystatus)
     else
       {
        /* check the result, if player wants to join, got next stage, else cancel game */
-       if($_REQUEST["in"] == "no")
+       if(!$skip && $_REQUEST["in"] == "no" )
          {
            /* cancel the game */
            $message = "Hello, \n\n".
@@ -181,6 +203,7 @@ switch($mystatus)
                    /* whos turn is it? */
                    DB_set_player_by_gameid($gameid,$user);
                    $ok = 0;
+                   break;
                  }
              };
            if($ok)
@@ -201,7 +224,7 @@ switch($mystatus)
                     DB_set_player_by_gameid($gameid,$who);
 
                     $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
-                    "Use this link to go the game: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
+                    "Use this link to go the game: ".$HOST.$INDEX."?action=game&amp;me=".$hash."\n\n" ;
                     mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
                    */
                  }
@@ -211,7 +234,7 @@ switch($mystatus)
   case 'init':
     /* here we ask the player if he is sick */
     $mycards = DB_get_hand($me);
-    sort($mycards);
+    $mycards = mysort($mycards,$gametype);
 
     /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
     echo "\n<ul class=\"tricks\">\n";
@@ -259,7 +282,7 @@ switch($mystatus)
        if($Nsickness>1)
          {
            echo "<p class=\"message\"> You selected more than one sickness, please go back ".
-             "and answer the <a href=\"$INDEX?action=game&me=$me&in=yes\">question</a> again.</p>";
+             "and answer the <a href=\"$INDEX?action=game&amp;me=$me&amp;in=yes\">question</a> again.</p>";
 
            echo "<div class=\"mycards\">Your cards are: <br />\n";
            foreach($mycards as $card)
@@ -301,7 +324,7 @@ switch($mystatus)
              }
            else if($_REQUEST["wedding"] == "yes")
              {
-               /* TODO: add silent solo somewhere*/
+               /* silent solo is set further down */
                echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
                DB_set_sickness_by_hash($me,"wedding");
              }
@@ -333,7 +356,7 @@ switch($mystatus)
     if($mystatus=='check')
       {
        $mycards = DB_get_hand($me);
-       sort($mycards);
+       $mycards = mysort($mycards,$gametype);
 
        /* output sickness of other playes, in case the already selected and are sitting in front of the current player */
        echo "\n<ul class=\"tricks\">\n";
@@ -372,6 +395,7 @@ switch($mystatus)
          {
            $ok = 0;
            DB_set_player_by_gameid($gameid,$user);
+           break;
          }
       };
 
@@ -591,12 +615,12 @@ switch($mystatus)
                  {
                    /* email startplayer */
                    $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
-                     "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
+                     "Use this link to play a card: ".$HOST.$INDEX."?action=game&amp;me=".$hash."\n\n" ;
                    mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
                  }
              }
            else
-             echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
+             echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
          }
        else
          {
@@ -606,7 +630,7 @@ switch($mystatus)
 
            $whoid = DB_get_userid('gameid-position',$gameid,$who);
            if($whoid==$myid)
-             echo " Please, <a href=\"$INDEX?action=game&me=$me\">start</a> the game.<br />\n";
+             echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
            else
              {
                $email   = DB_get_email('position-gameid',$who,$gameid);
@@ -617,7 +641,7 @@ switch($mystatus)
                  {
                    /* email player for poverty */
                    $message = "Poverty: It's your turn now in game ".DB_format_gameid($gameid).".\n".
-                     "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
+                     "Use this link to play a card: ".$HOST.$INDEX."?action=game&amp;me=".$whohash."\n\n" ;
                    mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
                  }
              }
@@ -632,6 +656,7 @@ switch($mystatus)
      *            poverty: set re/contra
      *            dpoverty: first time: set re, send email to second player
      *                      second time: set contra
+     *            poverty: set status of other players to 'play'
      *            set status to play in case 0 trump
      *      no -> set status to play,
      *            ask next player or cancle the game if no more players
@@ -663,6 +688,7 @@ switch($mystatus)
                               " WHERE hand_id='$myhand' AND card_id=".DB_quote_smart($exchange));
          };
       }
+
     /* update hand */
     $mycards = DB_get_hand($me);
     $mycards = mysort($mycards,$gametype);
@@ -683,7 +709,26 @@ switch($mystatus)
       {
        /* user is done, ready to play */
        DB_set_hand_status_by_hash($me,'play');
-       /* TODO if resolved; email start player, set startplayer */
+
+       /* email start player */
+       $startplayer = DB_get_startplayer_by_gameid($gameid);
+       $email       = DB_get_email('position-gameid',$startplayer,$gameid);
+       $hash        = DB_get_hash_from_game_and_pos($gameid,$startplayer);
+       $who         = DB_get_userid('email',$email);
+       DB_set_player_by_gameid($gameid,$who);
+
+       if($hash!=$me)
+         {
+           if(DB_get_email_pref_by_hash($hash)!="emailaddict")
+             {
+               /* email startplayer */
+               $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
+                 "Use this link to play a card: ".$HOST.$INDEX."?action=game&amp;me=".$hash."\n\n" ;
+               mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
+             }
+         }
+       else
+         echo " Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
       }
 
     /* the following is part A) of what needs to be done */
@@ -709,10 +754,10 @@ switch($mystatus)
                    foreach($cards as $card)
                      if($card<27) $nrtrump++;
                    echo "Player $name has $nrtrump trump. Do you want to take them?".
-                     "<a href=\"index.php?action=game&me=$me&amp;trump=$user\">yes</a> <br />\n";
+                     "<a href=\"index.php?action=game&amp;me=$me&amp;trump=$user\">Yes</a> <br />\n";
                  }
              }
-           echo "<a href=\"index.php?action=game&me=$me&amp;trump=no\">No,way I take those trump...</a> <br />\n";
+           echo "<a href=\"index.php?action=game&amp;me=$me&amp;trump=no\">No way</a> <br />\n";
            echo "</div><div>\n";
 
            echo "<div class=\"mycards\">Your cards are: <br />\n";
@@ -781,7 +826,7 @@ switch($mystatus)
                DB_set_hand_status_by_hash($userhash,'poverty');
 
                $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
-                 " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
+                 " ".$HOST.$INDEX."?action=game&amp;me=".$userhash."\n\n" ;
                mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
              }
          }
@@ -797,6 +842,7 @@ switch($mystatus)
            /* copy trump from player A to B */
            $result = DB_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
 
+           /* reload cards */
            $mycards = DB_get_hand($me);
 
            /* set re/contra */
@@ -807,18 +853,19 @@ switch($mystatus)
                  {
                    $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
                    if($hash==$userhash||$hash==$me)
-                     DB_set_party_by_hash($hash,"re");
+                     {
+                       DB_set_party_by_hash($hash,"re");
+                     }
                    else
-                     DB_set_party_by_hash($hash,"contra");
+                     {
+                       DB_set_party_by_hash($hash,"contra");
+                       DB_set_hand_status_by_hash($hash,'play'); /* the contra party is ready to play */
+                     }
                  }
-               /* check if we are done, if so, send everyone into the 'play' phase */
+               /* check if we are done (in case of no trump handed over), if so, go to 'play' phase right away*/
                if(count($mycards)==12)
                  {
-                   foreach($userids as $user)
-                     {
-                       $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
-                       DB_set_hand_status_by_hash($hash,'play');
-                     }
+                   DB_set_hand_status_by_hash($me,'play');
                  }
              }
            else /*dpoverty*/
@@ -865,13 +912,13 @@ switch($mystatus)
                    DB_set_hand_status_by_hash($userhash,'poverty');
 
                    $message = "Two people have poverty, it's your turn to decide, if you want to take the trump. Please visit:".
-                     " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
+                     " ".$HOST.$INDEX."?action=game&amp;me=".$userhash."\n\n" ;
                    mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
 
 
                  }
              }
-           echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&me=$me\">continue</a> here.</div>\n";
+           echo "<div class=\"message\"> Please, <a href=\"$INDEX?action=game&amp;me=$me\">continue</a> here.</div>\n";
          }
       }
     echo "</div>";
@@ -899,6 +946,7 @@ switch($mystatus)
              {
                $ok = 0;
                DB_set_player_by_gameid($gameid,$user);
+               break;
              }
          }
        if($ok)
@@ -917,7 +965,7 @@ switch($mystatus)
              {
                /* email startplayer) */
                $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
-                 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
+                 "Use this link to play a card: ".$HOST.$INDEX."?action=game&amp;me=".$hash."\n\n" ;
                mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
              }
          }
@@ -1007,7 +1055,7 @@ switch($mystatus)
                       "WHERE Trick.game_id='".$gameid."' ".
                       "GROUP BY Trick.id, sequence ".
                       "ORDER BY Trick.id, sequence  ASC");
-    $trickNR   = 1;
+    $trickNR   = 0;
     $lasttrick = DB_get_max_trickid($gameid);
 
     $play = array(); /* needed to calculate winner later  */
@@ -1023,7 +1071,7 @@ switch($mystatus)
     if($mygametype != 'normal' && $mygametype != 'silent') /* only show when needed */
       {
        echo "  <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
-         "    <div class=\"trick\" id=\"trick0\">\n";
+            "    <div class=\"trick\" id=\"trick0\">\n";
        $show = 1;
        for($mypos=1;$mypos<5;$mypos++)
          {
@@ -1051,6 +1099,10 @@ switch($mystatus)
        $comment = $r[4];
        $user    = $r[6];
 
+       /* count number of tricks */
+       if($seq==1)
+         $trickNR++;
+
        /* check if first schweinchen has been played */
        if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
          if(!$GAME['schweinchen-first'])
@@ -1098,7 +1150,6 @@ switch($mystatus)
        /* end of trick? */
        if($seq==4)
          {
-           $trickNR++;
            echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
          }
       }
@@ -1127,6 +1178,7 @@ switch($mystatus)
       {
        $card   = $_REQUEST["card"];
        $handid = DB_get_handid('hash',$me);
+       $commentSchweinchen =""; /* used to add a comment when Schweinchen is being played */
 
        /* check if we have card and that we haven't played it yet*/
        /* set played in hand_card to true where hand_id and card_id*/
@@ -1380,7 +1432,7 @@ switch($mystatus)
 
                $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
                  "It's your turn  now.\n".
-                 "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$next_hash."\n\n" ;
+                 "Use this link to play a card: ".$HOST.$INDEX."?action=game&amp;me=".$next_hash."\n\n" ;
                if( DB_get_email_pref_by_uid($who)!="emailaddict" )
                  mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
              }
@@ -1607,35 +1659,9 @@ switch($mystatus)
 
                  $session = DB_get_session_by_gameid($gameid);
                  $score = generate_score_table($session);
-                 /* convert html to ascii */
-                 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
-                 $score = str_replace("</table></div>\n","",$score);
-                 $score = str_replace("\n","",$score);
-                 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
-                 $score = explode("\n",$score);
-
-                 $header = array_slice($score,0,1);
-                 $header = explode("|",$header[0]);
-                 for($i=0;$i<sizeof($header);$i++)
-                   $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
-                 $header = implode("|",$header);
-                 $header.= "\n------+------+------+------+------+\n";
-                 if(sizeof($score)>5) $header.=   "                ...   \n";
-
-                 if(sizeof($score)>5) $score = array_slice($score,-5,5);
-                 for($i=0;$i<sizeof($score);$i++)
-                   {
-                     $line = explode("|",$score[$i]);
-                     for($j=0;$j<sizeof($line);$j++)
-                       $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
-                     $score[$i] = implode("|",$line);
-                   }
-
-                 $score = implode("\n",$score);
-                 $score = $header.$score;
 
                  $message .= "Score Table:\n";
-                 $message .= $score;
+                 $message .= format_score_table_ascii($score);
 
                  /* send out final email */
                  $all = array();
@@ -1653,7 +1679,7 @@ switch($mystatus)
                      $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
 
                      $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
-                       $HOST.$INDEX."?action=game&me=".$hash."\n\n" ;
+                       $HOST.$INDEX."?action=game&amp;me=".$hash."\n\n" ;
                      if( DB_get_email_pref_by_uid($user) != "emailaddict" )
                        mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
                    }
@@ -1669,7 +1695,7 @@ switch($mystatus)
          echo "please wait until it's your turn! <br />\n";
        }
 
-      if($seq!=4 && $trickNR>1)
+      if($seq!=4 && $trickNR>=1)
        echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
 
       /* display points in case game is over */
@@ -1696,14 +1722,15 @@ switch($mystatus)
          echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
        }
 
-
+      echo "  <li onclick=\"hl_prev();\" class=\"old\"><a href=\"#\">prev</a></li>\n";
+      echo "  <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
       echo "</ul>\n"; /* end ul tricks*/
 
       echo "<div class=\"notes\"> Personal notes: <br />\n";
       $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
       foreach($notes as $note)
-       echo "$note <hr \>\n";
-      echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
+       echo "$note <hr />\n";
+      echo "<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
       echo "</div> \n";
 
       $mycards = DB_get_hand($me);
@@ -1722,7 +1749,10 @@ switch($mystatus)
 
          foreach($mycards as $card)
            {
-             if($followsuit && !same_type($card,$firstcard))
+             if( ($followsuit && !same_type($card,$firstcard)) ||
+                 ( (int)($card)==19 && ($RULES['schweinchen']=='second'||$RULES['schweinchen']=='secondaftercall')
+                   && $GAME['schweinchen-who']==$me && !$GAME['schweinchen-first'] )
+                 )
                display_card($card,$PREF["cardset"]);
              else
                display_link_card($card,$PREF["cardset"]);
@@ -1823,15 +1853,6 @@ switch($mystatus)
       /* display rule set for this game */
     echo "<div class=\"gameinfo\">\n";
 
-    if($gamestatus != 'pre')
-      echo " Gametype: $GT <br />\n";
-
-    echo "Rules: <br />\n";
-    echo "10ofhearts : ".$RULES["dullen"]      ."<br />\n";
-    echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
-    echo "call:        ".$RULES["call"]        ."<br />\n";
-
-    echo "<hr />\n";
     if($gamestatus == 'play' )
       output_form_calls($me);
 
@@ -1842,15 +1863,9 @@ switch($mystatus)
     if($gamestatus == 'play' || $gameend < 60*60*24*7)
       {
        echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
-       echo "<hr />";
       }
 
-    echo "<input type=\"submit\" value=\"submit\" />\n<hr />\n";
-
-    $session = DB_get_session_by_gameid($gameid);
-    $score   = generate_score_table($session);
-
-    echo $score;
+    echo "<input type=\"submit\" value=\"submit\" />\n";
 
     echo "</div>\n";