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...";
38 /* check if we want to start a new game */
41 $names = DB_get_all_names();
42 output_form_for_new_game($names);
44 /*check if everything is ready to set up a new game */
45 else if( myisset("PlayerA", "PlayerB","PlayerC","PlayerD","dullen","schweinchen" ))
47 $PlayerA = $_REQUEST["PlayerA"];
48 $PlayerB = $_REQUEST["PlayerB"];
49 $PlayerC = $_REQUEST["PlayerC"];
50 $PlayerD = $_REQUEST["PlayerD"];
52 $dullen = $_REQUEST["dullen"];
53 $schweinchen = $_REQUEST["schweinchen"];
55 $EmailA = DB_get_email_by_name($PlayerA);
56 $EmailB = DB_get_email_by_name($PlayerB);
57 $EmailC = DB_get_email_by_name($PlayerC);
58 $EmailD = DB_get_email_by_name($PlayerD);
60 if($EmailA=="" || $EmailB=="" || $EmailC=="" || $EmailD=="")
62 echo "couldn't find one of the names, please start a new game";
67 $useridA = DB_get_userid_by_name($PlayerA);
68 $useridB = DB_get_userid_by_name($PlayerB);
69 $useridC = DB_get_userid_by_name($PlayerC);
70 $useridD = DB_get_userid_by_name($PlayerD);
72 /* create random numbers */
73 $randomNR = create_array_of_random_numbers();
74 $randomNRstring = join(":",$randomNR);
76 /* get ruleset information or create new one */
77 $ruleset = DB_get_ruleset($dullen,$schweinchen);
80 echo "Error defining ruleset: $ruleset";
87 if(myisset("followup") )
89 $followup= $_REQUEST["followup"];
90 $session = DB_get_session_by_gameid($followup);
91 $ruleset = DB_get_ruleset_by_gameid($followup); /* just copy ruleset from old game,
92 this way no manipulation is possible */
94 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','pre',".
95 "'$ruleset','$session' ,NULL)");
99 $max = DB_get_max_session();
101 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','pre',".
102 "'$ruleset','$max' ,NULL)");
103 mysql_query("UPDATE Game SET session='".$max."' WHERE id=".DB_quote_smart($followup));
107 mysql_query("INSERT INTO Game VALUES (NULL, NULL, '$randomNRstring', 'normal', NULL,NULL,'1','pre', ".
108 "'$ruleset',NULL ,NULL)");
109 $game_id = mysql_insert_id();
112 $hashA = md5("AGameOfDoko".$game_id.$PlayerA.$EmailA);
113 $hashB = md5("AGameOfDoko".$game_id.$PlayerB.$EmailB);
114 $hashC = md5("AGameOfDoko".$game_id.$PlayerC.$EmailC);
115 $hashD = md5("AGameOfDoko".$game_id.$PlayerD.$EmailD);
118 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridA).
119 ", ".DB_quote_smart($hashA).", 'start','1',NULL,NULL,NULL,'false','false',NULL)");
120 $hand_idA = mysql_insert_id();
121 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridB).
122 ", ".DB_quote_smart($hashB).", 'start','2',NULL,NULL,NULL,'false','false',NULL)");
123 $hand_idB = mysql_insert_id();
124 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridC).
125 ", ".DB_quote_smart($hashC).", 'start','3',NULL,NULL,NULL,'false','false',NULL)");
126 $hand_idC = mysql_insert_id();
127 mysql_query("INSERT INTO Hand VALUES (NULL,".DB_quote_smart($game_id).",".DB_quote_smart($useridD).
128 ", ".DB_quote_smart($hashD).", 'start','4',NULL,NULL,NULL,'false','false',NULL)");
129 $hand_idD = mysql_insert_id();
133 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idA', '".$randomNR[$i]."', 'false')");
134 for($i=12;$i<24;$i++)
135 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idB', '".$randomNR[$i]."', 'false')");
136 for($i=24;$i<36;$i++)
137 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idC', '".$randomNR[$i]."', 'false')");
138 for($i=36;$i<48;$i++)
139 mysql_query("INSERT INTO Hand_Card VALUES (NULL, '$hand_idD', '".$randomNR[$i]."', 'false')");
141 /* send out email, TODO: check for error with email */
143 "you are invited to play a game of DoKo (that is to debug the program ;).\n".
144 "Place comments and bug reports here:\n".
145 "http://wiki.nubati.net/index.php?title=EmailDoko\n\n".
146 "The whole round would consist of the following players:\n".
151 "If you want to join this game, please follow this link:\n\n".
154 mymail($EmailA,"You are invited to a game of DoKo","Hello $PlayerA,\n".$message.$hashA);
155 mymail($EmailB,"You are invited to a game of DoKo","Hello $PlayerB,\n".$message.$hashB);
156 mymail($EmailC,"You are invited to a game of DoKo","Hello $PlayerC,\n".$message.$hashC);
157 mymail($EmailD,"You are invited to a game of DoKo","Hello $PlayerD,\n".$message.$hashD);
159 echo "You started a new game. The emails have been sent out!";
160 } /* end set up a new game */
161 /* cancle a game, if nothing has happend in the last N minutes */
162 else if(myisset("cancle","me"))
164 $me = $_REQUEST["me"];
166 /* test for valid ID */
167 $myid = DB_get_userid_by_hash($me);
170 echo "Can't find you in the database, please check the url.<br />\n";
171 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
176 DB_update_user_timestamp($myid);
178 /* get some information from the DB */
179 $gameid = DB_get_gameid_by_hash($me);
180 $myname = DB_get_name_by_hash($me);
182 /* check if game really is old enough */
183 $result = mysql_query("SELECT mod_date from Game WHERE id='$gameid' " );
184 $r = mysql_fetch_array($result,MYSQL_NUM);
185 if(time()-strtotime($r[0]) > 60*60*24*30) /* = 1 month */
187 $message = "Hello, \n\n".
188 "Game $gameid has been cancled since nothing happend for a while and $myname requested it.\n";
190 $userids = DB_get_all_userid_by_gameid($gameid);
191 foreach($userids as $user)
193 $To = DB_get_email_by_userid($user);
194 mymail($To,$EmailName."game cancled (timed out)",$message);
197 /* delete everything from the dB */
200 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
203 echo "<p>You need to wait longer before you can cancle a game...</p>\n";
205 /* handle request from one specific player for one game,
206 * (the hash is set on a per game base) */
207 else if(myisset("me"))
209 $me = $_REQUEST["me"];
211 /* test for valid ID */
212 $myid = DB_get_userid_by_hash($me);
215 echo "Can't find you in the database, please check the url.<br />\n";
216 echo "perhaps the game has been cancled, check by login in <a href=\"$host\">here</a>.";
221 /* the user had done something, update the timestamp */
222 DB_update_user_timestamp($myid);
224 /* get some information from the DB */
225 $gameid = DB_get_gameid_by_hash($me);
226 $myname = DB_get_name_by_hash($me);
227 $mystatus = DB_get_status_by_hash($me);
228 $mypos = DB_get_pos_by_hash($me);
229 $myhand = DB_get_handid_by_hash($me);
231 /* get prefs and save them */
232 $result = mysql_query("SELECT value from User_Prefs".
233 " WHERE user_id='$myid' AND pref_key='cardset'" );
234 $r = mysql_fetch_array($result,MYSQL_NUM);
237 if($r[0]=="germancards" && (time()-strtotime( "2009-12-31 23:59:59")<0) ) /* licence only valid until then */
238 $PREF["cardset"]="altenburg";
240 $PREF["cardset"]="english";
243 $PREF["cardset"]="english";
246 /* get rule set for this game */
247 $result = mysql_query("SELECT * FROM Rulesets".
248 " LEFT JOIN Game ON Game.ruleset=Rulesets.id ".
249 " WHERE Game.id='$gameid'" );
250 $r = mysql_fetch_array($result,MYSQL_NUM);
252 $RULES["dullen"] = $r[2];
253 $RULES["schweinchen"] = $r[3];
255 /* get some infos about the game */
256 $gametype = DB_get_gametype_by_gameid($gameid);
257 $gamestatus = DB_get_game_status_by_gameid($gameid);
259 if($gametype=="solo")
261 $gametype = DB_get_solo_by_gameid($gameid);
262 $GT = $gametype." ".$GT;
265 /* display rule set for this game */
266 echo "<div class=\"ruleset\">\n";
268 if($gamestatus != 'pre')
269 echo " Gametype: $GT <br />\n";
271 echo "Rules: <br />\n";
272 echo "10ofhearts : ".$r[2]."<br />\n";
273 echo "schweinchen: ".$r[3]."<br />\n";
276 /* does anyone have both foxes */
277 $GAME["schweinchen"]=0;
280 $hash = DB_get_hash_from_game_and_pos($gameid,$i);
281 $cards = DB_get_all_hand($hash);
282 if( in_array("19",$cards) && in_array("20",$cards) )
284 $GAME["schweinchen"]=1;
285 $GAME["schweinchen-who"]=$hash;
289 /* mystatus gets the player through the different stages of a game.
291 * init: check values from start,
293 * check: check for return values from init
294 * poverty: handle poverty, wait here until all player have reached this state
295 * display sickness and move on to game
296 * play: game in progress
297 * gameover: are we revisiting a game
302 check_want_to_play($me);
303 /* move on to the next stage*/
304 DB_set_hand_status_by_hash($me,'init');
307 /* first check if everything went ok in the last step
308 * if not, send user back, if yes, check what he did
312 echo "<p> you need to answer the <a href=\"$host?me=$me\">question</a>.</p>";
313 DB_set_hand_status_by_hash($me,'start');
317 if($_REQUEST["in"] == "no")
319 /* cancle the game */
320 $message = "Hello, \n\n".
321 "the game has been canceled due to the request of one of the players.\n";
323 $userids = DB_get_all_userid_by_gameid($gameid);
324 foreach($userids as $user)
326 $To = DB_get_email_by_userid($user);
327 mymail($To,$EmailName."game canceled",$message);
330 /* delete everything from the dB */
335 echo "thanks for joining the game...";
337 $mycards = DB_get_hand($me);
339 echo "<p class=\"mycards\" style=\"margin-top:8em;\">your cards are: <br />\n";
340 foreach($mycards as $card)
341 display_card($card,$PREF["cardset"]);
344 check_for_sickness($me,$mycards);
346 /* move on to the next stage*/
347 DB_set_hand_status_by_hash($me,'check');
353 /* ok, user is in the game, saw his cards and selected his vorbehalt
354 * so first we check what he selected
356 echo "Processing what you selected in the last step...<br />";
358 if(!myisset("solo","wedding","poverty","nines") )
360 /* all these variables have a pre-selected default,
361 * so we should never get here,
362 * unless a user tries to cheat ;) */
363 echo "something went wrong...please contact the admin.";
367 /* check if this sickness needs to be handled first */
368 $gametype = DB_get_gametype_by_gameid($gameid);
369 $startplayer = DB_get_startplayer_by_gameid($gameid);
371 if( $_REQUEST["solo"]!="No")
373 /* user wants to play a solo */
375 /* store the info in the user's hand info */
376 DB_set_solo_by_hash($me,$_REQUEST["solo"]);
377 DB_set_sickness_by_hash($me,"solo");
379 echo "<br />Seems like you want to play a ".$_REQUEST["solo"]." solo. Got it.<br />\n";
381 if($gametype == "solo" && $startplayer<$mypos)
382 {}/* do nothing, since someone else already is playing solo */
385 /* this solo comes first
386 * store info in game table
388 DB_set_gametype_by_gameid($gameid,"solo");
389 DB_set_startplayer_by_gameid($gameid,$mypos);
390 DB_set_solo_by_gameid($gameid,$_REQUEST["solo"]);
393 else if($_REQUEST["wedding"] == "yes")
395 /* TODO: add silent solo somewhere*/
396 echo "Ok, you don't want to play a silent solo...wedding was chosen.<br />\n";
397 DB_set_sickness_by_hash($me,"wedding");
399 else if($_REQUEST["poverty"] == "yes")
401 echo "Don't think you can win with just a few trump...? ok, poverty chosen <br />\n";
402 DB_set_sickness_by_hash($me,"poverty");
404 else if($_REQUEST["nines"] == "yes")
406 echo "What? You just don't want to play a game because you have a few nines? Well, if no one".
407 " is playing solo, this game will be canceled.<br />\n";
408 DB_set_sickness_by_hash($me,"nines");
412 echo " Ok, done with checking, please go to the <a href=\"$host?me=$me\">next step of the setup</a>.<br />";
414 /* move on to the next stage*/
415 DB_set_hand_status_by_hash($me,'poverty');
419 /* here we need to check if there is a solo or some other form of sickness.
420 * If so, which one is the most important one
421 * set that one in the Game table
422 * tell people about it.
424 echo "<br /> Checking if someone else selected solo, nines or wedding or poverty.<br />";
426 /* check if everyone has reached this stage */
427 $userids = DB_get_all_userid_by_gameid($gameid);
429 foreach($userids as $user)
431 $userstat = DB_get_hand_status_by_userid_and_gameid($user,$gameid);
432 if($userstat!='poverty' && $userstat!='play')
438 echo "This step can only be handled after everyone finished the last step. ".
439 "Seems like this is not the case, so you need to wait a bit... please check back later....<br />";
443 echo "Everyone has finished checking their cards, let's see what they said...<br />";
445 /* check what kind of game we are playing, in case there are any solos this already
446 *will have the correct information in it */
447 $gametype = DB_get_gametype_by_gameid($gameid);
448 $startplayer = DB_get_startplayer_by_gameid($gameid);
450 /* check for different sickness and just output a general info */
457 foreach($userids as $user)
459 $name = DB_get_name_by_userid($user);
460 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
461 if($usersick == 'nines')
464 echo "$name has a Vorbehalt. <br />";
467 else if($usersick == 'poverty')
470 echo "$name has a Vorbehalt. <br />";
472 else if($usersick == 'wedding')
475 echo "$name has a Vorbehalt. <br />" ;
477 else if($usersick == 'solo')
480 echo "$name has a Vorbehalt. <br />" ;
484 /* now check which sickness comes first and set the gametype to it */
486 /* gamestatus == normal, => cancel game */
487 if($gametype == "solo")
494 /* TODO: should we keep statistics of this? */
495 $message = "Hello, \n\n".
496 "the game has been canceled because ".DB_get_name_by_userid($nines).
497 " has five or more nines and nobody is playing solo.\n";
499 /* TODO: add info about redeal in case this is a game of a series */
501 $userids = DB_get_all_userid_by_gameid($gameid);
502 foreach($userids as $user)
504 $To = DB_get_email_by_userid($user);
505 mymail($To,$EmailName."game canceled",$message);
508 /* delete everything from the dB */
511 echo "The game has been canceled because ".DB_get_name_by_userid($nines).
512 " has five or more nines and nobody is playing solo.\n";
518 DB_set_gametype_by_gameid($gameid,"poverty");
519 $gametype = "poverty";
520 $who=DB_get_sickness_by_gameid($gameid);
523 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
524 if($firstsick == "poverty")
525 DB_set_sickness_by_gameid($gameid,2); /* who needs to be asked first */
527 DB_set_sickness_by_gameid($gameid,1); /* who needs to be asked first */
532 DB_set_gametype_by_gameid($gameid,"dpoverty");
533 $gametype = "dpoverty";
534 $who=DB_get_sickness_by_gameid($gameid);
537 $firstsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
538 if($firstsick == "poverty")
540 $seconsick = DB_get_sickness_by_pos_and_gameid(1,$gameid);
541 if($secondsick == "poverty")
542 DB_set_sickness_by_gameid($gameid,30); /* who needs to be asked first */
544 DB_set_sickness_by_gameid($gameid,20); /* who needs to be asked first */
547 DB_set_sickness_by_gameid($gameid,10); /* who needs to be asked first */
552 DB_set_gametype_by_gameid($gameid,"wedding");
553 $gametype = "wedding";
558 /* now the gametype is set correctly (shouldn't matter that this is calculated for every user)
559 * output what kind of game we have */
562 foreach($userids as $user)
564 /* userids are sorted by position...
565 * so output whatever the firstone has, then whatever the next one has
566 * stop when the sickness is the same as the gametype
569 $name = DB_get_name_by_userid($user);
570 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
573 echo "$name has $usersick. <br />"; /*TODO: perhaps save this in a string and store in Game? */
575 if($usersick=="poverty")
577 if($usersick == "wedding" && $gametype =="wedding")
579 if($usersick == "poverty" && $gametype =="poverty")
581 if($usersick == "poverty" && $gametype =="dpoverty" && $poverty==2)
583 if($usersick == "solo" && $gametype =="solo")
587 /* output Schweinchen in case the rules need it */
588 if( $gametype != "solo")
589 if($GAME["schweinchen"] && $RULES["schweinchen"]=="both" )
590 echo DB_get_name_by_hash($GAME["schweinchen-who"])." has Schweinchen. <br />";
594 /* finished the setup, set re/contra parties if possible, go to next stage unless there is a case of poverty*/
598 /* are we the solo player? set us to re, else set us to contra */
599 $pos = DB_get_pos_by_hash($me);
600 if($pos == $startplayer)
601 DB_set_party_by_hash($me,"re");
603 DB_set_party_by_hash($me,"contra");
604 DB_set_hand_status_by_hash($me,'play');
608 /* set person with the wedding to re, do the rest during the game */
609 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
610 if($usersick == "wedding")
611 DB_set_party_by_hash($me,"re");
613 echo "Don't know who will be Re and Contra, you need to ".
614 "figure that out at the end of the game yourself <br />\n";
615 DB_set_hand_status_by_hash($me,'play');
619 $hand = DB_get_all_hand($me);
621 if(in_array('3',$hand)||in_array('4',$hand))
622 DB_set_party_by_hash($me,"re");
624 DB_set_party_by_hash($me,"contra");
625 DB_set_hand_status_by_hash($me,'play');
629 /* check if poverty resolved (e.g. DB.Game who set to NULL)
630 * yes? =>trump was taken, start game; break;
632 $who=DB_get_sickness_by_gameid($gameid);
634 { /* trump has been taken */
635 DB_set_hand_status_by_hash($me,'play');
639 if($who>9) /*= two people still have trump on the table*/
644 /* check if we are being asked now
645 * no, display wait message, e.g. player X is asked at the moment
647 $usersick = DB_get_sickness_by_userid_and_gameid($myid,$gameid);
648 if(myisset("trump") && $_REQUEST["trump"]=="no" && ($who==$mypos || $who==$mypos*10))
650 /* user doesn't want to take trump */
651 /* set next player who needs to be asked */
652 $firstsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
653 $secondsick = (string) DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
655 if($firstsick=="poverty")
657 if($secondsick=="poverty")
658 DB_set_sickness_by_gameid($gameid,$who+$add*3);
660 DB_set_sickness_by_gameid($gameid,$who+$add*2);
663 DB_set_sickness_by_gameid($gameid,$who+$add);
665 /* this user is done */
666 DB_set_hand_status_by_hash($me,'play');
669 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
671 /* user wants to take trump */
672 $trump = $_REQUEST["trump"];
674 /* get hand id for user $trump */
675 $userhand=DB_get_handid_by_gameid_and_userid($gameid,$trump);
676 /* copy trump from player A to B */
677 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
679 /* set re/contra, if it is not already set */
680 $party = DB_get_party_by_hash($me);
683 foreach($userids as $user)
685 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
686 if($user == $trump || $user == $myid)
687 DB_set_party_by_hash($hash,"re");
689 DB_set_party_by_hash($hash,"contra");
692 /* add hidden button with trump in it to get to the next point */
693 echo "<form action=\"index.php\" method=\"post\">\n";
694 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
695 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
696 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
697 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
700 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
702 $trump = $_REQUEST["trump"];
703 $exchange = $_REQUEST["exchange"];
704 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
706 /* if exchange is set to a value>0, exchange that card back to user $trump */
709 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
710 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
713 /* if number of cards == 12, set status to play for both users */
714 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
715 $r = mysql_fetch_array($result,MYSQL_NUM);
718 die("error in poverty");
722 if($gametype=="poverty" || $who<9)
724 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
726 else /* reduce poverty count by one, that is go to single digits $who */
731 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
732 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
733 if($firstsick!="poverty")
734 DB_set_sickness_by_gameid($gameid,$who+$add);
737 if($secondsick!="poverty")
738 DB_set_sickness_by_gameid($gameid,$who+$add*2);
740 DB_set_sickness_by_gameid($gameid,$who+$add*3);
744 /* this user is done */
745 DB_set_hand_status_by_hash($me,'play');
746 /* and so is his partner */
747 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
748 DB_set_hand_status_by_hash($hash,'play');
754 /* else show all trump, have lowest card pre-selected, have hidden setting for */
755 echo "you need to get rid of a few cards<br />\n";
757 set_gametype($gametype); /* this sets the $CARDS variable */
758 $mycards = DB_get_hand($me);
759 $mycards = mysort($mycards,$gametype);
761 echo "<form class=\"exchange\" action=\"index.php\" method=\"post\">\n";
763 foreach($mycards as $card)
764 display_link_card($card,$PREF["cardset"],$type);
765 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
766 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
767 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
771 else if($who == $mypos || $who == $mypos*10)
773 foreach($userids as $user)
775 $name = DB_get_name_by_userid($user);
776 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
778 if($usersick=="poverty")
780 $hash =DB_get_hash_from_gameid_and_userid($gameid,$user);
781 $cards=DB_get_hand($hash);
782 $nrtrump=count_trump($cards);
785 echo "Player $name has $nrtrump trump. Do you want to take them?".
786 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />";
789 echo "I don't want to take any trump: ".
790 "<a href=\"index.php?me=$me&trump=no\">yes</a> <br />";
794 echo "it's not your turn yet to decide if you want to take the trump or not.";
797 * yes, display number of trump and user's hand, ask if he wants to take it
798 * no, set whom-to-ask to next player, email next player, cancle game if no next player
799 * yes -> link to new page:display all cards, ask for N return cards
805 /* check if noone wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
806 $who=DB_get_sickness_by_gameid($gameid);
807 if($who==5 || $who==50)
809 $message = "Hello, \n\n".
810 "Game $gameid has been cancled since nobody wanted to take the trump.\n";
812 $userids = DB_get_all_userid_by_gameid($gameid);
813 foreach($userids as $user)
815 $To = DB_get_email_by_userid($user);
816 mymail($To,$EmailName."game cancled (poverty not resolved)",$message);
819 /* delete everything from the dB */
822 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
827 /* check if all players are ready to play */
829 foreach($userids as $user)
830 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
835 /* only set this after all poverty, etc. are handled*/
836 DB_set_game_status_by_gameid($gameid,'play');
838 /* email startplayer */
839 $startplayer = DB_get_startplayer_by_gameid($gameid);
840 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
841 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
845 /* email startplayer) */
846 $message = "It's your turn now.\n".
847 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
848 mymail($email,$EmailName."ready, set, go... ",$message);
851 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
854 echo "You finished the setup, once everyone else has done the same you'll get an email when it is your turn..<br />";
859 /* both entries here, so that the tricks are visible for both.
860 * in case of 'play' there is a break later that skips the last part
863 /* figure out what kind of game we are playing,
864 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
865 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
869 $gametype = DB_get_gametype_by_gameid($gameid);
871 if($gametype=="solo")
873 $gametype = DB_get_solo_by_gameid($gameid);
874 $GT = $gametype." ".$GT;
879 set_gametype($gametype); /* this sets the $CARDS variable */
881 /* get some infos about the game */
882 $gamestatus = DB_get_game_status_by_gameid($gameid);
884 /* display useful things in divs */
886 /* display links to the users status page */
887 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
888 $r = mysql_fetch_array($result,MYSQL_NUM);
890 display_links($r[0],$r[1]);
892 /* end display useful things*/
894 /* has the game started? No, then just wait here...*/
895 if($gamestatus == 'pre')
897 echo "You finished the setup, but not everyone else finished it...so you need to wait for the others... <br />";
898 break; /* not sure this works... the idea is that you can
899 * only play a card after everyone is ready to play */
902 /* display the table and the names */
903 $result = mysql_query("SELECT User.fullname as name,".
904 " Hand.position as position, ".
907 "LEFT JOIN User ON User.id=Hand.user_id ".
908 "WHERE Hand.game_id='".$gameid."' ".
909 "ORDER BY position ASC");
911 echo "<div class=\"table\">\n".
912 " <img src=\"pics/table.png\" alt=\"table\" />\n";
913 while($r = mysql_fetch_array($result,MYSQL_NUM))
919 $offset = DB_get_user_timezone($user);
920 $zone = return_timezone($offset);
921 date_default_timezone_set($zone);
923 echo " <span class=\"table".($pos-1)."\">\n";
924 echo " $name <br />\n";
925 echo " local time: ".date("Y-m-d H:i:s")."\n";
931 /* get everything relevant to display the tricks */
932 $result = mysql_query("SELECT Hand_Card.card_id as card,".
933 " Hand.position as position,".
934 " Play.sequence as sequence, ".
938 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
939 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
940 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
941 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
942 "WHERE Trick.game_id='".$gameid."' ".
943 "ORDER BY Trick.id,sequence ASC");
946 $lasttrick = DB_get_max_trickid($gameid);
948 $play = array(); /* needed to calculate winner later */
950 $pos = DB_get_startplayer_by_gameid($gameid)-1;
951 $firstcard = ""; /* first card in a trick */
953 echo "\n<ul class=\"tricks\">\n";
954 echo " <li class=\"nohighlight\"> Game $gameid: </li>\n";
956 while($r = mysql_fetch_array($result,MYSQL_NUM))
963 /* check if first schweinchen has been played */
964 if($r[0] == 19 || $r[0] == 20 )
965 $GAME["schweinchen"]++;
967 /* save card to be able to find the winner of the trick later */
968 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
972 /* first card in a trick, output some html */
973 if($trick!=$lasttrick)
975 /* start of an old trick? */
976 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
977 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
978 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
980 else if($trick==$lasttrick)
982 /* start of a last trick? */
983 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
984 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
985 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
988 /* remember first card, so that we are able to check, what cards can be played */
993 echo " <div class=\"card".($pos-1)."\">\n";
995 /* display comments */
997 echo " <span class=\"comment\">".$comment."</span>\n";
1000 display_card($r[0],$PREF["cardset"]);
1002 echo " </div>\n"; /* end div card */
1008 echo " </div>\n </li>\n"; /* end div table, end li table */
1012 if($seq!=4 && $trickNR>1)
1013 echo " </div>\n </li>\n"; /* end div table, end li table */
1017 /* whos turn is it? */
1020 $winner = get_winner($play,$gametype); /* returns the position */
1022 $firstcard = ""; /* new trick, no first card */
1027 if($next==5) $next=1;
1030 /* my turn?, display cards as links, ask for comments*/
1031 if(DB_get_pos_by_hash($me) == $next)
1036 /* do we want to play a card? */
1037 if(myisset("card") && $myturn)
1039 $card = $_REQUEST["card"];
1040 $handid = DB_get_handid_by_hash($me);
1042 /* check if we have card and that we haven't played it yet*/
1043 /* set played in hand_card to true where hand_id and card_id*/
1044 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1045 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1046 $r = mysql_fetch_array($result,MYSQL_NUM);
1047 $handcardid = $r[0];
1053 /* mark card as played */
1054 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1055 DB_quote_smart($card));
1056 /* update Game timestamp */
1057 DB_update_game_timestamp($gameid);
1059 /* check for schweinchen */
1060 //echo "schweinchen = ".$GAME["schweinchen"]." --$card-<br />";
1061 if($card == 19 || $card == 20 )
1063 $GAME["schweinchen"]++;
1064 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1065 $comment="Schweinchen! ";
1066 if($RULES["schweinchen"]=="both" )
1067 $comment="Schweinchen! ";
1068 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1071 /* get trick id or start new trick */
1072 $a = DB_get_current_trickid($gameid);
1076 $playid = DB_play_card($trickid,$handcardid,$sequence);
1078 /* check for coment */
1079 if(myisset("comment"))
1081 $comment.=$_REQUEST["comment"];
1084 DB_insert_comment($comment,$playid,$myid);
1086 /* display played card */
1087 echo "<div class=\"card\">";
1088 echo " you played <br />";
1089 display_card($card,$PREF["cardset"]);
1092 /*check if we still have cards left, else set status to gameover */
1093 if(sizeof(DB_get_hand($me))==0)
1095 DB_set_hand_status_by_hash($me,'gameover');
1096 $mystatus='gameover';
1099 /* if all players are done, set game status to game over,
1100 * get the points of the last trick and send out an email
1103 $userids = DB_get_all_userid_by_gameid($gameid);
1106 foreach($userids as $user)
1107 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1112 DB_set_game_status_by_gameid($gameid,"gameover");
1113 /* get score for last trick
1114 * all other tricks are handled a few lines further down*/
1115 $play = DB_get_cards_by_trick($trickid);
1116 $winner = get_winner($play,$gametype); /* returns the position */
1117 /* get points of last trick and save it */
1119 foreach($play as $card)
1120 $points = $points + card_value($card["card"]);
1121 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1123 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1125 echo "ERROR during scoring";
1127 /* email all players */
1128 /* individual score */
1129 $result = mysql_query("SELECT fullname, SUM(score), Hand.party FROM Score".
1130 " LEFT JOIN Hand ON Hand.id=hand_id".
1131 " LEFT JOIN User ON Hand.user_id=User.id".
1132 " WHERE Hand.game_id=$gameid".
1133 " GROUP BY fullname" );
1134 $message = "The game is over. Thanks for playing :)\n";
1135 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1136 $message .= " FINAL SCORE: ".$r[0]."(".$r[2].") ".$r[1]."\n";
1137 $message .= "\nIf your not in the list above your score is zero...\n\n";
1139 $result = mysql_query("SELECT Hand.party, SUM(score) FROM Score".
1140 " LEFT JOIN Hand ON Hand.id=hand_id".
1141 " LEFT JOIN User ON Hand.user_id=User.id".
1142 " WHERE Hand.game_id=$gameid".
1143 " GROUP BY Hand.party" );
1145 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1146 $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
1148 foreach($userids as $user)
1150 $To = DB_get_email_by_userid($user);
1151 mymail($To,$EmailName."game over",$message);
1156 /* email next player */
1157 if(DB_get_game_status_by_gameid($gameid)=='play')
1161 $play = DB_get_cards_by_trick($trickid);
1162 $winner = get_winner($play,$gametype); /* returns the position */
1164 /* get points of last trick and save it, last trick is handled
1165 * a few lines further up */
1167 foreach($play as $card)
1168 $points = $points + card_value($card["card"]);
1170 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1172 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1174 echo "ERROR during scoring";
1177 echo "DEBUG: $winner got $points <br />";
1179 /* who is the next player? */
1184 $next = DB_get_pos_by_hash($me)+1;
1186 if($next==5) $next=1;
1188 /* email next player */
1189 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1190 $email = DB_get_email_by_hash($next_hash);
1192 $message = "It's your turn now.\n".
1193 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1194 mymail($email,$EmailName."a card has been played",$message);
1197 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
1198 "\"> next player </a> <br />\n";
1204 echo "can't find that card?! <br />\n";
1207 else if(myisset("card") && !$myturn )
1209 echo "please wait until it's your turn! <br />\n";
1212 $mycards = DB_get_hand($me);
1213 $mycards = mysort($mycards,$gametype);
1214 echo "<div class=\"mycards\">\n";
1216 if($myturn && !myisset("card"))
1218 echo "Hello ".$myname.", it's your turn! <br />\n";
1219 echo "Your cards are: <br />\n";
1220 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
1222 /* do we have to follow suite? */
1224 if(have_suit($mycards,$firstcard))
1227 foreach($mycards as $card)
1229 if($followsuit && !same_type($card,$firstcard))
1230 display_card($card,$PREF["cardset"]);
1232 display_link_card($card,$PREF["cardset"]);
1235 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
1236 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
1237 echo "<input type=\"submit\" value=\"move\" />\n";
1240 else if($mystatus=='play')
1242 echo "Your cards are: <br />\n";
1243 foreach($mycards as $card)
1244 display_card($card,$PREF["cardset"]);
1246 else if($mystatus=='gameover')
1248 $oldcards = DB_get_all_hand($me);
1249 $oldcards = mysort($oldcards,$gametype);
1250 echo "Your cards were: <br />\n";
1251 foreach($oldcards as $card)
1252 display_card($card,$PREF["cardset"]);
1256 /* check if we need to set status to 'gameover' is done during playing of the card */
1257 if($mystatus=='play')
1259 /* the following happens only when the gamestatus is 'gameover' */
1260 /* check if game is over, display results */
1261 if(DB_get_game_status_by_gameid($gameid)=='play')
1263 echo "the game is over for you.. other people still need to play though";
1267 echo "the game is over now...<br />\n";
1269 $result = mysql_query("SELECT fullname, SUM(score), Hand.party FROM Score".
1270 " LEFT JOIN Hand ON Hand.id=hand_id".
1271 " LEFT JOIN User ON Hand.user_id=User.id".
1272 " WHERE Hand.game_id=$gameid".
1273 " GROUP BY fullname" );
1274 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1275 echo " FINAL SCORE: ".$r[0]."(".$r[2].") ".$r[1]."<br />";
1277 $result = mysql_query("SELECT Hand.party, SUM(score) FROM Score".
1278 " LEFT JOIN Hand ON Hand.id=hand_id".
1279 " LEFT JOIN User ON Hand.user_id=User.id".
1280 " WHERE Hand.game_id=$gameid".
1281 " GROUP BY Hand.party" );
1282 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1283 echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />\n";
1286 $session = DB_get_session_by_gameid($gameid);
1287 $result = mysql_query("SELECT id,create_date FROM Game".
1288 " WHERE session=$session".
1289 " ORDER BY create_date DESC".
1293 $r = mysql_fetch_array($result,MYSQL_NUM);
1295 if(!$session || $gameid==$r[0])
1297 /* suggest a new game with the same people in it, just rotated once */
1298 $names = DB_get_all_names_by_gameid($gameid);
1299 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1304 echo "error in testing the status";
1309 /* user status page */
1310 else if(myisset("email","password"))
1312 /* test id and password, should really be done in one step */
1313 $email = $_REQUEST["email"];
1314 $password = $_REQUEST["password"];
1316 if(myisset("forgot"))
1320 $uid = DB_get_userid_by_email($email);
1326 echo "Hmm, you forgot your passwort...nothing I can do at the moment:( ";
1327 echo " you need to email Arun for now... in the future it will be all automated and an ";
1328 echo "email with a new password will go to $email.";
1333 echo "you need to give me an email address!";
1335 echo "couldn't find a player with this email, please contact Arun, if you think this is a mistake";
1340 /* verify password and email */
1341 if(strlen($password)!=32)
1342 $password = md5($password);
1345 $uid = DB_get_userid_by_email_and_password($email,$password);
1351 if(myisset("setpref"))
1353 $setpref=$_REQUEST["setpref"];
1357 case "englishcards":
1358 $result = mysql_query("SELECT * from User_Prefs".
1359 " WHERE user_id='$uid' AND pref_key='cardset'" );
1360 if( mysql_fetch_array($result,MYSQL_NUM))
1361 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1362 " WHERE user_id='$uid' AND pref_key='cardset'" );
1364 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','cardset',".DB_quote_smart($setpref).")");
1365 echo "Ok, changed you preferences for the cards.\n";
1369 else /* output default user page */
1371 $time = DB_get_user_timestamp($uid);
1372 $unixtime =strtotime($time);
1374 $offset = DB_get_user_timezone($uid);
1375 $zone = return_timezone($offset);
1376 date_default_timezone_set($zone);
1378 /* display links to settings */
1379 output_user_settings($email,$password);
1381 echo "last login: ".date("r",$unixtime)."<br />";
1383 DB_update_user_timestamp($uid);
1385 echo "<p>these are the games you are playing in:<br />\n";
1386 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date from Hand".
1387 " LEFT JOIN Game On Hand.game_id=Game.id".
1388 " WHERE Hand.user_id='$uid' AND Game.status<>'gameover'" );
1389 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1391 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1392 if(time()-strtotime($r[2]) > 60*60*24*30)
1393 echo " The game has been running for over a month.".
1394 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1395 " (clicking here is final and can't be restored)";
1401 echo "<p>and these are your games that are already done:<br />Game: \n";
1402 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
1403 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1404 echo "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>, ";
1407 $names = DB_get_all_names();
1408 echo "<p>registered players:<br />\n";
1409 foreach ($names as $name)
1413 echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1418 echo "sorry email and password don't match <br />";
1424 /* page for registration */
1425 else if(myisset("register") )
1429 /* new user wants to register */
1430 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
1433 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
1435 echo "please chose another name<br />";
1438 if(DB_get_userid_by_email($_REQUEST["Remail"]))
1440 echo "this email address is already used ?!<br />";
1445 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
1446 ",".DB_quote_smart($_REQUEST["Remail"]).
1447 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
1448 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
1451 echo " added you to the database";
1453 echo " something went wrong";
1456 /* default login page */
1459 $pre=0;$game=0;$done=0;
1460 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1462 $pre = mysql_fetch_array($r,MYSQL_NUM);
1463 $game = mysql_fetch_array($r,MYSQL_NUM);
1464 $done = mysql_fetch_array($r,MYSQL_NUM);
1466 output_home_page($pre[0],$game[0],$done[0]);