From c81c7401e5db346fb07aa54183c1e6456d044a9b Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Sun, 7 Feb 2010 18:57:46 -0800
Subject: CLEANUP: removed ereg-functions (deprecated in newer php versions)

used strpos and preg_replace instead
---
 include/functions.php | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/include/functions.php b/include/functions.php
index 998ca02..16d094c 100644
--- a/include/functions.php
+++ b/include/functions.php
@@ -146,8 +146,8 @@ function sendmail($To,$Subject,$message)
        * change txt -> html
        */
       $message = str_replace("\n","<br />\n",$message);
-      $message = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
-                     "<a href=\"\\0\">\\0</a>", $message);
+      $message = preg_replace("#(\w+://[^<>\s]+[\w/]*)#",
+			      "<a href=\"$1\">$1</a>", $message);
 
       echo "<br />To: $To<br />";
       if($header != "")
@@ -980,21 +980,21 @@ function display_table ()
 	else
 	  echo "   <img src=\"pics/button/wedding_partner_button.png\" class=\"button\" alt=\"wedding partner\" title=\"wedding partner\" />";
 
-      if(ereg("solo",$GT) && $party=="re")
+      if( (strpos($GT,"solo")!==false) && $party=="re")
 	{
-	  if(ereg("queen",$GT))
+	  if(strpos($GT,"queen")!==false)
 	    echo "   <img src=\"pics/button/queensolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Queen solo\" />";
-	  else if(ereg("jack",$GT))
+	  else if(strpos($GT,"jack")!==false)
 	    echo "   <img src=\"pics/button/jacksolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Jack solo\" />";
-	  else if(ereg("club",$GT))
+	  else if(strpos($GT,"club")!==false)
 	    echo "   <img src=\"pics/button/clubsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Club solo\" />";
-	  else if(ereg("spade",$GT))
+	  else if(strpos($GT,"spade")!==false)
 	    echo "   <img src=\"pics/button/spadesolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Spade solo\" />";
-	  else if(ereg("heart",$GT))
+	  else if(strpos($GT,"heart")!==false)
 	    echo "   <img src=\"pics/button/heartsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Heart solo\" />";
-	  else if(ereg("trumpless",$GT))
+	  else if(strpos($GT,"trumpless")!==false)
 	    echo "   <img src=\"pics/button/notrumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trumpless solo\" />";
-	  else if(ereg("trump",$GT))
+	  else if(strpos($GT,"trump")!==false)
 	    echo "   <img src=\"pics/button/trumpsolo_button.png\" class=\"button\" alt=\"$GT\" title=\"Trump solo\" />";
 	}
 
-- 
cgit v1.2.3-18-g5258


From d6b5cc48df70ac62b7dbb3b90b614e7b50085754 Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Sun, 7 Feb 2010 19:04:24 -0800
Subject: CLEANUP: added some text on how to get a license for the german cards

---
 cards/altenburg/License | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/cards/altenburg/License b/cards/altenburg/License
index 02f0f20..303db83 100644
--- a/cards/altenburg/License
+++ b/cards/altenburg/License
@@ -1,8 +1,12 @@
-Verwendung der ASS Altenburger Spielkartenbilder mit Genehmigung der
+You need to a license from
+
 Spielkartenfabrik Altenburg GmbH,
 
 ASS Altenburger
-Leipziger Strasse 7, 04600 Altenburg bis zum 31.12.2009
+Leipziger Strasse 7, 04600 Altenburg
 
 Email: info@spielkarten.com
-internet: www.spielkarten.com
\ No newline at end of file
+internet: www.spielkarten.com
+
+for these images. This involves some paperwork, but was free for non-commercial purposes.
+
-- 
cgit v1.2.3-18-g5258


From bb470d6deeb78f69eb3875f0280a59abfeaa8e7d Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Sun, 7 Feb 2010 19:09:37 -0800
Subject: updated copyright notice in footer

---
 include/output.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/output.php b/include/output.php
index 06eb8de..ce72c4e 100644
--- a/include/output.php
+++ b/include/output.php
@@ -378,7 +378,7 @@ function output_footer()
 
   echo "</div>\n\n";
   echo "<div class=\"footer\">\n";
-  echo "  <p class=\"left\"> copyright 2006-2009 Arun Persaud, Lance Thornton(graphics), Jeff Zerger(database support) <br />\n".
+  echo "  <p class=\"left\"> copyright 2006,2007,2008,2009,2010 Arun Persaud, Lance Thornton(graphics),  Jeff Zerger(database support) <br />\n".
     "  Verwendung der [deutschen] Kartenbilder mit Genehmigung <br />der Spielkartenfabrik Altenburg GmbH,(c) ASS Altenburger <br />\n".
     "  - ASS Altenburger Spielkarten - Spielkartenfabrik Altenburg GmbH <br />\n".
     "  a Carta Mundi Company Email: info@spielkarten.com Internet: www.spielkarten.com</p>\n";
-- 
cgit v1.2.3-18-g5258


From 76ce476d3c48709bf6a69b3cc5bd333c4593d66b Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Thu, 4 Mar 2010 12:48:10 -0800
Subject: changed message for new dealer into message for new startplayer

since there really is no dealer, this was more confusing than helpful.
---
 include/output.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/output.php b/include/output.php
index ce72c4e..018372b 100644
--- a/include/output.php
+++ b/include/output.php
@@ -31,7 +31,7 @@ function output_ask_for_new_game($playerA,$playerB,$playerC,$playerD,$oldgameid)
   global $RULES;
 
   echo "<div class=\"message\">\n<form action=\"index.php?action=new\" method=\"post\">\n";
-  echo "Do you want to continue playing?(This will start a new game, with $playerD as dealer.)\n";
+  echo "Do you want to continue playing?(This will start a new game, with $playerA starting the game.)\n";
   echo "  <input type=\"hidden\" name=\"PlayerA\" value=\"$playerA\" />\n";
   echo "  <input type=\"hidden\" name=\"PlayerB\" value=\"$playerB\" />\n";
   echo "  <input type=\"hidden\" name=\"PlayerC\" value=\"$playerC\" />\n";
-- 
cgit v1.2.3-18-g5258


From f0e93ad1feb9ba47dc1db80354b5f28d29b5ab27 Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Thu, 4 Mar 2010 19:11:18 -0800
Subject: BUGFIX: show "It's your turn in these games" menu also when you
 played the last card in a game

When showing the menu we test for the status the user had before he played the card.

This fixes Issue #35.
---
 include/game.php | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/include/game.php b/include/game.php
index a1c02a0..93ab461 100644
--- a/include/game.php
+++ b/include/game.php
@@ -47,6 +47,7 @@ global $GAME,$RULES,$CARDS;
 $gameid   = DB_get_gameid_by_hash($me);
 $myname   = DB_get_name('hash',$me);
 $mystatus = DB_get_status_by_hash($me);
+$origmystatus = DB_get_status_by_hash($me); /* to show "it's your turn" menu when game has just finished */
 $mypos    = DB_get_pos_by_hash($me);
 $myhand   = DB_get_handid('hash',$me);
 $myparty  = DB_get_party_by_hash($me);
@@ -2031,11 +2032,20 @@ switch($mystatus)
 /* output other games where it is the users turn
  * make sure that the people looking at old games don't see the wrong games here
  */
