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 */
9 /* make sure that user has set all variables in config.php */
16 echo "Database error, can't connect... Please wait a while and try again. ".
17 "If the problem doesn't go away feel free to contact $ADMIN_NAME at $ADMIN_EMAIL.";
22 /* start a session, if it is not already running.
23 * This way people don't have to log in all the times.
24 * The session variables can also be read out from different
25 * php scripts, so that the code can be easily split up across several files
29 /* done major error checking, output header of HTML page */
32 /* does the user want to log out? */
38 echo "<div class=\"message\"><span class=\"bigger\">You are now logged out!</span><br />\n".
39 "(<a href=\"$INDEX\">This will take you back to the home-page</a>)</div>";
41 /* check if we want to start a new game */
42 else if(myisset("new"))
45 /* user need to be logged in to do this */
46 if( isset($_SESSION["name"]) )
48 $names = DB_get_all_names();
49 echo "<div class=\"user\">\n";
50 output_form_for_new_game($names);
56 echo "<div class=\"message\">Please <a href=\"$INDEX\">log in</a>.</div>";
59 /*check if everything is ready to set up a new game */
60 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","call" ))
63 /* user needs to be logged in */
64 if( !isset($_SESSION["name"]) )
66 echo "<div class=\"message\">Please <a href=\"$INDEX\">log in</a>.</div>";
71 $name = $_SESSION["name"];
73 /* the names of the four players */
74 $PlayerA = $_REQUEST["PlayerA"];
75 $PlayerB = $_REQUEST["PlayerB"];
76 $PlayerC = $_REQUEST["PlayerC"];
77 $PlayerD = $_REQUEST["PlayerD"];
79 /* the person who sets up the game has to be one of the players */
80 if(!in_array($name,array($PlayerA,$PlayerB,$PlayerC,$PlayerD)))
82 echo "<div class=\"message\">You need to be one of the players to start a <a href=\"$INDEX?new\">new game</a>.</div>";
88 /* what rules were selected */
89 $dullen = $_REQUEST["dullen"];
90 $schweinchen = $_REQUEST["schweinchen"];
91 $call = $_REQUEST["call"];
93 /* get the emails addresses of the players */
94 $EmailA = DB_get_email_by_name($PlayerA);
95 $EmailB = DB_get_email_by_name($PlayerB);
96 $EmailC = DB_get_email_by_name($PlayerC);
97 $EmailD = DB_get_email_by_name($PlayerD);
99 /* this is used to check if the player names are all ok */
100 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
102 echo "couldn't find one of the names, please start a new game";
109 $useridA = DB_get_userid_by_name($PlayerA);
110 $useridB = DB_get_userid_by_name($PlayerB);
111 $useridC = DB_get_userid_by_name($PlayerC);
112 $useridD = DB_get_userid_by_name($PlayerD);
114 /* create random numbers */
115 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
116 $randomNRstring = join(":",$randomNR);
120 /* is this game a follow up in an already started session? */
121 if(myisset("followup") )
123 $followup= $_REQUEST["followup"];
124 $session = DB_get_session_by_gameid($followup);
125 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
126 this way no manipulation is possible */
128 /* check if there is a game in pre or play mode, in that case do nothing */
129 if( DB_is_session_active($session) > 0 )
131 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
136 else if ( DB_is_session_active($session) < 0 )
138 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
145 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
146 "'$ruleset','$session' ,NULL)");
149 /* get max session and start a new one */
150 $max = DB_get_max_session();
152 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
153 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
154 "'$ruleset','$max' ,NULL)");
157 else /* no follow up, start a new session */
159 /* get ruleset information or create new one */
160 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
163 myerror("Error defining ruleset: $ruleset");
168 /* get max session */
169 $max = DB_get_max_session();
172 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
173 "'$ruleset','$max' ,NULL)");
175 $game_id = mysql_insert_id();
178 $TIME = (string) time(); /* to avoid collisions */
179 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
180 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
181 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
182 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
185 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
186 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
187 $hand_idA = mysql_insert_id();
188 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
189 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
190 $hand_idB = mysql_insert_id();
191 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
192 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
193 $hand_idC = mysql_insert_id();
194 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
195 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
196 $hand_idD = mysql_insert_id();
200 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
201 for($i=12;$i<24;$i++)
202 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
203 for($i=24;$i<36;$i++)
204 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
205 for($i=36;$i<48;$i++)
206 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
208 /* send out email, TODO: check for error with email */
210 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
211 "Place comments and bug reports here:\n".
212 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
213 "The whole round would consist of the following players:\n".
218 "If you want to join this game, please follow this link:\n\n".
219 "".$HOST.$INDEX."?me=";
221 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
222 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
223 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
224 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
226 echo "<div class=\"message\">You started a new game. The emails have been sent out!</div>\n";
228 /* end set up a new game */
230 /* cancel a game, if nothing has happend in the last N minutes */
231 else if(myisset("cancel","me"))
235 $me = $_REQUEST["me"];
237 /* test for valid ID */
238 $myid = DB_get_userid_by_hash($me);
241 echo "Can't find you in the database, please check the url.<br />\n";
242 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
248 DB_update_user_timestamp($myid);
250 /* get some information from the DB */
251 $gameid = DB_get_gameid_by_hash($me);
252 $myname = DB_get_name_by_hash($me);
254 /* check if game really is old enough to be canceled */
255 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
256 $r = mysql_fetch_array($result,MYSQL_NUM);
257 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
259 $message = "Hello, \n\n".
260 "Game ".DB_format_gameid($gameid).
261 " has been canceled since nothing happend for a while and $myname requested it.\n";
263 $userids = DB_get_all_userid_by_gameid($gameid);
264 foreach($userids as $user)
266 $To = DB_get_email_by_userid($user);
267 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (timed out)",$message);
270 /* delete everything from the dB */
273 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
274 " has been canceled.<br /><br /></p>";
277 echo "<p>You need to wait longer before you can cancel a game...</p>\n";
279 /* send out a reminder */
280 else if(myisset("remind","me"))
284 $me = $_REQUEST["me"];
286 /* test for valid ID */
287 $myid = DB_get_userid_by_hash($me);
290 echo "Can't find you in the database, please check the url.<br />\n";
291 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
297 DB_update_user_timestamp($myid);
299 /* get some information from the DB */
300 $gameid = DB_get_gameid_by_hash($me);
301 $myname = DB_get_name_by_hash($me);
303 /* check if player hasn't done anything in a while */
304 $result = mysql_query("SELECT mod_date,player,status from Game WHERE id='$gameid' " );
305 $r = mysql_fetch_array($result,MYSQL_NUM);
306 if( (time()-strtotime($r[0]) > 60*60*24*7) && ($r[2]!='gameover') ) /* = 1 week */
308 $name = DB_get_name_by_userid($r[1]);
309 $To = DB_get_email_by_userid($r[1]);
310 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$r[1]);
312 $message = "Hello $name, \n\n".
313 "It's your turn in game ".DB_format_gameid($gameid)." \n".
314 "Actually everyone else is waiting for you for more than a week now ;)\n\n".
315 "Please visit this link now to continue: \n".
316 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
318 /* make sure we don't send too many reminders to one person */
319 if(DB_get_reminder($r[1],$gameid)>0)
321 echo "<p>An email has already been sent out.</p>\n";
325 DB_set_reminder($r[1],$gameid);
326 mymail($To,$EmailName."Reminder: game ".DB_format_gameid($gameid)." it's your turn",$message);
328 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
329 ": an email has been sent out.<br /><br /></p>";
333 echo "<p>You need to wait longer before you can send out a reminder...</p>\n";
335 /* handle request from one specific player for one game,
336 * (the hash is set on a per game base) */
337 else if(myisset("me"))
339 $me = $_REQUEST["me"];
341 /* test for valid ID */
342 $myid = DB_get_userid_by_hash($me);
345 echo "Can't find you in the database, please check the url.<br />\n";
346 echo "perhaps the game has been canceled, check by login in <a href=\"$INDEX\">here</a>.";
352 /* user might get here by clicking on the link in an email, so session might not be set */
353 if(isset($_SESSION["name"]))
354 output_status($_SESSION["name"]);
356 /* the user had done something, update the timestamp */
357 DB_update_user_timestamp($myid);
359 /* get some information from the DB */
360 $gameid = DB_get_gameid_by_hash($me);
361 $myname = DB_get_name_by_hash($me);
362 $mystatus = DB_get_status_by_hash($me);
363 $mypos = DB_get_pos_by_hash($me);
364 $myhand = DB_get_handid_by_hash($me);
365 $session = DB_get_session_by_gameid($gameid);
367 /* get prefs and save them */
371 /* get rule set for this game */
372 $result = mysql_query("SELECT * FROM Rulesets".
373 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
374 " WHERE Game.id='$gameid'" );
375 $r = mysql_fetch_array($result,MYSQL_NUM);
377 $RULES["dullen"] = $r[2];
378 $RULES["schweinchen"] = $r[3];
379 $RULES["call"] = $r[4];
381 /* get some infos about the game */
382 $gametype = DB_get_gametype_by_gameid($gameid);
383 $gamestatus = DB_get_game_status_by_gameid($gameid);
385 if($gametype=="solo")
387 $gametype = DB_get_solo_by_gameid($gameid);
388 $GT = $gametype." ".$GT;
391 /* does anyone have both foxes */
392 $GAME["schweinchen"]=0;
395 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
396 $cards = DB_get_all_hand($hash);
397 if( in_array("19",$cards) && in_array("20",$cards) )
399 $GAME["schweinchen"]=1;
400 $GAME["schweinchen-who"]=$hash;
404 /* put everyting in a form */
405 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
409 /* output extra division in case this game is part of a session */
412 echo "<div class=\"session\">\n".
413 "This game is part of session $session: \n";
414 $hashes = DB_get_hashes_by_session($session,$myid);
416 foreach($hashes as $hash)
421 echo "<a href=\"".$INDEX."?me=".$hash."\">$i</a> \n";
427 /* display the table and the names */
430 /* mystatus gets the player through the different stages of a game.
432 * init: check values from start,
434 * check: check for return values from init
435 * poverty: handle poverty, wait here until all player have reached this state
436 * display sickness and move on to game
437 * play: game in progress
438 * gameover: are we revisiting a game
445 /* asks the player, if he wants to join the game */
446 output_check_want_to_play($me);
451 /* check the result, if player wants to join, got next stage, else cancel game */
452 if($_REQUEST["in"] == "no")
454 /* cancel the game */
455 $message = "Hello, \n\n".
456 "the game has been canceled due to the request of one of the players.\n";
458 $userids = DB_get_all_userid_by_gameid($gameid);
459 foreach($userids as $user)
461 $To = DB_get_email_by_userid($user);
462 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
465 /* delete everything from the dB */
471 /* user wants to join the game */
473 /* move on to the next stage,
474 * no break statement to immediately go to the next stage
477 DB_set_hand_status_by_hash($me,'init');
479 /* check if everyone has reached this stage, send out email */
480 $userids = DB_get_all_userid_by_gameid($gameid);
482 foreach($userids as $user)
484 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
485 if($userstat!='init')
487 /* whos turn is it? */
488 DB_set_player_by_gameid($gameid,$user);
494 /* all done, send out email unless this player is the startplayer */
495 $startplayer = DB_get_startplayer_by_gameid($gameid);
496 if($mypos == $startplayer)
498 /* do nothing, go to next stage */
502 /* email startplayer */
504 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
505 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
506 $who = DB_get_userid_by_email($email);
507 DB_set_player_by_gameid($gameid,$who);
509 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
510 "Use this link to go the game: ".$HOST.$INDEX."?me=".$hash."\n\n" ;
511 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
519 $mycards = DB_get_hand($me);
522 output_check_for_sickness($me,$mycards);
524 echo "<p class=\"mycards\">Your cards are: <br />\n";
525 foreach($mycards as $card)
526 display_card($card,$PREF["cardset"]);
529 /* move on to the next stage*/
530 DB_set_hand_status_by_hash($me,'check');
534 /* ok, user is in the game, saw his cards and selected his vorbehalt
535 * so first we check what he selected
537 if(!myisset("solo","wedding","poverty","nines") )
539 /* all these variables have a pre-selected default,
540 * so we should never get here,
541 * unless a user tries to cheat ;)
542 * can also happen if user reloads the page!
544 echo "<p class=\"message\"> You need to answer the <a href=\"$INDEX?me=$me&in=yes\">questions</a>.</p>";
545 DB_set_hand_status_by_hash($me,'init');
549 /* check if someone selected more than one vorbehalt */
551 if($_REQUEST["solo"]!="No") $Nvorbehalt++;
552 if($_REQUEST["wedding"] == "yes") $Nvorbehalt++;
553 if($_REQUEST["poverty"] == "yes") $Nvorbehalt++;
554 if($_REQUEST["nines"] == "yes") $Nvorbehalt++;
558 echo "<p class=\"message\"> You selected more than one vorbehalt, please go back ".
559 "and answer the <a href=\"$INDEX?me=$me&in=yes\">question</a> again.</p>";
560 DB_set_hand_status_by_hash($me,'init');
564 echo "<p class=\"message\">Processing what you selected in the last step...";
566 /* check if this sickness needs to be handled first */
567 $gametype = DB_get_gametype_by_gameid($gameid);
568 $startplayer = DB_get_startplayer_by_gameid($gameid);
570 if( $_REQUEST["solo"]!="No")
572 /* user wants to play a solo */
574 /* store the info in the user's hand info */
575 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
576 DB_set_sickness_by_hash($me,"solo");
578 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
580 if($gametype == "solo" && $startplayer<$mypos)
581 {}/* do nothing, since someone else already is playing solo */
584 /* this solo comes first
585 * store info in game table
587 DB_set_gametype_by_gameid($gameid,"solo");
588 DB_set_startplayer_by_gameid($gameid,$mypos);
589 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
592 else if($_REQUEST["wedding"] == "yes")
594 /* TODO: add silent solo somewhere*/
595 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
596 DB_set_sickness_by_hash($me,"wedding");
598 else if($_REQUEST["poverty"] == "yes")
600 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
601 DB_set_sickness_by_hash($me,"poverty");
603 else if($_REQUEST["nines"] == "yes")
605 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
606 " is playing solo, this game will be canceled.<br />\n";
607 DB_set_sickness_by_hash($me,"nines");
610 echo " Ok, done with checking, please go to the <a href=\"$INDEX?me=$me\">next step of the setup</a>.</p>";
612 /* move on to the next stage*/
613 DB_set_hand_status_by_hash($me,'poverty');
615 /* check if everyone has reached this stage, send out email */
616 $userids = DB_get_all_userid_by_gameid($gameid);
618 foreach($userids as $user)
620 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
621 if($userstat!='poverty' && $userstat!='play')
624 DB_set_player_by_gameid($gameid,$user);
629 /* reset player = everyone has to do something now */
630 DB_set_player_by_gameid($gameid,NULL);
632 foreach($userids as $user)
634 $To = DB_get_email_by_userid($user);
635 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
638 $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
639 "please visit this link now to continue: \n".
640 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
641 mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
650 /* here we need to check if there is a solo or some other form of sickness.
651 * If so, which one is the most important one
652 * set that one in the Game table
653 * tell people about it.
655 echo "<div class=\"message\">\n";
656 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
658 /* check if everyone has reached this stage */
659 $userids = DB_get_all_userid_by_gameid($gameid);
661 foreach($userids as $user)
663 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
664 if($userstat!='poverty' && $userstat!='play')
670 echo "This step can only be handled after everyone finished the last step. ".
671 "Seems like this is not the case, so you need to wait a bit... ".
672 "you will get an email once that is the case, please use the link in ".
673 "that email to continue the game.<br />";
677 echo "Everyone has finished checking their cards, let's see what they said...<br />";
679 /* check what kind of game we are playing, in case there are any solos this already
680 *will have the correct information in it */
681 $gametype = DB_get_gametype_by_gameid($gameid);
682 $startplayer = DB_get_startplayer_by_gameid($gameid);
684 /* check for different sickness and just output a general info */
689 foreach($userids as $user)
691 $name = DB_get_name_by_userid($user);
692 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
693 if($usersick == 'nines')
696 echo "$name has a Vorbehalt. <br />";
699 else if($usersick == 'poverty')
702 echo "$name has a Vorbehalt. <br />";
704 else if($usersick == 'wedding')
707 echo "$name has a Vorbehalt. <br />" ;
709 else if($usersick == 'solo')
712 echo "$name has a Vorbehalt. <br />" ;
716 /* now check which sickness comes first and set the gametype to it */
718 if($gametype == "solo")
725 /* TODO: should we keep statistics of this? */
726 $message = "Hello, \n\n".
727 " the game has been canceled because ".DB_get_name_by_userid($nines).
728 " has five or more nines and nobody is playing solo.\n\n".
729 " To redeal either start a new game or, in case the game was part of a tournament, \n".
730 " go to the last game and use the link at the bottom of the page to redeal.";
732 $userids = DB_get_all_userid_by_gameid($gameid);
733 foreach($userids as $user)
735 $To = DB_get_email_by_userid($user);
736 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
739 /* delete everything from the dB */
742 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
743 " has five or more nines and nobody is playing solo.\n";
748 else if($poverty==1) /* one person has poverty */
750 DB_set_gametype_by_gameid($gameid,"poverty");
751 $gametype = "poverty";
752 $who = DB_get_sickness_by_gameid($gameid);
755 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
756 if($firstsick == "poverty")
757 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
759 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
762 else if($poverty==2) /* two people have poverty */
764 DB_set_gametype_by_gameid($gameid,"dpoverty");
765 $gametype = "dpoverty";
766 $who = DB_get_sickness_by_gameid($gameid);
769 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
770 if($firstsick == "poverty")
772 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
773 if($secondsick == "poverty")
774 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
776 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
779 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
784 DB_set_gametype_by_gameid($gameid,"wedding");
785 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
786 $gametype = "wedding";
791 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
792 * output what kind of game we have */
795 foreach($userids as $user)
797 /* userids are sorted by position...
798 * so output whatever the first one has, then whatever the next one has
799 * stop when the sickness is the same as the gametype
802 $name = DB_get_name_by_userid($user);
803 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
806 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
808 if($usersick=="poverty")
810 if($usersick == "wedding" && $gametype =="wedding")
812 if($usersick == "poverty" && $gametype =="poverty")
814 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
816 if($usersick == "solo" && $gametype =="solo")
820 /* output Schweinchen in case the rules need it */
821 if( $gametype != "solo")
822 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
823 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
827 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
831 /* are we the solo player? set us to re, else set us to contra */
832 $pos = DB_get_pos_by_hash($me);
833 if($pos == $startplayer)
834 DB_set_party_by_hash($me,"re");
836 DB_set_party_by_hash($me,"contra");
837 DB_set_hand_status_by_hash($me,'play');
841 /* set person with the wedding to re, do the rest during the game */
842 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
843 if($usersick == "wedding")
844 DB_set_party_by_hash($me,"re");
846 DB_set_party_by_hash($me,"contra");
848 echo "Whoever will make the first trick will be on the re team. <br />\n";
849 echo " Ok, the game can start now, please finish <a href=\"$INDEX?me=$me\">the setup</a>.<br />";
850 DB_set_hand_status_by_hash($me,'play');
854 $hand = DB_get_all_hand($me);
856 if(in_array('3',$hand)||in_array('4',$hand))
857 DB_set_party_by_hash($me,"re");
859 DB_set_party_by_hash($me,"contra");
860 DB_set_hand_status_by_hash($me,'play');
864 /* check if poverty resolved (e.g. DB.Game who set to NULL)
865 * yes? =>trump was taken, start game; break;
867 $who = DB_get_sickness_by_gameid($gameid);
869 { /* trump has been taken */
870 DB_set_hand_status_by_hash($me,'play');
874 if($who>9) /*= two people still have trump on the table*/
879 /* check if we are being asked now
880 * no? display wait message, e.g. player X is asked at the moment
882 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
883 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
885 /* user doesn't want to take trump */
886 /* set next player who needs to be asked */
887 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
888 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
890 if($firstsick=="poverty")
892 if($secondsick=="poverty")
893 DB_set_sickness_by_gameid($gameid,$who+$add*3);
895 DB_set_sickness_by_gameid($gameid,$who+$add*2);
898 DB_set_sickness_by_gameid($gameid,$who+$add);
900 /* email next player */
901 $who = DB_get_sickness_by_gameid($gameid);
902 if($who>9) $who = $who/10;
906 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
907 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
908 $userid = DB_get_userid_by_email($To);
909 DB_set_player_by_gameid($gameid,$userid);
911 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
912 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
913 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
916 /* this user is done */
917 DB_set_hand_status_by_hash($me,'play');
920 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
922 /* user wants to take trump */
923 $trump = $_REQUEST["trump"];
925 /* get hand id for user $trump */
926 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
927 /* copy trump from player A to B */
928 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
930 /* add hidden button with trump in it to get to the next point */
931 echo "</div><div class=\"poverty\">\n";
932 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
933 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
934 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
935 echo "</div><div>\n";
937 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
939 $trump = $_REQUEST["trump"];
940 $exchange = $_REQUEST["exchange"];
941 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
943 /* if exchange is set to a value>0, exchange that card back to user $trump */
946 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
947 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
950 /* if number of cards == 12, set status to play for both users */
951 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
952 $r = mysql_fetch_array($result,MYSQL_NUM);
955 myerror("error in poverty");
960 if($gametype=="poverty" || $who<9)
962 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
964 else /* reduce poverty count by one, that is go to single digits $who */
969 /* whom to ask next */
970 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
971 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
973 if($firstsick!="poverty")
974 DB_set_sickness_by_gameid($gameid,$who+$add);
977 if($secondsick!="poverty")
978 DB_set_sickness_by_gameid($gameid,$who+$add*2);
980 DB_set_sickness_by_gameid($gameid,$who+$add*3);
983 /* email next player */
984 $who = DB_get_sickness_by_gameid($gameid);
987 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
988 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
989 $userid = DB_get_userid_by_email($To);
990 DB_set_player_by_gameid($gameid,$userid);
992 $message = "Someone has poverty, it's your turn to decide, ".
993 "if you want to take the trump. Please visit:".
994 " ".$HOST.$INDEX."?me=".$userhash."\n\n" ;
995 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
999 /* this user is done */
1000 DB_set_hand_status_by_hash($me,'play');
1001 /* and so is his partner */
1002 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
1003 DB_set_hand_status_by_hash($hash,'play');
1005 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
1007 foreach($userids as $user)
1009 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1010 $party = DB_get_party_by_hash($userhash);
1016 DB_set_party_by_hash($me,"contra");
1017 DB_set_party_by_hash($hash,"contra");
1021 foreach($userids as $user)
1023 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1024 if($userhash==$hash||$userhash==$me)
1025 DB_set_party_by_hash($userhash,"re");
1027 DB_set_party_by_hash($userhash,"contra");
1036 /* else show all trump, have lowest card pre-selected, have hidden setting for */
1037 echo "</div><div class=\"poverty\"> you need to get rid of a few cards</div>\n";
1039 set_gametype($gametype); /* this sets the $CARDS variable */
1040 $mycards = DB_get_hand($me);
1041 $mycards = mysort($mycards,$gametype);
1044 echo "<div class=\"mycards\">Your cards are: <br />\n";
1045 foreach($mycards as $card)
1046 display_link_card($card,$PREF["cardset"],$type);
1047 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
1048 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
1049 echo "</div><div>\n";
1052 else if($who == $mypos || $who == $mypos*10)
1054 echo "</div><div class=\"poverty\">\n";
1055 foreach($userids as $user)
1057 $name = DB_get_name_by_userid($user);
1058 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1060 if($usersick=="poverty")
1062 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1063 $cards = DB_get_hand($hash);
1064 $nrtrump = count_trump($cards);
1067 echo "Player $name has $nrtrump trump. Do you want to take them?".
1068 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />\n";
1071 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
1072 echo "</div><div>\n";
1074 echo "Your cards are: <br />\n";
1075 $mycards = DB_get_hand($me);
1077 echo "<p class=\"mycards\">Your cards are: <br />\n";
1078 foreach($mycards as $card)
1079 display_card($card,$PREF["cardset"]);
1084 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
1085 if($mysick=="poverty")
1086 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
1088 echo "it's not your turn yet to decide if you want to take the trump or not.";
1091 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
1092 $who = DB_get_sickness_by_gameid($gameid);
1093 if($who==5 || $who==50)
1095 $message = "Hello, \n\n".
1096 "Game ".DB_format_gameid($gameid)." has been canceled since nobody wanted to take the trump.\n";
1098 $userids = DB_get_all_userid_by_gameid($gameid);
1099 foreach($userids as $user)
1101 $To = DB_get_email_by_userid($user);
1102 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled (poverty not resolved)",$message);
1105 /* delete everything from the dB */
1106 DB_cancel_game($me);
1108 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been canceled.<br /><br /></p>";
1114 /* check if all players are ready to play */
1116 foreach($userids as $user)
1117 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
1120 DB_set_player_by_gameid($gameid,$user);
1125 /* only set this after all poverty, etc. are handled*/
1126 DB_set_game_status_by_gameid($gameid,'play');
1128 /* email startplayer */
1129 $startplayer = DB_get_startplayer_by_gameid($gameid);
1130 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
1131 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1132 $who = DB_get_userid_by_email($email);
1133 DB_set_player_by_gameid($gameid,$who);
1135 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
1137 /* email startplayer) */
1138 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1139 "Use this link to play a card: ".$HOST.$INDEX."?me=".$hash."\n\n" ;
1140 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1143 echo " Please, <a href=\"$INDEX?me=$me\">start</a> the game.<br />";
1152 /* both entries here, so that the tricks are visible for both.
1153 * in case of 'play' there is a break later that skips the last part
1156 /* figure out what kind of game we are playing,
1157 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
1158 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
1162 $gametype = DB_get_gametype_by_gameid($gameid);
1164 if($gametype=="solo")
1166 $gametype = DB_get_solo_by_gameid($gameid);
1167 $GT = $gametype." ".$GT;
1170 $gametype = "normal";
1172 set_gametype($gametype); /* this sets the $CARDS variable */
1174 /* get some infos about the game */
1175 $gamestatus = DB_get_game_status_by_gameid($gameid);
1177 /* has the game started? No, then just wait here...*/
1178 if($gamestatus == 'pre')
1180 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1181 "You need to wait for the others. Just wait for an email. </p>";
1182 break; /* not sure this works... the idea is that you can
1183 * only play a card after everyone is ready to play */
1186 /* get time from the last action of the game */
1187 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1188 $r = mysql_fetch_array($result,MYSQL_NUM);
1189 $gameend = time() - strtotime($r[0]);
1191 /* handel comments in case player didn't play a card, allow comments a week after the end of the game */
1192 if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1193 if(myisset("comment"))
1195 $comment = $_REQUEST["comment"];
1196 $playid = DB_get_current_playid($gameid);
1199 DB_insert_comment($comment,$playid,$myid);
1202 /* handle notes in case player didn't play a card, allow notes only during a game */
1203 if( (!myisset("card") && $mystatus=='play') )
1206 $note = $_REQUEST["note"];
1209 DB_insert_note($note,$gameid,$myid);
1212 /* get everything relevant to display the tricks */
1213 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1214 " Hand.position as position,".
1215 " Play.sequence as sequence, ".
1217 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1218 " SEPARATOR '\n' ), ".
1219 " Play.create_date, ".
1222 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1223 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1224 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1225 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1226 "LEFT JOIN User On User.id=Comment.user_id ".
1227 "WHERE Trick.game_id='".$gameid."' ".
1228 "GROUP BY Trick.id, sequence ".
1229 "ORDER BY Trick.id, sequence ASC");
1231 $lasttrick = DB_get_max_trickid($gameid);
1233 $play = array(); /* needed to calculate winner later */
1235 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1236 $firstcard = ""; /* first card in a trick */
1238 echo "\n<ul class=\"tricks\">\n";
1239 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1241 /* output vorbehalte */
1242 $mygametype = DB_get_gametype_by_gameid($gameid);
1243 if($mygametype != "normal") /* only show when needed */
1245 echo " <li onclick=\"hl('0');\" class=\"current\"><a href=\"#\">Pre</a>\n".
1246 " <div class=\"trick\" id=\"trick0\">\n";
1248 for($mypos=1;$mypos<5;$mypos++)
1250 $usersick = DB_get_sickness_by_pos_and_gameid($mypos,$gameid);
1253 echo " <div class=\"vorbehalt".($mypos-1)."\"> Vorbehalt <br />";
1255 echo " $usersick <br />";
1258 if($mygametype == $usersick)
1262 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1266 while($r = mysql_fetch_array($result,MYSQL_NUM))
1274 /* check if first schweinchen has been played */
1275 if( $GAME["schweinchen"] && ($r[0] == 19 || $r[0] == 20) )
1276 $GAME["schweinchen"]++;
1278 /* save card to be able to find the winner of the trick later */
1279 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1283 /* first card in a trick, output some html */
1284 if($trick!=$lasttrick)
1286 /* start of an old trick? */
1287 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1288 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1289 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1291 else if($trick==$lasttrick)
1293 /* start of a last trick? */
1294 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1295 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1296 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1299 /* remember first card, so that we are able to check, what cards can be played */
1304 echo " <div class=\"card".($pos-1)."\">\n";
1306 /* display comments */
1308 echo " <span class=\"comment\">".$comment."</span>\n";
1311 display_card($r[0],$PREF["cardset"]);
1313 echo " </div>\n"; /* end div card */
1319 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1323 /* whos turn is it? */
1326 $winner = get_winner($play,$gametype); /* returns the position */
1328 $firstcard = ""; /* new trick, no first card */
1333 if($next==5) $next = 1;
1336 /* my turn?, display cards as links, ask for comments*/
1337 if(DB_get_pos_by_hash($me) == $next)
1342 /* do we want to play a card? */
1343 if(myisset("card") && $myturn)
1345 $card = $_REQUEST["card"];
1346 $handid = DB_get_handid_by_hash($me);
1348 /* check if we have card and that we haven't played it yet*/
1349 /* set played in hand_card to true where hand_id and card_id*/
1350 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1351 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1352 $r = mysql_fetch_array($result,MYSQL_NUM);
1353 $handcardid = $r[0];
1355 if($handcardid) /* everything ok, play card */
1357 /* update Game timestamp */
1358 DB_update_game_timestamp($gameid);
1360 /* check if a call was made, must do this before we set the card status to played */
1361 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1362 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1363 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1364 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1365 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1366 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1367 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1368 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1369 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1370 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1372 /* mark card as played */
1373 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1374 DB_quote_smart($card));
1376 /* get trick id or start new trick */
1377 $a = DB_get_current_trickid($gameid);
1382 $playid = DB_play_card($trickid,$handcardid,$sequence);
1384 /* check for schweinchen */
1385 if($GAME["schweinchen"] && ($card == 19 || $card == 20) )
1387 $GAME["schweinchen"]++; // count how many have been played including this one
1388 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1389 DB_insert_comment("Schweinchen! ",$playid,$myid);
1390 if($RULES["schweinchen"]=="both" )
1391 DB_insert_comment("Schweinchen! ",$playid,$myid);
1393 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1396 /* if sequence == 4 check who one in case of wedding */
1397 if($sequence == 4 && $GT == "wedding")
1399 /* is wedding resolve */
1400 $resolved = DB_get_sickness_by_gameid($gameid);
1403 /* who has wedding */
1404 $userids = DB_get_all_userid_by_gameid($gameid);
1405 foreach($userids as $user)
1407 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1408 if($usersick == "wedding")
1411 /* who won the trick */
1412 $play = DB_get_cards_by_trick($trickid);
1413 $winner = get_winner($play,$gametype); /* returns the position */
1414 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1415 /* is tricknr <=3 */
1416 if($tricknr <=3 && $winnerid!=$whosick)
1418 /* set resolved at tricknr*/
1419 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1421 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1422 DB_set_party_by_hash($whash,"re");
1424 if($tricknr == 3 && $winnerid==$whosick)
1426 /* set resolved at tricknr*/
1427 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1432 /* if sequence == 4, set winner of the trick, count points and set the next player */
1435 $play = DB_get_cards_by_trick($trickid);
1436 $winner = get_winner($play,$gametype); /* returns the position */
1438 /* check if someone caught a fox */
1439 if(DB_get_gametype_by_gameid($gameid)!="solo")
1440 foreach($play as $played)
1442 if ( $played['card']==19 || $played['card']==20 )
1443 if ($played['pos']!= $winner )
1445 /* possible caught a fox, check party */
1446 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1447 $uid2 = DB_get_userid_by_gameid_and_position($gameid,$played['pos']);
1449 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1450 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1452 if($party1 != $party2)
1453 mysql_query("INSERT INTO Score".
1454 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1457 /* check for karlchen (jack of clubs in the last trick)*/
1458 if(DB_get_gametype_by_gameid($gameid)!="solo" && $tricknr == 12)
1459 foreach($play as $played)
1460 if ( $played['card']==11 || $played['card']==12 )
1461 if ($played['pos'] == $winner )
1463 /* possible caught a fox, check party */
1464 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1465 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1467 mysql_query("INSERT INTO Score".
1468 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1470 /* check for doppelopf (>40 points)*/
1472 foreach($play as $played)
1474 $points += DB_get_card_value_by_cardid($played['card']);
1478 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1479 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1481 mysql_query("INSERT INTO Score".
1482 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1486 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1488 echo "ERROR during scoring";
1491 echo "DEBUG: position $winner won the trick <br />";
1493 /* who is the next player? */
1498 $next = DB_get_pos_by_hash($me)+1;
1500 if($next==5) $next=1;
1502 /* check for coment */
1503 if(myisset("comment"))
1505 $comment = $_REQUEST["comment"];
1507 DB_insert_comment($comment,$playid,$myid);
1510 /* check for note */
1513 $note = $_REQUEST["note"];
1515 DB_insert_note($note,$gameid,$myid);
1518 /* display played card */
1519 $pos = DB_get_pos_by_hash($me);
1522 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1523 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1524 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1527 echo " <div class=\"card".($pos-1)."\">\n ";
1529 /* display comments */
1530 display_card($card,$PREF["cardset"]);
1532 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1535 /*check if we still have cards left, else set status to gameover */
1536 if(sizeof(DB_get_hand($me))==0)
1538 DB_set_hand_status_by_hash($me,'gameover');
1539 $mystatus = 'gameover';
1542 /* if all players are done, set game status to game over,
1543 * get the points of the last trick and send out an email
1546 $userids = DB_get_all_userid_by_gameid($gameid);
1549 foreach($userids as $user)
1550 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1554 DB_set_game_status_by_gameid($gameid,"gameover");
1556 /* email next player, if game is still running */
1557 if(DB_get_game_status_by_gameid($gameid)=='play')
1559 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1560 $email = DB_get_email_by_hash($next_hash);
1561 $who = DB_get_userid_by_email($email);
1562 DB_set_player_by_gameid($gameid,$who);
1564 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1565 "It's your turn now.\n".
1566 "Use this link to play a card: ".$HOST.$INDEX."?me=".$next_hash."\n\n" ;
1567 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1568 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1570 else /* send out final email */
1572 /* individual score */
1573 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1574 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1575 " LEFT JOIN User ON User.id=Hand.user_id".
1576 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1577 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1578 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1579 " WHERE Hand.game_id='$gameid'".
1580 " GROUP BY User.fullname" );
1581 $message = "The game is over. Thanks for playing :)\n";
1582 $message .= "Final score:\n";
1583 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1584 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1586 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1587 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1588 " LEFT JOIN User ON User.id=Hand.user_id".
1589 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1590 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1591 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1592 " WHERE Hand.game_id='$gameid'".
1593 " GROUP BY Hand.party" );
1594 $message .= "\nTotals:\n";
1597 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1599 $message .= " ".$r[0]." ".$r[1]."\n";
1602 else if($r[0] == "contra")
1607 * save score in database
1611 /* get calls from re/contra */
1613 $call_contra = NULL;
1614 foreach($userids as $user)
1616 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1617 $call = DB_get_call_by_hash($hash);
1618 $party = DB_get_party_by_hash($hash);
1622 $call = (int) $call;
1628 else if( $call < $call_re)
1631 else if($party=="contra")
1633 if($call_contra==NULL)
1634 $call_contra = $call;
1635 else if( $call < $call_re)
1636 $call_contra = $call;
1641 /* figure out who one */
1642 $winning_party = NULL;
1644 if($call_re == NULL && $call_contra==NULL)
1646 $winning_party="re";
1648 $winning_party="contra";
1653 $offset = 120 - $call_re;
1655 $offset--; /* since we use a > in the next equation */
1657 if($re > 120+$offset)
1658 $winning_party="re";
1659 else if ( $call_contra == NULL )
1660 $winning_party="contra";
1665 $offset = 120 - $call_contra;
1666 if($call_contra == 0)
1667 $offset--; /* since we use a > in the next equation */
1669 if($contra > 120+$offset)
1670 $winning_party="contra";
1671 else if ( $call_contra == NULL )
1672 $winning_party="re";
1676 /* one point for each call of the other party in case the other party didn't win
1677 * and one point each in case the party made more than points than one of the calls
1679 if($winning_party!="contra" && $call_contra!=NULL)
1681 for( $p=$call_contra;$p<=120; $p+=30 )
1683 mysql_query("INSERT INTO Score".
1684 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1687 for( $p=$call_contra; $p<120; $p+=30)
1690 mysql_query("INSERT INTO Score".
1691 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1694 if($winning_party!="re" and $call_re!=NULL)
1696 for( $p=$call_re;$p<=120; $p+=30 )
1698 mysql_query("INSERT INTO Score".
1699 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1702 for( $p=$call_re; $p<120; $p+=30)
1705 mysql_query("INSERT INTO Score".
1706 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1710 /* point in case contra won */
1711 if($winning_party=="contra")
1713 mysql_query("INSERT INTO Score".
1714 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1717 /* one point each for winning and each 30 points + calls */
1718 if($winning_party=="re")
1720 foreach(array(120,150,180,210,240) as $p)
1723 if($p==240 || $call_contra!=NULL)
1727 mysql_query("INSERT INTO Score".
1728 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1730 /* re called something and won */
1731 foreach(array(0,30,60,90,120) as $p)
1733 if($call_re!=NULL && $call_re<$p+1)
1734 mysql_query("INSERT INTO Score".
1735 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1738 else if( $winning_party=="contra")
1740 foreach(array(120,150,180,210,240) as $p)
1743 if($p==240 || $call_re!=NULL)
1746 if($contra>$p-$offset)
1747 mysql_query("INSERT INTO Score".
1748 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1750 /* re called something and won */
1751 foreach(array(0,30,60,90,120) as $p)
1753 if($call_contra!=NULL && $call_contra<$p+1)
1754 mysql_query("INSERT INTO Score".
1755 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1760 /* add score points to email */
1763 $message .= " Points Re: \n";
1764 $queryresult = mysql_query("SELECT score FROM Score ".
1765 " WHERE game_id=$gameid AND party='re'".
1767 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1769 $message .= " ".$r[0]."\n";
1772 $message .= " Points Contra: \n";
1773 $queryresult = mysql_query("SELECT score FROM Score ".
1774 " WHERE game_id=$gameid AND party='contra'".
1776 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1778 $message .= " ".$r[0]."\n";
1781 $message .= " Total Points (from the Re point of view): $Tpoint\n";
1784 $session = DB_get_session_by_gameid($gameid);
1785 $score = generate_score_table($session);
1786 /* convert html to ascii */
1787 $score = str_replace("<div class=\"scoretable\">\n<table class=\"score\">\n <tr>\n","",$score);
1788 $score = str_replace("</table></div>\n","",$score);
1789 $score = str_replace("\n","",$score);
1790 $score = str_replace(array("<tr>","</tr>","<td>","</td>"),array("","\n","","|"),$score);
1791 $score = explode("\n",$score);
1793 $header = array_slice($score,0,1);
1794 $header = explode("|",$header[0]);
1795 for($i=0;$i<sizeof($header);$i++)
1796 $header[$i]=str_pad($header[$i],6," ",STR_PAD_BOTH);
1797 $header = implode("|",$header);
1798 $header.= "\n------+------+------+------+------+\n";
1799 if(sizeof($score)>5) $header.= " ... \n";
1801 if(sizeof($score)>5) $score = array_slice($score,-5,5);
1802 for($i=0;$i<sizeof($score);$i++)
1804 $line = explode("|",$score[$i]);
1805 for($j=0;$j<sizeof($line);$j++)
1806 $line[$j]=str_pad($line[$j],6," ",STR_PAD_LEFT);
1807 $score[$i] = implode("|",$line);
1810 $score = implode("\n",$score);
1811 $score = $header.$score;
1813 $message .= "Score Table:\n";
1816 /* send out final email */
1819 foreach($userids as $user)
1820 $all[] = DB_get_email_by_userid($user);
1821 $To = implode(",",$all);
1823 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1824 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1826 foreach($userids as $user)
1828 $To = DB_get_email_by_userid($user);
1829 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1831 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".
1832 $HOST.$INDEX."?me=".$hash."\n\n" ;
1833 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1834 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1840 echo "can't find that card?! <br />\n";
1843 else if(myisset("card") && !$myturn )
1845 echo "please wait until it's your turn! <br />\n";
1848 if($seq!=4 && $trickNR>1)
1849 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1851 /* display points in case game is over */
1852 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1854 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1855 " <div class=\"trick\" id=\"trick13\">\n";
1856 /* add pic for re/contra
1857 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1859 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1860 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1861 " LEFT JOIN User ON User.id=Hand.user_id".
1862 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1863 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1864 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1865 " WHERE Hand.game_id='$gameid'".
1866 " GROUP BY User.fullname" );
1867 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1868 echo " <div class=\"card".($r[3]-1)."\">\n".
1869 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1872 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1876 echo "</ul>\n"; /* end ul tricks*/
1878 echo "<div class=\"notes\"> Personal notes: <br />\n";
1879 $notes = DB_get_notes_by_userid_and_gameid($myid,$gameid);
1880 foreach($notes as $note)
1881 echo "$note <hr \>\n";
1882 echo "Insert note:<input name=\"note\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1885 $mycards = DB_get_hand($me);
1886 $mycards = mysort($mycards,$gametype);
1887 echo "<div class=\"mycards\">\n";
1889 if($myturn && !myisset("card") && $mystatus=='play' )
1891 echo "Hello ".$myname.", it's your turn! <br />\n";
1892 echo "Your cards are: <br />\n";
1894 /* do we have to follow suite? */
1896 if(have_suit($mycards,$firstcard))
1899 foreach($mycards as $card)
1901 if($followsuit && !same_type($card,$firstcard))
1902 display_card($card,$PREF["cardset"]);
1904 display_link_card($card,$PREF["cardset"]);
1907 else if($mystatus=='play' )
1909 echo "Your cards are: <br />\n";
1910 foreach($mycards as $card)
1911 display_card($card,$PREF["cardset"]);
1913 else if($mystatus=='gameover')
1915 $oldcards = DB_get_all_hand($me);
1916 $oldcards = mysort($oldcards,$gametype);
1917 echo "Your cards were: <br />\n";
1918 foreach($oldcards as $card)
1919 display_card($card,$PREF["cardset"]);
1921 $userids = DB_get_all_userid_by_gameid($gameid);
1922 foreach($userids as $user)
1924 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1930 $name = DB_get_name_by_userid($user);
1931 $oldcards = DB_get_all_hand($userhash);
1932 $oldcards = mysort($oldcards,$gametype);
1933 echo "$name's cards were: <br />\n";
1934 foreach($oldcards as $card)
1935 display_card($card,$PREF["cardset"]);
1941 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1942 if($mystatus=='play')
1945 /* the following happens only when the gamestatus is 'gameover' */
1946 /* check if game is over, display results */
1947 if(DB_get_game_status_by_gameid($gameid)=='play')
1949 echo "The game is over for you.. other people still need to play though";
1953 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1954 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1955 " LEFT JOIN User ON User.id=Hand.user_id".
1956 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1957 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1958 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1959 " WHERE Hand.game_id='$gameid'".
1960 " GROUP BY Hand.party" );
1961 echo "<div class=\"total\"> Totals:<br />\n";
1962 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1963 echo " ".$r[0]." ".$r[1]."<br />\n";
1965 $queryresult = mysql_query("SELECT timediff(mod_date,create_date) ".
1966 " FROM Game WHERE id='$gameid'");
1967 $r = mysql_fetch_array($queryresult,MYSQL_NUM);
1968 echo "<p>This game took ".$r[0]." hours.</p>";
1970 echo "<div class=\"re\">\n Points Re: <br />\n";
1971 $queryresult = mysql_query("SELECT score FROM Score ".
1972 " WHERE game_id=$gameid AND party='re'".
1974 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1975 echo " ".$r[0]."<br />\n";
1978 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1979 $queryresult = mysql_query("SELECT score FROM Score ".
1980 " WHERE game_id=$gameid AND party='contra'".
1982 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1983 echo " ".$r[0]."<br />\n";
1992 myerror("error in testing the status");
1994 /* output left menu */
1995 display_user_menu();
1997 /* output right menu */
1999 /* display rule set for this game */
2000 echo "<div class=\"gameinfo\">\n";
2002 if($gamestatus != 'pre')
2003 echo " Gametype: $GT <br />\n";
2005 echo "Rules: <br />\n";
2006 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
2007 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
2008 echo "call: ".$RULES["call"] ."<br />\n";
2011 if($gamestatus == 'play' )
2012 output_form_calls($me);
2014 /* get time from the last action of the game */
2015 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
2016 $r = mysql_fetch_array($result,MYSQL_NUM);
2017 $gameend = time() - strtotime($r[0]);
2019 if($gamestatus == 'play' || $gameend < 60*60*24*7)
2021 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
2025 echo "<input type=\"submit\" value=\"submit\" />\n";
2028 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
2032 $session = DB_get_session_by_gameid($gameid);
2033 $result = mysql_query("SELECT id,create_date FROM Game".
2034 " WHERE session=$session".
2035 " ORDER BY create_date DESC".
2039 $r = mysql_fetch_array($result,MYSQL_NUM);
2041 if(!$session || $gameid==$r[0])
2043 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
2044 $names = DB_get_all_names_by_gameid($gameid);
2045 $type = DB_get_gametype_by_gameid($gameid);
2048 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
2050 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
2054 $session = DB_get_session_by_gameid($gameid);
2055 $score = generate_score_table($session);
2057 // if(size_of($score)>30)
2067 /* user status page */
2068 else if( myisset("email","password") || isset($_SESSION["name"]) )
2070 /* test id and password, should really be done in one step */
2071 if(!isset($_SESSION["name"]))
2073 $email = $_REQUEST["email"];
2074 $password = $_REQUEST["password"];
2078 $name = $_SESSION["name"];
2079 $email = DB_get_email_by_name($name);
2080 $password = DB_get_passwd_by_name($name);
2083 /* user has forgotten his password */
2084 if(myisset("forgot"))
2086 /* check if player is in the database */
2089 $myid = DB_get_userid_by_email($email);
2095 /* check how many entries in recovery table */
2096 $number = DB_get_number_of_passwords_recovery($myid);
2098 /* if less than N recent ones, add a new one and send out email */
2101 echo "Ok, I send you a new password. <br />";
2103 echo "N.B. You tried this already $number times during the last day and it will only work ".
2104 " 5 times during a day.<br />";
2105 echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
2106 echo "Back to the <a href=\"$INDEX\">main page</a>.";
2108 /* create temporary password, use the fist 8 letters of a md5 hash */
2109 $TIME = (string) time(); /* to avoid collisions */
2110 $hash = md5("Anewpassword".$email.$TIME);
2111 $newpw = substr($hash,1,8);
2113 $message = "Someone (hopefully you) requested a new password. \n".
2114 "You can use this email and the following password: \n".
2116 "to log into the server. The new password is valid for 24h, so make\n".
2117 "sure you reset your password to something new. Your old password will\n".
2118 " also still be valid until you set a new one\n";
2119 mymail($email,$EmailName."recovery ",$message);
2121 /* we save these in the database */
2122 DB_set_recovery_password($myid,md5($newpw));
2126 /* make it so that people (or a robot) can request thousands of passwords within a short time
2127 * and spam a user this way */
2128 echo "Sorry you already tried 5 times during the last 24h.<br />".
2129 "You need to use one of those passwords or wait to get a new one.<br />";
2130 echo "Back to the <a href=\"$INDEX\">main page</a>.";
2134 {/* can't find user id in the database */
2136 /* no email given? */
2138 echo "You need to give me an email address! <br />".
2139 "Please try <a href=\"$INDEX\">again</a>.";
2140 else /* default error message */
2141 echo "Couldn't find a player with this email! <br />".
2142 "Please contact Arun, if you think this is a mistake <br />".
2143 "or else try <a href=\"$INDEX\">again</a>.";
2147 { /* normal user page */
2148 /* verify password and email */
2149 if(strlen($password)!=32)
2150 $password = md5($password);
2153 $myid = DB_get_userid_by_email_and_password($email,$password);
2159 /* user information is ok */
2160 $myname = DB_get_name_by_email($email);
2161 $_SESSION["name"] = $myname;
2166 /* does the user want to change some preferences? */
2167 if(myisset("setpref"))
2169 $setpref=$_REQUEST["setpref"];
2173 case "englishcards":
2174 $result = mysql_query("SELECT * from User_Prefs".
2175 " WHERE user_id='$myid' AND pref_key='cardset'" );
2176 if( mysql_fetch_array($result,MYSQL_NUM))
2177 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
2178 " WHERE user_id='$myid' AND pref_key='cardset'" );
2180 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','cardset',".
2181 DB_quote_smart($setpref).")");
2182 echo "Ok, changed you preferences for the cards.\n";
2185 case "emailnonaddict":
2186 $result = mysql_query("SELECT * from User_Prefs".
2187 " WHERE user_id='$myid' AND pref_key='email'" );
2188 if( mysql_fetch_array($result,MYSQL_NUM))
2189 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
2190 " WHERE user_id='$myid' AND pref_key='email'" );
2192 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','email',".
2193 DB_quote_smart($setpref).")");
2194 echo "Ok, changed you preferences for sending out emails.\n";
2198 /* user wants to change his password or request a temporary one */
2199 else if(myisset("passwd"))
2201 if( $_REQUEST["passwd"]=="ask" )
2203 /* reset password form*/
2204 output_password_recovery($email,$password);
2206 else if($_REQUEST["passwd"]=="set")
2208 /* reset password */
2211 /* check if old password matches */
2212 $oldpasswd = md5($_REQUEST["password0"]);
2213 if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
2215 /* check if new passwords are types the same twice */
2216 if($_REQUEST["password1"] != $_REQUEST["password2"] )
2222 echo "The new passwords don't match. <br />";
2225 echo "The old password is not correct. <br />";
2228 echo "Changed the password.<br />";
2229 mysql_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
2230 "' WHERE id=".DB_quote_smart($myid));
2236 else /* output default user page */
2238 /* display links to settings */
2239 output_user_settings();
2241 DB_update_user_timestamp($myid);
2243 display_user_menu();
2245 /* display all games the user has played */
2246 echo "<div class=\"user\">";
2247 echo "<h4>These are all your games:</h4>\n";
2248 echo "<p>Session: <br />\n";
2249 echo "<span class=\"gamestatuspre\"> p </span> = pre-game phase ";
2250 echo "<span class=\"gamestatusplay\">P </span> = game in progess ";
2251 echo "<span class=\"gamestatusover\">F </span> = game finished <br />";
2255 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player,Game.status from Hand".
2256 " LEFT JOIN Game ON Game.id=Hand.game_id".
2257 " WHERE user_id='$myid'".
2258 " ORDER BY Game.session,Game.create_date" );
2260 echo "<table>\n <tr><td>\n";
2261 while( $r = mysql_fetch_array($result,MYSQL_NUM))
2263 $game = DB_format_gameid($r[1]);
2264 $gamenr = (int) $game;
2265 if($gamenrold < $gamenr)
2268 echo "</td></tr>\n <tr> <td>$gamenr:</td><td> ";
2270 echo "$gamenr:</td><td> ";
2271 $gamenrold = $gamenr;
2275 echo "\n <span class=\"gamestatuspre\"><a href=\"".$INDEX."?me=".$r[0]."\">p </a></span> ";
2278 else if ($r[4]=='gameover')
2279 echo "\n <span class=\"gamestatusover\"><a href=\"".$INDEX."?me=".$r[0]."\">F </a></span> ";
2282 echo "\n <span class=\"gamestatusplay\"><a href=\"".$INDEX."?me=".$r[0]."\">P </a></span> ";
2284 if($r[4] != 'gameover')
2286 echo "</td><td>\n ";
2287 if($r[3]==$myid || !$r[3])
2288 echo "(it's <strong>your</strong> turn)\n";
2291 $name = DB_get_name_by_userid($r[3]);
2293 if(DB_get_reminder($r[3],$gameid)==0)
2294 if(time()-strtotime($r[2]) > 60*60*24*7)
2296 "<a href=\"$INDEX?remind=1&me=".$r[0]."\">Send a reminder.</a>";
2297 echo "(it's $name's turn)\n";
2299 if(time()-strtotime($r[2]) > 60*60*24*30)
2301 "<a href=\"$INDEX?cancel=1&me=".$r[0]."\">Cancel?</a>".
2302 " (clicking here is final and can't be restored)";
2306 echo "</td></tr>\n</table>\n";
2308 /* display last 5 users that have signed up to e-DoKo */
2309 $names = DB_get_names_of_new_logins(5);
2310 echo "<h4>New Players:</h4>\n<p>\n";
2311 echo implode(", ",$names).",...\n";
2314 /* display last 5 users that logged on */
2315 $names = DB_get_names_of_last_logins(5);
2316 echo "<h4>Players last logged in:</h4>\n<p>\n";
2317 echo implode(", ",$names).",...\n";
2325 echo "<div class=\"message\">Sorry email and password don't match. Please <a href=\"$INDEX\">try again</a>. </div>";
2332 /* default login page */
2335 /* this outputs the default home page with some extra statistics on it */
2337 $pre[0]=0;$game[0]=0;$done[0]=0;
2338 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
2340 $pre = mysql_fetch_array($r,MYSQL_NUM);
2341 $game = mysql_fetch_array($r,MYSQL_NUM);
2342 $done = mysql_fetch_array($r,MYSQL_NUM);
2345 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
2347 $avgage= mysql_fetch_array($r,MYSQL_NUM);
2351 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);