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 "<div class=\"message\"><span class=\"bigger\">You are now logged out!</span><br />\n".
33 "(<a href=\"$host\">This will take you back to the home-page</a>)</div>";
35 else if(myisset("new"))
37 if( isset($_SESSION["name"]) )
39 $names = DB_get_all_names();
40 echo "<div class=\"user\">\n";
41 output_form_for_new_game($names);
47 echo "<div class=\"message\">Please <a href=\"$host\">log in</a>.</div>";
50 /*check if everything is ready to set up a new game */
51 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen","call" ))
53 $PlayerA = $_REQUEST["PlayerA"];
54 $PlayerB = $_REQUEST["PlayerB"];
55 $PlayerC = $_REQUEST["PlayerC"];
56 $PlayerD = $_REQUEST["PlayerD"];
58 $dullen = $_REQUEST["dullen"];
59 $schweinchen = $_REQUEST["schweinchen"];
60 $call = $_REQUEST["call"];
62 $EmailA = DB_get_email_by_name($PlayerA);
63 $EmailB = DB_get_email_by_name($PlayerB);
64 $EmailC = DB_get_email_by_name($PlayerC);
65 $EmailD = DB_get_email_by_name($PlayerD);
67 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
69 echo "couldn't find one of the names, please start a new game";
75 $useridA = DB_get_userid_by_name($PlayerA);
76 $useridB = DB_get_userid_by_name($PlayerB);
77 $useridC = DB_get_userid_by_name($PlayerC);
78 $useridD = DB_get_userid_by_name($PlayerD);
80 /* create random numbers */
81 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
82 $randomNRstring = join(":",$randomNR);
86 if(myisset("followup") )
88 $followup= $_REQUEST["followup"];
89 $session = DB_get_session_by_gameid($followup);
90 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
91 this way no manipulation is possible */
93 /* check if there is a game in pre or play mode, in that case do nothing */
94 if( DB_is_session_active($session) > 0 )
96 echo "<p class=\"message\"> There is already a game going on in session $session, you can't start a new one</p>";
101 else if ( DB_is_session_active($session) < 0 )
103 echo "<p class=\"message\"> ERROR: status of session $session couldn't be determined.</p>";
110 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
111 "'$ruleset','$session' ,NULL)");
114 /* get max session */
115 $max = DB_get_max_session();
117 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
118 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre',".
119 "'$ruleset','$max' ,NULL)");
124 /* get ruleset information or create new one */
125 $ruleset = DB_get_ruleset($dullen,$schweinchen,$call);
128 myerror("Error defining ruleset: $ruleset");
133 /* get max session */
134 $max = DB_get_max_session();
137 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1',NULL,'pre', ".
138 "'$ruleset','$max' ,NULL)");
140 $game_id = mysql_insert_id();
143 $TIME = (string) time(); /* to avoid collisions */
144 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
145 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
146 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
147 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
150 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
151 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
152 $hand_idA = mysql_insert_id();
153 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
154 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
155 $hand_idB = mysql_insert_id();
156 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
157 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
158 $hand_idC = mysql_insert_id();
159 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
160 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
161 $hand_idD = mysql_insert_id();
165 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
166 for($i=12;$i<24;$i++)
167 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
168 for($i=24;$i<36;$i++)
169 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
170 for($i=36;$i<48;$i++)
171 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
173 /* send out email, TODO: check for error with email */
175 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
176 "Place comments and bug reports here:\n".
177 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
178 "The whole round would consist of the following players:\n".
183 "If you want to join this game, please follow this link:\n\n".
186 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
187 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
188 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
189 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
191 echo "You started a new game. The emails have been sent out!";
192 } /* end set up a new game */
193 /* cancle a game, if nothing has happend in the last N minutes */
194 else if(myisset("cancle","me"))
196 $me = $_REQUEST["me"];
198 /* test for valid ID */
199 $myid = DB_get_userid_by_hash($me);
202 echo "Can't find you in the database, please check the url.<br />\n";
203 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
209 DB_update_user_timestamp($myid);
211 /* get some information from the DB */
212 $gameid = DB_get_gameid_by_hash($me);
213 $myname = DB_get_name_by_hash($me);
215 /* check if game really is old enough */
216 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
217 $r = mysql_fetch_array($result,MYSQL_NUM);
218 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
220 $message = "Hello, \n\n".
221 "Game ".DB_format_gameid($gameid).
222 " has been cancled since nothing happend for a while and $myname requested it.\n";
224 $userids = DB_get_all_userid_by_gameid($gameid);
225 foreach($userids as $user)
227 $To = DB_get_email_by_userid($user);
228 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." cancled (timed out)",$message);
231 /* delete everything from the dB */
234 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
235 " has been cancled.<br /><br /></p>";
238 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
240 /* send out a reminder */
241 else if(myisset("remind","me"))
243 $me = $_REQUEST["me"];
245 /* test for valid ID */
246 $myid = DB_get_userid_by_hash($me);
249 echo "Can't find you in the database, please check the url.<br />\n";
250 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
256 DB_update_user_timestamp($myid);
258 /* get some information from the DB */
259 $gameid = DB_get_gameid_by_hash($me);
260 $myname = DB_get_name_by_hash($me);
262 /* check if game really is old enough */
263 $result = mysql_query("SELECT mod_date,player,status from Game WHERE id='$gameid' " );
264 $r = mysql_fetch_array($result,MYSQL_NUM);
265 if( (time()-strtotime($r[0]) > 60*60*24*7) && ($r[2]!='gameover') ) /* = 1 week */
267 $name = DB_get_name_by_userid($r[1]);
268 $To = DB_get_email_by_userid($r[1]);
269 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$r[1]);
271 $message = "Hello $name, \n\n".
272 "It's your turn in game ".DB_format_gameid($gameid)." \n".
273 "Actually everyone else is waiting for you for more than a week now ;)\n\n".
274 "Please visit this link now to continue: \n".
275 " ".$host."?me=".$userhash."\n\n" ;
277 if(DB_get_reminder($r[1],$gameid)>0)
279 echo "<p>An email has already been sent out.</p>\n";
283 DB_set_reminder($r[1],$gameid);
284 mymail($To,$EmailName."Reminder: game ".DB_format_gameid($gameid)." it's your turn",$message);
286 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid).
287 ": an email has been sent out.<br /><br /></p>";
291 echo "<p>You need to wait longer before you can send out a reminder...</p>\n";
293 /* handle request from one specific player for one game,
294 * (the hash is set on a per game base) */
295 else if(myisset("me"))
297 $me = $_REQUEST["me"];
299 /* test for valid ID */
300 $myid = DB_get_userid_by_hash($me);
303 echo "Can't find you in the database, please check the url.<br />\n";
304 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
310 if(isset($_SESSION["name"]))
311 output_status($_SESSION["name"]);
313 /* the user had done something, update the timestamp */
314 DB_update_user_timestamp($myid);
316 /* get some information from the DB */
317 $gameid = DB_get_gameid_by_hash($me);
318 $myname = DB_get_name_by_hash($me);
319 $mystatus = DB_get_status_by_hash($me);
320 $mypos = DB_get_pos_by_hash($me);
321 $myhand = DB_get_handid_by_hash($me);
322 $session = DB_get_session_by_gameid($gameid);
324 /* get prefs and save them */
328 /* get rule set for this game */
329 $result = mysql_query("SELECT * FROM Rulesets".
330 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
331 " WHERE Game.id='$gameid'" );
332 $r = mysql_fetch_array($result,MYSQL_NUM);
334 $RULES["dullen"] = $r[2];
335 $RULES["schweinchen"] = $r[3];
336 $RULES["call"] = $r[4];
338 /* get some infos about the game */
339 $gametype = DB_get_gametype_by_gameid($gameid);
340 $gamestatus = DB_get_game_status_by_gameid($gameid);
342 if($gametype=="solo")
344 $gametype = DB_get_solo_by_gameid($gameid);
345 $GT = $gametype." ".$GT;
348 /* does anyone have both foxes */
349 $GAME["schweinchen"]=0;
352 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
353 $cards = DB_get_all_hand($hash);
354 if( in_array("19",$cards) && in_array("20",$cards) )
356 $GAME["schweinchen"]=1;
357 $GAME["schweinchen-who"]=$hash;
361 /* put everyting in a form */
362 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
366 /* output extra division in case this game is part of a session */
369 echo "<div class=\"session\">\n".
370 "This game is part of session $session: \n";
371 $hashes = DB_get_hashes_by_session($session,$myid);
373 foreach($hashes as $hash)
378 echo "<a href=\"".$host."?me=".$hash."\">$i</a> \n";
384 /* display the table and the names */
387 /* mystatus gets the player through the different stages of a game.
389 * init: check values from start,
391 * check: check for return values from init
392 * poverty: handle poverty, wait here until all player have reached this state
393 * display sickness and move on to game
394 * play: game in progress
395 * gameover: are we revisiting a game
402 output_check_want_to_play($me);
407 /* move on to the next stage*/
408 DB_set_hand_status_by_hash($me,'init');
411 /* first check if everything went ok in the last step
412 * if not, send user back, if yes, check what he did
416 echo "<p class=\"message\"> You need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
417 DB_set_hand_status_by_hash($me,'start');
421 if($_REQUEST["in"] == "no")
423 /* cancel the game */
424 $message = "Hello, \n\n".
425 "the game has been canceled due to the request of one of the players.\n";
427 $userids = DB_get_all_userid_by_gameid($gameid);
428 foreach($userids as $user)
430 $To = DB_get_email_by_userid($user);
431 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
434 /* delete everything from the dB */
439 $mycards = DB_get_hand($me);
442 output_check_for_sickness($me,$mycards);
444 echo "<p class=\"mycards\">your cards are: <br />\n";
445 foreach($mycards as $card)
446 display_card($card,$PREF["cardset"]);
449 /* move on to the next stage*/
450 DB_set_hand_status_by_hash($me,'check');
456 /* ok, user is in the game, saw his cards and selected his vorbehalt
457 * so first we check what he selected
459 if(!myisset("solo","wedding","poverty","nines") )
461 /* all these variables have a pre-selected default,
462 * so we should never get here,
463 * unless a user tries to cheat ;)
464 * can also happen if user reloads the page!
466 echo "<p class=\"message\"> You need to answer the <a href=\"$host?me=$me&in=yes\">questions</a>.</p>";
467 DB_set_hand_status_by_hash($me,'init');
471 /* check if someone selected more than one vorbehalt */
473 if($_REQUEST["solo"]!="No") $Nvorbehalt++;
474 if($_REQUEST["wedding"] == "yes") $Nvorbehalt++;
475 if($_REQUEST["poverty"] == "yes") $Nvorbehalt++;
476 if($_REQUEST["nines"] == "yes") $Nvorbehalt++;
480 echo "<p class=\"message\"> You selected more than one vorbehalt, please go back ".
481 "and answer the <a href=\"$host?me=$me&in=yes\">question</a> again.</p>";
482 DB_set_hand_status_by_hash($me,'init');
486 echo "<p class=\"message\">Processing what you selected in the last step...";
488 /* check if this sickness needs to be handled first */
489 $gametype = DB_get_gametype_by_gameid($gameid);
490 $startplayer = DB_get_startplayer_by_gameid($gameid);
492 if( $_REQUEST["solo"]!="No")
494 /* user wants to play a solo */
496 /* store the info in the user's hand info */
497 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
498 DB_set_sickness_by_hash($me,"solo");
500 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
502 if($gametype == "solo" && $startplayer<$mypos)
503 {}/* do nothing, since someone else already is playing solo */
506 /* this solo comes first
507 * store info in game table
509 DB_set_gametype_by_gameid($gameid,"solo");
510 DB_set_startplayer_by_gameid($gameid,$mypos);
511 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
514 else if($_REQUEST["wedding"] == "yes")
516 /* TODO: add silent solo somewhere*/
517 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
518 DB_set_sickness_by_hash($me,"wedding");
520 else if($_REQUEST["poverty"] == "yes")
522 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
523 DB_set_sickness_by_hash($me,"poverty");
525 else if($_REQUEST["nines"] == "yes")
527 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
528 " is playing solo, this game will be canceled.<br />\n";
529 DB_set_sickness_by_hash($me,"nines");
532 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.</p>";
534 /* move on to the next stage*/
535 DB_set_hand_status_by_hash($me,'poverty');
537 /* check if everyone has reached this stage, send out email */
538 $userids = DB_get_all_userid_by_gameid($gameid);
540 foreach($userids as $user)
542 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
543 if($userstat!='poverty' && $userstat!='play')
546 DB_set_player_by_gameid($gameid,$user);
551 /* reset player = everyone has to do something now */
552 DB_set_player_by_gameid($gameid,NULL);
554 foreach($userids as $user)
556 $To = DB_get_email_by_userid($user);
557 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
560 $message = "Everyone finish the questionary in game ".DB_format_gameid($gameid).", ".
561 "please visit this link now to continue: \n".
562 " ".$host."?me=".$userhash."\n\n" ;
563 mymail($To,$EmailName." finished setup in game ".DB_format_gameid($gameid),$message);
572 /* here we need to check if there is a solo or some other form of sickness.
573 * If so, which one is the most important one
574 * set that one in the Game table
575 * tell people about it.
577 echo "<div class=\"message\">\n";
578 echo "<p> Checking if someone else selected solo, nines, wedding or poverty.</p>";
580 /* check if everyone has reached this stage */
581 $userids = DB_get_all_userid_by_gameid($gameid);
583 foreach($userids as $user)
585 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
586 if($userstat!='poverty' && $userstat!='play')
592 echo "This step can only be handled after everyone finished the last step. ".
593 "Seems like this is not the case, so you need to wait a bit... ".
594 "you will get an email once that is the case, please use the link in ".
595 "that email to continue the game.<br />";
599 echo "Everyone has finished checking their cards, let's see what they said...<br />";
601 /* check what kind of game we are playing, in case there are any solos this already
602 *will have the correct information in it */
603 $gametype = DB_get_gametype_by_gameid($gameid);
604 $startplayer = DB_get_startplayer_by_gameid($gameid);
606 /* check for different sickness and just output a general info */
611 foreach($userids as $user)
613 $name = DB_get_name_by_userid($user);
614 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
615 if($usersick == 'nines')
618 echo "$name has a Vorbehalt. <br />";
621 else if($usersick == 'poverty')
624 echo "$name has a Vorbehalt. <br />";
626 else if($usersick == 'wedding')
629 echo "$name has a Vorbehalt. <br />" ;
631 else if($usersick == 'solo')
634 echo "$name has a Vorbehalt. <br />" ;
638 /* now check which sickness comes first and set the gametype to it */
640 if($gametype == "solo")
647 /* TODO: should we keep statistics of this? */
648 $message = "Hello, \n\n".
649 " the game has been canceled because ".DB_get_name_by_userid($nines).
650 " has five or more nines and nobody is playing solo.\n\n".
651 " To redeal either start a new game or, in case the game was part of a tournament, \n".
652 " go to the last game and use the link at the bottom of the page to redeal.";
654 $userids = DB_get_all_userid_by_gameid($gameid);
655 foreach($userids as $user)
657 $To = DB_get_email_by_userid($user);
658 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." canceled",$message);
661 /* delete everything from the dB */
664 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
665 " has five or more nines and nobody is playing solo.\n";
670 else if($poverty==1) /* one person has poverty */
672 DB_set_gametype_by_gameid($gameid,"poverty");
673 $gametype = "poverty";
674 $who = DB_get_sickness_by_gameid($gameid);
677 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
678 if($firstsick == "poverty")
679 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
681 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
684 else if($poverty==2) /* two people have poverty */
686 DB_set_gametype_by_gameid($gameid,"dpoverty");
687 $gametype = "dpoverty";
688 $who = DB_get_sickness_by_gameid($gameid);
691 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
692 if($firstsick == "poverty")
694 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
695 if($secondsick == "poverty")
696 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
698 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
701 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
706 DB_set_gametype_by_gameid($gameid,"wedding");
707 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
708 $gametype = "wedding";
713 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
714 * output what kind of game we have */
717 foreach($userids as $user)
719 /* userids are sorted by position...
720 * so output whatever the first one has, then whatever the next one has
721 * stop when the sickness is the same as the gametype
724 $name = DB_get_name_by_userid($user);
725 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
728 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
730 if($usersick=="poverty")
732 if($usersick == "wedding" && $gametype =="wedding")
734 if($usersick == "poverty" && $gametype =="poverty")
736 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
738 if($usersick == "solo" && $gametype =="solo")
742 /* output Schweinchen in case the rules need it */
743 if( $gametype != "solo")
744 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
745 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
749 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
753 /* are we the solo player? set us to re, else set us to contra */
754 $pos = DB_get_pos_by_hash($me);
755 if($pos == $startplayer)
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 /* set person with the wedding to re, do the rest during the game */
764 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
765 if($usersick == "wedding")
766 DB_set_party_by_hash($me,"re");
768 DB_set_party_by_hash($me,"contra");
770 echo "Whoever will make the first trick will be on the re team. <br />\n";
771 echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";
772 DB_set_hand_status_by_hash($me,'play');
776 $hand = DB_get_all_hand($me);
778 if(in_array('3',$hand)||in_array('4',$hand))
779 DB_set_party_by_hash($me,"re");
781 DB_set_party_by_hash($me,"contra");
782 DB_set_hand_status_by_hash($me,'play');
786 /* check if poverty resolved (e.g. DB.Game who set to NULL)
787 * yes? =>trump was taken, start game; break;
789 $who = DB_get_sickness_by_gameid($gameid);
791 { /* trump has been taken */
792 DB_set_hand_status_by_hash($me,'play');
796 if($who>9) /*= two people still have trump on the table*/
801 /* check if we are being asked now
802 * no? display wait message, e.g. player X is asked at the moment
804 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
805 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
807 /* user doesn't want to take trump */
808 /* set next player who needs to be asked */
809 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
810 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
812 if($firstsick=="poverty")
814 if($secondsick=="poverty")
815 DB_set_sickness_by_gameid($gameid,$who+$add*3);
817 DB_set_sickness_by_gameid($gameid,$who+$add*2);
820 DB_set_sickness_by_gameid($gameid,$who+$add);
822 /* email next player */
823 $who = DB_get_sickness_by_gameid($gameid);
824 if($who>9) $who = $who/10;
828 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
829 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
830 DB_set_player_by_gameid($gameid,$who);
832 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
833 " ".$host."?me=".$userhash."\n\n" ;
834 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
837 /* this user is done */
838 DB_set_hand_status_by_hash($me,'play');
841 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
843 /* user wants to take trump */
844 $trump = $_REQUEST["trump"];
846 /* get hand id for user $trump */
847 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
848 /* copy trump from player A to B */
849 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
851 /* add hidden button with trump in it to get to the next point */
852 echo "</div><div class=\"poverty\">\n";
853 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
854 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
855 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
856 echo "</div><div>\n";
858 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
860 $trump = $_REQUEST["trump"];
861 $exchange = $_REQUEST["exchange"];
862 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
864 /* if exchange is set to a value>0, exchange that card back to user $trump */
867 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
868 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
871 /* if number of cards == 12, set status to play for both users */
872 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
873 $r = mysql_fetch_array($result,MYSQL_NUM);
876 myerror("error in poverty");
881 if($gametype=="poverty" || $who<9)
883 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
885 else /* reduce poverty count by one, that is go to single digits $who */
890 /* whom to ask next */
891 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
892 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
894 if($firstsick!="poverty")
895 DB_set_sickness_by_gameid($gameid,$who+$add);
898 if($secondsick!="poverty")
899 DB_set_sickness_by_gameid($gameid,$who+$add*2);
901 DB_set_sickness_by_gameid($gameid,$who+$add*3);
904 /* email next player */
905 $who = DB_get_sickness_by_gameid($gameid);
908 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
909 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
910 DB_set_player_by_gameid($gameid,$who);
912 $message = "Someone has poverty, it's your turn to decide, ".
913 "if you want to take the trump. Please visit:".
914 " ".$host."?me=".$userhash."\n\n" ;
915 mymail($To,$EmailName." poverty (game ".DB_format_gameid($gameid).")",$message);
919 /* this user is done */
920 DB_set_hand_status_by_hash($me,'play');
921 /* and so is his partner */
922 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
923 DB_set_hand_status_by_hash($hash,'play');
925 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
927 foreach($userids as $user)
929 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
930 $party = DB_get_party_by_hash($userhash);
936 DB_set_party_by_hash($me,"contra");
937 DB_set_party_by_hash($hash,"contra");
941 foreach($userids as $user)
943 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
944 if($userhash==$hash||$userhash==$me)
945 DB_set_party_by_hash($userhash,"re");
947 DB_set_party_by_hash($userhash,"contra");
956 /* else show all trump, have lowest card pre-selected, have hidden setting for */
957 echo "</div><div class=\"poverty\"> you need to get rid of a few cards</div>\n";
959 set_gametype($gametype); /* this sets the $CARDS variable */
960 $mycards = DB_get_hand($me);
961 $mycards = mysort($mycards,$gametype);
964 echo "<div class=\"mycards\">Your cards are: <br />\n";
965 foreach($mycards as $card)
966 display_link_card($card,$PREF["cardset"],$type);
967 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
968 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
969 echo "</div><div>\n";
972 else if($who == $mypos || $who == $mypos*10)
974 echo "</div><div class=\"poverty\">\n";
975 foreach($userids as $user)
977 $name = DB_get_name_by_userid($user);
978 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
980 if($usersick=="poverty")
982 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
983 $cards = DB_get_hand($hash);
984 $nrtrump = count_trump($cards);
987 echo "Player $name has $nrtrump trump. Do you want to take them?".
988 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />\n";
991 echo "<a href=\"index.php?me=$me&trump=no\">No,way I take those trump...</a> <br />\n";
992 echo "</div><div>\n";
994 echo "Your cards are: <br />\n";
995 $mycards = DB_get_hand($me);
997 echo "<p class=\"mycards\">your cards are: <br />\n";
998 foreach($mycards as $card)
999 display_card($card,$PREF["cardset"]);
1004 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
1005 if($mysick=="poverty")
1006 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
1008 echo "it's not your turn yet to decide if you want to take the trump or not.";
1011 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
1012 $who = DB_get_sickness_by_gameid($gameid);
1013 if($who==5 || $who==50)
1015 $message = "Hello, \n\n".
1016 "Game ".DB_format_gameid($gameid)." has been cancled since nobody wanted to take the trump.\n";
1018 $userids = DB_get_all_userid_by_gameid($gameid);
1019 foreach($userids as $user)
1021 $To = DB_get_email_by_userid($user);
1022 mymail($To,$EmailName."game ".DB_format_gameid($gameid)." cancled (poverty not resolved)",$message);
1025 /* delete everything from the dB */
1026 DB_cancel_game($me);
1028 echo "<p style=\"background-color:red\";>Game ".DB_format_gameid($gameid)." has been cancled.<br /><br /></p>";
1034 /* check if all players are ready to play */
1036 foreach($userids as $user)
1037 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
1040 DB_set_player_by_gameid($gameid,$user);
1045 /* only set this after all poverty, etc. are handled*/
1046 DB_set_game_status_by_gameid($gameid,'play');
1048 /* email startplayer */
1049 $startplayer = DB_get_startplayer_by_gameid($gameid);
1050 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
1051 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
1052 $who = DB_get_userid_by_email($email);
1053 DB_set_player_by_gameid($gameid,$who);
1055 if($hash!=$me && DB_get_email_pref_by_hash($hash)!="emailaddict")
1057 /* email startplayer) */
1058 $message = "It's your turn now in game ".DB_format_gameid($gameid).".\n".
1059 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
1060 mymail($email,$EmailName."ready, set, go... (game ".DB_format_gameid($gameid).") ",$message);
1063 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
1072 /* both entries here, so that the tricks are visible for both.
1073 * in case of 'play' there is a break later that skips the last part
1076 /* figure out what kind of game we are playing,
1077 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
1078 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
1082 $gametype = DB_get_gametype_by_gameid($gameid);
1084 if($gametype=="solo")
1086 $gametype = DB_get_solo_by_gameid($gameid);
1087 $GT = $gametype." ".$GT;
1090 $gametype = "normal";
1092 set_gametype($gametype); /* this sets the $CARDS variable */
1094 /* get some infos about the game */
1095 $gamestatus = DB_get_game_status_by_gameid($gameid);
1097 /* has the game started? No, then just wait here...*/
1098 if($gamestatus == 'pre')
1100 echo "<p class=\"message\"> You finished the setup, but not everyone else finished it... ".
1101 "so you need to wait for the others. Just wait for the an email... </p>";
1102 break; /* not sure this works... the idea is that you can
1103 * only play a card after everyone is ready to play */
1106 /* get time from the last action of the game */
1107 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1108 $r = mysql_fetch_array($result,MYSQL_NUM);
1109 $gameend = time() - strtotime($r[0]);
1111 /* handel comments in case player didn't play a card, allow comments a week after the end of the game */ if( (!myisset("card") && $mystatus=='play') || ($mystatus=='gameover' && ($gameend < 60*60*24*7)) )
1112 if(myisset("comment"))
1114 $comment = $_REQUEST["comment"];
1115 $playid = DB_get_current_playid($gameid);
1118 DB_insert_comment($comment,$playid,$myid);
1121 /* get everything relevant to display the tricks */
1122 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1123 " Hand.position as position,".
1124 " Play.sequence as sequence, ".
1126 " GROUP_CONCAT(CONCAT('<span>',User.fullname,': ',Comment.comment,'</span>')".
1127 " SEPARATOR '\n' ), ".
1128 " Play.create_date, ".
1131 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1132 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1133 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1134 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1135 "LEFT JOIN User On User.id=Comment.user_id ".
1136 "WHERE Trick.game_id='".$gameid."' ".
1137 "GROUP BY Trick.id, sequence ".
1138 "ORDER BY Trick.id, sequence ASC");
1140 $lasttrick = DB_get_max_trickid($gameid);
1142 $play = array(); /* needed to calculate winner later */
1144 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1145 $firstcard = ""; /* first card in a trick */
1147 echo "\n<ul class=\"tricks\">\n";
1148 echo " <li class=\"nohighlight\"> Game ".DB_format_gameid($gameid).": </li>\n";
1150 while($r = mysql_fetch_array($result,MYSQL_NUM))
1156 $timeplayed = strtotime($r[5]);
1159 $offset = DB_get_user_timezone($user);
1160 $zone = return_timezone($offset);
1161 date_default_timezone_set($zone);
1163 /* check if first schweinchen has been played */
1164 if( $GAME["schweinchen"] && ($r[0] == 19 || $r[0] == 20) )
1165 $GAME["schweinchen"]++;
1167 /* save card to be able to find the winner of the trick later */
1168 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1172 /* first card in a trick, output some html */
1173 if($trick!=$lasttrick)
1175 /* start of an old trick? */
1176 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1177 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1178 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1180 else if($trick==$lasttrick)
1182 /* start of a last trick? */
1183 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1184 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1185 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1188 /* remember first card, so that we are able to check, what cards can be played */
1193 echo " <div class=\"card".($pos-1)."\">\n";
1195 /* display comments */
1197 echo " <span class=\"comment\">".$comment."</span>\n";
1200 display_card($r[0],$PREF["cardset"]);
1202 echo " </div>\n"; /* end div card */
1208 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1212 /* whos turn is it? */
1215 $winner = get_winner($play,$gametype); /* returns the position */
1217 $firstcard = ""; /* new trick, no first card */
1222 if($next==5) $next = 1;
1225 /* my turn?, display cards as links, ask for comments*/
1226 if(DB_get_pos_by_hash($me) == $next)
1231 /* do we want to play a card? */
1232 if(myisset("card") && $myturn)
1234 $card = $_REQUEST["card"];
1235 $handid = DB_get_handid_by_hash($me);
1237 /* check if we have card and that we haven't played it yet*/
1238 /* set played in hand_card to true where hand_id and card_id*/
1239 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1240 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1241 $r = mysql_fetch_array($result,MYSQL_NUM);
1242 $handcardid = $r[0];
1244 if($handcardid) /* everything ok, play card */
1246 /* update Game timestamp */
1247 DB_update_game_timestamp($gameid);
1249 /* check if a call was made, must do this before we set the card status to played */
1250 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1251 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1252 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1253 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1254 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1255 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1256 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1257 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1258 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1259 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1261 /* mark card as played */
1262 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1263 DB_quote_smart($card));
1265 /* get trick id or start new trick */
1266 $a = DB_get_current_trickid($gameid);
1271 $playid = DB_play_card($trickid,$handcardid,$sequence);
1273 /* check for schweinchen */
1274 if($GAME["schweinchen"] && ($card == 19 || $card == 20) )
1276 $GAME["schweinchen"]++; // count how many have been played including this one
1277 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1278 DB_insert_comment("Schweinchen! ",$playid,$myid);
1279 if($RULES["schweinchen"]=="both" )
1280 DB_insert_comment("Schweinchen! ",$playid,$myid);
1282 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1285 /* if sequence == 4 check who one in case of wedding */
1286 if($sequence == 4 && $GT == "wedding")
1288 /* is wedding resolve */
1289 $resolved = DB_get_sickness_by_gameid($gameid);
1292 /* who has wedding */
1293 $userids = DB_get_all_userid_by_gameid($gameid);
1294 foreach($userids as $user)
1296 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1297 if($usersick == "wedding")
1300 /* who won the trick */
1301 $play = DB_get_cards_by_trick($trickid);
1302 $winner = get_winner($play,$gametype); /* returns the position */
1303 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1304 /* is tricknr <=3 */
1305 if($tricknr <=3 && $winnerid!=$whosick)
1307 /* set resolved at tricknr*/
1308 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1310 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1311 DB_set_party_by_hash($whash,"re");
1313 if($tricknr == 3 && $winnerid==$whosick)
1315 /* set resolved at tricknr*/
1316 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1321 /* if sequence == 4, set winner of the trick, count points and set the next player */
1324 $play = DB_get_cards_by_trick($trickid);
1325 $winner = get_winner($play,$gametype); /* returns the position */
1327 /* check if someone caught a fox */
1328 if(DB_get_gametype_by_gameid($gameid)!="solo")
1329 foreach($play as $played)
1331 if ( $played['card']==19 || $played['card']==20 )
1332 if ($played['pos']!= $winner )
1334 /* possible caught a fox, check party */
1335 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1336 $uid2 = DB_get_userid_by_gameid_and_position($gameid,$played['pos']);
1338 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1339 $party2 = DB_get_party_by_gameid_and_userid($gameid,$uid2);
1341 if($party1 != $party2)
1342 mysql_query("INSERT INTO Score".
1343 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,$uid2,'fox')");
1346 /* check for karlchen (jack of clubs in the last trick)*/
1347 if(DB_get_gametype_by_gameid($gameid)!="solo" && $tricknr == 12)
1348 foreach($play as $played)
1349 if ( $played['card']==11 || $played['card']==12 )
1350 if ($played['pos'] == $winner )
1352 /* possible caught a fox, check party */
1353 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1354 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1356 mysql_query("INSERT INTO Score".
1357 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'karlchen')");
1359 /* check for doppelopf (>40 points)*/
1361 foreach($play as $played)
1363 $points += DB_get_card_value_by_cardid($played['card']);
1367 $uid1 = DB_get_userid_by_gameid_and_position($gameid,$winner);
1368 $party1 = DB_get_party_by_gameid_and_userid($gameid,$uid1);
1370 mysql_query("INSERT INTO Score".
1371 " VALUES( NULL,NULL,$gameid,'$party1',$uid1,NULL,'doko')");
1375 mysql_query("UPDATE Trick SET winner='$winner' WHERE id='$trickid'");
1377 echo "ERROR during scoring";
1380 echo "DEBUG: position $winner won the trick <br />";
1382 /* who is the next player? */
1387 $next = DB_get_pos_by_hash($me)+1;
1389 if($next==5) $next=1;
1391 /* check for coment */
1392 if(myisset("comment"))
1394 $comment = $_REQUEST["comment"];
1396 DB_insert_comment($comment,$playid,$myid);
1399 /* display played card */
1400 $pos = DB_get_pos_by_hash($me);
1403 echo " <li onclick=\"hl('".($tricknr)."');\" class=\"current\"><a href=\"#\">Trick ".($tricknr)."</a>\n".
1404 " <div class=\"trick\" id=\"trick".($tricknr)."\">\n".
1405 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1408 echo " <div class=\"card".($pos-1)."\">\n ";
1410 /* display comments */
1411 display_card($card,$PREF["cardset"]);
1413 echo "\n <span class=\"comment\"> ".$comment."</span>\n";
1416 /*check if we still have cards left, else set status to gameover */
1417 if(sizeof(DB_get_hand($me))==0)
1419 DB_set_hand_status_by_hash($me,'gameover');
1420 $mystatus = 'gameover';
1423 /* if all players are done, set game status to game over,
1424 * get the points of the last trick and send out an email
1427 $userids = DB_get_all_userid_by_gameid($gameid);
1430 foreach($userids as $user)
1431 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1435 DB_set_game_status_by_gameid($gameid,"gameover");
1437 /* email next player, if game is still running */
1438 if(DB_get_game_status_by_gameid($gameid)=='play')
1440 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1441 $email = DB_get_email_by_hash($next_hash);
1442 $who = DB_get_userid_by_email($email);
1443 DB_set_player_by_gameid($gameid,$who);
1445 $message = "A card has been played in game ".DB_format_gameid($gameid).".\n\n".
1446 "It's your turn now.\n".
1447 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1448 if( DB_get_email_pref_by_uid($who)!="emailaddict" )
1449 mymail($email,$EmailName."a card has been played in game ".DB_format_gameid($gameid),$message);
1451 else /* send out final email */
1453 /* individual score */
1454 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party FROM Hand".
1455 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1456 " LEFT JOIN User ON User.id=Hand.user_id".
1457 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1458 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1459 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1460 " WHERE Hand.game_id='$gameid'".
1461 " GROUP BY User.fullname" );
1462 $message = "The game is over. Thanks for playing :)\n";
1463 $message .= "Final score:\n";
1464 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1465 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1467 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1468 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1469 " LEFT JOIN User ON User.id=Hand.user_id".
1470 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1471 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1472 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1473 " WHERE Hand.game_id='$gameid'".
1474 " GROUP BY Hand.party" );
1475 $message .= "\nTotals:\n";
1478 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1480 $message .= " ".$r[0]." ".$r[1]."\n";
1483 else if($r[0] == "contra")
1488 * save score in database
1492 /* get calls from re/contra */
1494 $call_contra = NULL;
1495 foreach($userids as $user)
1497 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1498 $call = DB_get_call_by_hash($hash);
1499 $party = DB_get_party_by_hash($hash);
1503 $call = (int) $call;
1509 else if( $call < $call_re)
1512 else if($party=="contra")
1514 if($call_contra==NULL)
1515 $call_contra = $call;
1516 else if( $call < $call_re)
1517 $call_contra = $call;
1522 /* figure out who one */
1523 $winning_party = NULL;
1525 if($call_re == NULL && $call_contra==NULL)
1527 $winning_party="re";
1529 $winning_party="contra";
1534 $offset = 120 - $call_re;
1536 $offset--; /* since we use a > in the next equation */
1538 if($re > 120+$offset)
1539 $winning_party="re";
1540 else if ( $call_contra == NULL )
1541 $winning_party="contra";
1546 $offset = 120 - $call_contra;
1547 if($call_contra == 0)
1548 $offset--; /* since we use a > in the next equation */
1550 if($contra > 120+$offset)
1551 $winning_party="contra";
1552 else if ( $call_contra == NULL )
1553 $winning_party="re";
1557 /* one point for each call of the other party in case the other party didn't win
1558 * and one point each in case the party made more than points than one of the calls
1560 if($winning_party!="contra" && $call_contra!=NULL)
1562 for( $p=$call_contra;$p<=120; $p+=30 )
1564 mysql_query("INSERT INTO Score".
1565 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'against$p')");
1568 for( $p=$call_contra; $p<120; $p+=30)
1571 mysql_query("INSERT INTO Score".
1572 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'made$p')");
1575 if($winning_party!="re" and $call_re!=NULL)
1577 for( $p=$call_re;$p<=120; $p+=30 )
1579 mysql_query("INSERT INTO Score".
1580 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'against$p')");
1583 for( $p=$call_re; $p<120; $p+=30)
1586 mysql_query("INSERT INTO Score".
1587 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'made$p')");
1591 /* point in case contra won */
1592 if($winning_party=="contra")
1594 mysql_query("INSERT INTO Score".
1595 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'againstqueens')");
1598 /* one point each for winning and each 30 points + calls */
1599 if($winning_party=="re")
1601 foreach(array(120,150,180,210,240) as $p)
1604 if($p==240 || $call_contra!=NULL)
1608 mysql_query("INSERT INTO Score".
1609 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'".(240-$p)."')");
1611 /* re called something and won */
1612 foreach(array(0,30,60,90,120) as $p)
1614 if($call_re!=NULL && $call_re<$p+1)
1615 mysql_query("INSERT INTO Score".
1616 " VALUES( NULL,NULL,$gameid,'re',NULL,NULL,'call$p')");
1619 else if( $winning_party=="contra")
1621 foreach(array(120,150,180,210,240) as $p)
1624 if($p==240 || $call_re!=NULL)
1627 if($contra>$p-$offset)
1628 mysql_query("INSERT INTO Score".
1629 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'".(240-$p)."')");
1631 /* re called something and won */
1632 foreach(array(0,30,60,90,120) as $p)
1634 if($call_contra!=NULL && $call_contra<$p+1)
1635 mysql_query("INSERT INTO Score".
1636 " VALUES( NULL,NULL,$gameid,'contra',NULL,NULL,'call$p')");
1641 /* send out final email */
1644 foreach($userids as $user)
1645 $all[] = DB_get_email_by_userid($user);
1646 $To = implode(",",$all);
1648 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1649 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 1(2)",$message.$help);
1651 foreach($userids as $user)
1653 $To = DB_get_email_by_userid($user);
1654 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1656 $link = "Use this link to have a look at game ".DB_format_gameid($gameid).": ".$host."?me=".$hash."\n\n" ;
1657 if( DB_get_email_pref_by_uid($user) != "emailaddict" )
1658 mymail($To,$EmailName."game over (game ".DB_format_gameid($gameid).") part 2(2)",$link);
1664 echo "can't find that card?! <br />\n";
1667 else if(myisset("card") && !$myturn )
1669 echo "please wait until it's your turn! <br />\n";
1672 if($seq!=4 && $trickNR>1)
1673 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1675 /* display points in case game is over */
1676 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1678 echo " <li onclick=\"hl('13');\" class=\"current\"><a href=\"#\">Score</a>\n".
1679 " <div class=\"trick\" id=\"trick13\">\n";
1680 /* add pic for re/contra
1681 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";*/
1683 $result = mysql_query("SELECT User.fullname, IFNULL(SUM(Card.points),0), Hand.party,Hand.position FROM Hand".
1684 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1685 " LEFT JOIN User ON User.id=Hand.user_id".
1686 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1687 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1688 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1689 " WHERE Hand.game_id='$gameid'".
1690 " GROUP BY User.fullname" );
1691 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1692 echo " <div class=\"card".($r[3]-1)."\">\n".
1693 " <div class=\"score\">".$r[2]."<br /> ".$r[1]."</div>\n".
1696 echo " </div>\n </li>\n"; /* end div trick, end li trick */
1700 echo "</ul>\n"; /* end ul tricks*/
1702 $mycards = DB_get_hand($me);
1703 $mycards = mysort($mycards,$gametype);
1704 echo "<div class=\"mycards\">\n";
1706 if($myturn && !myisset("card") && $mystatus=='play' )
1708 echo "Hello ".$myname.", it's your turn! <br />\n";
1709 echo "Your cards are: <br />\n";
1711 /* do we have to follow suite? */
1713 if(have_suit($mycards,$firstcard))
1716 foreach($mycards as $card)
1718 if($followsuit && !same_type($card,$firstcard))
1719 display_card($card,$PREF["cardset"]);
1721 display_link_card($card,$PREF["cardset"]);
1724 else if($mystatus=='play' )
1726 echo "Your cards are: <br />\n";
1727 foreach($mycards as $card)
1728 display_card($card,$PREF["cardset"]);
1730 else if($mystatus=='gameover')
1732 $oldcards = DB_get_all_hand($me);
1733 $oldcards = mysort($oldcards,$gametype);
1734 echo "Your cards were: <br />\n";
1735 foreach($oldcards as $card)
1736 display_card($card,$PREF["cardset"]);
1738 $userids = DB_get_all_userid_by_gameid($gameid);
1739 foreach($userids as $user)
1741 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1747 $name = DB_get_name_by_userid($user);
1748 $oldcards = DB_get_all_hand($userhash);
1749 $oldcards = mysort($oldcards,$gametype);
1750 echo "$name's cards were: <br />\n";
1751 foreach($oldcards as $card)
1752 display_card($card,$PREF["cardset"]);
1758 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1759 if($mystatus=='play')
1762 /* the following happens only when the gamestatus is 'gameover' */
1763 /* check if game is over, display results */
1764 if(DB_get_game_status_by_gameid($gameid)=='play')
1766 echo "The game is over for you.. other people still need to play though";
1770 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(Card.points),0) FROM Hand".
1771 " LEFT JOIN Trick ON Trick.winner=Hand.position AND Trick.game_id=Hand.game_id".
1772 " LEFT JOIN User ON User.id=Hand.user_id".
1773 " LEFT JOIN Play ON Trick.id=Play.trick_id".
1774 " LEFT JOIN Hand_Card ON Hand_Card.id=Play.hand_card_id".
1775 " LEFT JOIN Card ON Card.id=Hand_Card.card_id".
1776 " WHERE Hand.game_id='$gameid'".
1777 " GROUP BY Hand.party" );
1778 echo "<div class=\"total\"> Totals:<br />\n";
1779 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1780 echo " ".$r[0]." ".$r[1]."<br />\n";
1782 echo "<div class=\"re\">\n Points Re: <br />\n";
1783 $queryresult = mysql_query("SELECT score FROM Score ".
1784 " WHERE game_id=$gameid AND party='re'".
1786 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1787 echo " ".$r[0]."<br />\n";
1790 echo "<div class=\"contra\">\n Points Contra: <br />\n";
1791 $queryresult = mysql_query("SELECT score FROM Score ".
1792 " WHERE game_id=$gameid AND party='contra'".
1794 while($r = mysql_fetch_array($queryresult,MYSQL_NUM) )
1795 echo " ".$r[0]."<br />\n";
1804 myerror("error in testing the status");
1806 /* output left menu */
1807 display_user_menu();
1809 /* output right menu */
1811 /* display rule set for this game */
1812 echo "<div class=\"gameinfo\">\n";
1814 if($gamestatus != 'pre')
1815 echo " Gametype: $GT <br />\n";
1817 echo "Rules: <br />\n";
1818 echo "10ofhearts : ".$RULES["dullen"] ."<br />\n";
1819 echo "schweinchen: ".$RULES["schweinchen"] ."<br />\n";
1820 echo "call: ".$RULES["call"] ."<br />\n";
1823 if($gamestatus == 'play' )
1824 output_form_calls($me);
1826 /* get time from the last action of the game */
1827 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
1828 $r = mysql_fetch_array($result,MYSQL_NUM);
1829 $gameend = time() - strtotime($r[0]);
1831 if($gamestatus == 'play' || $gameend < 60*60*24*7)
1833 echo "<br />\nA short comment:<input name=\"comment\" type=\"text\" size=\"15\" maxlength=\"100\" />\n";
1837 echo "<input type=\"submit\" value=\"submit\" />\n";
1840 if($mystatus=='gameover' && DB_get_game_status_by_gameid($gameid)=='gameover' )
1844 $session = DB_get_session_by_gameid($gameid);
1845 $result = mysql_query("SELECT id,create_date FROM Game".
1846 " WHERE session=$session".
1847 " ORDER BY create_date DESC".
1851 $r = mysql_fetch_array($result,MYSQL_NUM);
1853 if(!$session || $gameid==$r[0])
1855 /* suggest a new game with the same people in it, just rotated once (unless last game was solo) */
1856 $names = DB_get_all_names_by_gameid($gameid);
1857 $type = DB_get_gametype_by_gameid($gameid);
1860 output_ask_for_new_game($names[0],$names[1],$names[2],$names[3],$gameid);
1862 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1866 $session = DB_get_session_by_gameid($gameid);
1867 $score = generate_score_table($session);
1869 // if(size_of($score)>30)
1880 /* user status page */
1881 else if( myisset("email","password") || isset($_SESSION["name"]) )
1883 /* test id and password, should really be done in one step */
1884 if(!isset($_SESSION["name"]))
1886 $email = $_REQUEST["email"];
1887 $password = $_REQUEST["password"];
1891 $name = $_SESSION["name"];
1892 $email = DB_get_email_by_name($name);
1893 $password = DB_get_passwd_by_name($name);
1896 if(myisset("forgot"))
1900 $myid = DB_get_userid_by_email($email);
1906 /* check how many entries in recovery table */
1907 $number = DB_get_number_of_passwords_recovery($myid);
1909 /* if less than N recent ones, add a new one and send out email */
1912 echo "Ok, I send you a new password. <br />";
1914 echo "N.B. You tried this already $number times during the last day and it will only work ".
1915 " 5 times during a day.<br />";
1916 echo "The new password will be valid for one day, make sure you reset it to something else.<br />";
1917 echo "Back to the <a href=\"$host\">main page</a>.";
1919 $TIME = (string) time(); /* to avoid collisions */
1920 $hash = md5("Anewpassword".$email.$TIME);
1921 $newpw = substr($hash,1,8);
1923 $message = "Someone (hopefully you) requested a new password. \n".
1924 "You can use this email and the following password: \n".
1926 "to log into the server. The new password is valid for 24h, so make\n".
1927 "sure you reset your password to something new. Your old password will\n".
1928 " also still be valid until you set a new one\n";
1929 mymail($email,$EmailName."recovery ",$message);
1931 DB_set_recovery_password($myid,md5($newpw));
1935 echo "Sorry you already tried 5 times during the last 24h.<br />".
1936 "You need to use one of those passwords or wait to get a new one.<br />";
1937 echo "Back to the <a href=\"$host\">main page</a>.";
1943 echo "You need to give me an email address! <br />".
1944 "Please try <a href=\"$host\">again</a>.";
1946 echo "Couldn't find a player with this email! <br />".
1947 "Please contact Arun, if you think this is a mistake <br />".
1948 "or else try <a href=\"$host\">again</a>.";
1953 /* verify password and email */
1954 if(strlen($password)!=32)
1955 $password = md5($password);
1958 $myid = DB_get_userid_by_email_and_password($email,$password);
1966 if(myisset("setpref"))
1968 $setpref=$_REQUEST["setpref"];
1972 case "englishcards":
1973 $result = mysql_query("SELECT * from User_Prefs".
1974 " WHERE user_id='$myid' AND pref_key='cardset'" );
1975 if( mysql_fetch_array($result,MYSQL_NUM))
1976 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1977 " WHERE user_id='$myid' AND pref_key='cardset'" );
1979 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','cardset',".
1980 DB_quote_smart($setpref).")");
1981 echo "Ok, changed you preferences for the cards.\n";
1984 case "emailnonaddict":
1985 $result = mysql_query("SELECT * from User_Prefs".
1986 " WHERE user_id='$myid' AND pref_key='email'" );
1987 if( mysql_fetch_array($result,MYSQL_NUM))
1988 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1989 " WHERE user_id='$myid' AND pref_key='email'" );
1991 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$myid','email',".
1992 DB_quote_smart($setpref).")");
1993 echo "Ok, changed you preferences for sending out emails.\n";
1997 else if(myisset("passwd"))
1999 if( $_REQUEST["passwd"]=="ask" )
2001 /* reset password form*/
2002 output_password_recovery($email,$password);
2004 else if($_REQUEST["passwd"]=="set")
2006 /* reset password */
2009 /* check if old password matches */
2010 $oldpasswd = md5($_REQUEST["password0"]);
2011 if(!( ($password == $oldpasswd) || DB_check_recovery_passwords($oldpasswd,$email) ))
2013 /* check if new passwords are types the same twice */
2014 if($_REQUEST["password1"] != $_REQUEST["password2"] )
2020 echo "The new passwords don't match. <br />";
2023 echo "The old password is not correct. <br />";
2026 echo "Changed the password.<br />";
2027 mysql_query("UPDATE User SET password='".md5($_REQUEST["password1"]).
2028 "' WHERE id=".DB_quote_smart($myid));
2034 else /* output default user page */
2036 $time = DB_get_user_timestamp($myid);
2037 $unixtime = strtotime($time);
2039 $offset = DB_get_user_timezone($myid);
2040 $zone = return_timezone($offset);
2041 date_default_timezone_set($zone);
2043 $myname = DB_get_name_by_email($email);
2044 $_SESSION["name"] = $myname;
2046 if(isset($_SESSION["name"]))
2047 output_status($_SESSION["name"]);
2049 /* display links to settings */
2050 output_user_settings();
2052 echo "<div class=\"lastlogin\">last login: ".date("r",$unixtime)."</div>";
2054 DB_update_user_timestamp($myid);
2056 display_user_menu();
2058 echo "<div class=\"user\">";
2059 echo "<h4>These are all your games:</h4>\n";
2060 echo "<p>Session: <br />\n";
2061 echo "<span class=\"gamestatuspre\"> p </span> = pre-game phase ";
2062 echo "<span class=\"gamestatusplay\">P </span> = game in progess ";
2063 echo "<span class=\"gamestatusover\">F </span> = game finished <br />";
2067 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player,Game.status from Hand".
2068 " LEFT JOIN Game ON Game.id=Hand.game_id".
2069 " WHERE user_id='$myid'".
2070 " ORDER BY Game.session,Game.create_date" );
2072 echo "<table>\n <tr><td>\n";
2073 while( $r = mysql_fetch_array($result,MYSQL_NUM))
2075 $game = DB_format_gameid($r[1]);
2076 $gamenr = (int) $game;
2077 if($gamenrold < $gamenr)
2080 echo "</td></tr>\n <tr> <td>$gamenr:</td><td> ";
2082 echo "$gamenr:</td><td> ";
2083 $gamenrold = $gamenr;
2087 echo "\n <span class=\"gamestatuspre\"><a href=\"".$host."?me=".$r[0]."\">p </a></span> ";
2090 else if ($r[4]=='gameover')
2091 echo "\n <span class=\"gamestatusover\"><a href=\"".$host."?me=".$r[0]."\">F </a></span> ";
2094 echo "\n <span class=\"gamestatusplay\"><a href=\"".$host."?me=".$r[0]."\">P </a></span> ";
2096 if($r[4] != 'gameover')
2098 echo "</td><td>\n ";
2102 echo "(it's <strong>your</strong> turn)\n";
2105 $name = DB_get_name_by_userid($r[3]);
2107 if(DB_get_reminder($r[3],$gameid)==0)
2108 if(time()-strtotime($r[2]) > 60*60*24*7)
2110 "<a href=\"$host?remind=1&me=".$r[0]."\">Send a reminder.</a>";
2111 echo "(it's $name's turn)\n";
2114 if(time()-strtotime($r[2]) > 60*60*24*30)
2116 "<a href=\"$host?cancle=1&me=".$r[0]."\">Cancel?</a>".
2117 " (clicking here is final and can't be restored)";
2121 echo "</td></tr>\n</table>\n";
2122 $names = DB_get_all_names();
2123 echo "<h4>Registered players:</h4>\n<p>\n";
2124 echo implode(", ",$names)."\n";
2125 echo "</p>\n</div>";
2130 echo "<div class=\"message\">Sorry email and password don't match. Please <a href=\"$host\">try again</a>. </div>";
2137 /* default login page */
2140 $pre[0]=0;$game[0]=0;$done[0]=0;
2141 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
2143 $pre = mysql_fetch_array($r,MYSQL_NUM);
2144 $game = mysql_fetch_array($r,MYSQL_NUM);
2145 $done = mysql_fetch_array($r,MYSQL_NUM);
2148 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
2150 $avgage= mysql_fetch_array($r,MYSQL_NUM);
2154 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);