-if( $mystatus != 'gameover' )
-  display_user_menu($myid);
+if( $gamestatus != 'gameover' )
+  {
+    /* game isn't over, only valid user can get here, so show menu */
+    display_user_menu($myid);
+  }
+else if(  $origmystatus != 'gameover' )
+  {
+    /* user just played the very last card, game is now over, it's still ok to show the menu though */
+    display_user_menu($myid);
+  }
 else if(  $mystatus == 'gameover' &&
-       isset($_SESSION['id']) )
+	  isset($_SESSION['id']) )
   {
+    /* user is looking at someone else's game, show the menu for the correct user */
     display_user_menu($_SESSION['id']);
   }
 else
-- 
cgit v1.2.3-18-g5258


From bc566c5be2acb50a04003e25d3ec9cde4e791786 Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Thu, 4 Mar 2010 21:02:56 -0800
Subject: fixed typo in create statement

---
 create_database.sql | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/create_database.sql b/create_database.sql
index fd7b22a..e9577c9 100644
--- a/create_database.sql
+++ b/create_database.sql
@@ -263,7 +263,7 @@ CREATE TABLE `Card_Exchange` (
 
 
 /*!40000 ALTER TABLE `Hand_Card` DISABLE KEYS */;
-LOCK TABLES `Hand_Card` WRITE;
+LOCK TABLES `Card_Exchange` WRITE;
 UNLOCK TABLES;
 /*!40000 ALTER TABLE `Hand_Card` ENABLE KEYS */;
 
-- 
cgit v1.2.3-18-g5258


From fceb0d9bc48ae848461e728abaf5065a2578da61 Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Thu, 4 Mar 2010 21:43:37 -0800
Subject: CLEANUP: moved more statistics into the sortable table

---
 include/functions.php | 53 ++++++++++++++++++++++++++++++++++++++++++++++++---
 include/stats.php     | 48 ++++++----------------------------------------
 2 files changed, 56 insertions(+), 45 deletions(-)

diff --git a/include/functions.php b/include/functions.php
index 16d094c..173ccd4 100644
--- a/include/functions.php
+++ b/include/functions.php
@@ -1156,7 +1156,8 @@ function generate_global_score_table()
 
   /* save information in an array */
   while( $r = DB_fetch_array($result))
-    $player[$r[0]] = array('name'=> $r[1], 'points' => 0 ,'nr' => 0);
+    $player[$r[0]] = array('name'=> $r[1], 'points' => 0 , 'nr' => 0, 'active' => 0,
+			   'response' => 0 , 'solo' => 0, 'soloavg' => 0);
 
   /* get points and generate table */
   foreach($gameids as $gameid)
@@ -1182,9 +1183,53 @@ function generate_global_score_table()
 	}
     }
 
+  /* add number of active games */
+  $result = DB_query_array_all("SELECT user_id, COUNT(*) as c  " .
+			       " FROM Hand".
+			       " LEFT JOIN Game ON Game.id=game_id".
+			       " WHERE Game.status IN ('pre','play')".
+			       " GROUP BY user_id");
+
+  foreach($result as $res)
+    {
+      $player[$res[0]]['active'] = $res[1];
+    }
+
+  /* response time of users*/
+  $result = DB_query_array_all("SELECT user_id,".
+                              "IFNULL(AVG(if(P1.sequence in (2,3,4),".
+                              "-timestampdiff(MINUTE,mod_date,(select mod_date from Play P2 where P1.trick_id=P2.trick_id  and P2.sequence=P1.sequence-1)),NULL )),1e9) as a ".
+                              "FROM Play P1 ".
+                              "LEFT JOIN Hand_Card ON P1.hand_card_id=Hand_Card.id ".
+                              "LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
+                              "GROUP BY user_id ");
+
+  foreach($result as $res)
+    {
+      $player[$res[0]]['response'] = $res[1];
+    }
+
+  /* most solos */
+  $result = DB_query_array_all("SELECT user_id as uid,".
+			       "       COUNT(*), ".
+			       "       COUNT(*)/(SELECT COUNT(*) FROM Hand LEFT JOIN User ON User.id=Hand.user_id WHERE User.id=uid) as c ".
+			       " FROM Game ".
+			       " LEFT JOIN Hand ON Hand.position=startplayer AND Game.id=Hand.game_id ".
+			       " WHERE type='solo' AND Game.status='gameover' ".
+			       " GROUP BY user_id ");
+
+  foreach($result as $res)
+    {
+      $player[$res[0]]['solo'] = $res[1];
+      $player[$res[0]]['soloavg'] = $res[2];
+    }
+
+
+  /* sort everything nicely */
+
   function cmp($a,$b)
   {
-    if($a['nr']==0 ) return 1;
+    if($a['nr']==0) return 1;
     if($b['nr']==0) return 1;
 
     $a=$a['points']/$a['nr'];
@@ -1196,11 +1241,13 @@ function generate_global_score_table()
   }
   usort($player,'cmp');
 
+
   foreach($player as $pl)
     {
       /* limit to players with at least 10 games */
       if($pl['nr']>10)
-	$return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr']);
+	$return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr'],$pl['active'],
+			   $pl['response'],$pl['solo'],$pl['soloavg']);
     }
 
   return $return;
diff --git a/include/stats.php b/include/stats.php
index fb56df8..61e7d88 100644
--- a/include/stats.php
+++ b/include/stats.php
@@ -24,6 +24,9 @@
 if(!isset($HOST))
   exit;
 
+/* turn warnings off (problem with two warnings in function.php */
+error_reporting(!E_ALL & ~E_NOTICE);
+
 $name  = $_SESSION["name"];
 $email = DB_get_email('name',$name);
 
@@ -190,46 +193,6 @@ if( !$content = getCache("cache/stats.html",60*60*24) )
   array_unshift($result,array("Position","Number of tricks"));
   echo output_table($result,"Tricks at the table","stats");
 
-  /* most solos */
-  $result = DB_query_array_all("SELECT fullname as fname,".
-			       "       COUNT(*), ".
-			       "       COUNT(*)/(SELECT COUNT(*) FROM Hand LEFT JOIN User ON User.id=Hand.user_id WHERE fullname=fname) as c ".
-			       " FROM Game ".
-			       " LEFT JOIN Hand ON Hand.position=startplayer AND Game.id=Hand.game_id ".
-			       " LEFT JOIN User ON User.id=Hand.user_id ".
-			       " WHERE type='solo' AND Game.status='gameover' ".
-			       " GROUP BY user_id ".
-			       " ORDER BY c DESC;");
-  array_unshift($result,array("Name","Number of solos","Solos/game"));
-  echo output_table($result,"Most solos","stats");
-
-
-  /* most active games */
-  $result = DB_query_array_all("SELECT fullname, COUNT(*) as c  " .
-			       " FROM Hand".
-			       " LEFT JOIN User ON User.id=user_id".
-			       " LEFT JOIN Game ON Game.id=game_id".
-			       " WHERE Game.status IN ('pre','play')".
-			       " GROUP BY user_id".
-			       " ORDER BY c DESC LIMIT 7" );
-  array_unshift($result,array("Name","Number of active games"));
-  echo output_table($result,"Active games","stats");
-
-  /* response time of users*/
-  $result = DB_query_array_all("SELECT User.fullname,".
-			       "IFNULL(AVG(if(P1.sequence in (2,3,4),".
-			       "-timestampdiff(MINUTE,mod_date,(select mod_date from Play P2 where P1.trick_id=P2.trick_id  and P2.sequence=P1.sequence-1)),NULL )),1e9) as a, ".
-			       " COUNT(*) as na ".
-			       "FROM Play P1 ".
-			       "LEFT JOIN Hand_Card ON P1.hand_card_id=Hand_Card.id ".
-			       "LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
-			       "LEFT JOIN User ON Hand.user_id=User.id ".
-			       "GROUP BY user_id ".
-			       "HAVING na>8 ".
-			       "ORDER BY a " );
-  array_unshift($result,array("Name","Average minutes before response","trick count"));
-  echo output_table($result,"Response","stats");
-
   /*
  does the party win more often if they start
 
@@ -244,8 +207,9 @@ if( !$content = getCache("cache/stats.html",60*60*24) )
  echo " games</p>\n";
   */
   $result = generate_global_score_table();
