BUGFIX: show comment for Schweinchen the moment it is played
[e-DoKo.git] / include / game.php
index 5f2c0029f85ccab90300e06e39a3898925a283b9..b41ca26382b2b45283622106b7b2f6a019cbc6c8 100644 (file)
@@ -57,18 +57,35 @@ if($gametype=="solo")
     $GT  = $gametype." ".$GT;
   }
 
     $GT  = $gametype." ".$GT;
   }
 
-/* does anyone have both foxes */
-$GAME["schweinchen"]=0;
-for($i=1;$i<5;$i++)
-  {
-    $hash  = DB_get_hash_from_game_and_pos($gameid,$i);
-    $cards = DB_get_all_hand($hash);
-    if( in_array("19",$cards) && in_array("20",$cards) )
-      {
-       $GAME["schweinchen"]=1;
-       $GAME["schweinchen-who"]=$hash;
-      }
-  };
+/* do we need to worry about Schweinchen?
+ * check gametype and rules
+ * 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($ok)
+{
+  /* need to check for Schweinchen */
+  for($i=1;$i<5;$i++)
+    {
+      $hash  = DB_get_hash_from_game_and_pos($gameid,$i);
+      $cards = DB_get_all_hand($hash);
+      if( in_array("19",$cards) && in_array("20",$cards) )
+       $GAME['schweinchen-who']=$hash;
+    };
+  $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 */
 
 /* put everyting in a form */
 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
 
 /* put everyting in a form */
 echo "<form action=\"index.php?action=game&me=$me\" method=\"post\">\n";
@@ -530,6 +547,25 @@ switch($mystatus)
                  DB_set_hand_status_by_hash($userhash,'poverty');
              }
          }
                  DB_set_hand_status_by_hash($userhash,'poverty');
              }
          }
+       /* check for silent solo, set game type to solo in this case */
+       $gametype = DB_get_gametype_by_gameid($gameid);
+       $userids  = DB_get_all_userid_by_gameid($gameid);
+       foreach($userids as $userid)
+         {
+           $userhash = DB_get_hash_from_gameid_and_userid($gameid,$userid);
+
+           if($gametype=='normal')
+             {
+               $userhand = DB_get_all_hand($userhash);
+               if(check_wedding($userhand))
+                 {
+                   /* normal game type and player has both queens -> silent solo */
+                   /* keep startplayer, just set gametype to silent solo */
+                   DB_set_gametype_by_gameid($gameid,"solo");
+                   DB_set_solo_by_gameid($gameid,'silent');
+                 }
+             }
+         }
 
        /* send out email to first player or poverty person*/
        if($gametype!="poverty" && $gametype!="dpoverty")
 
        /* send out email to first player or poverty person*/
        if($gametype!="poverty" && $gametype!="dpoverty")
@@ -838,39 +874,45 @@ switch($mystatus)
      * in case of 'play' there is a break later that skips the last part
      */
 
      * in case of 'play' there is a break later that skips the last part
      */
 
-    /* check if all players are ready to play */
-    $ok = 1;
-    $userids = DB_get_all_userid_by_gameid($gameid);
-    foreach($userids as $user)
-      {
-       $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
-       if($userstatus !='play')
-       {
-         $ok = 0;
-         DB_set_player_by_gameid($gameid,$user);
-       }
-      }
-    if($ok)
+    /* check if all players are ready to play,
+     * if so, send out email to the startplayer
+     * only need to do this if the game hasn't started yet
+     */
+    $gamestatus = DB_get_game_status_by_gameid($gameid);
+    if($gamestatus == 'pre')
       {
       {
-       /* only set this after all poverty, etc. are handled*/
-       DB_set_game_status_by_gameid($gameid,'play');
+       $ok = 1;
+       $userids = DB_get_all_userid_by_gameid($gameid);
+       foreach($userids as $user)
+         {
+           $userstatus = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
+           if($userstatus !='play' && $userstatus!='gameover')
+             {
+               $ok = 0;
+               DB_set_player_by_gameid($gameid,$user);
+             }
+         }
+       if($ok)
+         {
+           /* only set this after all poverty, etc. are handled*/
+           DB_set_game_status_by_gameid($gameid,'play');
 
 
-       /* email startplayer */
-       $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);
+           /* email startplayer */
+           $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 && 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);
+           if($hash!=$me && 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);
+             }
          }
       }
          }
       }
-
     /* figure out what kind of game we are playing,
      * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
      * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
     /* figure out what kind of game we are playing,
      * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
      * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
@@ -882,14 +924,17 @@ switch($mystatus)
     if($gametype=="solo")
       {
        $gametype = DB_get_solo_by_gameid($gameid);
     if($gametype=="solo")
       {
        $gametype = DB_get_solo_by_gameid($gameid);
-       $GT       = $gametype." ".$GT;
+       if($gametype=='silent')
+         $GT = 'normal';
+       else
+         $GT = $gametype." ".$GT;
       }
     else
       $gametype = "normal";
 
     set_gametype($gametype); /* this sets the $CARDS variable */
 
       }
     else
       $gametype = "normal";
 
     set_gametype($gametype); /* this sets the $CARDS variable */
 
