2 error_reporting(E_ALL);
7 include_once("config.php");
8 include_once("output.php"); /* html output only */
9 include_once("db.php"); /* database only */
10 include_once("functions.php"); /* the rest */
12 /* check if some variables are set in the config file, else set defaults */
13 if(!isset($EmailName))
19 echo "Working on the database...please check back in a few mintues";
27 /* check if we want to start a new game */
30 $names = DB_get_all_names();
31 output_form_for_new_game($names);
33 /*check if everything is ready to set up a new game */
34 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen" ))
36 $PlayerA = $_REQUEST["PlayerA"];
37 $PlayerB = $_REQUEST["PlayerB"];
38 $PlayerC = $_REQUEST["PlayerC"];
39 $PlayerD = $_REQUEST["PlayerD"];
41 $dullen = $_REQUEST["dullen"];
42 $schweinchen = $_REQUEST["schweinchen"];
44 $EmailA = DB_get_email_by_name($PlayerA);
45 $EmailB = DB_get_email_by_name($PlayerB);
46 $EmailC = DB_get_email_by_name($PlayerC);
47 $EmailD = DB_get_email_by_name($PlayerD);
49 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
51 echo "couldn't find one of the names, please start a new game";
56 $useridA = DB_get_userid_by_name($PlayerA);
57 $useridB = DB_get_userid_by_name($PlayerB);
58 $useridC = DB_get_userid_by_name($PlayerC);
59 $useridD = DB_get_userid_by_name($PlayerD);
61 /* create random numbers */
62 $randomNR = create_array_of_random_numbers();
63 $randomNRstring = join(":",$randomNR);
65 /* get ruleset information or create new one */
66 $ruleset = DB_get_ruleset($dullen,$schweinchen);
69 echo "Error defining ruleset: $ruleset";
76 if(myisset("followup") )
78 $followup= $_REQUEST["followup"];
79 $session = DB_get_session_by_gameid($followup);
80 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
81 this way no manipulation is possible */
83 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre',".
84 "'$ruleset','$session' ,NULL)");
88 $max = DB_get_max_session();
90 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre',".
91 "'$ruleset','$max' ,NULL)");
92 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
96 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,'1','pre', ".
97 "'$ruleset',NULL ,NULL)");
98 $game_id = mysql_insert_id();
101 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
102 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
103 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
104 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
107 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
108 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
109 $hand_idA = mysql_insert_id();
110 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
111 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
112 $hand_idB = mysql_insert_id();
113 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
114 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
115 $hand_idC = mysql_insert_id();
116 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
117 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
118 $hand_idD = mysql_insert_id();
122 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
123 for($i=12;$i<24;$i++)
124 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
125 for($i=24;$i<36;$i++)
126 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
127 for($i=36;$i<48;$i++)
128 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
130 /* send out email, TODO: check for error with email */
132 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
133 "Place comments and bug reports here:\n".
134 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
135 "The whole round would consist of the following players:\n".
140 "If you want to join this game, please follow this link:\n\n".
143 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
144 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
145 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
146 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
148 echo "You started a new game. The emails have been sent out!";
149 } /* end set up a new game */
150 else if(myisset("cancle","me"))
152 /* cancle a game, if it is older than N minutes */
154 $me = $_REQUEST["me"];
156 /* test for valid ID */
157 $myid = DB_get_userid_by_hash($me);
160 echo "Can't find you in the database, please check the url.<br />\n";
161 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
166 DB_update_user_timestamp($myid);
168 /* get some information from the DB */
169 $gameid = DB_get_gameid_by_hash($me);
170 $myname = DB_get_name_by_hash($me);
172 /* check if game really is old enough */
173 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
174 $r = mysql_fetch_array($result,MYSQL_NUM);
175 if(time()-strtotime($r[0]) > 60*60*24*30)
177 $message = "Hello, \n\n".
178 "Game $gameid has been cancled since nothing happend for a while and $myname requested it.\n";
180 $userids = DB_get_all_userid_by_gameid($gameid);
181 foreach($userids as $user)
183 $To = DB_get_email_by_userid($user);
184 mymail($To,$EmailName."game cancled (timed out)",$message);
187 /* delete everything from the dB */
190 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
193 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
195 else if(myisset("me"))
197 /* handle request from one specific player,
198 * the hash is set on a per game base
201 $me = $_REQUEST["me"];
203 /* test for valid ID */
204 $myid = DB_get_userid_by_hash($me);
207 echo "Can't find you in the database, please check the url.<br />\n";
208 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
213 DB_update_user_timestamp($myid);
215 /* get some information from the DB */
216 $gameid = DB_get_gameid_by_hash($me);
217 $myname = DB_get_name_by_hash($me);
218 $mystatus = DB_get_status_by_hash($me);
219 $mypos = DB_get_pos_by_hash($me);
222 /* display rule set */
223 echo "<div class=\"ruleset\">\n";
224 $result = mysql_query("SELECT * FROM Rulesets LEFT JOIN Game ON Game.ruleset=Rulesets.id WHERE Game.id='$gameid'" );
225 $r = mysql_fetch_array($result,MYSQL_NUM);
227 $RULES["dullen"]=$r[2];
228 $RULES["schweinchen"]=$r[3];
230 /* get some infos about the game */
231 $gametype = DB_get_gametype_by_gameid($gameid);
232 $gamestatus = DB_get_game_status_by_gameid($gameid);
234 if($gametype=="solo")
236 $gametype = DB_get_solo_by_gameid($gameid);
237 $GT = $gametype." ".$GT;
242 if($gamestatus != 'pre')
243 echo " Gametype: $GT <br />\n";
245 echo "Rules: <br />\n";
246 echo "10ofhearts : ".$r[2]."<br />\n";
247 echo "schweinchen: ".$r[3]."<br />\n";
250 /* does anyone have both foxes */
251 $GAME["schweinchen"]=0;
254 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
255 $cards = DB_get_all_hand($hash);
256 if( in_array("19",$cards) && in_array("20",$cards) )
258 $GAME["schweinchen"]=1;
259 $GAME["schweinchen-who"]=$hash;
263 /* mystatus gets the player through the different stages of a game.
265 * init: check values from start,
267 * check: check for return values from init
268 * poverty: handle poverty, wait here until all player have reached this state
269 * display sickness and move on to game
270 * play: game in progress
271 * gameover: are we revisiting a game
276 check_want_to_play($me);
277 /* move on to the next stage*/
278 DB_set_hand_status_by_hash($me,'init');
283 echo "you need to answer the question";
284 DB_set_hand_status_by_hash($me,'start');
288 if($_REQUEST["in"] == "no")
290 /* cancle the game */
291 $message = "Hello, \n\n".
292 "the game has been canceled due to the request of one of the players.\n";
294 $userids = DB_get_all_userid_by_gameid($gameid);
295 foreach($userids as $user)
297 $To = DB_get_email_by_userid($user);
298 mymail($To,$EmailName."game canceled",$message);
301 /* delete everything from the dB */
306 echo "thanks for joining the game...";
308 $mycards = DB_get_hand($me);
310 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
311 foreach($mycards as $card)
315 check_for_sickness($me,$mycards);
317 /* move on to the next stage*/
318 DB_set_hand_status_by_hash($me,'check');
324 echo "checking if you selected solo or nines...<br />".
325 " Please click <a href=\"$host?me=$me\">here</a> to finish the setup.<br />";
326 if(!myisset("solo","wedding","poverty","nines") )
328 /* all these variables have a pre-selected default,
329 * so we should never get here,
330 * unless a user tries to cheat ;) */
331 echo "something went wrong...please contact the admin.";
335 /* check if this sickness needs to be handled first */
336 $gametype = DB_get_gametype_by_gameid($gameid);
337 $startplayer = DB_get_startplayer_by_gameid($gameid);
339 if( $_REQUEST["solo"]!="No")
341 /* store the info in the user's hand info */
342 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
343 DB_set_sickness_by_hash($me,"solo");
344 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
346 if($gametype == "solo" && $startplayer<$mypos)
347 {}/* do nothing, since someone else already is playing solo */
350 /* this solo comes first
351 * store info in game table
353 DB_set_gametype_by_gameid($gameid,"solo");
354 DB_set_startplayer_by_gameid($gameid,$mypos);
355 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
358 else if($_REQUEST["wedding"] == "yes")
360 /* TODO: add silent solo somewhere*/
361 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
362 DB_set_sickness_by_hash($me,"wedding");
364 else if($_REQUEST["poverty"] == "yes")
366 echo "So you got poverty. You might as well have said nothing, since this is not implemented yet,".
367 " so you need to play a normal game...to make it a bit harder, I'll tell the other people that".
368 " you only have a few trump... should make the game more interesting (although perhaps not for you:))<br />\n";
369 DB_set_sickness_by_hash($me,"poverty");
371 else if($_REQUEST["nines"] == "yes")
373 echo "What you just don't want to play a game because you have a few nines? Well, if no one".
374 " is playing solo, this game will be canceled.<br />\n";
375 DB_set_sickness_by_hash($me,"nines");
379 /* move on to the next stage*/
380 DB_set_hand_status_by_hash($me,'poverty');
385 /* here we need to check if there is a solo or some other form of sickness.
386 * If so, which one counts
387 * set that one in the Game table
388 * tell people about it.
390 echo "<br />checking if someone else selected solo or nines... poverty not handled at the moment<br />".
391 " Please click <a href=\"$host?me=$me\">here</a> to finish the setup.<br />";
393 /* check if everyone has reached this stage */
394 $userids = DB_get_all_userid_by_gameid($gameid);
396 foreach($userids as $user)
398 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
399 if($userstat!='poverty' && $userstat!='play')
405 echo "Everyone has finished checking their cards, let's see what they said...<br />";
406 /* check what kind of game we are playing */
407 $gametype = DB_get_gametype_by_gameid($gameid);
408 $startplayer = DB_get_startplayer_by_gameid($gameid);
412 /* check for nines */
413 foreach($userids as $user)
414 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'nines')
417 /* gamestatus == normal, => cancel game */
418 if($nines && $gametype != "solo")
420 /* TODO: should we keep statistics of this? */
421 $message = "Hello, \n\n".
422 "the game has been canceled because ".DB_get_name_by_userid($nines)." has five or more nines.\n";
424 $userids = DB_get_all_userid_by_gameid($gameid);
425 foreach($userids as $user)
427 $To = DB_get_email_by_userid($user);
428 mymail($To,$EmailName."game canceled",$message);
431 /* delete everything from the dB */
437 /* check for different sickness and just output a general info */
439 /* check players for poverty */
441 foreach($userids as $user)
443 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'poverty')
446 $name = DB_get_name_by_userid($user);
447 echo "$name has a Vorbehalt. <br />";
451 /* check players for wedding */
453 foreach($userids as $user)
455 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'wedding')
458 $name = DB_get_name_by_userid($user);
459 echo "$name has a Vorbehalt. <br />" ;
463 /* check for solo, output vorbehalt */
465 foreach($userids as $user)
467 if(DB_get_sickness_by_userid_and_gameid($user,$gameid) == 'solo')
470 $name = DB_get_name_by_userid($user);
471 echo "$name has a Vorbehalt. <br />" ;
475 /* now check which sickness comes first and set the gametype to it */
477 /* if gamestatus == normal, set poverty or dpovert (in case two people have poverty) */
478 if($poverty>0 && $gametype == "normal")
482 DB_set_gametype_by_gameid($gameid,"poverty");
483 $gametype = "poverty";
487 DB_set_gametype_by_gameid($gameid,"dpoverty");
488 $gametype = "dpoverty";
491 /* if gamestatus == normal, set wedding */
492 if($wedding> 0 && $gametype == "normal")
494 DB_set_gametype_by_gameid($gameid,"wedding");
495 $gametype = "wedding";
498 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
499 * output what kind of game we have */
504 foreach($userids as $user)
506 $name = DB_get_name_by_userid($user);
507 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
508 if($usersick=="poverty")
511 echo "$name has $usersick <br />";
512 if($usersick == "wedding" && $gametype =="wedding")
514 if($usersick == "poverty" && $gametype =="poverty")
516 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
518 if($usersick == "solo" && $gametype =="solo")
523 if( $gametype != "solo")
524 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
525 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
529 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
533 /* are we the solo player? set us to re, else set us to contra */
534 $pos = DB_get_pos_by_hash($me);
535 if($pos == $startplayer)
536 DB_set_party_by_hash($me,"re");
538 DB_set_party_by_hash($me,"contra");
539 DB_set_hand_status_by_hash($me,'play');
542 echo "Don't know who will be Re and Contra, you need to ".
543 "figure that out at the end of the game yourself <br />\n";
544 DB_set_hand_status_by_hash($me,'play');
547 $hand = DB_get_all_hand($me);
549 if(in_array('3',$hand)||in_array('4',$hand))
550 DB_set_party_by_hash($me,"re");
552 DB_set_party_by_hash($me,"contra");
553 DB_set_hand_status_by_hash($me,'play');
556 /* figure out who has poverty */
557 /* check who was asked already
558 * everyone or trump was taken?
559 * trump was taken, start game
560 * trump was not taken, cancle game
562 * not everyone, figure out who is next in the list
563 * is the next person this one?
564 * no, display wait message, e.g. player X is asked at the moment
565 * yes, display trump, ask if he wants to take it
566 * no, set player asked to true, email next player
567 * yes, display all cards, ask for N return cards
572 echo "TODO: handle double poverty here";
573 DB_set_hand_status_by_hash($me,'play');
578 echo "You need to wait for the others, the game can only start after everyone finished checking their cards.<br />";
581 /* check if all players are ready to play */
583 foreach($userids as $user)
584 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
589 /* only set this after all poverty, etc. are handled*/
590 DB_set_game_status_by_gameid($gameid,'play');
592 /* email startplayer */
593 $startplayer = DB_get_startplayer_by_gameid($gameid);
594 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
595 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
597 $message = "It's your turn now.\n".
598 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
599 mymail($email,$EmailName."ready, set, go... ",$message);
605 /* both entries here, so that the tricks are visible for both.
606 * in case of 'play' there is a break later that skips the last part
609 /* figure out what kind of game we are playing,
610 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
611 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
615 $gametype = DB_get_gametype_by_gameid($gameid);
617 if($gametype=="solo")
619 $gametype = DB_get_solo_by_gameid($gameid);
620 $GT = $gametype." ".$GT;
625 set_gametype($gametype); /* this sets the $CARDS variable */
627 /* get some infos about the game */
628 $gamestatus = DB_get_game_status_by_gameid($gameid);
630 /* display useful things in divs */
632 /* display links to the users status page */
633 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
634 $r = mysql_fetch_array($result,MYSQL_NUM);
636 display_links($r[0],$r[1]);
638 /* end display useful things*/
640 /* has the game started? No, then just wait here...*/
641 if($gamestatus == 'pre')
643 echo "you need to wait for the others... <br />";
644 break; /* not sure this works... the idea is that you can
645 * only play a card after everyone is ready to play */
648 /* display the table and the names */
649 $result = mysql_query("SELECT User.fullname as name,".
650 " Hand.position as position, ".
653 "LEFT JOIN User ON User.id=Hand.user_id ".
654 "WHERE Hand.game_id='".$gameid."' ".
655 "ORDER BY position ASC");
657 echo "<div class=\"table\">\n".
658 " <img src=\"pics/table.png\" alt=\"table\" />\n";
659 while($r = mysql_fetch_array($result,MYSQL_NUM))
665 $offset = DB_get_user_timezone($user);
666 $zone = return_timezone($offset);
667 date_default_timezone_set($zone);
669 echo " <span class=\"table".($pos-1)."\">\n";
670 echo " $name <br />\n";
671 echo " local time: ".date("Y-m-d H:i:s")."\n";
677 /* get everything relevant to display the tricks */
678 $result = mysql_query("SELECT Hand_Card.card_id as card,".
679 " Hand.position as position,".
680 " Play.sequence as sequence, ".
684 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
685 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
686 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
687 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
688 "WHERE Trick.game_id='".$gameid."' ".
689 "ORDER BY Trick.id,sequence ASC");
692 $lasttrick = DB_get_max_trickid($gameid);
694 $play = array(); /* needed to calculate winner later */
696 $pos = DB_get_startplayer_by_gameid($gameid)-1;
697 $firstcard = ""; /* first card in a trick */
699 echo "\n<ul class=\"tricks\">\n";
700 echo " <li class=\"nohighlight\"> Game $gameid: </li>\n";
702 while($r = mysql_fetch_array($result,MYSQL_NUM))
709 /* check if first schweinchen has been played */
710 if($r[0] == 19 || $r[0] == 20 )
711 $GAME["schweinchen"]++;
713 /* save card to be able to find the winner of the trick later */
714 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
718 /* first card in a trick, output some html */
719 if($trick!=$lasttrick)
721 /* start of an old trick? */
722 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
723 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
724 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
726 else if($trick==$lasttrick)
728 /* start of a last trick? */
729 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
730 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
731 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
734 /* remember first card, so that we are able to check, what cards can be played */
739 echo " <div class=\"card".($pos-1)."\">\n";
741 /* display comments */
743 echo " <span class=\"comment\">".$comment."</span>\n";
748 echo " </div>\n"; /* end div card */
754 echo " </div>\n </li>\n"; /* end div table, end li table */
758 if($seq!=4 && $trickNR>1)
759 echo " </div>\n </li>\n"; /* end div table, end li table */
763 /* whos turn is it? */
766 $winner = get_winner($play,$gametype); /* returns the position */
768 $firstcard = ""; /* new trick, no first card */
773 if($next==5) $next=1;
776 /* my turn?, display cards as links, ask for comments*/
777 if(DB_get_pos_by_hash($me) == $next)
782 /* do we want to play a card? */
783 if(myisset("card") && $myturn)
785 $card = $_REQUEST["card"];
786 $handid = DB_get_handid_by_hash($me);
788 /* check if we have card and that we haven't played it yet*/
789 /* set played in hand_card to true where hand_id and card_id*/
790 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
791 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
792 $r = mysql_fetch_array($result,MYSQL_NUM);
799 /* mark card as played */
800 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
801 DB_quote_smart($card));
802 /* update Game timestamp */
803 DB_update_game_timestamp($gameid);
805 /* check for schweinchen */
806 //echo "schweinchen = ".$GAME["schweinchen"]." --$card-<br />";
807 if($card == 19 || $card == 20 )
809 $GAME["schweinchen"]++;
810 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
811 $comment="Schweinchen! ";
812 if($RULES["schweinchen"]=="both" )
813 $comment="Schweinchen! ";
814 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
817 /* get trick id or start new trick */
818 $a = DB_get_current_trickid($gameid);
822 $playid = DB_play_card($trickid,$handcardid,$sequence);
824 /* check for coment */
825 if(myisset("comment"))
827 $comment.=$_REQUEST["comment"];
830 DB_insert_comment($comment,$playid,$myid);
832 /* display played card */
833 echo "<div class=\"card\">";
834 echo " you played <br />";
838 /*check if we still have cards left, else set status to gameover */
839 if(sizeof(DB_get_hand($me))==0)
841 DB_set_hand_status_by_hash($me,'gameover');
842 $mystatus='gameover';
845 /* if all players are done, set game status to game over,
846 * get the points of the last trick and send out an email
849 $userids = DB_get_all_userid_by_gameid($gameid);
852 foreach($userids as $user)
853 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
858 DB_set_game_status_by_gameid($gameid,"gameover");
859 /* get score for last trick
860 * all other tricks are handled a few lines further down*/
861 $play = DB_get_cards_by_trick($trickid);
862 $winner = get_winner($play,$gametype); /* returns the position */
863 /* get points of last trick and save it */
865 foreach($play as $card)
866 $points = $points + card_value($card["card"]);
867 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
869 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
871 echo "ERROR during scoring";
873 /* email all players */
874 /* individual score */
875 $result = mysql_query("SELECT fullname, SUM(score), Hand.party FROM Score".
876 " LEFT JOIN Hand ON Hand.id=hand_id".
877 " LEFT JOIN User ON Hand.user_id=User.id".
878 " WHERE Hand.game_id=$gameid".
879 " GROUP BY fullname" );
880 $message = "The game is over. Thanks for playing :)\n";
881 while( $r = mysql_fetch_array($result,MYSQL_NUM))
882 $message .= " FINAL SCORE: ".$r[0]."(".$r[2].") ".$r[1]."\n";
883 $message .= "\nIf your not in the list above your score is zero...\n\n";
885 $result = mysql_query("SELECT Hand.party, SUM(score) FROM Score".
886 " LEFT JOIN Hand ON Hand.id=hand_id".
887 " LEFT JOIN User ON Hand.user_id=User.id".
888 " WHERE Hand.game_id=$gameid".
889 " GROUP BY Hand.party" );
891 while( $r = mysql_fetch_array($result,MYSQL_NUM))
892 $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
894 foreach($userids as $user)
896 $To = DB_get_email_by_userid($user);
897 mymail($To,$EmailName."game over",$message);
902 /* email next player */
903 if(DB_get_game_status_by_gameid($gameid)=='play')
907 $play = DB_get_cards_by_trick($trickid);
908 $winner = get_winner($play,$gametype); /* returns the position */
910 /* get points of last trick and save it, last trick is handled
911 * a few lines further up */
913 foreach($play as $card)
914 $points = $points + card_value($card["card"]);
916 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
918 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
920 echo "ERROR during scoring";
923 echo "DEBUG: $winner got $points <br />";
925 /* who is the next player? */
930 $next = DB_get_pos_by_hash($me)+1;
932 if($next==5) $next=1;
934 /* email next player */
935 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
936 $email = DB_get_email_by_hash($next_hash);
938 $message = "It's your turn now.\n".
939 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
940 mymail($email,$EmailName."a card has been played",$message);
943 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
944 "\"> next player </a> <br />\n";
950 echo "can't find that card?! <br />\n";
953 else if(myisset("card") && !$myturn )
955 echo "please wait until it's your turn! <br />\n";
958 $mycards = DB_get_hand($me);
959 $mycards = mysort($mycards,$gametype);
960 echo "<div class=\"mycards\">\n";
962 if($myturn && !myisset("card"))
964 echo "Hello ".$myname.", it's your turn! <br />\n";
965 echo "Your cards are: <br />\n";
966 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
968 /* do we have to follow suite? */
970 if(have_suit($mycards,$firstcard))
973 foreach($mycards as $card)
975 if($followsuit && !same_type($card,$firstcard))
978 display_link_card($card);
981 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
982 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
983 echo "<input type=\"submit\" value=\"move\" />\n";
986 else if($mystatus=='play')
988 echo "Your cards are: <br />\n";
989 foreach($mycards as $card)
992 else if($mystatus=='gameover')
994 $oldcards = DB_get_all_hand($me);
995 $oldcards = mysort($oldcards,$gametype);
996 echo "Your cards were: <br />\n";
997 foreach($oldcards as $card)
1002 /* check if we need to set status to 'gameover' is done during playing of the card */
1003 if($mystatus=='play')
1005 /* the following happens only when the gamestatus is 'gameover' */
1006 /* check if game is over, display results */
1007 if(DB_get_game_status_by_gameid($gameid)=='play')
1009 echo "the game is over for you.. other people still need to play though";
1013 echo "the game is over now...<br />\n";
1015 $result = mysql_query("SELECT fullname, SUM(score), Hand.party FROM Score".
1016 " LEFT JOIN Hand ON Hand.id=hand_id".
1017 " LEFT JOIN User ON Hand.user_id=User.id".
1018 " WHERE Hand.game_id=$gameid".
1019 " GROUP BY fullname" );
1020 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1021 echo " FINAL SCORE: ".$r[0]."(".$r[2].") ".$r[1]."<br />";
1023 $result = mysql_query("SELECT Hand.party, SUM(score) FROM Score".
1024 " LEFT JOIN Hand ON Hand.id=hand_id".
1025 " LEFT JOIN User ON Hand.user_id=User.id".
1026 " WHERE Hand.game_id=$gameid".
1027 " GROUP BY Hand.party" );
1028 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1029 echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />\n";
1032 $session = DB_get_session_by_gameid($gameid);
1033 $result = mysql_query("SELECT id,create_date FROM Game".
1034 " WHERE session=$session".
1035 " ORDER BY create_date DESC".
1039 $r = mysql_fetch_array($result,MYSQL_NUM);
1041 if(!$session || $gameid==$r[0])
1043 /* suggest a new game with the same people in it, just rotated once */
1044 $names = DB_get_all_names_by_gameid($gameid);
1045 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1050 echo "error in testing the status";
1055 /* user status page */
1056 else if(myisset("email","password"))
1058 /* test id and password, should really be done in one step */
1059 $email = $_REQUEST["email"];
1060 $password = $_REQUEST["password"];
1062 if(myisset("forgot"))
1066 $uid = DB_get_userid_by_email($email);
1072 echo "Hmm, you forgot your passwort...nothing I can do at the moment:( ";
1073 echo " you need to email Arun for now... in the future it will be all automated and an ";
1074 echo "email with a new password will go to $email.";
1079 echo "you need to give me an email address!";
1081 echo "couldn't find a player with this email, please contact Arun, if you think this is a mistake";
1086 if(strlen($password)!=32)
1087 $password = md5($password);
1090 $uid = DB_get_userid_by_email_and_password($email,$password);
1096 $time = DB_get_user_timestamp($uid);
1097 $unixtime =strtotime($time);
1099 $offset = DB_get_user_timezone($uid);
1100 $zone = return_timezone($offset);
1101 date_default_timezone_set($zone);
1103 echo "last login: ".date("r",$unixtime)."<br />";
1105 DB_update_user_timestamp($uid);
1107 echo "<p>these are the games you are playing in:<br />\n";
1108 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date from Hand".
1109 " LEFT JOIN Game On Hand.game_id=Game.id".
1110 " WHERE Hand.user_id='$uid' AND Game.status<>'gameover'" );
1111 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1113 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1114 if(time()-strtotime($r[2]) > 60*60*24*30)
1115 echo " The game has been running for over a month.".
1116 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1117 " (clicking here is final and can't be restored)";
1123 echo "<p>and these are your games that are already done:<br />Game: \n";
1124 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
1125 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1126 echo "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>, ";
1129 $names = DB_get_all_names();
1130 echo "<p>registered players:<br />\n";
1131 foreach ($names as $name)
1135 echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1139 echo "sorry email and password don't match <br />";
1145 /* page for registration */
1146 else if(myisset("register") )
1150 /* new user wants to register */
1151 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
1154 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
1156 echo "please chose another name<br />";
1159 if(DB_get_userid_by_email($_REQUEST["Remail"]))
1161 echo "this email address is already used ?!<br />";
1166 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
1167 ",".DB_quote_smart($_REQUEST["Remail"]).
1168 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
1169 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
1172 echo " added you to the database";
1174 echo " something went wrong";
1177 /* default login page */
1180 $pre=0;$game=0;$done=0;
1181 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1183 $pre = mysql_fetch_array($r,MYSQL_NUM);
1184 $game = mysql_fetch_array($r,MYSQL_NUM);
1185 $done = mysql_fetch_array($r,MYSQL_NUM);
1187 output_home_page($pre[0],$game[0],$done[0]);