Merge branch 'master' of http://nubati.net/git/e-DoKo
[e-DoKo.git] / include / game.php
index fbf554fbd19e859fadbf519eabd77e455677068a..25e2b2ff8166d24097d722dd81273d49b5063d38 100644 (file)
@@ -9,9 +9,7 @@ if(!isset($HOST))
 if(!myisset("me"))
   {
     echo "Hmm, you really shouldn't mess with the urls.<br />\n";
-    output_footer();
-    DB_close();
-    exit();
+    return;
   }
 $me = $_REQUEST["me"];
 
@@ -21,17 +19,11 @@ if(!$myid)
   {
     echo "Can't find you in the database, please check the url.<br />\n";
     echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
-    output_footer();
-    DB_close();
-    exit();
+    return;
   }
 
 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"]);
-
 /* the user has done something, update the timestamp */
 DB_update_user_timestamp($myid);
 
@@ -107,23 +99,54 @@ set_gametype('normal');
 /* put everyting in a form */
 echo "<form action=\"index.php?action=game&amp;me=$me\" method=\"post\">\n";
 
-/* output game */
+/* handle user notes (only possible while game is running)*/
+if( $mystatus!='gameover'  )
+  if(myisset("note"))
+{
+  $note = $_REQUEST['note'];
+  
+  if($note != "")
+    DB_insert_note($note,$gameid,$myid);
+};
+output_user_notes($myid,$gameid,$mystatus);
+
+/* handle calls */
+if(myisset("call")  && $_REQUEST["call"]  == "120" && can_call(120,$me))
+  $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
+if(myisset("call")  && $_REQUEST["call"]  == "90" && can_call(90,$me))
+  $result = DB_query("UPDATE Hand SET point_call='90'  WHERE hash='$me' ");
+if(myisset("call")  && $_REQUEST["call"]  == "60" && can_call(60,$me))
+  $result = DB_query("UPDATE Hand SET point_call='60'  WHERE hash='$me' ");
+if(myisset("call")  && $_REQUEST["call"]  == "30" && can_call(30,$me))
+  $result = DB_query("UPDATE Hand SET point_call='30'  WHERE hash='$me' ");
+if(myisset("call")  && $_REQUEST["call"]  == "0" && can_call(0,$me))
+  $result = DB_query("UPDATE Hand SET point_call='0'   WHERE hash='$me' ");
 
 /* 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&amp;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";
   }
 
@@ -155,7 +178,7 @@ switch($mystatus)
     else
       {
        /* check the result, if player wants to join, got next stage, else cancel game */
-       if($_REQUEST["in"] == "no" && !$skip)
+       if(!$skip && $_REQUEST["in"] == "no" )
          {
            /* cancel the game */
            $message = "Hello, \n\n".
@@ -470,9 +493,7 @@ switch($mystatus)
 
            echo "The game has been canceled because ".DB_get_name('userid',$nines).
              " has five or more nines and nobody is playing solo.\n";
-           output_footer();
-           DB_close();
-           exit();
+           return;
          }
        else if($poverty==1) /* one person has poverty */
          {
@@ -605,7 +626,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&amp;me=".$hash."\n\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);
                  }
              }
@@ -631,7 +652,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&amp;me=".$whohash."\n\n" ;
+                     "Use this link to play a card: ".$HOST.$INDEX."?action=game&me=".$whohash."\n\n" ;
                    mymail($email,$EmailName."Poverty (game ".DB_format_gameid($gameid).") ",$message);
                  }
              }
@@ -713,7 +734,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&amp;me=".$hash."\n\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);
              }
          }
@@ -801,9 +822,7 @@ switch($mystatus)
                DB_cancel_game($me);
 
                echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
-               output_footer();
-               DB_close();
-               exit();
+               return;
              }
            else
              {
@@ -902,7 +921,7 @@ 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&amp;me=".$userhash."\n\n" ;
+                     " ".$HOST.$INDEX."?action=game&me=".$userhash."\n\n" ;
                    mymail($To,$EmailName." double poverty (game ".DB_format_gameid($gameid).")",$message);
 
 
@@ -955,7 +974,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&amp;me=".$hash."\n\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);
              }
          }
@@ -1017,16 +1036,6 @@ switch($mystatus)
            DB_insert_comment($comment,$playid,$myid);
        };
 
-    /* handle notes in case player didn't play a card, allow notes only during a game */
-    if( (!myisset("card") && $mystatus=='play')  )
-      if(myisset("note"))
-       {
-         $note = $_REQUEST["note"];
-
-         if($note != "")
-           DB_insert_note($note,$gameid,$myid);
-       };
-
     /* get everything relevant to display the tricks */
     $result = DB_query("SELECT Hand_Card.card_id as card,".
                       "       Hand.position as position,".
@@ -1045,7 +1054,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  */
@@ -1061,7 +1070,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++)
          {
@@ -1089,6 +1098,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'])
@@ -1136,7 +1149,6 @@ switch($mystatus)
        /* end of trick? */
        if($seq==4)
          {
-           $trickNR++;
            echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
          }
       }
@@ -1178,18 +1190,6 @@ switch($mystatus)
            /* update Game timestamp */
            DB_update_game_timestamp($gameid);
 
-           /* check if a call was made, must do this before we set the card status to played */
-           if(myisset("call")  && $_REQUEST["call"]  == "120" && can_call(120,$me))
-             $result = DB_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
-           if(myisset("call")  && $_REQUEST["call"]  == "90" && can_call(90,$me))
-             $result = DB_query("UPDATE Hand SET point_call='90'  WHERE hash='$me' ");
-           if(myisset("call")  && $_REQUEST["call"]  == "60" && can_call(60,$me))
-             $result = DB_query("UPDATE Hand SET point_call='60'  WHERE hash='$me' ");
-           if(myisset("call")  && $_REQUEST["call"]  == "30" && can_call(30,$me))
-             $result = DB_query("UPDATE Hand SET point_call='30'  WHERE hash='$me' ");
-           if(myisset("call")  && $_REQUEST["call"]  == "0" && can_call(0,$me))
-             $result = DB_query("UPDATE Hand SET point_call='0'   WHERE hash='$me' ");
-
            /* mark card as played */
            DB_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
                     DB_quote_smart($card));
@@ -1419,7 +1419,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&amp;me=".$next_hash."\n\n" ;
+                 "Use this link to play a card: ".$HOST.$INDEX."?action=game&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);
              }
@@ -1646,35 +1646,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();
@@ -1692,7 +1666,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&amp;me=".$hash."\n\n" ;
+                       $HOST.$INDEX."?action=game&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);
                    }
@@ -1708,7 +1682,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 */
@@ -1736,14 +1710,8 @@ switch($mystatus)
        }
 
       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);
-      foreach($notes as $note)
-       echo "$note <hr />\n";
-      echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
-      echo "</div> \n";
+      echo "  <li onclick=\"hl_next();\" class=\"old\"><a href=\"#\">next</a></li>\n";
+      echo "</ul>\n"; /* end ul tricks*/
 
       $mycards = DB_get_hand($me);
       $mycards = mysort($mycards,$gametype);
@@ -1865,15 +1833,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);
 
@@ -1884,15 +1843,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";
 
@@ -1929,9 +1882,4 @@ switch($mystatus)
              output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
          }
       }
-
-
-    output_footer();
-    DB_close();
-    exit();
 ?>
\ No newline at end of file