-    /* get some infos about the game */
+    /* get some infos about the game, need to reset this, since it might have changed */
     $gamestatus = DB_get_game_status_by_gameid($gameid);
 
     /* has the game started? No, then just wait here...*/
     $gamestatus = DB_get_game_status_by_gameid($gameid);
 
     /* has the game started? No, then just wait here...*/
@@ -966,7 +1011,7 @@ switch($mystatus)
 
     /* output vorbehalte */
     $mygametype =  DB_get_gametype_by_gameid($gameid);
 
     /* output vorbehalte */
     $mygametype =  DB_get_gametype_by_gameid($gameid);
-    if($mygametype != "normal") /* only show when needed */
+    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";
       {
        echo "  <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
          "    <div class=\"trick\" id=\"trick0\">\n";
@@ -998,8 +1043,11 @@ switch($mystatus)
        $user    = $r[6];
 
        /* check if first schweinchen has been played */
        $user    = $r[6];
 
        /* check if first schweinchen has been played */
-       if( $GAME["schweinchen"] && ($r[0] == 19 || $r[0] == 20) )
-         $GAME["schweinchen"]++;
+       if( $GAME['schweinchen-who'] && ($r[0] == 19 || $r[0] == 20) )
+         if(!$GAME['schweinchen-first'])
+           $GAME['schweinchen-first'] = 1; /* playing the first fox */
+         else
+           $GAME['schweinchen-second'] = 1; /* this must be the second fox */
 
        /* save card to be able to find the winner of the trick later */
        $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
 
        /* save card to be able to find the winner of the trick later */
        $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
@@ -1106,20 +1154,23 @@ switch($mystatus)
 
            $playid = DB_play_card($trickid,$handcardid,$sequence);
 
 
            $playid = DB_play_card($trickid,$handcardid,$sequence);
 
-           /* check special output for schweinchen in case:
-            * schweinchen is in the rules, a fox has been played and the gametype is correct
+           /* check special output for schweinchen in case in case a fox is being played
+            * check for correct rules, etc. has already been done
             */
             */
-           if( $GAME["schweinchen"] &&
-               ($card == 19 || $card == 20) &&
-               ($gametype == "normal" || $gametype == "silent"|| $gametype=="trump"))
+           if( $GAME["schweinchen-who"] && ($card == 19 || $card == 20) )
              {
              {
-               $GAME["schweinchen"]++; // count how many have been played including this one
-               if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
-                 DB_insert_comment("Schweinchen! ",$playid,$myid);
-               if($RULES["schweinchen"]=="both" )
-                 DB_insert_comment("Schweinchen! ",$playid,$myid);
+               if(!$GAME['schweinchen-first'])
+                 $GAME['schweinchen-first'] = 1; /* playing the first fox */
+               else
+                 $GAME['schweinchen-second'] = 1; /* this must be the second fox */
+
+               if( ($GAME['schweinchen-second']==1 && $RULES['schweinchen']=='second') || $RULES['schweinchen']=='both')
+                 {
+                   DB_insert_comment("Schweinchen! ",$playid,$myid);
+                   $commentSchweinchen = "Schweinchen! ";
+                 }
                if ($debug)
                if ($debug)
-                 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
+                 echo "schweinchen = ".$GAME["schweinchen-who"]." ---<br />";
              }
 
            /* if sequence == 4 check who one in case of wedding */
              }
 
            /* if sequence == 4 check who one in case of wedding */
@@ -1172,7 +1223,7 @@ switch($mystatus)
                if(DB_get_gametype_by_gameid($gameid)=="solo")
                  {
                    $solo = DB_get_solo_by_gameid($gameid);
                if(DB_get_gametype_by_gameid($gameid)=="solo")
                  {
                    $solo = DB_get_solo_by_gameid($gameid);
-                   if($solo == "trump" || $solo == "silent")
+                   if($solo == 'trump' || $solo == 'silent')
                      $ok = 1; /* for trump solos and silent solos, foxes are ok */
                  }
                else
                      $ok = 1; /* for trump solos and silent solos, foxes are ok */
                  }
                else
@@ -1260,6 +1311,8 @@ switch($mystatus)
                $comment = $_REQUEST["comment"];
                if($comment != "")
                  DB_insert_comment($comment,$playid,$myid);
                $comment = $_REQUEST["comment"];
                if($comment != "")
                  DB_insert_comment($comment,$playid,$myid);
+               if($commentSchweinchen)
+                 $comment = $commentSchweinchen . $comment;
              };
 
            /* check for note */
              };
 
            /* check for note */
@@ -1813,8 +1866,15 @@ switch($mystatus)
            $type  = DB_get_gametype_by_gameid($gameid);
 
            if($type=="solo")
            $type  = DB_get_gametype_by_gameid($gameid);
 
            if($type=="solo")
-             output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
-           else
+             {
+               $solo = DB_get_solo_by_gameid($gameid);
+
+               if($solo!='silent') /* repeat game with same first player */
+                 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
+               else /* rotate normally */
+                 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
+             }
+           else /* rotate normally */
              output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
          }
       }
              output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
          }
       }