\n";
-
-/* end header */
-
-
-$history=array();
-
-/* check for status file and read it, if possible */
-
-if(file_exists("status.txt"))
- $lines = file("status.txt");
-else
- die("no file");
+DB_open();
+output_header();
/* check if we want to start a new game */
-if( isset($_REQUEST["PlayerA"]) &&
- isset($_REQUEST["PlayerB"]) &&
- isset($_REQUEST["PlayerC"]) &&
- isset($_REQUEST["PlayerD"]) &&
- isset($_REQUEST["EmailA"]) &&
- isset($_REQUEST["EmailB"]) &&
- isset($_REQUEST["EmailC"]) &&
- isset($_REQUEST["EmailD"]) && sizeof($lines<2))
+if(myisset("new"))
+ {
+ $names = DB_get_all_names();
+ output_form_for_new_game($names);
+ }
+/*check if everything is ready to set up a new game */
+ else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen" ))
{
$PlayerA = $_REQUEST["PlayerA"];
$PlayerB = $_REQUEST["PlayerB"];
$PlayerC = $_REQUEST["PlayerC"];
$PlayerD = $_REQUEST["PlayerD"];
- $EmailA = $_REQUEST["EmailA"] ;
- $EmailB = $_REQUEST["EmailB"] ;
- $EmailC = $_REQUEST["EmailC"] ;
- $EmailD = $_REQUEST["EmailD"] ;
-
- $hashA = md5("AGameOfDoko".$PlayerA.$EmailA);
- $hashB = md5("AGameOfDoko".$PlayerB.$EmailB);
- $hashC = md5("AGameOfDoko".$PlayerC.$EmailC);
- $hashD = md5("AGameOfDoko".$PlayerD.$EmailD);
-
- /* send out email, check for error with email */
+ $dullen = $_REQUEST["dullen"];
+ $schweinchen = $_REQUEST["schweinchen"];
+
+ $EmailA = DB_get_email_by_name($PlayerA);
+ $EmailB = DB_get_email_by_name($PlayerB);
+ $EmailC = DB_get_email_by_name($PlayerC);
+ $EmailD = DB_get_email_by_name($PlayerD);
+
+ if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
+ {
+ echo "couldn't find one of the names, please start a new game";
+ output_footer();
+ exit();
+ }
+
+ $useridA = DB_get_userid_by_name($PlayerA);
+ $useridB = DB_get_userid_by_name($PlayerB);
+ $useridC = DB_get_userid_by_name($PlayerC);
+ $useridD = DB_get_userid_by_name($PlayerD);
+
+ /* create random numbers */
+ $randomNR = create_array_of_random_numbers();
+ $randomNRstring = join(":",$randomNR);
+
+ /* get ruleset information or create new one */
+ $ruleset = DB_get_ruleset($dullen,$schweinchen);
+ if($ruleset <0)
+ {
+ echo "Error defining ruleset: $ruleset";
+ output_footer();
+ exit();
+ };
+
+ /* create game */
+ $followup = NULL;
+ if(myisset("followup") )
+ {
+ $followup= $_REQUEST["followup"];
+ $session = DB_get_session_by_gameid($followup);
+ $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
+ this way no manipulation is possible */
+ if($session)
+ mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre',".
+ "'$ruleset','$session' ,NULL)");
+ else
+ {
+ /* get max session */
+ $max = DB_get_max_session();
+ $max++;
+ mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre',".
+ "'$ruleset','$max' ,NULL)");
+ mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
+ }
+ }
+ else
+ mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre', ".
+ "'$ruleset',NULL ,NULL)");
+ $game_id = mysql_insert_id();
+
+ /* create hash */
+ $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
+ $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
+ $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
+ $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
+
+ /* create hands */
+ mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
+ ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
+ $hand_idA = mysql_insert_id();
+ mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
+ ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
+ $hand_idB = mysql_insert_id();
+ mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
+ ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
+ $hand_idC = mysql_insert_id();
+ mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
+ ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
+ $hand_idD = mysql_insert_id();
+
+ /* save cards */
+ for($i=0;$i<12;$i++)
+ mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
+ for($i=12;$i<24;$i++)
+ mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
+ for($i=24;$i<36;$i++)
+ mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
+ for($i=36;$i<48;$i++)
+ mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
+
+ /* send out email, TODO: check for error with email */
$message = "\n".
"you are invited to play a game of DoKo (that is to debug the program ;).\n".
"Place comments and bug reports here:\n".
@@ -475,677 +138,1025 @@ if( isset($_REQUEST["PlayerA"]) &&
"$PlayerC\n".
"$PlayerD\n\n".
"If you want to join this game, please follow this link:\n\n".
- " ".$host."?a=";
+ " ".$host."?me=";
mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
- /* read in random.txt */
- if(file_exists("random.txt"))
- $random = file("random.txt");
- else
- die("no random file");
-
- $randomNR = explode( ":", $random[2] );
+ echo "You started a new game. The emails have been sent out!";
+ } /* end set up a new game */
+else if(myisset("cancle","me"))
+ {
+ /* cancle a game, if it is older than N minutes */
+
+ $me = $_REQUEST["me"];
- /* write initial status into file */
- $output = fopen("status.txt","w");
- if ($output)
+ /* test for valid ID */
+ $myid = DB_get_userid_by_hash($me);
+ if(!$myid)
{
- fwrite($output, "$hashA:$PlayerA:$EmailA:::" );
- for($i=0;$i<11;$i++)
- fwrite($output,"$randomNR[$i];" );
- fwrite($output,"$randomNR[11]:" ); $i++;
- fwrite($output,"\n");
-
- fwrite($output, "$hashB:$PlayerB:$EmailB:::" );
- for(;$i<23;$i++)
- fwrite($output,"$randomNR[$i];" );
- fwrite($output,"$randomNR[23]:" ); $i++;
- fwrite($output,"\n");
+ echo "Can't find you in the database, please check the url. \n";
+ echo "perhaps the game has been cancled, check by login in here.";
+ output_footer();
+ exit();
+ }
+
+ DB_update_user_timestamp($myid);
+
+ /* get some information from the DB */
+ $gameid = DB_get_gameid_by_hash($me);
+ $myname = DB_get_name_by_hash($me);
+
+ /* check if game really is old enough */
+ $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
+ $r = mysql_fetch_array($result,MYSQL_NUM);
+ if(time()-strtotime($r[0]) > 60*60*24*30)
+ {
+ $message = "Hello, \n\n".
+ "Game $gameid has been cancled since nothing happend for a while and $myname requested it.\n";
- fwrite($output, "$hashC:$PlayerC:$EmailC:::" );
- for(;$i<35;$i++)
- fwrite($output,"$randomNR[$i];" );
- fwrite($output,"$randomNR[35]:" ); $i++;
- fwrite($output,"\n");
+ $userids = DB_get_all_userid_by_gameid($gameid);
+ foreach($userids as $user)
+ {
+ $To = DB_get_email_by_userid($user);
+ mymail($To,$EmailName."game cancled (timed out)",$message);
+ }
- fwrite($output, "$hashD:$PlayerD:$EmailD:::");
- for(;$i<47;$i++)
- fwrite($output,"$randomNR[$i];" );
- fwrite($output,"$randomNR[47]:" );
- fwrite($output,"\n");
+ /* delete everything from the dB */
+ DB_cancel_game($me);
- fclose($output);
+ echo "
Game $gameid has been cancled.
";
}
else
- echo "can't open file for writing";
- };
-/* reread file */
-if(file_exists("status.txt"))
- $lines = file("status.txt");
- else
- die("no file");
-
-/* test if a game is running, else output everything for a new game */
-if(sizeof($lines)<2)
+ echo "
You need to wait longer before you can cancle a game...
\n";
+ }
+else if(myisset("me"))
{
-?>
-
no game in progress, please input 4 names and email addresses, please make sure that the addresses are correct!
- \n";
- }
- }
-/* ***
- * * *
- * ***
- * * *
- * ***
- * yes? email him his hand, ask for solo, poverty, email every move or every card?
- */
- if(isset($_REQUEST["b"]))
- {
- $b = $_REQUEST["b"];
-
- if( ereg("s",$player[$b]["option"]) && $game["init"]<4)
- { /* the player already filled out everything */
- echo "just wait for the game to start";
- }
- else if( (!isset($_REQUEST["in"])|| !isset($_REQUEST["update"])) && !ereg("i",$player[$b]["option"]))
- { /* the player didn't fill out the form at "a" correctly */
- echo "go back to ";
- echo " here and fill out the form \n";
- }
- else
- { /* show the hand and check if the player is sick*/
- if($_REQUEST["in"]=="no")
- { /* player doesn't want to play, cancel the game */
- for($i=0;$i<4;$i++)
- {
- $message = "Hello ".$player[$hash[$i]]["name"].",\n\n".
- "the game has been canceled due to the request of one of the players.\n";
- mymail($player[$hash[$i]]["email"],"[DoKo-Debug] the game has been canceled",$message);
- }
- /* canceling the game is as easy as removing the contents of the status file*/
- $output = fopen("status.txt","w");
- if($output)
- fclose($output);
- else
- echo "problem opening file";
- }
- else
- {
- /* player wants to play, save information from "a"*/
- if($_REQUEST["update"]=="card")
- $player[$b]["option"] .= "c";
- else
- $player[$b]["option"] .= "t";
-
- $player[$b]["option"] .= "i"; /* player finished stage "a" */
-
- save_status();
-
- $allcards = $player[$b]["cards"];
- $mycards = explode(";",$allcards);
-
- sort($mycards);
- echo "
your cards are: \n";
- foreach($mycards as $card)
- display_card($card);
- echo "
\n";
- ?>
-
aehm... at the moment poverty is not implented. so I guess you need to play a normal game, even if you have less than 3 trump :(...sorry
+ if($ok)
+ {
+ echo "Everyone has finished checking their cards, let's see what they said... ";
+ /* check what kind of game we are playing */
+ $gametype = DB_get_gametype_by_gameid($gameid);
+ $startplayer = DB_get_startplayer_by_gameid($gameid);
+
+ /* nines? */
+ $nines = 0;
+ /* check for nines */
+ foreach($userids as $user)
+ if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'nines')
+ $nines = $user;
+
+ /* gamestatus == normal, => cancel game */
+ if($nines && $gametype != "solo")
+ {
+ /* TODO: should we keep statistics of this? */
+ $message = "Hello, \n\n".
+ "the game has been canceled because ".DB_get_name_by_userid($nines)." has five or more nines.\n";
+
+ $userids = DB_get_all_userid_by_gameid($gameid);
+ foreach($userids as $user)
+ {
+ $To = DB_get_email_by_userid($user);
+ mymail($To,$EmailName."game canceled",$message);
+ }
+
+ /* delete everything from the dB */
+ DB_cancel_game($me);
+ output_footer();
+ exit();
+ }
-
- recorvery:
-
-
-=0)
- {
- $message = "Please try this link: ".$host."?me=".$hash[$ok]."\n".
- "\n if this doesn't work, contact the admin.error4\n";
- mymail($recovery,"[DoKo-Debug] recovery ",$message);
- echo "
An email with the game information has been sent.
\n";
- }
- else
- {
- echo "
can't find this email address, sorry.
\n";
- };
- } /* end recovery */
- }
+ if($r)
+ echo " added you to the database";
else
- { /* $me is set */
- $me = $_REQUEST["me"];
-
- /* output if we are playing a solo or a wedding */
- if($game["solo-who"]>=0)
- echo $player[$hash[$game["solo-who"]]]["name"]." is playing a ".$game["solo-what"]." solo! \n";
- else if($game["wedding"]>=0)
- echo $player[$hash[$game["wedding"]]]["name"]." is playing a wedding! \n";
-
- /* show history */
- /* old tricks as list */
- echo "
\n";
- echo "
History:
\n";
- $j=0;
- foreach($history as $play)
- {
- $j++;
- $trick = explode(":",$play);
-
- /* found old trick, display it */
- if(sizeof($trick)==5)
- echo "
\n";
- }
- echo "\n";
-
- /* figure out who needs to play next */
- $next = $last + 1;
- if ($next>=4)
- $next -= 4 ;
-
- /* if no one has played yet or we are at the start of a new trick */
- if($last<0)
- $next = $history[sizeof($history)-1][0];
-
- /* are we trying to play a card? */
- if(isset($_REQUEST["card"]))
- {
- if($hash[$next]==$me)
- {
- $card = $_REQUEST["card"];
- $mycards = explode(";",$player[$me]["cards"]);
-
- /* do we have that card */
- if(in_array($card,$mycards))
- {
- /* delete card from array */
- $tmp = array();
- foreach($mycards as $m)
- if($m != $card)
- $tmp[]=$m;
-
- $tmp2="";
- for($i=0;$i".$card.":\n";
- }
- else
- {
- $tmp = explode(":",$history[sizeof($history)-1]);
- $tmp[sizeof($tmp)-1] = "".$player[$me]["number"]."->".$card.":";
- $history[sizeof($history)-1]=join(":",$tmp);
- }
- save_status();
-
- echo "
";
- echo " you played ";
- display_card($card);
- echo "
\n";
-
- ?>
-
- A short comment:
-
-
-
-3)$mynext-=4;
-
- if((ereg("c",$player[$hash[$i]]["option"]) || $i==$mynext) && $hash[$i]!=$me)
- {
- $message = " Hello ".$player[$hash[$i]]["name"].",\n\n";
-
- if($i==$mynext)
- {
- $message .= "it's your turn now.\n".
- "Use this link to play a card: ".$host."?me=".$hash[$i]."\n\n" ;
- }
- $message .= $player[$me]["name"]." has played the following card ".
- card_to_name($card)."\n";
-
- if($game["solo-who"]>=0)
- $message .= $player[$hash[$game["solo-who"]]]["name"]." is playing a ".
- $game["solo-what"]." solo!\n";
-
- mymail($player[$hash[$i]]["email"],"[DoKo-debug] a card has been played",$message);
-
- if($debug)
- echo " next player \n";
- }
- }
- }
- else
- echo "seems like you don't have that card \n";
-
- }
-
- } /* end if card is set */
- else if(isset($_REQUEST["comment"]))
- { /*save comment */
- $comment = $_REQUEST["comment"];
- $tmp = explode(":",$history[sizeof($history)-1]); /*last played trick */
- $tmp2 = explode("->",$tmp[sizeof($tmp)-2]); /*last played card */
-
- $comment = str_replace(":","",$comment); /*can't have ":" in comments */
-
- if(sizeof($tmp2)<=2)
- $tmp[sizeof($tmp)-2] .= "->".$comment;
- $history[sizeof($history)-1]=join(":",$tmp);
-
- save_status();
- }
- else if(isset($_REQUEST["win"]) && substr_count($history[sizeof($history)-1],":")==4)
- { /* count points, email winner */
- $win = $_REQUEST["win"];
-
- if(strlen($player[$hash[0]]["cards"]))
- $history[] = $win.":\n";
-
- /* email the player who needs to move next*/
- for($i=0;$i<4;$i++)
- {
- if((ereg("c",$player[$hash[$i]]["option"]) || $i==$win) )
- {
- $message = " Hello ".$player[$hash[$i]]["name"].",\n\n";
-
- if($i == $win)
- {
- $message .= "You won the last trick,it's your turn now.\n".
- "Use this link to play a card: ".$host."?me=".$hash[$i]."\n\n" ;
- }
- else
- $message .= $player[$hash[$win]]["name"]." has won the last trick\n".
- "Use this link to look at the game: ".$host."?me=".$hash[$i]."\n\n" ;
-
- if($game["solo-who"]>=0)
- $message.= $player[$hash[$game["solo-who"]]]["name"]." is playing a ".
- $game["solo-what"]." solo!\n";
-
- mymail($player[$hash[$i]]["email"],"[DoKo-debug] a card has been played",$message);
-
- if($debug)
- echo " next player \n";
- }
- }
-
- /* count points of the last trick */
- $points = 0;
-
- $tmp = explode(":",$history[sizeof($history)-2]);
- for($i=0;$i<4;$i++)
- {
- $tmp2 = explode("->",$tmp[$i]);
- $c = $tmp2[1];
- $points += card_value($c);
- }
- $player[$hash[$win]]["points"] += $points;
- echo " \n ".$player[$hash[$win]]["name"]." won: $points Points \n";
-
- save_status();
- }; /* end if win is set */
- echo " \n";
-
- /* check last history entry: end of a trick? ask who won it, unless it was the last trick */
- $tmp = explode(":",$history[sizeof($history)-1]);
- if(sizeof($tmp)==5 && strlen($player[$hash[0]]["cards"]))
- {
- ?>
-
-who won?
- ";
- echo "";
-?>
-
-
-
-the next player \n";
- if(strlen(trim($player[$me]["cards"]))==0)
- {
- echo " game over, count points \n";
- for($i=0;$i<4;$i++)
- {
- echo $player[$hash[$i]]["name"]." got ".$player[$hash[$i]]["points"]." \n";
- }
- }
- }
- echo " \n";
- } /* end check for winner */
-
- /* do we still have cards? display them */
- if(strlen(trim($player[$me]["cards"]))>0 )
- {
- $allcards = trim($player[$me]["cards"]);
- $mycards = explode(";",$allcards);
-
- sort($mycards);
-
- echo "
\n";
- /* is it our turn? */
- if($hash[$next]==$me && !isset($_REQUEST["card"]) && !isset($_REQUEST["win"]))
- {
- echo "ITS YOUR TURN! \n";
- echo "Your cards are: \n";
- foreach($mycards as $card)
- display_link_card($card,$me);
- }
- else
- { /* not our turn, just show the hand */
- echo "Your cards are: \n";
- foreach($mycards as $card)
- display_card($card);
- }
- echo "