2 error_reporting(E_ALL);
4 include_once("config.php");
5 include_once("output.php"); /* html output only */
6 include_once("db.php"); /* database only */
7 include_once("functions.php"); /* the rest */
14 echo "Database error, can't connect... Please wait a while and try again. ".
15 "If the problem doesn't go away feel free to contact $ADMIN_NAME at $ADMIN_EMAIL.";
20 /* start a session, if it is not already running */
23 /* done major error checking, output header of HTML page */
26 /* check if we want to start a new game */
32 echo "you are now logged out!";
34 else if(myisset("new"))
36 if( isset($_SESSION["name"]) )
38 $names = DB_get_all_names();
39 output_form_for_new_game($names);
43 echo "Please log in.";
46 /*check if everything is ready to set up a new game */
47 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","call" ))
49 $PlayerA = $_REQUEST["PlayerA"];
50 $PlayerB = $_REQUEST["PlayerB"];
51 $PlayerC = $_REQUEST["PlayerC"];
52 $PlayerD = $_REQUEST["PlayerD"];
54 $dullen = $_REQUEST["dullen"];
55 $schweinchen = $_REQUEST["schweinchen"];
56 $call = $_REQUEST["call"];
58 $EmailA = DB_get_email_by_name($PlayerA);
59 $EmailB = DB_get_email_by_name($PlayerB);
60 $EmailC = DB_get_email_by_name($PlayerC);
61 $EmailD = DB_get_email_by_name($PlayerD);
63 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
65 echo "couldn't find one of the names, please start a new game";
71 $useridA = DB_get_userid_by_name($PlayerA);
72 $useridB = DB_get_userid_by_name($PlayerB);
73 $useridC = DB_get_userid_by_name($PlayerC);
74 $useridD = DB_get_userid_by_name($PlayerD);
76 /* create random numbers */
77 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
78 $randomNRstring = join(":",$randomNR);
82 if(myisset("followup") )
84 $followup= $_REQUEST["followup"];
85 $session = DB_get_session_by_gameid($followup);
86 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
87 this way no manipulation is possible */
89 /* check if there is a game in pre or play mode, in that case do nothing */
90 if( DB_is_session_active($session) > 0 )
92 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
97 else if ( DB_is_session_active($session) < 0 )
99 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
106 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
107 "'$ruleset','$session' ,NULL)");
110 /* get max session */
111 $max = DB_get_max_session();
113 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
114 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
115 "'$ruleset','$max' ,NULL)");
120 /* get ruleset information or create new one */
121 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
124 myerror("Error defining ruleset: $ruleset");
129 /* get max session */
130 $max = DB_get_max_session();
133 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
134 "'$ruleset','$max' ,NULL)");
136 $game_id = mysql_insert_id();
139 $TIME = (string) time(); /* to avoid collisions */
140 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
141 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
142 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
143 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
146 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
147 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
148 $hand_idA = mysql_insert_id();
149 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
150 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
151 $hand_idB = mysql_insert_id();
152 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
153 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
154 $hand_idC = mysql_insert_id();
155 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
156 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
157 $hand_idD = mysql_insert_id();
161 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
162 for($i=12;$i<24;$i++)
163 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
164 for($i=24;$i<36;$i++)
165 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
166 for($i=36;$i<48;$i++)
167 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
169 /* send out email, TODO: check for error with email */
171 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
172 "Place comments and bug reports here:\n".
173 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
174 "The whole round would consist of the following players:\n".
179 "If you want to join this game, please follow this link:\n\n".
182 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
183 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
184 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
185 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
187 echo "You started a new game. The emails have been sent out!";
188 } /* end set up a new game */
189 /* cancle a game, if nothing has happend in the last N minutes */
190 else if(myisset("cancle","me"))
192 $me = $_REQUEST["me"];
194 /* test for valid ID */
195 $myid = DB_get_userid_by_hash($me);
198 echo "Can't find you in the database, please check the url.<br />\n";
199 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
205 DB_update_user_timestamp($myid);
207 /* get some information from the DB */
208 $gameid = DB_get_gameid_by_hash($me);
209 $myname = DB_get_name_by_hash($me);
211 /* check if game really is old enough */
212 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
213 $r = mysql_fetch_array($result,MYSQL_NUM);
214 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
216 $message = "Hello, \n\n".
217 "Game ".DB_format_gameid($gameid)." has been cancled since nothing happend for a while and $myname requested it.\n";
219 $userids = DB_get_all_userid_by_gameid($gameid);
220 foreach($userids as $user)
222 $To = DB_get_email_by_userid($user);
223 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." cancled (timed out)",$message);
226 /* delete everything from the dB */
229 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been cancled.<br /><br /></p>";
232 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
234 /* send out a reminder */
235 else if(myisset("remind","me"))
237 $me = $_REQUEST["me"];
239 /* test for valid ID */
240 $myid = DB_get_userid_by_hash($me);
243 echo "Can't find you in the database, please check the url.<br />\n";
244 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
250 DB_update_user_timestamp($myid);
252 /* get some information from the DB */
253 $gameid = DB_get_gameid_by_hash($me);
254 $myname = DB_get_name_by_hash($me);
256 /* check if game really is old enough */
257 $result = mysql_query("SELECT mod_date,player,status from Game WHERE id='$gameid' " );
258 $r = mysql_fetch_array($result,MYSQL_NUM);
259 if( (time()-strtotime($r[0]) > 60*60*24*7) && ($r[2]!='gameover') ) /* = 1 week */
261 $name = DB_get_name_by_userid($r[1]);
262 $To = DB_get_email_by_userid($r[1]);
263 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$r[1]);
265 $message = "Hello $name, \n\n".
266 "It's your turn in game ".DB_format_gameid($gameid)." \n".
267 "Actually everyone else is waiting for you for more than a week now ;)\n\n".
268 "Please visit this link now to continue: \n".
269 " ".$host."?me=".$userhash."\n\n" ;
271 if(DB_get_reminder($r[1],$gameid)>0)
273 echo "<p>An email has already been sent out.</p>\n";
277 DB_set_reminder($r[1],$gameid);
278 mymail($To,$EmailName."Reminder: game ".DB_format_gameid($gameid)." it's your turn",$message);
280 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).": an email has been sent out.<br /><br /></p>";
284 echo "<p>You need to wait longer before you can send out a reminder...</p>\n";
286 /* handle request from one specific player for one game,
287 * (the hash is set on a per game base) */
288 else if(myisset("me"))
290 $me = $_REQUEST["me"];
292 /* test for valid ID */
293 $myid = DB_get_userid_by_hash($me);
296 echo "Can't find you in the database, please check the url.<br />\n";
297 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
303 if(isset($_SESSION["name"]))
304 output_status($_SESSION["name"]);
306 /* the user had done something, update the timestamp */
307 DB_update_user_timestamp($myid);
309 /* get some information from the DB */
310 $gameid = DB_get_gameid_by_hash($me);
311 $myname = DB_get_name_by_hash($me);
312 $mystatus = DB_get_status_by_hash($me);
313 $mypos = DB_get_pos_by_hash($me);
314 $myhand = DB_get_handid_by_hash($me);
315 $session = DB_get_session_by_gameid($gameid);
317 /* get prefs and save them */
321 /* get rule set for this game */
322 $result = mysql_query("SELECT * FROM Rulesets".
323 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
324 " WHERE Game.id='$gameid'" );
325 $r = mysql_fetch_array($result,MYSQL_NUM);
327 $RULES["dullen"] = $r[2];
328 $RULES["schweinchen"] = $r[3];
329 $RULES["call"] = $r[4];
331 /* get some infos about the game */
332 $gametype = DB_get_gametype_by_gameid($gameid);
333 $gamestatus = DB_get_game_status_by_gameid($gameid);
335 if($gametype=="solo")
337 $gametype = DB_get_solo_by_gameid($gameid);
338 $GT = $gametype." ".$GT;
341 /* does anyone have both foxes */
342 $GAME["schweinchen"]=0;
345 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
346 $cards = DB_get_all_hand($hash);
347 if( in_array("19",$cards) && in_array("20",$cards) )
349 $GAME["schweinchen"]=1;
350 $GAME["schweinchen-who"]=$hash;
354 /* put everyting in a form */
355 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
359 /* output extra division in case this game is part of a session */
362 echo "<div class=\"session\">\n".
363 "This game is part of session $session: \n";
364 $hashes = DB_get_hashes_by_session($session,$myid);
366 foreach($hashes as $hash)
371 echo "<a href=\"".$host."?me=".$hash."\">$i</a> \n";
377 /* display the table and the names */
380 /* mystatus gets the player through the different stages of a game.
382 * init: check values from start,
384 * check: check for return values from init
385 * poverty: handle poverty, wait here until all player have reached this state
386 * display sickness and move on to game
387 * play: game in progress
388 * gameover: are we revisiting a game
395 output_check_want_to_play($me);
400 /* move on to the next stage*/
401 DB_set_hand_status_by_hash($me,'init');
404 /* first check if everything went ok in the last step
405 * if not, send user back, if yes, check what he did
409 echo "<p class=\"message\"> You need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
410 DB_set_hand_status_by_hash($me,'start');
414 if($_REQUEST["in"] == "no")
416 /* cancel the game */
417 $message = "Hello, \n\n".
418 "the game has been canceled due to the request of one of the players.\n";
420 $userids = DB_get_all_userid_by_gameid($gameid);
421 foreach($userids as $user)
423 $To = DB_get_email_by_userid($user);
424 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
427 /* delete everything from the dB */
432 $mycards = DB_get_hand($me);
435 output_check_for_sickness($me,$mycards);
437 echo "<p class=\"mycards\">your cards are: <br />\n";
438 foreach($mycards as $card)
439 display_card($card,$PREF["cardset"]);
442 /* move on to the next stage*/
443 DB_set_hand_status_by_hash($me,'check');
449 /* ok, user is in the game, saw his cards and selected his vorbehalt
450 * so first we check what he selected
452 if(!myisset("solo","wedding","poverty","nines") )
454 /* all these variables have a pre-selected default,
455 * so we should never get here,
456 * unless a user tries to cheat ;)
457 * can also happen if user reloads the page!
459 echo "<p class=\"message\"> You need to answer the <a href=\"$host?me=$me&in=yes\">questions</a>.</p>";
460 DB_set_hand_status_by_hash($me,'init');
464 echo "<p class=\"message\">Processing what you selected in the last step...";
466 /* check if this sickness needs to be handled first */
467 $gametype = DB_get_gametype_by_gameid($gameid);
468 $startplayer = DB_get_startplayer_by_gameid($gameid);
470 if( $_REQUEST["solo"]!="No")
472 /* user wants to play a solo */
474 /* store the info in the user's hand info */
475 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
476 DB_set_sickness_by_hash($me,"solo");
478 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
480 if($gametype == "solo" && $startplayer<$mypos)
481 {}/* do nothing, since someone else already is playing solo */
484 /* this solo comes first
485 * store info in game table
487 DB_set_gametype_by_gameid($gameid,"solo");
488 DB_set_startplayer_by_gameid($gameid,$mypos);
489 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
492 else if($_REQUEST["wedding"] == "yes")
494 /* TODO: add silent solo somewhere*/
495 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
496 DB_set_sickness_by_hash($me,"wedding");
498 else if($_REQUEST["poverty"] == "yes")
500 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
501 DB_set_sickness_by_hash($me,"poverty");
503 else if($_REQUEST["nines"] == "yes")
505 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
506 " is playing solo, this game will be canceled.<br />\n";
507 DB_set_sickness_by_hash($me,"nines");
510 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.</p>";
512 /* move on to the next stage*/
513 DB_set_hand_status_by_hash($me,'poverty');
515 /* check if everyone has reached this stage, send out email */
516 $userids = DB_get_all_userid_by_gameid($gameid);
518 foreach($userids as $user)
520 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
521 if($userstat!='poverty' && $userstat!='play')
524 DB_set_player_by_gameid($gameid,$user);
529 /* reset player = everyone has to do something now */
530 DB_set_player_by_gameid($gameid,NULL);
532 foreach($userids as $user)
534 $To = DB_get_email_by_userid($user);
535 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user); if($userhash != $me)
537 $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
538 "please visit this link now to continue: \n".
539 " ".$host."?me=".$userhash."\n\n" ;
540 mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
549 /* here we need to check if there is a solo or some other form of sickness.
550 * If so, which one is the most important one
551 * set that one in the Game table
552 * tell people about it.
554 echo "<div class=\"message\">\n";
555 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
557 /* check if everyone has reached this stage */
558 $userids = DB_get_all_userid_by_gameid($gameid);
560 foreach($userids as $user)
562 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
563 if($userstat!='poverty' && $userstat!='play')
569 echo "This step can only be handled after everyone finished the last step. ".
570 "Seems like this is not the case, so you need to wait a bit... ".
571 "you will get an email once that is the case, please use the link in ".
572 "that email to continue the game.<br />";
576 echo "Everyone has finished checking their cards, let's see what they said...<br />";
578 /* check what kind of game we are playing, in case there are any solos this already
579 *will have the correct information in it */
580 $gametype = DB_get_gametype_by_gameid($gameid);
581 $startplayer = DB_get_startplayer_by_gameid($gameid);
583 /* check for different sickness and just output a general info */
588 foreach($userids as $user)
590 $name = DB_get_name_by_userid($user);
591 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
592 if($usersick == 'nines')
595 echo "$name has a Vorbehalt. <br />";
598 else if($usersick == 'poverty')
601 echo "$name has a Vorbehalt. <br />";
603 else if($usersick == 'wedding')
606 echo "$name has a Vorbehalt. <br />" ;
608 else if($usersick == 'solo')
611 echo "$name has a Vorbehalt. <br />" ;
615 /* now check which sickness comes first and set the gametype to it */
617 if($gametype == "solo")
624 /* TODO: should we keep statistics of this? */
625 $message = "Hello, \n\n".
626 " the game has been canceled because ".DB_get_name_by_userid($nines).
627 " has five or more nines and nobody is playing solo.\n\n".
628 " To redeal either start a new game or, in case the game was part of a tournament, \n".
629 " go to the last game and use the link at the bottom of the page to redeal.";
631 $userids = DB_get_all_userid_by_gameid($gameid);
632 foreach($userids as $user)
634 $To = DB_get_email_by_userid($user);
635 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
638 /* delete everything from the dB */
641 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
642 " has five or more nines and nobody is playing solo.\n";
647 else if($poverty==1) /* one person has poverty */
649 DB_set_gametype_by_gameid($gameid,"poverty");
650 $gametype = "poverty";
651 $who = DB_get_sickness_by_gameid($gameid);
654 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
655 if($firstsick == "poverty")
656 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
658 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
661 else if($poverty==2) /* two people have poverty */
663 DB_set_gametype_by_gameid($gameid,"dpoverty");
664 $gametype = "dpoverty";
665 $who = DB_get_sickness_by_gameid($gameid);
668 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
669 if($firstsick == "poverty")
671 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
672 if($secondsick == "poverty")
673 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
675 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
678 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
683 DB_set_gametype_by_gameid($gameid,"wedding");
684 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
685 $gametype = "wedding";
690 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
691 * output what kind of game we have */
694 foreach($userids as $user)
696 /* userids are sorted by position...
697 * so output whatever the first one has, then whatever the next one has
698 * stop when the sickness is the same as the gametype
701 $name = DB_get_name_by_userid($user);
702 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
705 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
707 if($usersick=="poverty")
709 if($usersick == "wedding" && $gametype =="wedding")
711 if($usersick == "poverty" && $gametype =="poverty")
713 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
715 if($usersick == "solo" && $gametype =="solo")
719 /* output Schweinchen in case the rules need it */
720 if( $gametype != "solo")
721 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
722 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
726 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
730 /* are we the solo player? set us to re, else set us to contra */
731 $pos = DB_get_pos_by_hash($me);
732 if($pos == $startplayer)
733 DB_set_party_by_hash($me,"re");
735 DB_set_party_by_hash($me,"contra");
736 DB_set_hand_status_by_hash($me,'play');
740 /* set person with the wedding to re, do the rest during the game */
741 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
742 if($usersick == "wedding")
743 DB_set_party_by_hash($me,"re");
745 DB_set_party_by_hash($me,"contra");
747 echo "Whoever will make the first trick will be on the re team. <br />\n";
748 echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";
749 DB_set_hand_status_by_hash($me,'play');
753 $hand = DB_get_all_hand($me);
755 if(in_array('3',$hand)||in_array('4',$hand))
756 DB_set_party_by_hash($me,"re");
758 DB_set_party_by_hash($me,"contra");
759 DB_set_hand_status_by_hash($me,'play');
763 /* check if poverty resolved (e.g. DB.Game who set to NULL)
764 * yes? =>trump was taken, start game; break;
766 $who = DB_get_sickness_by_gameid($gameid);
768 { /* trump has been taken */
769 DB_set_hand_status_by_hash($me,'play');
773 if($who>9) /*= two people still have trump on the table*/
778 /* check if we are being asked now
779 * no? display wait message, e.g. player X is asked at the moment
781 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
782 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
784 /* user doesn't want to take trump */
785 /* set next player who needs to be asked */
786 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
787 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
789 if($firstsick=="poverty")
791 if($secondsick=="poverty")
792 DB_set_sickness_by_gameid($gameid,$who+$add*3);
794 DB_set_sickness_by_gameid($gameid,$who+$add*2);
797 DB_set_sickness_by_gameid($gameid,$who+$add);
799 /* email next player */
800 $who = DB_get_sickness_by_gameid($gameid);
801 if($who>9) $who = $who/10;
805 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
806 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
807 DB_set_player_by_gameid($gameid,$who);
809 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
810 " ".$host."?me=".$userhash."\n\n" ;
811 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
814 /* this user is done */
815 DB_set_hand_status_by_hash($me,'play');
818 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
820 /* user wants to take trump */
821 $trump = $_REQUEST["trump"];
823 /* get hand id for user $trump */
824 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
825 /* copy trump from player A to B */
826 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
828 /* add hidden button with trump in it to get to the next point */
829 echo "</div><div class=\"poverty\">\n";
830 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
831 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
832 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
833 echo "</div><div>\n";
835 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
837 $trump = $_REQUEST["trump"];
838 $exchange = $_REQUEST["exchange"];
839 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
841 /* if exchange is set to a value>0, exchange that card back to user $trump */
844 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
845 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
848 /* if number of cards == 12, set status to play for both users */
849 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
850 $r = mysql_fetch_array($result,MYSQL_NUM);
853 myerror("error in poverty");
858 if($gametype=="poverty" || $who<9)
860 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
862 else /* reduce poverty count by one, that is go to single digits $who */
867 /* whom to ask next */
868 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
869 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
871 if($firstsick!="poverty")
872 DB_set_sickness_by_gameid($gameid,$who+$add);
875 if($secondsick!="poverty")
876 DB_set_sickness_by_gameid($gameid,$who+$add*2);
878 DB_set_sickness_by_gameid($gameid,$who+$add*3);
881 /* email next player */
882 $who = DB_get_sickness_by_gameid($gameid);
885 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
886 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
887 DB_set_player_by_gameid($gameid,$who);
889 $message = "Someone has poverty, it's your turn to decide, ".
890 "if you want to take the trump. Please visit:".
891 " ".$host."?me=".$userhash."\n\n" ;
892 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
896 /* this user is done */
897 DB_set_hand_status_by_hash($me,'play');
898 /* and so is his partner */
899 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
900 DB_set_hand_status_by_hash($hash,'play');
902 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
904 foreach($userids as $user)
906 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
907 $party = DB_get_party_by_hash($userhash);
913 DB_set_party_by_hash($me,"contra");
914 DB_set_party_by_hash($hash,"contra");
918 foreach($userids as $user)
920 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
921 if($userhash==$hash||$userhash==$me)
922 DB_set_party_by_hash($userhash,"re");
924 DB_set_party_by_hash($userhash,"contra");
933 /* else show all trump, have lowest card pre-selected, have hidden setting for */
934 echo "</div><div class=\"poverty\"> you need to get rid of a few cards</div>\n";
936 set_gametype($gametype); /* this sets the $CARDS variable */
937 $mycards = DB_get_hand($me);
938 $mycards = mysort($mycards,$gametype);
941 echo "<div class=\"mycards\">Your cards are: <br />\n";
942 foreach($mycards as $card)
943 display_link_card($card,$PREF["cardset"],$type);
944 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
945 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
946 echo "</div><div>\n";
949 else if($who == $mypos || $who == $mypos*10)
951 echo "</div><div class=\"poverty\">\n";
952 foreach($userids as $user)
954 $name = DB_get_name_by_userid($user);
955 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
957 if($usersick=="poverty")
959 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
960 $cards = DB_get_hand($hash);
961 $nrtrump = count_trump($cards);
964 echo "Player $name has $nrtrump trump. Do you want to take them?".
965 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />\n";
968 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
969 echo "</div><div>\n";
971 echo "Your cards are: <br />\n";
972 $mycards = DB_get_hand($me);
974 echo "<p class=\"mycards\">your cards are: <br />\n";
975 foreach($mycards as $card)
976 display_card($card,$PREF["cardset"]);
981 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
982 if($mysick=="poverty")
983 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
985 echo "it's not your turn yet to decide if you want to take the trump or not.";
988 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
989 $who = DB_get_sickness_by_gameid($gameid);
990 if($who==5 || $who==50)
992 $message = "Hello, \n\n".
993 "Game ".DB_format_gameid($gameid)." has been cancled since nobody wanted to take the trump.\n";
995 $userids = DB_get_all_userid_by_gameid($gameid);
996 foreach($userids as $user)
998 $To = DB_get_email_by_userid($user);
999 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." cancled (poverty not resolved)",$message);
1002 /* delete everything from the dB */
1003 DB_cancel_game($me);
1005 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been cancled.<br /><br /></p>";
1011 /* check if all players are ready to play */
1013 foreach($userids as $user)
1014 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
1017 DB_set_player_by_gameid($gameid,$user);
1022 /* only set this after all poverty, etc. are handled*/
1023 DB_set_game_status_by_gameid($gameid,'play');
1025 /* email startplayer */
1026 $startplayer = DB_get_startplayer_by_gameid($gameid);
1027 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
1028 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1029 $who = DB_get_userid_by_email($email);
1030 DB_set_player_by_gameid($gameid,$who);
1034 /* email startplayer) */
1035 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1036 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
1037 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1040 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
1049 /* both entries here, so that the tricks are visible for both.
1050 * in case of 'play' there is a break later that skips the last part
1053 /* figure out what kind of game we are playing,
1054 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
1055 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
1059 $gametype = DB_get_gametype_by_gameid($gameid);
1061 if($gametype=="solo")
1063 $gametype = DB_get_solo_by_gameid($gameid);
1064 $GT = $gametype." ".$GT;
1067 $gametype = "normal";
1069 set_gametype($gametype); /* this sets the $CARDS variable */
1071 /* get some infos about the game */
1072 $gamestatus = DB_get_game_status_by_gameid($gameid);
1074 /* has the game started? No, then just wait here...*/
1075 if($gamestatus == 'pre')
1077 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1078 "so you need to wait for the others. Just wait for the an email... </p>";
1079 break; /* not sure this works... the idea is that you can
1080 * only play a card after everyone is ready to play */
1083 /* get time from the last action of the game */
1084 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1085 $r = mysql_fetch_array($result,MYSQL_NUM);
1086 $gameend = time() - strtotime($r[0]);
1088 /* handel comments in case player didn't play a card, allow comments a week after the end of the game */
1089 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1090 if(myisset("comment"))
1092 $comment = $_REQUEST["comment"];
1093 $playid = DB_get_current_playid($gameid);
1096 DB_insert_comment($comment,$playid,$myid);
1099 /* get everything relevant to display the tricks */
1100 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1101 " Hand.position as position,".
1102 " Play.sequence as sequence, ".
1104 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>') SEPARATOR '\n' ), ".
1105 " Play.create_date, ".
1108 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1109 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1110 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1111 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1112 "LEFT JOIN User On User.id=Comment.user_id ".
1113 "WHERE Trick.game_id='".$gameid."' ".
1114 "GROUP BY Trick.id, sequence ".
1115 "ORDER BY Trick.id, sequence ASC");
1117 $lasttrick = DB_get_max_trickid($gameid);
1119 $play = array(); /* needed to calculate winner later */
1121 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1122 $firstcard = ""; /* first card in a trick */
1124 echo "\n<ul class=\"tricks\">\n";
1125 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1127 while($r = mysql_fetch_array($result,MYSQL_NUM))
1133 $timeplayed = strtotime($r[5]);
1136 $offset = DB_get_user_timezone($user);
1137 $zone = return_timezone($offset);
1138 date_default_timezone_set($zone);
1140 /* check if first schweinchen has been played */
1141 if( $GAME["schweinchen"] && ($r[0] == 19 || $r[0] == 20) )
1142 $GAME["schweinchen"]++;
1144 /* save card to be able to find the winner of the trick later */
1145 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1149 /* first card in a trick, output some html */
1150 if($trick!=$lasttrick)
1152 /* start of an old trick? */
1153 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1154 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1155 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1157 else if($trick==$lasttrick)
1159 /* start of a last trick? */
1160 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1161 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1162 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1165 /* remember first card, so that we are able to check, what cards can be played */
1170 echo " <div class=\"card".($pos-1)."\">\n";
1172 /* display comments */
1174 echo " <span class=\"comment\">".$comment."</span>\n";
1177 display_card($r[0],$PREF["cardset"]);
1179 echo " </div>\n"; /* end div card */
1185 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1189 /* whos turn is it? */
1192 $winner = get_winner($play,$gametype); /* returns the position */
1194 $firstcard = ""; /* new trick, no first card */
1199 if($next==5) $next = 1;
1202 /* my turn?, display cards as links, ask for comments*/
1203 if(DB_get_pos_by_hash($me) == $next)
1208 /* do we want to play a card? */
1209 if(myisset("card") && $myturn)
1211 $card = $_REQUEST["card"];
1212 $handid = DB_get_handid_by_hash($me);
1214 /* check if we have card and that we haven't played it yet*/
1215 /* set played in hand_card to true where hand_id and card_id*/
1216 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1217 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1218 $r = mysql_fetch_array($result,MYSQL_NUM);
1219 $handcardid = $r[0];
1221 if($handcardid) /* everything ok, play card */
1223 /* update Game timestamp */
1224 DB_update_game_timestamp($gameid);
1226 /* check if a call was made, must do this before we set the card status to played */
1227 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1228 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1229 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1230 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1231 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1232 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1233 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1234 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1235 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1236 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1238 /* mark card as played */
1239 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1240 DB_quote_smart($card));
1242 /* get trick id or start new trick */
1243 $a = DB_get_current_trickid($gameid);
1248 $playid = DB_play_card($trickid,$handcardid,$sequence);
1250 /* check for schweinchen */
1251 if($GAME["schweinchen"] && ($card == 19 || $card == 20) )
1253 $GAME["schweinchen"]++; // count how many have been played including this one
1254 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1255 DB_insert_comment("Schweinchen! ",$playid,$myid);
1256 if($RULES["schweinchen"]=="both" )
1257 DB_insert_comment("Schweinchen! ",$playid,$myid);
1259 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1262 /* if sequence == 4 check who one in case of wedding */
1263 if($sequence == 4 && $GT == "wedding")
1265 /* is wedding resolve */
1266 $resolved = DB_get_sickness_by_gameid($gameid);
1269 /* who has wedding */
1270 $userids = DB_get_all_userid_by_gameid($gameid);
1271 foreach($userids as $user)
1273 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1274 if($usersick == "wedding")
1277 /* who won the trick */
1278 $play = DB_get_cards_by_trick($trickid);
1279 $winner = get_winner($play,$gametype); /* returns the position */
1280 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1281 /* is tricknr <=3 */
1282 if($tricknr <=3 && $winnerid!=$whosick)
1284 /* set resolved at tricknr*/
1285 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1287 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1288 DB_set_party_by_hash($whash,"re");
1290 if($tricknr == 3 && $winnerid==$whosick)
1292 /* set resolved at tricknr*/
1293 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1298 /* if sequence == 4, set winner of the trick, count points and set the next player */
1301 $play = DB_get_cards_by_trick($trickid);
1302 $winner = get_winner($play,$gametype); /* returns the position */
1305 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1307 echo "ERROR during scoring";
1310 echo "DEBUG: position $winner won the trick <br />";
1312 /* who is the next player? */
1317 $next = DB_get_pos_by_hash($me)+1;
1319 if($next==5) $next=1;
1321 /* check for coment */
1322 if(myisset("comment"))
1324 $comment = $_REQUEST["comment"];
1326 DB_insert_comment($comment,$playid,$myid);
1329 /* display played card */
1330 $pos = DB_get_pos_by_hash($me);
1333 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1334 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1335 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1338 echo "<div class=\"card".($pos-1)."\">";
1339 /* display comments */
1340 display_card($card,$PREF["cardset"]);
1342 echo " <span class=\"comment\"> ".$comment."</span>\n";
1343 echo "</div></div></li>\n";
1345 /*check if we still have cards left, else set status to gameover */
1346 if(sizeof(DB_get_hand($me))==0)
1348 DB_set_hand_status_by_hash($me,'gameover');
1349 $mystatus='gameover';
1352 /* if all players are done, set game status to game over,
1353 * get the points of the last trick and send out an email
1356 $userids = DB_get_all_userid_by_gameid($gameid);
1359 foreach($userids as $user)
1360 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1364 DB_set_game_status_by_gameid($gameid,"gameover");
1366 /* email next player, if game is still running */
1367 if(DB_get_game_status_by_gameid($gameid)=='play')
1369 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1370 $email = DB_get_email_by_hash($next_hash);
1371 $who = DB_get_userid_by_email($email);
1372 DB_set_player_by_gameid($gameid,$who);
1374 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1375 "It's your turn now.\n".
1376 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1377 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1379 else /* send out final email */
1381 /* individual score */
1382 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1383 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1384 " LEFT JOIN User ON User.id=Hand.user_id".
1385 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1386 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1387 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1388 " WHERE Hand.game_id='$gameid'".
1389 " GROUP BY User.fullname" );
1390 $message = "The game is over. Thanks for playing :)\n";
1391 $message .= "Final score:\n";
1392 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1393 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1395 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1396 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1397 " LEFT JOIN User ON User.id=Hand.user_id".
1398 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1399 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1400 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1401 " WHERE Hand.game_id='$gameid'".
1402 " GROUP BY Hand.party" );
1403 $message .= "\nTotals:\n";
1404 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1405 $message .= " ".$r[0]." ".$r[1]."\n";
1407 /* send out final email */
1410 foreach($userids as $user)
1411 $all[] = DB_get_email_by_userid($user);
1412 $To = implode(",",$all);
1414 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1415 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1417 foreach($userids as $user)
1419 $To = DB_get_email_by_userid($user);
1420 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1422 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".$host."?me=".$hash."\n\n" ;
1423 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1429 echo "can't find that card?! <br />\n";
1432 else if(myisset("card") && !$myturn )
1434 echo "please wait until it's your turn! <br />\n";
1437 if($seq!=4 && $trickNR>1)
1438 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1440 /* display points in case game is over */
1441 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1443 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1444 " <div class=\"trick\" id=\"trick13\">\n";
1445 /* add pic for re/contra
1446 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1448 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1449 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1450 " LEFT JOIN User ON User.id=Hand.user_id".
1451 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1452 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1453 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1454 " WHERE Hand.game_id='$gameid'".
1455 " GROUP BY User.fullname" );
1456 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1457 echo " <div class=\"card".($r[3]-1)."\">\n".
1458 " <span class=\"score\">".$r[2]."<br /> ".$r[1]."</span>\n".
1461 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1465 echo "</ul>\n"; /* end ul tricks*/
1467 $mycards = DB_get_hand($me);
1468 $mycards = mysort($mycards,$gametype);
1469 echo "<div class=\"mycards\">\n";
1471 if($myturn && !myisset("card") && $mystatus=='play' )
1473 echo "Hello ".$myname.", it's your turn! <br />\n";
1474 echo "Your cards are: <br />\n";
1476 /* do we have to follow suite? */
1478 if(have_suit($mycards,$firstcard))
1481 foreach($mycards as $card)
1483 if($followsuit && !same_type($card,$firstcard))
1484 display_card($card,$PREF["cardset"]);
1486 display_link_card($card,$PREF["cardset"]);
1489 else if($mystatus=='play' )
1491 echo "Your cards are: <br />\n";
1492 foreach($mycards as $card)
1493 display_card($card,$PREF["cardset"]);
1495 else if($mystatus=='gameover')
1497 $oldcards = DB_get_all_hand($me);
1498 $oldcards = mysort($oldcards,$gametype);
1499 echo "Your cards were: <br />\n";
1500 foreach($oldcards as $card)
1501 display_card($card,$PREF["cardset"]);
1503 $userids = DB_get_all_userid_by_gameid($gameid);
1504 foreach($userids as $user)
1506 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1512 $name = DB_get_name_by_userid($user);
1513 $oldcards = DB_get_all_hand($userhash);
1514 $oldcards = mysort($oldcards,$gametype);
1515 echo "$name's cards were: <br />\n";
1516 foreach($oldcards as $card)
1517 display_card($card,$PREF["cardset"]);
1523 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1524 if($mystatus=='play')
1527 /* the following happens only when the gamestatus is 'gameover' */
1528 /* check if game is over, display results */
1529 if(DB_get_game_status_by_gameid($gameid)=='play')
1531 echo "the game is over for you.. other people still need to play though";
1535 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1536 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1537 " LEFT JOIN User ON User.id=Hand.user_id".
1538 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1539 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1540 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1541 " WHERE Hand.game_id='$gameid'".
1542 " GROUP BY Hand.party" );
1543 echo "<div class=\"total\"> Totals:<br />\n";
1544 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1545 echo " ".$r[0]." ".$r[1]."<br />\n";
1551 myerror("error in testing the status");
1553 /* output left menu */
1554 display_user_menu();
1556 /* output right menu */
1558 /* display rule set for this game */
1559 echo "<div class=\"gameinfo\">\n";
1561 if($gamestatus != 'pre')
1562 echo " Gametype: $GT <br />\n";
1564 echo "Rules: <br />\n";
1565 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1566 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1567 echo "call: ".$RULES["call"] ."<br />\n";
1570 if($gamestatus == 'play' )
1571 output_form_calls($me);
1573 /* get time from the last action of the game */
1574 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1575 $r = mysql_fetch_array($result,MYSQL_NUM);
1576 $gameend = time() - strtotime($r[0]);
1578 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1580 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1584 echo "<input type=\"submit\" value=\"submit\" />\n";
1587 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1591 $session = DB_get_session_by_gameid($gameid);
1592 $result = mysql_query("SELECT id,create_date FROM Game".
1593 " WHERE session=$session".
1594 " ORDER BY create_date DESC".
1598 $r = mysql_fetch_array($result,MYSQL_NUM);
1600 if(!$session || $gameid==$r[0])
1602 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1603 $names = DB_get_all_names_by_gameid($gameid);
1604 $type = DB_get_gametype_by_gameid($gameid);
1607 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1609 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1621 /* user status page */
1622 else if( myisset("email","password") || isset($_SESSION["name"]) )
1624 /* test id and password, should really be done in one step */
1625 if(!isset($_SESSION["name"]))
1627 $email = $_REQUEST["email"];
1628 $password = $_REQUEST["password"];
1632 $name = $_SESSION["name"];
1633 $email = DB_get_email_by_name($name);
1634 $password = DB_get_passwd_by_name($name);
1637 if(myisset("forgot"))
1641 $myid = DB_get_userid_by_email($email);
1647 /* check how many entries in recovery table */
1648 $number = DB_get_number_of_passwords_recovery($myid);
1650 /* if less than N recent ones, add a new one and send out email */
1653 echo "Ok, I send you a new password. <br />";
1655 echo "N.B. You tried this already $number times during the last day and it will only work ".
1656 " 5 times during a day.<br />";
1657 echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
1658 echo "Back to the <a href=\"$host\">main page</a>.";
1660 $TIME = (string) time(); /* to avoid collisions */
1661 $hash = md5("Anewpassword".$email.$TIME);
1662 $newpw = substr($hash,1,8);
1664 $message = "Someone (hopefully you) requested a new password. \n".
1665 "You can use this email and the following password: \n".
1667 "to log into the server. The new password is valid for 24h, so make\n".
1668 "sure you reset your password to something new. Your old password will\n".
1669 " also still be valid until you set a new one\n";
1670 mymail($email,$EmailName."recovery ",$message);
1672 DB_set_recovery_password($myid,md5($newpw));
1676 echo "Sorry you already tried 5 times during the last 24h.<br />".
1677 "You need to use one of those passwords or wait to get a new one.<br />";
1678 echo "Back to the <a href=\"$host\">main page</a>.";
1684 echo "You need to give me an email address! <br />".
1685 "Please try <a href=\"$host\">again</a>.";
1687 echo "Couldn't find a player with this email! <br />".
1688 "Please contact Arun, if you think this is a mistake <br />".
1689 "or else try <a href=\"$host\">again</a>.";
1694 /* verify password and email */
1695 if(strlen($password)!=32)
1696 $password = md5($password);
1699 $myid = DB_get_userid_by_email_and_password($email,$password);
1707 if(myisset("setpref"))
1709 $setpref=$_REQUEST["setpref"];
1713 case "englishcards":
1714 $result = mysql_query("SELECT * from User_Prefs".
1715 " WHERE user_id='$myid' AND pref_key='cardset'" );
1716 if( mysql_fetch_array($result,MYSQL_NUM))
1717 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1718 " WHERE user_id='$myid' AND pref_key='cardset'" );
1720 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','cardset',".
1721 DB_quote_smart($setpref).")");
1722 echo "Ok, changed you preferences for the cards.\n";
1726 else if(myisset("passwd"))
1728 if( $_REQUEST["passwd"]=="ask" )
1730 /* reset password form*/
1731 output_password_recovery($email,$password);
1733 else if($_REQUEST["passwd"]=="set")
1735 /* reset password */
1738 /* check if old password matches */
1739 $oldpasswd = md5($_REQUEST["password0"]);
1740 if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
1742 /* check if new passwords are types the same twice */
1743 if($_REQUEST["password1"] != $_REQUEST["password2"] )
1749 echo "The new passwords don't match. <br />";
1752 echo "The old password is not correct. <br />";
1755 echo "Changed the password.<br />";
1756 mysql_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
1757 "' WHERE id=".DB_quote_smart($myid));
1763 else /* output default user page */
1765 $time = DB_get_user_timestamp($myid);
1766 $unixtime = strtotime($time);
1768 $offset = DB_get_user_timezone($myid);
1769 $zone = return_timezone($offset);
1770 date_default_timezone_set($zone);
1772 $myname = DB_get_name_by_email($email);
1773 $_SESSION["name"] = $myname;
1775 if(isset($_SESSION["name"]))
1776 output_status($_SESSION["name"]);
1778 /* display links to settings */
1779 output_user_settings($email,$password);
1781 echo "<div style=\"position:absolute; font-size:smaller; top:0; left:0; \">last login: ".date("r",$unixtime)."</div>";
1783 DB_update_user_timestamp($myid);
1785 display_user_menu();
1787 echo "<div class=\"user\">";
1788 echo "<h4>These are all your games:</h4>\n";
1789 echo "<p>Session: <br />\n";
1790 echo "<span class=\"gamestatuspre\"> p </span> = pre-game phase ";
1791 echo "<span class=\"gamestatusplay\">P </span> = game in progess ";
1792 echo "<span class=\"gamestatusover\">F </span> = game finished <br />";
1796 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player,Game.status from Hand".
1797 " LEFT JOIN Game ON Game.id=Hand.game_id".
1798 " WHERE user_id='$myid'".
1799 " ORDER BY Game.session,Game.create_date" );
1801 echo "<table>\n <tr><td>\n";
1802 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1804 $game = DB_format_gameid($r[1]);
1805 $gamenr = (int) $game;
1806 if($gamenrold < $gamenr)
1809 echo "</td></tr>\n <tr> <td>$gamenr:</td><td> ";
1811 echo "$gamenr:</td><td> ";
1812 $gamenrold = $gamenr;
1816 echo "\n <span class=\"gamestatuspre\"><a href=\"".$host."?me=".$r[0]."\">p </a></span> ";
1819 else if ($r[4]=='gameover')
1820 echo "\n <span class=\"gamestatusover\"><a href=\"".$host."?me=".$r[0]."\">F </a></span> ";
1823 echo "\n <span class=\"gamestatusplay\"><a href=\"".$host."?me=".$r[0]."\">P </a></span> ";
1825 if($r[4] != 'gameover')
1827 echo "</td><td>\n ";
1831 echo "(it's <strong>your</strong> turn)\n";
1834 $name = DB_get_name_by_userid($r[3]);
1836 if(DB_get_reminder($r[3],$gameid)==0)
1837 if(time()-strtotime($r[2]) > 60*60*24*7)
1839 "<a href=\"$host?remind=1&me=".$r[0]."\">Send a reminder.</a>";
1840 echo "(it's $name's turn)\n";
1843 if(time()-strtotime($r[2]) > 60*60*24*30)
1845 "<a href=\"$host?cancle=1&me=".$r[0]."\">Cancel?</a>".
1846 " (clicking here is final and can't be restored)";
1850 echo "</td></tr>\n</table>\n";
1851 $names = DB_get_all_names();
1852 echo "<h4>Registered players:</h4>\n<p>\n";
1853 echo implode(", ",$names)."\n";
1854 echo "</p>\n</div>";
1859 echo "Sorry email and password don't match. Please <a href=\"$host\">try again</a>. <br />";
1866 /* default login page */
1869 $pre[0]=0;$game[0]=0;$done[0]=0;
1870 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1872 $pre = mysql_fetch_array($r,MYSQL_NUM);
1873 $game = mysql_fetch_array($r,MYSQL_NUM);
1874 $done = mysql_fetch_array($r,MYSQL_NUM);
1877 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
1879 $avgage= mysql_fetch_array($r,MYSQL_NUM);
1883 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);