+ if($winner>0)
+ mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
+ else
+ echo "ERROR during scoring";
+
+ if($debug)
+ echo "DEBUG: position $winner won the trick <br />";
+
+ /* who is the next player? */
+ $next = $winner;
+ }
+ else
+ {
+ $next = DB_get_pos_by_hash($me)+1;
+ }
+ if($next==5) $next=1;
+
+ /* check for coment */
+ if(myisset("comment"))
+ {
+ $comment = $_REQUEST["comment"];
+ if($comment != "")
+ DB_insert_comment($comment,$playid,$myid);
+ };
+
+ /* display played card */
+ echo "<div class=\"card\">";
+ echo " you played <br />";
+ /* display comments */
+ display_card($card,$PREF["cardset"]);
+ if($comment!="")
+ echo " <br /> Your comment:<br /><span class=\"comment\">".$comment."</span>\n";
+ echo "</div>\n";
+
+ /*check if we still have cards left, else set status to gameover */
+ if(sizeof(DB_get_hand($me))==0)
+ {
+ DB_set_hand_status_by_hash($me,'gameover');
+ $mystatus='gameover';
+ }
+
+ /* if all players are done, set game status to game over,
+ * get the points of the last trick and send out an email
+ * to all players
+ */
+ $userids = DB_get_all_userid_by_gameid($gameid);
+
+ $done=1;
+ foreach($userids as $user)
+ if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
+ $done=0;
+
+ if($done)
+ DB_set_game_status_by_gameid($gameid,"gameover");
+
+ /* email next player, if game is still running */
+ if(DB_get_game_status_by_gameid($gameid)=='play')
+ {
+ $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
+ $email = DB_get_email_by_hash($next_hash);
+ $who = DB_get_userid_by_email($email);
+ DB_set_player_by_gameid($gameid,$who);
+
+ $message = "A card has been played in game $gameid.\n\n".
+ "It's your turn now.\n".
+ "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
+ mymail($email,$EmailName."a card has been played in game $gameid",$message);
+ }
+ else /* send out final email */
+ {
+ /* individual score */
+ $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
+ " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
+ " LEFT JOIN User ON User.id=Hand.user_id".
+ " LEFT JOIN Play ON Trick.id=Play.trick_id".
+ " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
+ " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
+ " WHERE Hand.game_id='$gameid'".
+ " GROUP BY User.fullname" );
+ $message = "The game is over. Thanks for playing :)\n";
+ $message .= "Final score:\n";
+ while( $r = mysql_fetch_array($result,MYSQL_NUM))
+ $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
+
+ $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
+ " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
+ " LEFT JOIN User ON User.id=Hand.user_id".
+ " LEFT JOIN Play ON Trick.id=Play.trick_id".
+ " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
+ " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
+ " WHERE Hand.game_id='$gameid'".
+ " GROUP BY Hand.party" );
+ $message .= "\nTotals:\n";
+ while( $r = mysql_fetch_array($result,MYSQL_NUM))
+ $message .= " ".$r[0]." ".$r[1]."\n";
+
+ /* send out final email */
+ $all = array();
+
+ foreach($userids as $user)
+ $all[] = DB_get_email_by_userid($user);
+ $To = implode(",",$all);
+
+ $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
+ mymail($To,$EmailName."game over (game $gameid) part 1(2)",$message.$help);
+
+ foreach($userids as $user)
+ {
+ $To = DB_get_email_by_userid($user);
+ $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+
+ $link = "Use this link to have a look at game $gameid: ".$host."?me=".$hash."\n\n" ;
+ mymail($To,$EmailName."game over (game $gameid) part 2(2)",$link);
+ }
+ }
+ }
+ else
+ {
+ echo "can't find that card?! <br />\n";
+ }
+ }
+ else if(myisset("card") && !$myturn )
+ {
+ echo "please wait until it's your turn! <br />\n";
+ }
+
+ $mycards = DB_get_hand($me);
+ $mycards = mysort($mycards,$gametype);
+ echo "<div class=\"mycards\">\n";
+
+ if($myturn && !myisset("card") && $mystatus=='play' )
+ {
+ echo "Hello ".$myname.", it's your turn! <br />\n";
+ echo "Your cards are: <br />\n";
+ echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
+
+ /* do we have to follow suite? */
+ $followsuit = 0;
+ if(have_suit($mycards,$firstcard))
+ $followsuit = 1;
+
+ foreach($mycards as $card)
+ {
+ if($followsuit && !same_type($card,$firstcard))
+ display_card($card,$PREF["cardset"]);
+ else
+ display_link_card($card,$PREF["cardset"]);
+ }
+
+ output_form_calls($me);
+
+ echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"100\" />\n";
+ echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
+ echo "<input type=\"submit\" value=\"submit\" />\n";
+ echo "</form>\n";
+ }
+ else if($mystatus=='play' )
+ {
+ echo "Your cards are: <br />\n";
+ foreach($mycards as $card)
+ display_card($card,$PREF["cardset"]);
+
+ echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
+ output_form_calls($me);
+ echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"100\" />\n";
+ echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
+ echo "<input type=\"submit\" value=\"submit\" />\n";
+ echo "</form>\n";
+
+ }
+ else if($mystatus=='gameover')
+ {
+ /* get time from the last action of the game */
+ $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
+ $r = mysql_fetch_array($result,MYSQL_NUM);
+ $gameend = time() - strtotime($r[0]);
+
+ if( $gameend < 60*60*24*7 )
+ {
+ echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
+ echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"100\" />\n";
+ echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
+ echo "<input type=\"submit\" value=\"submit\" />\n";
+ echo "</form>\n";
+ }
+
+ $oldcards = DB_get_all_hand($me);
+ $oldcards = mysort($oldcards,$gametype);
+ echo "Your cards were: <br />\n";
+ foreach($oldcards as $card)
+ display_card($card,$PREF["cardset"]);
+
+ $userids = DB_get_all_userid_by_gameid($gameid);
+ foreach($userids as $user)
+ {
+ $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
+
+ if($userhash!=$me)
+ {
+ echo "<br />";
+
+ $name = DB_get_name_by_userid($user);
+ $oldcards = DB_get_all_hand($userhash);
+ $oldcards = mysort($oldcards,$gametype);
+ echo "$name's cards were: <br />\n";
+ foreach($oldcards as $card)
+ display_card($card,$PREF["cardset"]);
+ }
+ };
+ }
+ echo "</div>\n";
+
+ /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
+ if($mystatus=='play')
+ break;
+
+ /* the following happens only when the gamestatus is 'gameover' */
+ /* check if game is over, display results */
+ if(DB_get_game_status_by_gameid($gameid)=='play')
+ {
+ echo "the game is over for you.. other people still need to play though";
+ }
+ else
+ {
+ echo "the game is over now...<br />\n";
+
+ $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
+ " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
+ " LEFT JOIN User ON User.id=Hand.user_id".
+ " LEFT JOIN Play ON Trick.id=Play.trick_id".
+ " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
+ " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
+ " WHERE Hand.game_id='$gameid'".
+ " GROUP BY User.fullname" );
+ echo "Final Score:<br />\n".
+ " <table>\n";;
+ while( $r = mysql_fetch_array($result,MYSQL_NUM))
+ echo " <tr><td> ".$r[0]."</td><td>(".$r[2].")</td><td> ".$r[1]."</td></tr>";
+ echo "</table>\n";
+
+
+ $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
+ " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
+ " LEFT JOIN User ON User.id=Hand.user_id".
+ " LEFT JOIN Play ON Trick.id=Play.trick_id".
+ " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
+ " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
+ " WHERE Hand.game_id='$gameid'".
+ " GROUP BY Hand.party" );
+ echo "Totals:<br />\n".
+ " <table> \n";
+ while( $r = mysql_fetch_array($result,MYSQL_NUM))
+ echo " <tr><td>".$r[0]."</td><td> ".$r[1]."</td></tr>\n";
+ echo "</table>\n";
+
+ $session = DB_get_session_by_gameid($gameid);
+ $result = mysql_query("SELECT id,create_date FROM Game".
+ " WHERE session=$session".
+ " ORDER BY create_date DESC".
+ " LIMIT 1");
+ $r = -1;
+ if($result)
+ $r = mysql_fetch_array($result,MYSQL_NUM);
+
+ if(!$session || $gameid==$r[0])
+ {
+ /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
+ $names = DB_get_all_names_by_gameid($gameid);
+ $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
+ output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
+ }
+ }
+ break;
+ default:
+ myerror("error in testing the status");
+ }
+ output_footer();
+ DB_close();