CLEANUP: removed todo item for silent solo from code
[e-DoKo.git] / include / game.php
index 3191793ced4ccc9a09131a4a684a5de3fb2986f8..b4ed57dc29a1184ee1c882c81e2c865d3aab5028 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"]);
@@ -57,17 +59,31 @@ if($gametype=="solo")
     $GT  = $gametype." ".$GT;
   }
 
-/* do we need to worry about Schweinchen? 
+/* do we need to worry about Schweinchen?
  * check gametype and rules
- * if yes, figure out if someone actually has Schweinchen  
+ * if yes, figure out if someone actually has Schweinchen
  * 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)     
+if($ok)
 {
   /* need to check for Schweinchen */
   for($i=1;$i<5;$i++)
@@ -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";
 
@@ -172,6 +189,7 @@ switch($mystatus)
                    /* whos turn is it? */
                    DB_set_player_by_gameid($gameid,$user);
                    $ok = 0;
+                   break;
                  }
              };
            if($ok)
@@ -202,7 +220,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 +310,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 +342,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 +381,7 @@ switch($mystatus)
          {
            $ok = 0;
            DB_set_player_by_gameid($gameid,$user);
+           break;
          }
       };
 
@@ -890,6 +909,7 @@ switch($mystatus)
              {
                $ok = 0;
                DB_set_player_by_gameid($gameid,$user);
+               break;
              }
          }
        if($ok)
@@ -1118,6 +1138,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*/
@@ -1164,10 +1185,11 @@ switch($mystatus)
                else
                  $GAME['schweinchen-second'] = 1; /* this must be the second fox */
 
-               if($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second' )
-                 DB_insert_comment("Schweinchen! ",$playid,$myid);
-               if($RULES['schweinchen']=='both' )
-                 DB_insert_comment("Schweinchen! ",$playid,$myid);
+               if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
+                 {
+                   DB_insert_comment("Schweinchen! ",$playid,$myid);
+                   $commentSchweinchen = "Schweinchen! ";
+                 }
                if ($debug)
                  echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
              }
@@ -1310,6 +1332,8 @@ switch($mystatus)
                $comment = $_REQUEST["comment"];
                if($comment != "")
                  DB_insert_comment($comment,$playid,$myid);
+               if($commentSchweinchen)
+                 $comment = $commentSchweinchen . $comment;
              };
 
            /* check for note */
@@ -1710,7 +1734,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"]);