LAYOUT: more tables on the statistics page
[e-DoKo.git] / include / game.php
index b41ca26382b2b45283622106b7b2f6a019cbc6c8..3bbfccd6d7096e5e2829859f65b019e362110ce3 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"]);
@@ -63,9 +65,23 @@ if($gametype=="solo")
  * save information in $GAME
  */
 $ok=0;
-if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' )
-  if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
-     $ok=1;
+if( $gamestatus == 'pre' )
+  {
+    /* always need to use Schweinchen to figure out for example who has poverty */
+    $ok=1;
+  }
+else
+  {
+    /* in a game Schweinchen is not valid in all types of games */
+    if( $gametype == 'normal' || $gametype == 'silent' || $gametype=='trump' )
+      if( in_array($RULES['schweinchen'],array('both','second','secondaftercall')) )
+       $ok=1;
+  }
+
+/* these are the defaults */
+$GAME['schweinchen-who']    = NULL;
+$GAME['schweinchen-first']  = NULL;
+$GAME['schweinchen-second'] = NULL;
 
 if($ok)
 {
@@ -80,13 +96,14 @@ 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";
 
@@ -125,7 +142,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);
@@ -134,7 +155,7 @@ switch($mystatus)
     else
       {
        /* check the result, if player wants to join, got next stage, else cancel game */
-       if($_REQUEST["in"] == "no")
+       if($_REQUEST["in"] == "no" && !$skip)
          {
            /* cancel the game */
            $message = "Hello, \n\n".
@@ -172,6 +193,7 @@ switch($mystatus)
                    /* whos turn is it? */
                    DB_set_player_by_gameid($gameid,$user);
                    $ok = 0;
+                   break;
                  }
              };
            if($ok)
@@ -202,7 +224,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";
@@ -292,7 +314,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");
              }
@@ -324,7 +346,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";
@@ -363,6 +385,7 @@ switch($mystatus)
          {
            $ok = 0;
            DB_set_player_by_gameid($gameid,$user);
+           break;
          }
       };
 
@@ -623,6 +646,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
@@ -654,6 +678,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);
@@ -674,7 +699,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&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";
       }
 
     /* the following is part A) of what needs to be done */
@@ -700,10 +744,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&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&me=$me&amp;trump=no\">No way</a> <br />\n";
            echo "</div><div>\n";
 
            echo "<div class=\"mycards\">Your cards are: <br />\n";
@@ -788,6 +832,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 */
@@ -798,18 +843,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*/
@@ -890,6 +936,7 @@ switch($mystatus)
              {
                $ok = 0;
                DB_set_player_by_gameid($gameid,$user);
+               break;
              }
          }
        if($ok)
@@ -1118,6 +1165,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*/
@@ -1687,8 +1735,8 @@ switch($mystatus)
          echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
        }
 
-
-      echo "</ul>\n"; /* end ul tricks*/
+      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</ul>\n"; /* end ul tricks*/
 
       echo "<div class=\"notes\"> Personal notes: <br />\n";
       $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
@@ -1713,7 +1761,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"]);