-  array_unshift($result,array("Name","Average score per game","Total Points","Number of games"));
-  echo output_table($result,"Points per game (need more than 10 games)","stats","ScoreTable");
+  array_unshift($result,array('Name','Average score per game','Total Points','Number of games', 'Active games',
+			      'Response Time [min]','Number of solos','Solos/game'));
+  echo output_table($result,'Players (need more than 10 games)','stats','ScoreTable');
 
   /*
    * how often is the last trick a non-trump trick
-- 
cgit v1.2.3-18-g5258


From cf764594cd00e66ee812c5d63b016bf286178f67 Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Sat, 13 Mar 2010 11:52:36 -0800
Subject: added check in DB_open for correct DB-version

---
 include/db.php | 11 +++++++++--
 index.php      | 12 +++++++++---
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/include/db.php b/include/db.php
index b79da87..5503a4a 100644
--- a/include/db.php
+++ b/include/db.php
@@ -30,17 +30,24 @@ if(!isset($HOST))
 
 function DB_open()
 {
+  $version_needed = 1;
+
   global $DB,$DB_user,$DB_host,$DB_database,$DB_password;
   $DB = @mysql_connect($DB_host,$DB_user, $DB_password);
   if ( $DB )
     {
-      mysql_select_db($DB_database) or die('Could not select database');
+      mysql_select_db($DB_database) or die('Error: Could not select database');
     }
   else
     {
       echo mysql_errno() . ": " . mysql_error(). "\n";
       return -1;
-    }
+    };
+
+  $version = DB_get_version();
+  if ($version != $version_needed)
+    return -2;
+
   return 0;
 }
 
diff --git a/index.php b/index.php
index 83618f3..54efdcb 100644
--- a/index.php
+++ b/index.php
@@ -36,11 +36,17 @@ include_once("./include/functions.php");   /* the rest */
 config_check();
 
 /* open the database */
-if(DB_open()<0)
+$DBopen = DB_open();
+if($DBopen<0)
   {
     output_header();
-    echo "Database error, can't connect... Please wait a while and try again. ".
-      "If the problem doesn't go away feel free to contact $ADMIN_NAME at $ADMIN_EMAIL.";
+
+    if($DBopen == -1)
+      echo "Database error, can't connect... Please wait a while and try again. ".
+	"If the problem doesn't go away feel free to contact $ADMIN_NAME at $ADMIN_EMAIL.";
+    else if ($DBopen == -2)
+      echo "Wrong database version, please update your database using the update.php script.";
+
     output_footer();
     exit();
   }
-- 
cgit v1.2.3-18-g5258


From 850378a3e2da48d387dc565cc6e3c2b47f12e34d Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Sat, 13 Mar 2010 15:59:07 -0800
Subject: NEW FEATURE: having only low trump (fox and below) can now be a
 Vorbehalt

The low trump can either be treated as poverty or similar to 5 nines, that is the game is canceled unless someone plays a solo.

Also minor code cleanup.
---
 create_database.sql   |  7 ++--
 include/db.php        | 15 ++++----
 include/functions.php | 52 ++++++++++++++++++++++-----
 include/game.php      | 98 +++++++++++++++++++++++++++++++++++----------------
 include/newgame.php   |  5 +--
 include/output.php    | 40 ++++++++++++++++++---
 update_db.php         | 16 +++++++--
 7 files changed, 176 insertions(+), 57 deletions(-)

