2 error_reporting(E_ALL);
7 include_once("config.php");
8 include_once("output.php"); /* html output only */
9 include_once("db.php"); /* database only */
10 include_once("functions.php"); /* the rest */
12 /* check if some variables are set in the config file, else set defaults */
13 if(!isset($EmailName))
17 /* in case work has to be done on the database or other section we can
18 * shut down the server and tell people to come back later
23 echo "Working on the database...please check back in a few mintues";
31 echo "Database error, can't connect...";
36 /* done major error checking, output header of HTML page */
39 /* check if we want to start a new game */
42 $names = DB_get_all_names();
43 output_form_for_new_game($names);
45 /*check if everything is ready to set up a new game */
46 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen" ))
48 $PlayerA = $_REQUEST["PlayerA"];
49 $PlayerB = $_REQUEST["PlayerB"];
50 $PlayerC = $_REQUEST["PlayerC"];
51 $PlayerD = $_REQUEST["PlayerD"];
53 $dullen = $_REQUEST["dullen"];
54 $schweinchen = $_REQUEST["schweinchen"];
56 $EmailA = DB_get_email_by_name($PlayerA);
57 $EmailB = DB_get_email_by_name($PlayerB);
58 $EmailC = DB_get_email_by_name($PlayerC);
59 $EmailD = DB_get_email_by_name($PlayerD);
61 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
63 echo "couldn't find one of the names, please start a new game";
69 $useridA = DB_get_userid_by_name($PlayerA);
70 $useridB = DB_get_userid_by_name($PlayerB);
71 $useridC = DB_get_userid_by_name($PlayerC);
72 $useridD = DB_get_userid_by_name($PlayerD);
74 /* create random numbers */
75 $randomNR = create_array_of_random_numbers($useridA,$useridB,$useridC,$useridD);
76 $randomNRstring = join(":",$randomNR);
78 /* get ruleset information or create new one */
79 $ruleset = DB_get_ruleset($dullen,$schweinchen);
82 echo "Error defining ruleset: $ruleset";
90 if(myisset("followup") )
92 $followup= $_REQUEST["followup"];
93 $session = DB_get_session_by_gameid($followup);
94 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
95 this way no manipulation is possible */
97 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre',".
98 "'$ruleset','$session' ,NULL)");
101 /* get max session */
102 $max = DB_get_max_session();
104 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
105 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre',".
106 "'$ruleset','$max' ,NULL)");
110 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','1','pre', ".
111 "'$ruleset',NULL ,NULL)");
112 $game_id = mysql_insert_id();
115 $TIME = (string) time(); /* to avoid collisions */
116 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA.$TIME);
117 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB.$TIME);
118 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC.$TIME);
119 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD.$TIME);
122 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
123 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,NULL)");
124 $hand_idA = mysql_insert_id();
125 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
126 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,NULL)");
127 $hand_idB = mysql_insert_id();
128 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
129 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,NULL)");
130 $hand_idC = mysql_insert_id();
131 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
132 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,NULL)");
133 $hand_idD = mysql_insert_id();
137 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
138 for($i=12;$i<24;$i++)
139 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
140 for($i=24;$i<36;$i++)
141 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
142 for($i=36;$i<48;$i++)
143 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
145 /* send out email, TODO: check for error with email */
147 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
148 "Place comments and bug reports here:\n".
149 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
150 "The whole round would consist of the following players:\n".
155 "If you want to join this game, please follow this link:\n\n".
158 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
159 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
160 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
161 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
163 echo "You started a new game. The emails have been sent out!";
164 } /* end set up a new game */
165 /* cancle a game, if nothing has happend in the last N minutes */
166 else if(myisset("cancle","me"))
168 $me = $_REQUEST["me"];
170 /* test for valid ID */
171 $myid = DB_get_userid_by_hash($me);
174 echo "Can't find you in the database, please check the url.<br />\n";
175 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
181 DB_update_user_timestamp($myid);
183 /* get some information from the DB */
184 $gameid = DB_get_gameid_by_hash($me);
185 $myname = DB_get_name_by_hash($me);
187 /* check if game really is old enough */
188 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
189 $r = mysql_fetch_array($result,MYSQL_NUM);
190 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
192 $message = "Hello, \n\n".
193 "Game $gameid has been cancled since nothing happend for a while and $myname requested it.\n";
195 $userids = DB_get_all_userid_by_gameid($gameid);
196 foreach($userids as $user)
198 $To = DB_get_email_by_userid($user);
199 mymail($To,$EmailName."game $gameid cancled (timed out)",$message);
202 /* delete everything from the dB */
205 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
208 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
210 /* handle request from one specific player for one game,
211 * (the hash is set on a per game base) */
212 else if(myisset("me"))
214 $me = $_REQUEST["me"];
216 /* test for valid ID */
217 $myid = DB_get_userid_by_hash($me);
220 echo "Can't find you in the database, please check the url.<br />\n";
221 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
227 /* the user had done something, update the timestamp */
228 DB_update_user_timestamp($myid);
230 /* get some information from the DB */
231 $gameid = DB_get_gameid_by_hash($me);
232 $myname = DB_get_name_by_hash($me);
233 $mystatus = DB_get_status_by_hash($me);
234 $mypos = DB_get_pos_by_hash($me);
235 $myhand = DB_get_handid_by_hash($me);
236 $session = DB_get_session_by_gameid($gameid);
238 /* get prefs and save them */
243 /* get rule set for this game */
244 $result = mysql_query("SELECT * FROM Rulesets".
245 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
246 " WHERE Game.id='$gameid'" );
247 $r = mysql_fetch_array($result,MYSQL_NUM);
249 $RULES["dullen"] = $r[2];
250 $RULES["schweinchen"] = $r[3];
251 $RULES["call"] = $r[4];
254 /* get some infos about the game */
255 $gametype = DB_get_gametype_by_gameid($gameid);
256 $gamestatus = DB_get_game_status_by_gameid($gameid);
258 if($gametype=="solo")
260 $gametype = DB_get_solo_by_gameid($gameid);
261 $GT = $gametype." ".$GT;
264 /* display rule set for this game */
265 echo "<div class=\"ruleset\">\n";
267 if($gamestatus != 'pre')
268 echo " Gametype: $GT <br />\n";
270 echo "Rules: <br />\n";
271 echo "10ofhearts : ".$r[2]."<br />\n";
272 echo "schweinchen: ".$r[3]."<br />\n";
273 echo "call: ".$r[4]."<br />\n";
276 /* output extra division in case this game is part of a session */
279 echo "<div class=\"session\">\n".
280 "This game is part of session $session: \n";
281 $hashes = DB_get_hashes_by_session($session,$myid);
283 foreach($hashes as $hash)
288 echo "<a href=\"".$host."?me=".$hash."\">$i</a> ";
295 /* does anyone have both foxes */
296 $GAME["schweinchen"]=0;
299 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
300 $cards = DB_get_all_hand($hash);
301 if( in_array("19",$cards) && in_array("20",$cards) )
303 $GAME["schweinchen"]=1;
304 $GAME["schweinchen-who"]=$hash;
308 /* mystatus gets the player through the different stages of a game.
310 * init: check values from start,
312 * check: check for return values from init
313 * poverty: handle poverty, wait here until all player have reached this state
314 * display sickness and move on to game
315 * play: game in progress
316 * gameover: are we revisiting a game
321 check_want_to_play($me);
322 /* move on to the next stage*/
323 DB_set_hand_status_by_hash($me,'init');
326 /* first check if everything went ok in the last step
327 * if not, send user back, if yes, check what he did
331 echo "<p> you need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
332 DB_set_hand_status_by_hash($me,'start');
336 if($_REQUEST["in"] == "no")
338 /* cancle the game */
339 $message = "Hello, \n\n".
340 "the game has been canceled due to the request of one of the players.\n";
342 $userids = DB_get_all_userid_by_gameid($gameid);
343 foreach($userids as $user)
345 $To = DB_get_email_by_userid($user);
346 mymail($To,$EmailName."game $gameid canceled",$message);
349 /* delete everything from the dB */
354 echo "thanks for joining the game...";
356 $mycards = DB_get_hand($me);
358 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
359 foreach($mycards as $card)
360 display_card($card,$PREF["cardset"]);
363 output_check_for_sickness($me,$mycards);
365 /* move on to the next stage*/
366 DB_set_hand_status_by_hash($me,'check');
372 /* ok, user is in the game, saw his cards and selected his vorbehalt
373 * so first we check what he selected
375 echo "Processing what you selected in the last step...<br />";
377 if(!myisset("solo","wedding","poverty","nines") )
379 /* all these variables have a pre-selected default,
380 * so we should never get here,
381 * unless a user tries to cheat ;) */
382 echo "something went wrong...please contact the admin.";
386 /* check if this sickness needs to be handled first */
387 $gametype = DB_get_gametype_by_gameid($gameid);
388 $startplayer = DB_get_startplayer_by_gameid($gameid);
390 if( $_REQUEST["solo"]!="No")
392 /* user wants to play a solo */
394 /* store the info in the user's hand info */
395 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
396 DB_set_sickness_by_hash($me,"solo");
398 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
400 if($gametype == "solo" && $startplayer<$mypos)
401 {}/* do nothing, since someone else already is playing solo */
404 /* this solo comes first
405 * store info in game table
407 DB_set_gametype_by_gameid($gameid,"solo");
408 DB_set_startplayer_by_gameid($gameid,$mypos);
409 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
412 else if($_REQUEST["wedding"] == "yes")
414 /* TODO: add silent solo somewhere*/
415 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
416 DB_set_sickness_by_hash($me,"wedding");
418 else if($_REQUEST["poverty"] == "yes")
420 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
421 DB_set_sickness_by_hash($me,"poverty");
423 else if($_REQUEST["nines"] == "yes")
425 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
426 " is playing solo, this game will be canceled.<br />\n";
427 DB_set_sickness_by_hash($me,"nines");
430 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.<br />";
432 /* move on to the next stage*/
433 DB_set_hand_status_by_hash($me,'poverty');
435 /* check if everyone has reached this stage, send out email */
436 $userids = DB_get_all_userid_by_gameid($gameid);
438 foreach($userids as $user)
440 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
441 if($userstat!='poverty' && $userstat!='play')
445 foreach($userids as $user)
447 $To = DB_get_email_by_userid($user);
448 $userhash =DB_get_hash_from_gameid_and_userid($gameid,$user);
451 $message = "Everyone finish the questionary in game $gameid, ".
452 "please visit this link now to continue: \n".
453 " ".$host."?me=".$userhash."\n\n" ;
454 mymail($To,$EmailName." finished setup in game $gameid",$message);
462 /* here we need to check if there is a solo or some other form of sickness.
463 * If so, which one is the most important one
464 * set that one in the Game table
465 * tell people about it.
467 echo "<br /> Checking if someone else selected solo, nines or wedding or poverty.<br />";
469 /* check if everyone has reached this stage */
470 $userids = DB_get_all_userid_by_gameid($gameid);
472 foreach($userids as $user)
474 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
475 if($userstat!='poverty' && $userstat!='play')
481 echo "This step can only be handled after everyone finished the last step. ".
482 "Seems like this is not the case, so you need to wait a bit... ".
483 "you will get an email once that is the case, please use the link in ".
484 "that email to continue the game.<br />";
488 echo "Everyone has finished checking their cards, let's see what they said...<br />";
490 /* check what kind of game we are playing, in case there are any solos this already
491 *will have the correct information in it */
492 $gametype = DB_get_gametype_by_gameid($gameid);
493 $startplayer = DB_get_startplayer_by_gameid($gameid);
495 /* check for different sickness and just output a general info */
500 foreach($userids as $user)
502 $name = DB_get_name_by_userid($user);
503 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
504 if($usersick == 'nines')
507 echo "$name has a Vorbehalt. <br />";
510 else if($usersick == 'poverty')
513 echo "$name has a Vorbehalt. <br />";
515 else if($usersick == 'wedding')
518 echo "$name has a Vorbehalt. <br />" ;
520 else if($usersick == 'solo')
523 echo "$name has a Vorbehalt. <br />" ;
527 /* now check which sickness comes first and set the gametype to it */
529 if($gametype == "solo")
536 /* TODO: should we keep statistics of this? */
537 $message = "Hello, \n\n".
538 "the game has been canceled because ".DB_get_name_by_userid($nines).
539 " has five or more nines and nobody is playing solo.\n";
541 /* TODO: add info about redeal in case this is a game of a series */
543 $userids = DB_get_all_userid_by_gameid($gameid);
544 foreach($userids as $user)
546 $To = DB_get_email_by_userid($user);
547 mymail($To,$EmailName."game $gameid canceled",$message);
550 /* delete everything from the dB */
553 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
554 " has five or more nines and nobody is playing solo.\n";
559 else if($poverty==1) /* one person has poverty */
561 DB_set_gametype_by_gameid($gameid,"poverty");
562 $gametype = "poverty";
563 $who=DB_get_sickness_by_gameid($gameid);
566 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
567 if($firstsick == "poverty")
568 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
570 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
573 else if($poverty==2) /* two people have poverty */
575 DB_set_gametype_by_gameid($gameid,"dpoverty");
576 $gametype = "dpoverty";
577 $who=DB_get_sickness_by_gameid($gameid);
580 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
581 if($firstsick == "poverty")
583 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
584 if($secondsick == "poverty")
585 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
587 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
590 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
595 DB_set_gametype_by_gameid($gameid,"wedding");
596 DB_set_sickness_by_gameid($gameid,'-1'); /* wedding not resolved yet */
597 $gametype = "wedding";
602 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
603 * output what kind of game we have */
606 foreach($userids as $user)
608 /* userids are sorted by position...
609 * so output whatever the firstone has, then whatever the next one has
610 * stop when the sickness is the same as the gametype
613 $name = DB_get_name_by_userid($user);
614 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
617 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
619 if($usersick=="poverty")
621 if($usersick == "wedding" && $gametype =="wedding")
623 if($usersick == "poverty" && $gametype =="poverty")
625 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
627 if($usersick == "solo" && $gametype =="solo")
631 /* output Schweinchen in case the rules need it */
632 if( $gametype != "solo")
633 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
634 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
638 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
642 /* are we the solo player? set us to re, else set us to contra */
643 $pos = DB_get_pos_by_hash($me);
644 if($pos == $startplayer)
645 DB_set_party_by_hash($me,"re");
647 DB_set_party_by_hash($me,"contra");
648 DB_set_hand_status_by_hash($me,'play');
652 /* set person with the wedding to re, do the rest during the game */
653 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
654 if($usersick == "wedding")
655 DB_set_party_by_hash($me,"re");
657 DB_set_party_by_hash($me,"contra");
659 echo "Whoever will make the first trick will be on the re team. <br />\n";
660 echo " Ok, the game can start now, please finish <a href=\"$host?me=$me\">the setup</a>.<br />";
661 DB_set_hand_status_by_hash($me,'play');
665 $hand = DB_get_all_hand($me);
667 if(in_array('3',$hand)||in_array('4',$hand))
668 DB_set_party_by_hash($me,"re");
670 DB_set_party_by_hash($me,"contra");
671 DB_set_hand_status_by_hash($me,'play');
675 /* check if poverty resolved (e.g. DB.Game who set to NULL)
676 * yes? =>trump was taken, start game; break;
678 $who = DB_get_sickness_by_gameid($gameid);
680 { /* trump has been taken */
681 DB_set_hand_status_by_hash($me,'play');
685 if($who>9) /*= two people still have trump on the table*/
690 /* check if we are being asked now
691 * no? display wait message, e.g. player X is asked at the moment
693 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
694 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
696 /* user doesn't want to take trump */
697 /* set next player who needs to be asked */
698 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
699 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
701 if($firstsick=="poverty")
703 if($secondsick=="poverty")
704 DB_set_sickness_by_gameid($gameid,$who+$add*3);
706 DB_set_sickness_by_gameid($gameid,$who+$add*2);
709 DB_set_sickness_by_gameid($gameid,$who+$add);
711 /* email next player */
712 $who = DB_get_sickness_by_gameid($gameid);
713 if($who>9) $who = $who/10;
717 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
718 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
719 DB_set_player_by_gameid($gameid,$who);
721 $message = "Someone has poverty, it's your turn to decide, if you want to take the trump. Please visit:".
722 " ".$host."?me=".$userhash."\n\n" ;
723 mymail($To,$EmailName." poverty (game $gameid)",$message);
726 /* this user is done */
727 DB_set_hand_status_by_hash($me,'play');
730 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
732 /* user wants to take trump */
733 $trump = $_REQUEST["trump"];
735 /* get hand id for user $trump */
736 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
737 /* copy trump from player A to B */
738 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
740 /* add hidden button with trump in it to get to the next point */
741 echo "<form action=\"index.php\" method=\"post\">\n";
742 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
743 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
744 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
745 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
748 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
750 $trump = $_REQUEST["trump"];
751 $exchange = $_REQUEST["exchange"];
752 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
754 /* if exchange is set to a value>0, exchange that card back to user $trump */
757 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
758 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
761 /* if number of cards == 12, set status to play for both users */
762 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
763 $r = mysql_fetch_array($result,MYSQL_NUM);
766 die("error in poverty");
770 if($gametype=="poverty" || $who<9)
772 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
774 else /* reduce poverty count by one, that is go to single digits $who */
779 /* whom to ask next */
780 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
781 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
783 if($firstsick!="poverty")
784 DB_set_sickness_by_gameid($gameid,$who+$add);
787 if($secondsick!="poverty")
788 DB_set_sickness_by_gameid($gameid,$who+$add*2);
790 DB_set_sickness_by_gameid($gameid,$who+$add*3);
793 /* email next player */
794 $who = DB_get_sickness_by_gameid($gameid);
797 $To = DB_get_email_by_pos_and_gameid($who,$gameid);
798 $userhash = DB_get_hash_from_game_and_pos($gameid,$who);
799 DB_set_player_by_gameid($gameid,$who);
801 $message = "Someone has poverty, it's your turn to decide, ".
802 "if you want to take the trump. Please visit:".
803 " ".$host."?me=".$userhash."\n\n" ;
804 mymail($To,$EmailName." poverty (game $gameid)",$message);
809 /* this user is done */
810 DB_set_hand_status_by_hash($me,'play');
811 /* and so is his partner */
812 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
813 DB_set_hand_status_by_hash($hash,'play');
815 /* set party to re, unless we had dpoverty, in that case check if we need to set re/contra*/
817 foreach($userids as $user)
819 $userhash =DB_get_hash_from_gameid_and_userid($gameid,$user);
820 $party=DB_get_party_by_hash($userhash);
826 DB_set_party_by_hash($me,"contra");
827 DB_set_party_by_hash($hash,"contra");
831 foreach($userids as $user)
833 $userhash =DB_get_hash_from_gameid_and_userid($gameid,$user);
834 if($userhash==$hash||$userhash==$me)
835 DB_set_party_by_hash($userhash,"re");
837 DB_set_party_by_hash($userhash,"contra");
846 /* else show all trump, have lowest card pre-selected, have hidden setting for */
847 echo "you need to get rid of a few cards<br />\n";
849 set_gametype($gametype); /* this sets the $CARDS variable */
850 $mycards = DB_get_hand($me);
851 $mycards = mysort($mycards,$gametype);
853 echo "<form class=\"exchange\" action=\"index.php\" method=\"post\">\n";
855 foreach($mycards as $card)
856 display_link_card($card,$PREF["cardset"],$type);
857 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
858 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
859 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
863 else if($who == $mypos || $who == $mypos*10)
865 foreach($userids as $user)
867 $name = DB_get_name_by_userid($user);
868 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
870 if($usersick=="poverty")
872 $hash =DB_get_hash_from_gameid_and_userid($gameid,$user);
873 $cards=DB_get_hand($hash);
874 $nrtrump=count_trump($cards);
877 echo "Player $name has $nrtrump trump. Do you want to take them?".
878 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />";
881 echo "I don't want to take any trump: ".
882 "<a href=\"index.php?me=$me&trump=no\">yes</a> <br />";
884 echo "Your cards are: <br />\n";
885 $mycards = DB_get_hand($me);
887 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
888 foreach($mycards as $card)
889 display_card($card,$PREF["cardset"]);
894 $mysick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
895 if($mysick=="poverty")
896 echo "The others are asked if they want to take your trump, you have to wait (you'll get an email).";
898 echo "it's not your turn yet to decide if you want to take the trump or not.";
901 * yes, display number of trump and user's hand, ask if he wants to take it
902 * no, set whom-to-ask to next player, email next player, cancle game if no next player
903 * yes -> link to new page:display all cards, ask for N return cards
909 /* check if no one wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
910 $who = DB_get_sickness_by_gameid($gameid);
911 if($who==5 || $who==50)
913 $message = "Hello, \n\n".
914 "Game $gameid has been cancled since nobody wanted to take the trump.\n";
916 $userids = DB_get_all_userid_by_gameid($gameid);
917 foreach($userids as $user)
919 $To = DB_get_email_by_userid($user);
920 mymail($To,$EmailName."game $gameid cancled (poverty not resolved)",$message);
923 /* delete everything from the dB */
926 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
932 /* check if all players are ready to play */
934 foreach($userids as $user)
935 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
940 /* only set this after all poverty, etc. are handled*/
941 DB_set_game_status_by_gameid($gameid,'play');
943 /* email startplayer */
944 $startplayer = DB_get_startplayer_by_gameid($gameid);
945 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
946 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
947 $who = DB_get_userid_by_email($email);
948 DB_set_player_by_gameid($gameid,$who);
952 /* email startplayer) */
953 $message = "It's your turn now in game $gameid.\n".
954 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
955 mymail($email,$EmailName."ready, set, go... (game $gameid) ",$message);
958 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
966 /* both entries here, so that the tricks are visible for both.
967 * in case of 'play' there is a break later that skips the last part
970 /* figure out what kind of game we are playing,
971 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
972 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
976 $gametype = DB_get_gametype_by_gameid($gameid);
978 if($gametype=="solo")
980 $gametype = DB_get_solo_by_gameid($gameid);
981 $GT = $gametype." ".$GT;
986 set_gametype($gametype); /* this sets the $CARDS variable */
988 /* get some infos about the game */
989 $gamestatus = DB_get_game_status_by_gameid($gameid);
991 /* display useful things in divs */
993 /* display links to the users status page */
994 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
995 $r = mysql_fetch_array($result,MYSQL_NUM);
997 display_links($r[0],$r[1]);
999 /* end display useful things*/
1001 /* has the game started? No, then just wait here...*/
1002 if($gamestatus == 'pre')
1004 echo "You finished the setup, but not everyone else finished it... ".
1005 "so you need to wait for the others. Just wait for the an email... <br />";
1006 break; /* not sure this works... the idea is that you can
1007 * only play a card after everyone is ready to play */
1010 /* display the table and the names */
1011 $result = mysql_query("SELECT User.fullname as name,".
1012 " Hand.position as position, ".
1014 " Hand.party as party, ".
1015 " Hand.sickness as sickness, ".
1016 " Hand.point_call, ".
1017 " User.last_login, ".
1020 "LEFT JOIN User ON User.id=Hand.user_id ".
1021 "WHERE Hand.game_id='".$gameid."' ".
1022 "ORDER BY position ASC");
1024 echo "<div class=\"table\">\n".
1025 " <img src=\"pics/table.png\" alt=\"table\" />\n";
1026 while($r = mysql_fetch_array($result,MYSQL_NUM))
1034 $lastlogin = strtotime($r[6]);
1037 $offset = DB_get_user_timezone($user);
1038 $zone = return_timezone($offset);
1039 date_default_timezone_set($zone);
1041 echo " <span class=\"table".($pos-1)."\">\n";
1046 echo "<a href=\"".$host."?me=".$hash."\">$name</a>\n";
1048 /* add hints for poverty, wedding, solo, etc */
1049 if($GT=="poverty" && $party=="re")
1050 if($sickness=="poverty")
1052 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1053 $cards = DB_get_all_hand($userhash);
1054 $trumpNR = count_trump($cards);
1056 echo "(poverty < trump back)";
1065 if($sickness=="poverty")
1067 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1068 $cards = DB_get_all_hand($userhash);
1069 $trumpNR = count_trump($cards);
1071 echo "(poverty A < trump back)";
1073 echo "(poverty A <)";
1076 echo "(poverty A >)";
1078 if($sickness=="poverty")
1080 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1081 $cards = DB_get_all_hand($userhash);
1082 $trumpNR = count_trump($cards);
1084 echo "(poverty B < trump back)";
1086 echo "(poverty B <)";
1089 echo "(poverty B >)";
1091 if($GT=="wedding" && $party=="re")
1092 if($sickness=="wedding")
1097 if(ereg("solo",$GT) && $party=="re")
1100 /* add point calls */
1102 echo " $party $call ";
1105 echo " local time: ".date("Y-m-d H:i:s")."<br />\n";
1106 echo " last login: ".date("Y-m-d H:i:s",$lastlogin)."<br />\n";
1112 /* get everything relevant to display the tricks */
1113 $result = mysql_query("SELECT Hand_Card.card_id as card,".
1114 " Hand.position as position,".
1115 " Play.sequence as sequence, ".
1117 " Comment.comment, ".
1118 " Play.create_date, ".
1121 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
1122 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
1123 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
1124 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
1125 "WHERE Trick.game_id='".$gameid."' ".
1126 "ORDER BY Trick.id,sequence ASC");
1129 $lasttrick = DB_get_max_trickid($gameid);
1131 $play = array(); /* needed to calculate winner later */
1133 $pos = DB_get_startplayer_by_gameid($gameid)-1;
1134 $firstcard = ""; /* first card in a trick */
1136 echo "\n<ul class=\"tricks\">\n";
1137 echo " <li class=\"nohighlight\"> Game $gameid: </li>\n";
1139 while($r = mysql_fetch_array($result,MYSQL_NUM))
1145 $timeplayed = strtotime($r[5]);
1148 $offset = DB_get_user_timezone($user);
1149 $zone = return_timezone($offset);
1150 date_default_timezone_set($zone);
1152 /* check if first schweinchen has been played */
1153 if($r[0] == 19 || $r[0] == 20 )
1154 $GAME["schweinchen"]++;
1156 /* save card to be able to find the winner of the trick later */
1157 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
1161 /* first card in a trick, output some html */
1162 if($trick!=$lasttrick)
1164 /* start of an old trick? */
1165 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
1166 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1167 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1169 else if($trick==$lasttrick)
1171 /* start of a last trick? */
1172 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
1173 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
1174 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
1177 /* remember first card, so that we are able to check, what cards can be played */
1182 echo " <div class=\"card".($pos-1)."\">\n";
1184 /* display comments */
1186 echo " <span class=\"comment\">".$comment."</span>\n";
1189 display_card($r[0],$PREF["cardset"]);
1191 echo " </div>\n"; /* end div card */
1197 echo " </div>\n </li>\n"; /* end div table, end li table */
1201 if($seq!=4 && $trickNR>1)
1202 echo " </div>\n </li>\n"; /* end div table, end li table */
1206 /* whos turn is it? */
1209 $winner = get_winner($play,$gametype); /* returns the position */
1211 $firstcard = ""; /* new trick, no first card */
1216 if($next==5) $next=1;
1219 /* my turn?, display cards as links, ask for comments*/
1220 if(DB_get_pos_by_hash($me) == $next)
1225 /* do we want to play a card? */
1226 if(myisset("card") && $myturn)
1228 $card = $_REQUEST["card"];
1229 $handid = DB_get_handid_by_hash($me);
1231 /* check if we have card and that we haven't played it yet*/
1232 /* set played in hand_card to true where hand_id and card_id*/
1233 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1234 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1235 $r = mysql_fetch_array($result,MYSQL_NUM);
1236 $handcardid = $r[0];
1238 if($handcardid) /* everything ok, play card */
1242 /* update Game timestamp */
1243 DB_update_game_timestamp($gameid);
1245 /* check if a call was made, must do this before we set the card status to played */
1246 if(myisset("call120") && $_REQUEST["call120"] == "yes" && can_call(120,$me))
1247 $result = mysql_query("UPDATE Hand SET point_call='120' WHERE hash='$me' ");
1248 if(myisset("call90") && $_REQUEST["call90"] == "yes" && can_call(90,$me))
1249 $result = mysql_query("UPDATE Hand SET point_call='90' WHERE hash='$me' ");
1250 if(myisset("call60") && $_REQUEST["call60"] == "yes" && can_call(60,$me))
1251 $result = mysql_query("UPDATE Hand SET point_call='60' WHERE hash='$me' ");
1252 if(myisset("call30") && $_REQUEST["call30"] == "yes" && can_call(30,$me))
1253 $result = mysql_query("UPDATE Hand SET point_call='30' WHERE hash='$me' ");
1254 if(myisset("call0") && $_REQUEST["call0"] == "yes" && can_call(0,$me))
1255 $result = mysql_query("UPDATE Hand SET point_call='0' WHERE hash='$me' ");
1257 /* mark card as played */
1258 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1259 DB_quote_smart($card));
1261 /* check for schweinchen */
1262 //echo "schweinchen = ".$GAME["schweinchen"]." --$card-<br />";
1263 if($card == 19 || $card == 20 )
1265 $GAME["schweinchen"]++;
1266 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1267 $comment="Schweinchen! ";
1268 if($RULES["schweinchen"]=="both" )
1269 $comment="Schweinchen! ";
1270 if ($debug) echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1273 /* get trick id or start new trick */
1274 $a = DB_get_current_trickid($gameid);
1279 $playid = DB_play_card($trickid,$handcardid,$sequence);
1281 /* if sequence == 4 check who one in case of wedding */
1282 if($sequence == 4 && $GT == "wedding")
1284 /* is wedding resolve */
1285 $resolved = DB_get_sickness_by_gameid($gameid);
1288 /* who has wedding */
1289 $userids = DB_get_all_userid_by_gameid($gameid);
1290 foreach($userids as $user)
1292 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
1293 if($usersick == "wedding")
1296 /* who won the trick */
1297 $play = DB_get_cards_by_trick($trickid);
1298 $winner = get_winner($play,$gametype); /* returns the position */
1299 $winnerid = DB_get_userid_by_gameid_and_position($gameid,$winner);
1300 /* is tricknr <=3 */
1301 if($tricknr <=3 && $winnerid!=$whosick)
1303 /* set resolved at tricknr*/
1304 $resolved = DB_set_sickness_by_gameid($gameid,$tricknr);
1306 $whash = DB_get_hash_from_gameid_and_userid($gameid,$winnerid);
1307 DB_set_party_by_hash($whash,"re");
1309 if($tricknr == 3 && $winnerid==$whosick)
1311 /* set resolved at tricknr*/
1312 $resolved = DB_set_sickness_by_gameid($gameid,'3');
1317 /* check for coment */
1318 if(myisset("comment"))
1320 $comment.=$_REQUEST["comment"];
1323 DB_insert_comment($comment,$playid,$myid);
1325 /* display played card */
1326 echo "<div class=\"card\">";
1327 echo " you played <br />";
1328 display_card($card,$PREF["cardset"]);
1331 /*check if we still have cards left, else set status to gameover */
1332 if(sizeof(DB_get_hand($me))==0)
1334 DB_set_hand_status_by_hash($me,'gameover');
1335 $mystatus='gameover';
1338 /* if all players are done, set game status to game over,
1339 * get the points of the last trick and send out an email
1342 $userids = DB_get_all_userid_by_gameid($gameid);
1345 foreach($userids as $user)
1346 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1351 DB_set_game_status_by_gameid($gameid,"gameover");
1352 /* get score for last trick
1353 * all other tricks are handled a few lines further down*/
1354 $play = DB_get_cards_by_trick($trickid);
1355 $winner = get_winner($play,$gametype); /* returns the position */
1356 /* get points of last trick and save it */
1358 foreach($play as $card)
1359 $points = $points + card_value($card["card"]);
1360 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1362 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1364 echo "ERROR during scoring";
1366 /* email all players */
1367 /* individual score */
1368 $result = mysql_query("SELECT fullname, IFNULL(SUM(score),0), Hand.party FROM Hand".
1369 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1370 " LEFT JOIN User ON Hand.user_id=User.id".
1371 " WHERE Hand.game_id=$gameid".
1372 " GROUP BY fullname" );
1373 $message = "The game is over. Thanks for playing :)\n";
1374 $message .= "Final score:\n";
1375 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1376 $message .= " ".$r[0]."(".$r[2].") ".$r[1]."\n";
1378 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(score),0) FROM Hand".
1379 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1380 " LEFT JOIN User ON Hand.user_id=User.id".
1381 " WHERE Hand.game_id=$gameid".
1382 " GROUP BY Hand.party" );
1383 $message .= "\nTotals:\n";
1384 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1385 $message .= " ".$r[0]." ".$r[1]."\n";
1387 /* send out final email */
1390 foreach($userids as $user)
1391 $all[] = DB_get_email_by_userid($user);
1392 $TO = implode(",",$all);
1394 $help = "\n\n (you can use reply all on this email to reach all the players.)\n";
1395 mymail($To,$EmailName."game over (game $gameid) part 1(2)",$message.$help);
1397 foreach($userids as $user)
1399 $To = DB_get_email_by_userid($user);
1400 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1402 $link = "Use this link to have a look at game $gameid: ".$host."?me=".$hash."\n\n" ;
1403 mymail($To,$EmailName."game over (game $gameid) part 2(2)",$link);
1408 /* email next player */
1409 if(DB_get_game_status_by_gameid($gameid)=='play')
1413 $play = DB_get_cards_by_trick($trickid);
1414 $winner = get_winner($play,$gametype); /* returns the position */
1416 /* get points of last trick and save it, last trick is handled
1417 * a few lines further up */
1419 foreach($play as $card)
1420 $points = $points + card_value($card["card"]);
1422 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1424 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1426 echo "ERROR during scoring";
1429 echo "DEBUG: $winner got $points <br />";
1431 /* who is the next player? */
1436 $next = DB_get_pos_by_hash($me)+1;
1438 if($next==5) $next=1;
1440 /* email next player */
1441 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1442 $email = DB_get_email_by_hash($next_hash);
1443 $who = DB_get_userid_by_email($email);
1444 DB_set_player_by_gameid($gameid,$who);
1446 $message = "A card has been played in game $gameid.\n\n".
1447 "It's your turn now.\n".
1448 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1449 mymail($email,$EmailName."a card has been played in game $gameid",$message);
1454 echo "can't find that card?! <br />\n";
1457 else if(myisset("card") && !$myturn )
1459 echo "please wait until it's your turn! <br />\n";
1462 $mycards = DB_get_hand($me);
1463 $mycards = mysort($mycards,$gametype);
1464 echo "<div class=\"mycards\">\n";
1466 if($myturn && !myisset("card"))
1468 echo "Hello ".$myname.", it's your turn! <br />\n";
1469 echo "Your cards are: <br />\n";
1470 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
1472 /* do we have to follow suite? */
1474 if(have_suit($mycards,$firstcard))
1477 foreach($mycards as $card)
1479 if($followsuit && !same_type($card,$firstcard))
1480 display_card($card,$PREF["cardset"]);
1482 display_link_card($card,$PREF["cardset"]);
1485 if( can_call(120,$me) )
1486 echo " re/contra (120):".
1487 " <input type=\"radio\" name=\"call120\" value=\"yes\" /> ";
1488 if( can_call(90,$me) )
1490 " <input type=\"radio\" name=\"call90\" value=\"yes\" /> ";
1491 if( can_call(60,$me) )
1493 " <input type=\"radio\" name=\"call60\" value=\"yes\" /> ";
1494 if( can_call(30,$me) )
1496 " <input type=\"radio\" name=\"call30\" value=\"yes\" /> ";
1497 if( can_call(0,$me) )
1499 " <input type=\"radio\" name=\"call0\" value=\"yes\" /> ".
1501 " <input type=\"radio\" name=\"call0\" value=\"no\" /> ";
1503 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
1504 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
1505 echo "<input type=\"submit\" value=\"move\" />\n";
1508 else if($mystatus=='play')
1510 echo "Your cards are: <br />\n";
1511 foreach($mycards as $card)
1512 display_card($card,$PREF["cardset"]);
1514 else if($mystatus=='gameover')
1516 $oldcards = DB_get_all_hand($me);
1517 $oldcards = mysort($oldcards,$gametype);
1518 echo "Your cards were: <br />\n";
1519 foreach($oldcards as $card)
1520 display_card($card,$PREF["cardset"]);
1522 $userids = DB_get_all_userid_by_gameid($gameid);
1523 foreach($userids as $user)
1525 $userhash = DB_get_hash_from_gameid_and_userid($gameid,$user);
1531 $name = DB_get_name_by_userid($user);
1532 $oldcards = DB_get_all_hand($userhash);
1533 $oldcards = mysort($oldcards,$gametype);
1534 echo "$name's cards were: <br />\n";
1535 foreach($oldcards as $card)
1536 display_card($card,$PREF["cardset"]);
1542 /* if the game is over do some extra stuff, therefore exit the swtich statement if we are still playing*/
1543 if($mystatus=='play')
1545 /* the following happens only when the gamestatus is 'gameover' */
1546 /* check if game is over, display results */
1547 if(DB_get_game_status_by_gameid($gameid)=='play')
1549 echo "the game is over for you.. other people still need to play though";
1553 echo "the game is over now...<br />\n";
1555 $result = mysql_query("SELECT fullname, IFNULL(SUM(score),0), Hand.party FROM Hand".
1556 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1557 " LEFT JOIN User ON Hand.user_id=User.id".
1558 " WHERE Hand.game_id=$gameid".
1559 " GROUP BY fullname" );
1560 echo "Final Score:<br />\n".
1562 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1563 echo " <tr><td> ".$r[0]."</td><td>(".$r[2].")</td><td> ".$r[1]."</td></tr>";
1566 $result = mysql_query("SELECT Hand.party, IFNULL(SUM(score),0) FROM Hand".
1567 " LEFT JOIN Score ON Hand.id=Score.hand_id".
1568 " LEFT JOIN User ON Hand.user_id=User.id".
1569 " WHERE Hand.game_id=$gameid".
1570 " GROUP BY Hand.party" );
1571 echo "Totals:<br />\n".
1573 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1574 echo " <tr><td>".$r[0]."</td><td> ".$r[1]."</td></tr>\n";
1577 $session = DB_get_session_by_gameid($gameid);
1578 $result = mysql_query("SELECT id,create_date FROM Game".
1579 " WHERE session=$session".
1580 " ORDER BY create_date DESC".
1584 $r = mysql_fetch_array($result,MYSQL_NUM);
1586 if(!$session || $gameid==$r[0])
1588 /* suggest a new game with the same people in it, just rotated once */
1589 $names = DB_get_all_names_by_gameid($gameid);
1590 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1595 echo "error in testing the status";
1601 /* user status page */
1602 else if(myisset("email","password"))
1604 /* test id and password, should really be done in one step */
1605 $email = $_REQUEST["email"];
1606 $password = $_REQUEST["password"];
1609 if(myisset("forgot"))
1613 $uid = DB_get_userid_by_email($email);
1619 echo "Hmm, you forgot your passwort...nothing I can do at the moment:( ";
1620 echo " you need to email Arun for now... in the future it will be all automated and an ";
1621 echo "email with a new password will go to $email.";
1626 echo "you need to give me an email address!";
1628 echo "couldn't find a player with this email, please contact Arun, if you think this is a mistake";
1633 /* verify password and email */
1634 if(strlen($password)!=32)
1635 $password = md5($password);
1638 $uid = DB_get_userid_by_email_and_password($email,$password);
1646 if(myisset("setpref"))
1648 $setpref=$_REQUEST["setpref"];
1652 case "englishcards":
1653 $result = mysql_query("SELECT * from User_Prefs".
1654 " WHERE user_id='$uid' AND pref_key='cardset'" );
1655 if( mysql_fetch_array($result,MYSQL_NUM))
1656 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1657 " WHERE user_id='$uid' AND pref_key='cardset'" );
1659 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','cardset',".DB_quote_smart($setpref).")");
1660 echo "Ok, changed you preferences for the cards.\n";
1664 else /* output default user page */
1666 $time = DB_get_user_timestamp($uid);
1667 $unixtime =strtotime($time);
1669 $offset = DB_get_user_timezone($uid);
1670 $zone = return_timezone($offset);
1671 date_default_timezone_set($zone);
1673 /* display links to settings */
1674 output_user_settings($email,$password);
1676 echo "last login: ".date("r",$unixtime)."<br />";
1678 DB_update_user_timestamp($uid);
1680 echo "<p>these are your games that haven't started yet:<br />\n";
1681 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1682 " LEFT JOIN Game On Hand.game_id=Game.id".
1683 " WHERE Hand.user_id='$uid' AND Game.status='pre'" );
1684 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1686 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1690 echo "(it's <strong>your</strong> turn)\n";
1693 $name = DB_get_name_by_userid($r[3]);
1694 echo "(it's $name's turn)\n";
1697 if(time()-strtotime($r[2]) > 60*60*24*30)
1698 echo " The game has been running for over a month.".
1699 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1700 " (clicking here is final and can't be restored)";
1705 echo "<p>these are the games you are playing in:<br />\n";
1706 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date,Game.player from Hand".
1707 " LEFT JOIN Game On Hand.game_id=Game.id".
1708 " WHERE Hand.user_id='$uid' AND Game.status='play'" );
1709 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1711 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1715 echo "(it's <strong>your</strong> turn)\n";
1718 $name = DB_get_name_by_userid($r[3]);
1719 echo "(it's $name's turn)\n";
1722 if(time()-strtotime($r[2]) > 60*60*24*30)
1723 echo " The game has been running for over a month.".
1724 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1725 " (clicking here is final and can't be restored)";
1731 echo "<p>and these are your games that are already done:<br />Game: \n";
1733 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
1734 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1735 $output[]= "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>";
1736 echo implode(", ",$output)."</p>\n";
1738 $names = DB_get_all_names();
1739 echo "<p>registered players:<br />\n";
1740 echo implode(", ",$names)."\n";
1743 echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1748 echo "sorry email and password don't match <br />";
1755 /* page for registration */
1756 else if(myisset("register") )
1760 /* new user wants to register */
1761 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
1764 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
1766 echo "please chose another name<br />";
1769 if(DB_get_userid_by_email($_REQUEST["Remail"]))
1771 echo "this email address is already used ?!<br />";
1776 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
1777 ",".DB_quote_smart($_REQUEST["Remail"]).
1778 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
1779 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
1782 echo " added you to the database";
1784 echo " something went wrong";
1787 /* default login page */
1790 $pre[0]=0;$game[0]=0;$done[0]=0;
1791 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1793 $pre = mysql_fetch_array($r,MYSQL_NUM);
1794 $game = mysql_fetch_array($r,MYSQL_NUM);
1795 $done = mysql_fetch_array($r,MYSQL_NUM);
1798 $r=mysql_query("SELECT AVG(datediff(mod_date,create_date)) FROM Game where status='gameover' ");
1800 $avgage= mysql_fetch_array($r,MYSQL_NUM);
1804 output_home_page($pre[0],$game[0],$done[0],$avgage[0]);