diff --git a/create_database.sql b/create_database.sql
index e9577c9..71a6dfc 100644
--- a/create_database.sql
+++ b/create_database.sql
@@ -48,7 +48,7 @@ CREATE TABLE `Version` (
 
 /*!40000 ALTER TABLE `Card` DISABLE KEYS */;
 LOCK TABLES `Version` WRITE;
-INSERT INTO `Version` VALUES (1);
+INSERT INTO `Version` VALUES (2);
 UNLOCK TABLES;
 /*!40000 ALTER TABLE `Version` ENABLE KEYS */;
 
@@ -141,7 +141,7 @@ CREATE TABLE `Game` (
   `sickness` int(11) default NULL,
   `startplayer` tinyint(4) default '1',
   `player` int(11) default NULL,
-  `status` enum('pre','play','gameover','cancel-timedout','cancel-nines','cancel-trump','cancel-noplay') default NULL,
+  `status` enum('pre','play','gameover','cancel-timedout','cancel-nines','cancel-trump','cancel-noplay','cancel-lowtrump') default NULL,
   `ruleset` int(11) default NULL,
   `session` int(11) default NULL,
   `id` int(11) NOT NULL auto_increment,
@@ -171,6 +171,7 @@ CREATE TABLE `Rulesets` (
   `dullen` enum('none','firstwins','secondwins') default 'secondwins',
   `schweinchen` enum ('none','both','second','secondaftercall') default 'second',
   `call` enum ('1st-own-card','5th-card','9-cards') default '1st-own-card',
+  `lowtrump` enum('poverty','cancel','none') default 'poverty',
   `id` int(11) NOT NULL auto_increment,
   UNIQUE KEY `id` (`id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
@@ -200,7 +201,7 @@ CREATE TABLE `Hand` (
   `status` enum('start','init','check','poverty','play','gameover') default 'start',
   `position` tinyint(4) NOT NULL default '0',
   `party` enum('re','contra') default NULL,
-  `sickness` enum('wedding','nines','poverty','solo') default NULL,
+  `sickness` enum('wedding','nines','poverty','solo','lowtrump') default NULL,
   `solo` enum('trumpless','jack','queen','trump','club','spade','heart','silent') default NULL,
   `point_call` enum('120','90','60','30','0') default NULL,
   UNIQUE KEY `id` (`id`),
diff --git a/include/db.php b/include/db.php
index 5503a4a..2a927f5 100644
--- a/include/db.php
+++ b/include/db.php
@@ -30,7 +30,7 @@ if(!isset($HOST))
 
 function DB_open()
 {
-  $version_needed = 1;
+  $version_needed = 2;
 
   global $DB,$DB_user,$DB_host,$DB_database,$DB_password;
   $DB = @mysql_connect($DB_host,$DB_user, $DB_password);
@@ -703,14 +703,15 @@ function DB_get_hashes_by_session($session,$user)
   return $r;
 }
 
-function DB_get_ruleset($dullen,$schweinchen,$call)
+function DB_get_ruleset($dullen,$schweinchen,$call,$lowtrump)
 {
   $r = array();
 
   $result = DB_query("SELECT id FROM Rulesets WHERE".
 		     " dullen=".DB_quote_smart($dullen)." AND ".
 		     " Rulesets.call=".DB_quote_smart($call)." AND ".
-		     " schweinchen=".DB_quote_smart($schweinchen));
+		     " schweinchen=".DB_quote_smart($schweinchen)." AND ".
+		     " lowtrump=".DB_quote_smart($lowtrump));
   if($result)
     $r    = DB_fetch_array($result);
 
@@ -722,6 +723,7 @@ function DB_get_ruleset($dullen,$schweinchen,$call)
       $result = DB_query("INSERT INTO Rulesets VALUES (NULL, NULL, ".
 			 DB_quote_smart($dullen).",".
 			 DB_quote_smart($schweinchen).",".
+			 DB_quote_smart($lowtrump).",".
 			 DB_quote_smart($call).
 			 ", NULL)");
       if($result)
@@ -834,9 +836,10 @@ function DB_get_RULES($gameid)
 		      " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
 		      " WHERE Game.id='$gameid'" );
 
-  $RULES["dullen"]      = $r[2];
-  $RULES["schweinchen"] = $r[3];
-  $RULES["call"]        = $r[4];
+  $RULES['dullen']      = $r[2];
+  $RULES['schweinchen'] = $r[3];
+  $RULES['lowtrump']    = $r[4];
+  $RULES['call']        = $r[5];
 
   return $RULES;
 }
diff --git a/include/functions.php b/include/functions.php
index 173ccd4..a06f3d4 100644
--- a/include/functions.php
+++ b/include/functions.php
@@ -455,6 +455,39 @@ function count_trump($cards,$status='pregame')
   return $trump;
 }
 
+function check_low_trump($cards)
+{
+  global $RULES;
+
+  if($RULES['lowtrump']=='none')
+    return 0;
+
+  /* check if we have low trump */
+
+  $lowtrump=1;
+  foreach($cards as $card)
+    {
+      /* card a trump, but not a diamond? */
+      if( $card<19 )
+	 $lowtrump=0;
+    }
+
+  /* handle case where player has schweinchen */
+  if( in_array("19",$cards) && in_array("20",$cards) )
+    switch($RULES["schweinchen"])
+      {
+      case "both":
+      case "second":
+      case "secondaftercall":
+	$lowtrump=0;
+	break;
+      case "none":
+	break;
+      }
+
+  return $lowtrump;
+}
+
 function  create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
 {
   global $debug;
@@ -467,12 +500,12 @@ function  create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD)
       $r[ 1]=2;     $r[13]=23;   $r[25]=14;	  $r[37]=38;
       $r[ 2]=3;     $r[14]=27;   $r[26]=15;	  $r[38]=39;
       $r[ 3]=4;     $r[15]=16;   $r[27]=28;	  $r[39]=40;
-      $r[ 4]=5;     $r[16]=17;   $r[28]=29;	  $r[40]=41;
+      $r[ 4]=5;     $r[16]=17;   $r[28]=29;	  $r[40]=21;
       $r[ 5]=18;    $r[17]=6;    $r[29]=30;	  $r[41]=42;
-      $r[ 6]=21;    $r[18]=7;    $r[30]=31;	  $r[42]=43;
-      $r[ 7]=22;    $r[19]=8;    $r[31]=32;	  $r[43]=44;
-      $r[ 8]=45;    $r[20]=9;    $r[32]=19;	  $r[44]=33;
-      $r[ 9]=46;    $r[21]=10;   $r[33]=20;	  $r[45]=24;
+      $r[ 6]=41;    $r[18]=7;    $r[30]=31;	  $r[42]=43;
+      $r[ 7]=22;    $r[19]=8;    $r[31]=32;	  $r[43]=20;
+      $r[ 8]=45;    $r[20]=9;    $r[32]=33;	  $r[44]=19;
+      $r[ 9]=46;    $r[21]=10;   $r[33]=44;	  $r[45]=24;
       $r[10]=35;    $r[22]=11;   $r[34]=48;	  $r[46]=25;
       $r[11]=36;    $r[23]=12;   $r[35]=34;	  $r[47]=26;
     }
@@ -933,7 +966,7 @@ function display_table ()
 	  echo " Schweinchen. <br />";
 
       if($GT=="poverty" && $party=="re")
-	if($sickness=="poverty")
+	if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
 	  {
 	    $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
 	    $cards    = DB_get_all_hand($userhash);
@@ -948,7 +981,7 @@ function display_table ()
 
       if($GT=="dpoverty")
 	if($party=="re")
-	  if($sickness=="poverty")
+	  if($sickness=="poverty" || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
 	    {
 	      $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
 	      $cards    = DB_get_all_hand($userhash);
@@ -961,7 +994,7 @@ function display_table ()
 	  else
 	    echo "   <img src=\"pics/button/poverty_partner_button.png\" class=\"button\" alt=\"poverty >\" title=\"poverty partner\" />";
 	else
-	  if($sickness=="poverty")
+	  if($sickness=="poverty"  || ($RULES['lowtrump']=='poverty' && $sickness=='lowtrump'))
 	    {
 	      $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
 	      $cards    = DB_get_all_hand($userhash);
@@ -1438,6 +1471,9 @@ function cancel_game($why,$gameid)
     case 'noplay':
       DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
       break;
+    case 'lowtrump':
+      DB_query("UPDATE Game SET status='cancel-lowtrump' WHERE id=$gameid");
+      break;
     }
   /* set each player to gameover */
   $result = DB_query("SELECT id FROM Hand WHERE game_id=".DB_quote_smart($gameid));
diff --git a/include/game.php b/include/game.php
index 93ab461..3cc1d65 100644
--- a/include/game.php
+++ b/include/game.php
@@ -223,6 +223,7 @@ if($session)
     echo "       10ofhearts : {$RULES['dullen']}      <br />\n";
     echo "       schweinchen: {$RULES['schweinchen']} <br />\n";
     echo "       call:        {$RULES['call']}        <br />\n";
+    echo "       lowtrump:    {$RULES['lowtrump']}    <br />\n";
     echo "  </div>\n  </div>\n";
 
     /* show score */
@@ -400,7 +401,7 @@ switch($mystatus)
     $mycards = DB_get_hand($me);
     $mycards = mysort($mycards,$gametype);
 
-    if(!myisset('solo','wedding','poverty','nines') )
+    if(!myisset('solo','wedding','poverty','nines','lowtrump') )
       {
 	/* 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";
@@ -439,9 +440,10 @@ switch($mystatus)
 	/* check if someone selected more than one sickness */
 	$Nsickness = 0;
 	if($_REQUEST['solo']!='No')       $Nsickness++;
-	if($_REQUEST['wedding'] == 'yes') $Nsickness++;
-	if($_REQUEST['poverty'] == 'yes') $Nsickness++;
-	if($_REQUEST['nines'] == 'yes')   $Nsickness++;
+	if($_REQUEST['wedding']  == 'yes') $Nsickness++;
+	if($_REQUEST['poverty']  == 'yes') $Nsickness++;
+	if($_REQUEST['nines']    == 'yes') $Nsickness++;
+	if($_REQUEST['lowtrump'] == 'yes') $Nsickness++;
 
 	if($Nsickness>1)
 	  {
@@ -503,6 +505,16 @@ switch($mystatus)
 		  " is playing solo, this game will be canceled.<br />\n";
 		DB_set_sickness_by_hash($me,'nines');
 	      }
+	    else if($_REQUEST['lowtrump'] == 'yes')
+	      {
+		if($RULES['lowtrump']=='cancel')
+		  echo "What? You just don't want to play a game because you have low trump? Well, if no one".
+		  " is playing solo, this game will be canceled.<br />\n";
+		else
+		  echo "Don't think you can win with low trumps...? ok, poverty chosen <br />.<br />\n";
+
+		DB_set_sickness_by_hash($me,'lowtrump');
+	      }
 
 	    echo "</p>\n";
 
@@ -592,7 +604,7 @@ switch($mystatus)
 	$startplayer = DB_get_startplayer_by_gameid($gameid);
 
 	/* check for sickness */
-	$nines   = 0;
+	$cancel  = 0;
 	$poverty = 0;
 	$wedding = 0;
 	$solo    = 0;
@@ -600,12 +612,13 @@ switch($mystatus)
 	  {
 	    $name     = DB_get_name('userid',$user);
 	    $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
-	    if($usersick == 'nines')
+	    if($usersick == 'nines' || ($RULES['lowtrump']=='cancel' && $usersick=='lowtrump') )
 	      {
-		$nines = $user;
+		$cancel     = $user;
+		$cancelsick = $usersick;
 		break; /* no need to check for other poverties, since only solo can win and that is already set */
 	      }
-	    else if($usersick == 'poverty')
+	    else if($usersick == 'poverty' || ($RULES['lowtrump']=='poverty' && $usersick=='lowtrump'))
 	      $poverty++;
 	    else if($usersick == 'wedding')
 	      $wedding=$user;
@@ -618,13 +631,35 @@ switch($mystatus)
 	  {
 	    /* do nothing */
 	  }
-	else if($nines)
+	else if($cancel)
 	  {
 	    /* cancel game */
-	    $message = "The game has been canceled because ".DB_get_name('userid',$nines).
-	      " has five or more nines and nobody is playing solo.\n\n".
-	      "To redeal either start a new game or, in case the game was part of a tournament,\n".
-	      "go to the last game and use the link at the bottom of the page to redeal.\n\n";
+	    if($cancelsick == 'nines')
+	      {
+		$message = "The game has been canceled because ".DB_get_name('userid',$cancel).
+		  " has five or more nines and nobody is playing solo.\n\n".
+		  "To redeal either start a new game or, in case the game was part of a tournament,\n".
+		  "go to the last game and use the link at the bottom of the page to redeal.\n\n";
+
+		/* update game status */
+		cancel_game('nines',$gameid);
+
+		echo "<p>The game has been canceled because ".DB_get_name('userid',$cancel).
+		  " has five or more nines and nobody is playing solo.</p>\n";
+	      }
+	    else if ($cancelsick == 'lowtrump')
+	      {
+		$message = "The game has been canceled because ".DB_get_name('userid',$cancel).
+		  " has low trump and nobody is playing solo.\n\n".
+		  "To redeal either start a new game or, in case the game was part of a tournament,\n".
+		  "go to the last game and use the link at the bottom of the page to redeal.\n\n";
+
+		/* update game status */
+		cancel_game('lowtrump',$gameid);
+
+		echo "<p>The game has been canceled because ".DB_get_name('userid',$cancel).
+		  " has low trump and nobody is playing solo.</p>\n";
+	      };
 
 	    $userids = DB_get_all_userid_by_gameid($gameid);
 	    foreach($userids as $user)
@@ -633,11 +668,6 @@ switch($mystatus)
 		mymail($user,$subject,$message);
 	      }
 
-	    /* update game status */
-	    cancel_game('nines',$gameid);
-
-	    echo "<p>The game has been canceled because ".DB_get_name('userid',$nines).
-	      " has five or more nines and nobody is playing solo.</p>\n";
 	    echo "</div>\n";
 	    break;
 	  }
@@ -649,7 +679,7 @@ switch($mystatus)
 	    if(!$who)
 	      {
 		$firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
-		if($firstsick == 'poverty')
+		if($firstsick == 'poverty' || ($RULES['lowtrump']=='poverty' && $firstsick=='lowtrump'))
 		  DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
 		else
 		  DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
@@ -663,10 +693,10 @@ switch($mystatus)
 	    if(!$who)
 	      {
 		$firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
-		if($firstsick == 'poverty')
+		if($firstsick == 'poverty' || ($RULES['lowtrump']=='poverty' && $firstsick=='lowtrump'))
 		  {
-		    $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
-		    if($secondsick == 'poverty')
+		    $secondsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
+		    if($secondsick == 'poverty'  || ($RULES['lowtrump']=='poverty' && $secondsick=='lowtrump'))
 		      DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
 		    else
 		      DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
@@ -726,7 +756,7 @@ switch($mystatus)
 	      case 'dpoverty':
 		/* set person with poverty to play status */
 		$usersick = DB_get_sickness_by_userid_and_gameid($userid,$gameid);
-		if($usersick == 'poverty')
+		if($usersick == 'poverty'  || ($RULES['lowtrump']=='poverty' && $usersick=='lowtrump'))
 		  DB_set_hand_status_by_hash($userhash,'play');
 
 		/* set status of first player to be asked to poverty */
@@ -806,6 +836,7 @@ switch($mystatus)
 	echo "</div>\n";
 	break;
       }
+    echo "</div>\n";
   case 'poverty':
     /* user only gets here in a poverty game, several things have to be handled here:
      * A) ask, if user wants to take trump
@@ -919,7 +950,7 @@ switch($mystatus)
 		$userhash  = DB_get_hash_from_gameid_and_userid($gameid,$user);
 		$userparty = DB_get_party_by_hash($userhash);
 
-		if($usersick=='poverty' && !$userparty)
+		if(($usersick=='poverty'|| ($RULES['lowtrump']=='poverty' && $usersick=='lowtrump')) && !$userparty)
 		  {
 		    $hash    = DB_get_hash_from_gameid_and_userid($gameid,$user);
 		    $cards   = DB_get_hand($hash);
@@ -927,7 +958,10 @@ switch($mystatus)
 		    $nrtrump = 0;
 		    foreach($cards as $card)
 		      if($card<27) $nrtrump++;
-		    echo "Player $name has $nrtrump trump. Do you want to take them?".
+		    $low='';
+		    if($usersick=='lowtrump')
+		      $low='low';
+		    echo "Player $name has $nrtrump $low trump. Do you want to take them?".
 		      "<a href=\"index.php?action=game&amp;me=$me&amp;trump=$user\">Yes</a> <br />\n";
 		  }
 	      }
@@ -956,9 +990,9 @@ switch($mystatus)
 
 	    /* don't ask people who have poverty */
 	    $next=1;
-	    if($firstsick=='poverty')
+	    if($firstsick=='poverty' || ($RULES['lowtrump']=='poverty' && $firstsick=='lowtrump'))
 	      {
-		if($secondsick=='poverty')
+		if($secondsick=='poverty'|| ($RULES['lowtrump']=='poverty' && $secondsick=='lowtrump'))
 		  $next=3;
 		else
 		  $next=2;
@@ -1073,8 +1107,8 @@ switch($mystatus)
 		    $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
 
 		    $next=1;
-		    if($firstsick=='poverty')
-		      if($secondsick=='poverty')
+		    if($firstsick=='poverty'|| ($RULES['lowtrump']=='poverty' && $firstsick=='lowtrump'))
+		      if($secondsick=='poverty'|| ($RULES['lowtrump']=='poverty' && $secondsick=='lowtrump'))
 			$next=3;
 		      else
 			$next=2;
@@ -1097,7 +1131,7 @@ switch($mystatus)
 	    echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&amp;me=$me\">continue</a> here.</div>\n";
 	  }
       }
-    echo "</div>";
+    echo "</div>\n";
     break;
 
   case 'play':
@@ -1116,9 +1150,11 @@ switch($mystatus)
 	echo "<div class=\"message\"><p>The game has been canceled because one player wasn't responding.</p><p>If this was a mistake all 4 players need to send an Email to $ADMIN_NAME at $ADMIN_EMAIL requesting that the game should be restarted.</p></div>";
 	break;
       case 'cancel-nines':
-      case 'cancel-timedout':
 	echo "<div class=\"message\"><p>The game has been canceled because one player had too many nines.</p></div>";
 	break;
+      case 'cancel-lowtrump':
+	echo "<div class=\"message\"><p>The game has been canceled because one player had low trump.</p></div>";
+	break;
       case 'cancel-trump':
 	echo "<div class=\"message\"><p>The game has been canceled because nobody wanted to take the trump.</p></div>";
 	break;
diff --git a/include/newgame.php b/include/newgame.php
index d7a2d7f..f06bebe 100644
--- a/include/newgame.php
+++ b/include/newgame.php
@@ -40,7 +40,7 @@ else
 
     DB_update_user_timestamp($myid);
 
-    if( !myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","callrule" ))
+    if( !myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","callrule","lowtrump" ))
       {
 	/* only get players that want to be in new games */
 	$names = DB_get_all_user_names_open_for_games();
@@ -80,6 +80,7 @@ else
 	$dullen      = $_REQUEST["dullen"];
 	$schweinchen = $_REQUEST["schweinchen"];
 	$call        = $_REQUEST["callrule"];
+	$lowtrump    = $_REQUEST["lowtrump"];
 
 	/* get the emails addresses of the players */
 	$EmailA  = DB_get_email('name',$PlayerA);
@@ -142,7 +143,7 @@ else
 	else /* no follow up, start a new session */
 	  {
 	    /* get ruleset information or create new one */
-	    $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
+	    $ruleset = DB_get_ruleset($dullen,$schweinchen,$call,$lowtrump);
 	    if($ruleset <0)
 	      {
 		myerror("Error defining ruleset: $ruleset");
diff --git a/include/output.php b/include/output.php
index 018372b..135fe13 100644
--- a/include/output.php
+++ b/include/output.php
@@ -123,10 +123,19 @@ function output_form_for_new_game($names)
      <p>
        <select name="callrule">
 	 <option value="1st-own-card" selected="selected">  Can call re/contra on the first <strong>own</strong> card played, 90 on the second, etc.</option>
-	 <option value="5th-card">  Can call re/contra until 5th card is played, 90 until 9th card is played, etc.</option>
+	 <option value="5th-card"> Can call re/contra until 5th card is played, 90 until 9th card is played, etc.</option>
 	 <option value="9-cards" > Can call re/contra until 5th card is played, 90 if player still has 9 cards, etc.</option>
        </select>
      </p>
+     <h4>Low trump</h4>
+     <p>
+       Player can't trump a fox, that is none of his trump is higher than a fox.
+       <select name="lowtrump">
+	 <option value="poverty">The trump will be treated as poverty and offered to another player.</option>
+	 <option value="cancel">  The game will be canceled unless there is a solo.</option>
+	 <option value="none">   Bad luck, the player needs to play a normal game.</option>
+       </select>
+     </p>
      <h3>Scoring-related</h3>
      <h4>(not yet implemented)</h4>
      <p><input type="submit" value="start game"></p>
@@ -205,10 +214,11 @@ function display_link_card($card,$dir="english",$type="card")
 
 function output_check_for_sickness($me,$mycards)
 {
+  global $RULES;
  ?>
   <div class="sickness"> Thanks for joining the game...<br />
 
-    do you want to play solo?
+    Do you want to play solo?
     <select name="solo" size="1">
       <option selected="selected">No</option>
       <option>trumpless</option>
@@ -245,7 +255,7 @@ function output_check_for_sickness($me,$mycards)
       echo " no <input type=\"hidden\" name=\"poverty\" value=\"no\" /> <br />\n";
     };
 
-   echo "Do you have too many nines?";
+  echo "Do you have too many nines?";
   if(count_nines($mycards)>4)
      {
        echo " yes<input type=\"radio\" name=\"nines\" value=\"yes\" checked=\"checked\" />";
@@ -256,6 +266,26 @@ function output_check_for_sickness($me,$mycards)
        echo " no <input type=\"hidden\" name=\"nines\" value=\"no\" /> <br />\n";
      };
 
+  if($RULES['lowtrump']=='cancel' || $RULES['lowtrump']=='poverty')
+    {
+      if($RULES['lowtrump']=='cancel')
+	echo "Do you have low trump (cancel game)?";
+      else
+	echo "Do you have low trump (poverty)?";
+
+      if(check_low_trump($mycards))
+	{
+	  echo " yes<input type=\"radio\" name=\"lowtrump\" value=\"yes\" checked=\"checked\" />";
+	  echo " no <input type=\"radio\" name=\"lowtrump\" value=\"no\" /> <br />\n";
+	}
+      else
+	{
+	  echo " no <input type=\"hidden\" name=\"lowtrump\" value=\"no\" /> <br />\n";
+	};
+    }
+  else
+    echo "<input type=\"hidden\" name=\"lowtrump\" value=\"no\" />";
+
    echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
    echo "<input type=\"submit\" value=\"count me in\" />\n";
 
@@ -556,7 +586,7 @@ function output_exchanged_cards()
    */
 
   /* need some information about the game */
-  global $gameid,$mygametype, $PREF,$me,$mystatus;
+  global $gameid,$mygametype, $PREF,$me,$mystatus, $RULES;
 
   /* some variables to track where the people with poverty are sitting */
   $partnerpos1 = 0;
@@ -573,7 +603,7 @@ function output_exchanged_cards()
       for($mypos=1;$mypos<5;$mypos++)
 	{
 	  $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
-	  if($usersick == 'poverty')
+	  if($usersick == 'poverty' || ($RULES['lowtrump']=='poverty' && $usersick=='lowtrump'))
 	    if($povertypos1)
 	      $povertypos2 = $mypos;
 	    else
diff --git a/update_db.php b/update_db.php
index deae974..498b24e 100644
--- a/update_db.php
+++ b/update_db.php
@@ -28,7 +28,8 @@ include_once("./include/functions.php");   /* the rest */
 config_check();
 
 /* open the database */
-if(DB_open()<0)
+$return = DB_open();
+if($return<0 && $return != -2) /* -2 = wrong DB version is ok */
   exit();
 
 /* only callable via cron or CLI */
@@ -36,7 +37,7 @@ if(isset($_SERVER['REMOTE_ADDR']))
   exit();
 
 $old_version = DB_get_version();
-$current_version = 1;
+$current_version = 2;
 
 if($old_version < $current_version)
   echo "Will upgrade your database now:\n";
@@ -56,6 +57,17 @@ switch($old_version)
 	     " index (email))");
     DB_query("UPDATE Version set version=1");
     echo "Upgraded to version 1.\n";
+  case 1:
+    /* add new rules */
+    DB_query("ALTER TABLE Rulesets".
+	     " ADD COLUMN `lowtrump` enum('poverty','cancel','none') default 'poverty' AFTER schweinchen");
+    DB_query("ALTER TABLE Hand".
+	     " MODIFY COLUMN `sickness` enum('wedding','nines','poverty','solo','lowtrump') default NULL");
+    DB_query("ALTER TABLE Game".
+	     " MODIFY COLUMN `status` enum('pre','play','gameover','cancel-timedout','cancel-nines','cancel-trump','cancel-noplay','cancel-lowtrump') default NULL");
+
+    DB_query("UPDATE Version set version=2");
+    echo "Upgraded to version 2.\n";
   }
 
 ?>
\ No newline at end of file
-- 
cgit v1.2.3-18-g5258


From e7e541e70a2aabdd399aa9952e0f8dee435b402b Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Sat, 13 Mar 2010 16:24:21 -0800
Subject: BUGFIX: missing ul-tag in poverty check messed up layout

also removed some div-tags that were not needed
---
 include/game.php | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/include/game.php b/include/game.php
index 3cc1d65..b94ee90 100644
--- a/include/game.php
+++ b/include/game.php
@@ -668,7 +668,7 @@ switch($mystatus)
 		mymail($user,$subject,$message);
 	      }
 
-	    echo "</div>\n";
+	    echo "</div>\n"; /* end div message */
 	    break;
 	  }
 	else if($poverty==1) /* one person has poverty */
@@ -807,7 +807,7 @@ switch($mystatus)
 		  }
 	      }
 	    else
-	      echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.</div>\n";
+	      echo "Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br />\n";
 	  }
 	else
 	  {
@@ -817,7 +817,7 @@ switch($mystatus)
 
 	    $whoid = DB_get_userid('gameid-position',$gameid,$who);
 	    if($whoid==$myid)
-	      echo "<div class=\"message\">Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.</div>\n";
+	      echo "Please, <a href=\"$INDEX?action=game&amp;me=$me\">start</a> the game.<br /\n";
 	    else
 	      {
 		$whohash = DB_get_hash_from_game_and_pos($gameid,$who);
@@ -836,7 +836,7 @@ switch($mystatus)
 	echo "</div>\n";
 	break;
       }
-    echo "</div>\n";
+
   case 'poverty':
     /* user only gets here in a poverty game, several things have to be handled here:
      * A) ask, if user wants to take trump
@@ -894,7 +894,7 @@ switch($mystatus)
 	/* get information so show the cards that have been handed over in a poverty game */
 	output_exchanged_cards();
 
-	echo "    </div>\n  </li>\n";  /* end div trick, end li trick */
+	echo "    </div>\n  </li>\n</ul>\n\n";  /* end div trick, end li trick , end ul tricks */
       }
     /* end output pre-game trick */
 
-- 
cgit v1.2.3-18-g5258


From c05246b378470eab9c172d8d96328a821302a273 Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Sun, 14 Mar 2010 11:11:38 -0700
Subject: CLEANUP: cleaned up code for Karlchen

---
 include/game.php | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/include/game.php b/include/game.php
index b94ee90..db27bde 100644
--- a/include/game.php
+++ b/include/game.php
@@ -1524,28 +1524,31 @@ switch($mystatus)
 
 		/* same as for foxes, karlchen doesn't always make sense
 		 * check what kind of game it is and set karlchen accordingly */
-		$ok = 1; /* default: karlchen should be accounted for */
-		if($tricknr != 12 )
-		  $ok = 0; /* Karlchen works only in the last trick */
-		if($ok && DB_get_gametype_by_gameid($gameid)=='solo' )
+
+		if($tricknr == 12 ) /* Karlchen works only in the last trick */
 		  {
-		    $solo = DB_get_solo_by_gameid($gameid);
-		    if($solo == 'trumpless' || $solo == 'jack' || $solo == 'queen' )
-		      $ok = 0; /* no Karlchen in these solos */
-		  }
+		    /* check for solo */
+		    $solo = 'none';
+		    if(DB_get_gametype_by_gameid($gameid)=='solo' )
+		      $solo = DB_get_solo_by_gameid($gameid);
 
-		if($ok)
-		  foreach($play as $played)
-		    if ( $played['card']==11 || $played['card']==12 )
-		      if ($played['pos'] == $winner )
-			{
-			  /* possible caught a fox, check party */
-			  $uid1   = DB_get_userid('gameid-position',$gameid,$winner);
-			  $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
+		    /* no Karlchen in these solos */
+		    if($solo != 'trumpless' && $solo != 'jack' && $solo != 'queen' )
+		      {
+			foreach($play as $played)
+			  if ( $played['card']==11 || $played['card']==12 )
+			    if ($played['pos'] == $winner )
+			      {
+				/* save Karlchen */
+				$uid1   = DB_get_userid('gameid-position',$gameid,$winner);
+				$party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
+
+				DB_query("INSERT INTO Score".
+					 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
+			      };
+		      };
+		  }; /* end scoring Karlchen */
 
-			  DB_query("INSERT INTO Score".
-				   " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
-			}
 		/*
 		 * check for doppelopf (>40 points)
 		 ***********************************/
-- 
cgit v1.2.3-18-g5258


From 83fc1a851c69e13808d14f2d5a284c3369409018 Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Sun, 14 Mar 2010 14:11:52 -0700
Subject: NEW FEATURE: added localization support

using gettext to add support for localization
---
 INSTALL                        |   2 +-
 TRANSLATION                    |  23 +++++++++++++++++++++++
 include/db.php                 |   7 +++++++
 include/output.php             |  24 +++++++++++++++++++++++-
 include/preferences.php        |  24 +++++++++++++++++++++++-
 index.php                      |  33 ++++++++++++++++++++++++++++++---
 locale/de/LC_MESSAGES/edoko.mo | Bin 0 -> 476 bytes
 po/de.po                       |  21 +++++++++++++++++++++
 po/messages.pot                |  21 +++++++++++++++++++++
 9 files changed, 149 insertions(+), 6 deletions(-)
 create mode 100644 TRANSLATION
 create mode 100644 locale/de/LC_MESSAGES/edoko.mo
 create mode 100644 po/de.po
 create mode 100644 po/messages.pot

diff --git a/INSTALL b/INSTALL
index 0fada71..1929213 100644
--- a/INSTALL
+++ b/INSTALL
@@ -3,7 +3,7 @@ Installation
 ------------
 
 -1) What you need
-   all you need is PHP and MYSQL
+   all you need is PHP and MYSQL. PHP has to include gettext support.
 
 0) Get files
 
diff --git a/TRANSLATION b/TRANSLATION
new file mode 100644
index 0000000..38bbaaa
--- /dev/null
+++ b/TRANSLATION
@@ -0,0 +1,23 @@
+Translation:
+
+to update the pot file run:
+
+xgettext -L PHP -n --package-name=edoko --msgid-bugs-address=arun@nubati.net *php */*php
+mv messages.po po/messages.pot
+
+
+To start a new translation use something like (here shown to create de.po):
+
+cd po
+msginit -i messages.pot  -o de.po
+
+To update a po file run:
+
+cd po
+msgmerge de.po messages.pot
+
+Start editing and then after updating a po run:
+
+msgconv -t ISO-8859-1 de.po > de.po.new && mv de.po.new de.po
+msgfmt -cv -o de.mo de.po
+mv de.mo ../locale/de/LC_MESSAGES/edoko.mo
\ No newline at end of file
diff --git a/include/db.php b/include/db.php
index 2a927f5..ad6c6f0 100644
--- a/include/db.php
+++ b/include/db.php
@@ -772,6 +772,7 @@ function DB_get_PREF($myid)
   $PREF['vacation_start']	=  NULL;
   $PREF['vacation_stop']	=  NULL;
   $PREF['vacation_comment']	=  '';
+  $PREF['language']	        =  'en';
 
   /* get all preferences */
   $r = DB_query('SELECT pref_key, value FROM User_Prefs'.
@@ -825,8 +826,14 @@ function DB_get_PREF($myid)
 	  if($pref[1])
 	    $PREF['vacation_comment'] = $pref[1];
 	  break;
+
+	case 'language':
+	  if($pref[1])
+	    $PREF['language'] = $pref[1];
+	  break;
 	}
     }
+  $_SESSION['language'] =  $PREF['language'];
   return $PREF;
 }
 
diff --git a/include/output.php b/include/output.php
index 135fe13..79e42c2 100644
--- a/include/output.php
+++ b/include/output.php
@@ -394,7 +394,9 @@ function output_header()
   </head>
 <body onload="high_last();">
 <div class="header">
-<h1> Welcome to E-Doko </h1>
+<?php
+  echo '<h1> '._('Welcome to E-Doko').' </h1>';
+?>
 </div>
 <?php
 
@@ -516,6 +518,26 @@ function output_select_timezone($name,$timezone="")
   return;
 }
 
+function output_select_language($name,$language="")
+{
+  $LOCALE = array ("English"     => "en",
+		   "Deutsch"     => "de" );
+
+  echo "  <select id=\"$name\" name=\"$name\" size=\"1\">\n";
+
+  foreach($LOCALE as $place=>$locale)
+    {
+      if($language==$locale)
+	echo "   <option value=\"$locale\" selected=\"selected\">$place</option>\n";
+      else
+	echo "   <option value=\"$locale\">$place</option>\n";
+    }
+  echo "  </select>\n";
+
+  return;
+}
+
+
 function output_password_recovery($email,$password)
 {
 ?>
diff --git a/include/preferences.php b/include/preferences.php
index 6648b99..3904899 100644
--- a/include/preferences.php
+++ b/include/preferences.php
@@ -43,6 +43,7 @@ $changed_openforgames = 0;
 $changed_vacation     = 0;
 $changed_openid       = 0;
 $changed_digest       = 0;
+$changed_language     = 0;
 
 display_user_menu($myid);
 
@@ -297,6 +298,25 @@ if(myisset("openid_url") && $_REQUEST['openid_url']!='')
     DB_AttachOpenID($openid_url, $myid);
   }
 
+if(myisset("language"))
+  {
+    $language = $_REQUEST['language'];
+    if($language != $PREF['language'])
+      {
+	/* check if we already have an entry for the user, if so change it, if not create new one */
+	$result = DB_query("SELECT * from User_Prefs".
+			   " WHERE user_id='$myid' AND pref_key='language'" );
+	if( DB_fetch_array($result))
+	  $result = DB_query("UPDATE User_Prefs SET value=".DB_quote_smart($language).
+			     " WHERE user_id='$myid' AND pref_key='language'" );
+	else
+	  $result = DB_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','language',".
+			     DB_quote_smart($language).")");
+	$changed_language = 1;
+      }
+  }
+
+
 /* get infos again in case they have changed */
 $PREF     = DB_get_PREF($myid);
 $timezone = DB_get_user_timezone($myid);
@@ -461,7 +481,9 @@ echo "        <tr><td>Timezone:              </td><td>\n";
 output_select_timezone("timezone",$timezone);
 if($changed_timezone) echo "changed";
 echo "</td></tr>\n";
-
+echo "        <tr><td>Language:              </td><td>\n";
+output_select_language("language",$PREF['language']);
+echo "</td></tr>\n";
 echo "        <tr><td>Password(old):         </td><td>",
   "<input type=\"password\" id=\"password0\" name=\"password0\" size=\"20\" maxlength=\"30\" />";
 switch($changed_password)
diff --git a/index.php b/index.php
index 54efdcb..61b0e3f 100644
--- a/index.php
+++ b/index.php
@@ -51,7 +51,36 @@ if($DBopen<0)
     exit();
   }
 
-/* done major error checking, output5B header of HTML page */
+/* localization */
+/* needs to be in front of output_header, but we don't know the users preferences at this time,
+ * so we go by the session variable or if language is set
+ */
+if(myisset('language') || isset($_SESSION['language']))
+  {
+    $language = 'en';
+    if(isset($_SESSION['language']))
+       $language = $_SESSION['language'];
+    if(myisset('language'))
+      {
+	$language = $_REQUEST['language'];
+	$_SESSION['language'] = $language; /* overrule preferences */
+      }
+    switch($language)
+      {
+      case 'de':
+	putenv("LC_ALL=de_DE");
+	setlocale(LC_ALL, "de_DE");
+	// Specify location of translation tables
+	bindtextdomain("edoko", "./locale");
+	// Choose domain
+	textdomain("edoko");
+	break;
+      default:
+	/* do nothing */
+      }
+  }
+
+/* done major error checking, output header of HTML page */
 output_header();
 
 /* The rest of the file consists of handling user input.
@@ -120,5 +149,3 @@ DB_close();
  *End:
  */
 ?>
-
-
diff --git a/locale/de/LC_MESSAGES/edoko.mo b/locale/de/LC_MESSAGES/edoko.mo
new file mode 100644
index 0000000..e72f5ab
Binary files /dev/null and b/locale/de/LC_MESSAGES/edoko.mo differ
diff --git a/po/de.po b/po/de.po
new file mode 100644
index 0000000..9d27938
--- /dev/null
+++ b/po/de.po
@@ -0,0 +1,21 @@
+# English translations for PACKAGE package.
+# Copyright (C) 2010 THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# Arun Persaud <arun@nubati.net>, 2010.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: edoko\n"
+"Report-Msgid-Bugs-To: arun@nubati.net\n"
+"POT-Creation-Date: 2010-03-14 11:35-0700\n"
+"PO-Revision-Date: 2010-03-14 12:01-0700\n"
+"Last-Translator: Arun Persaud <arun@nubati.net>\n"
+"Language-Team: English\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=ISO-8859-1\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+#: include/output.php:398
+msgid "Welcome to E-Doko"
+msgstr "Willkommen bei E-Doko"
diff --git a/po/messages.pot b/po/messages.pot
new file mode 100644
index 0000000..08d1463
--- /dev/null
+++ b/po/messages.pot
@@ -0,0 +1,21 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: edoko\n"
+"Report-Msgid-Bugs-To: arun@nubati.net\n"
+"POT-Creation-Date: 2010-03-14 11:35-0700\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=CHARSET\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#: include/output.php:398
+msgid "Welcome to E-Doko"
+msgstr ""
-- 
cgit v1.2.3-18-g5258