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 "you need to answer the <a href=\"$host?me=$me\">question</a>.";
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 = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
653 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
654 if($firstsick!="poverty")
655 DB_set_sickness_by_gameid($gameid,$who+$add);
658 if($secondsick!="poverty")
659 DB_set_sickness_by_gameid($gameid,$who+$add*2);
661 DB_set_sickness_by_gameid($gameid,$who+$add*3);
664 /* this user is done */
665 DB_set_hand_status_by_hash($me,'play');
668 else if(myisset("trump") && !myisset("exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
670 /* user wants to take trump */
671 $trump = $_REQUEST["trump"];
673 /* get hand id for user $trump */
674 $userhand=DB_get_handid_by_gameid_and_userid($gameid,$trump);
675 /* copy trump from player A to B */
676 $result = mysql_query("UPDATE Hand_Card SET hand_id='$myhand' WHERE hand_id='$userhand' AND card_id<'27'" );
678 /* set re/contra, if it is not already set */
679 $party = DB_get_party_by_hash($me);
682 foreach($userids as $user)
684 $hash = DB_get_hash_from_gameid_and_userid($gameid,$user);
685 if($user == $trump || $user == $myid)
686 DB_set_party_by_hash($hash,"re");
688 DB_set_party_by_hash($hash,"contra");
691 /* add hidden button with trump in it to get to the next point */
692 echo "<form action=\"index.php\" method=\"post\">\n";
693 echo " <input type=\"hidden\" name=\"exchange\" value=\"-1\" />\n";
694 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
695 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
696 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select cards to give back\" />\n";
699 else if(myisset("trump","exchange") && $_REQUEST["trump"]>0 && ($who==$mypos || $who==$mypos*10))
701 $trump = $_REQUEST["trump"];
702 $exchange = $_REQUEST["exchange"];
703 $userhand = DB_get_handid_by_gameid_and_userid($gameid,$trump);
705 echo "you give $exchange to your partner";
707 /* if exchange is set to a value>0, exchange that card back to user $trump */
710 $result = mysql_query("UPDATE Hand_Card SET hand_id='$userhand'".
711 " WHERE hand_id='$myhand' AND card_id='$exchange'" );
714 /* if number of cards == 12, set status to play for both users */
715 $result = mysql_query("SELECT COUNT(*) FROM Hand_Card WHERE hand_id='$myhand'" );
716 $r = mysql_fetch_array($result,MYSQL_NUM);
719 die("error in poverty");
723 if($gametype=="poverty" || $who<9)
725 DB_set_sickness_by_gameid($gameid,-1); /* done with poverty */
727 else /* reduce poverty count by one, that is go to single digits $who */
732 $firstsick = DB_get_sickness_by_pos_and_gameid($mypos+1,$gameid);
733 $secondsick = DB_get_sickness_by_pos_and_gameid($mypos+2,$gameid);
734 if($firstsick!="poverty")
735 DB_set_sickness_by_gameid($gameid,$who+$add);
738 if($secondsick!="poverty")
739 DB_set_sickness_by_gameid($gameid,$who+$add*2);
741 DB_set_sickness_by_gameid($gameid,$who+$add*3);
745 /* this user is done */
746 DB_set_hand_status_by_hash($me,'play');
747 /* and so is his partner */
748 $hash = DB_get_hash_from_gameid_and_userid($gameid,$trump);
749 DB_set_hand_status_by_hash($hash,'play');
755 /* else show all trump, have lowest card pre-selected, have hidden setting for */
756 echo "you need to get rid of a few cards<br />\n";
758 set_gametype($gametype); /* this sets the $CARDS variable */
759 $mycards = DB_get_hand($me);
760 $mycards = mysort($mycards,$gametype);
762 echo "<form class=\"exchange\" action=\"index.php\" method=\"post\">\n";
764 foreach($mycards as $card)
765 display_link_card($card,$PREF["cardset"],$type);
766 echo " <input type=\"hidden\" name=\"trump\" value=\"".$trump."\" />\n";
767 echo " <input type=\"hidden\" name=\"me\" value=\"".$me."\" />\n";
768 echo " <input type=\"submit\" class=\"submitbutton\" value=\"select one card to give back\" />\n";
772 else if($who == $mypos || $who == $mypos*10)
774 foreach($userids as $user)
776 $name = DB_get_name_by_userid($user);
777 $usersick = DB_get_sickness_by_userid_and_gameid($user,$gameid);
779 if($usersick=="poverty")
781 $hash =DB_get_hash_from_gameid_and_userid($gameid,$user);
782 $cards=DB_get_hand($hash);
783 $nrtrump=count_trump($cards);
786 echo "Player $name has $nrtrump trump. Do you want to take them?".
787 "<a href=\"index.php?me=$me&trump=$user\">yes</a> <br />";
790 echo "I don't want to take any trump: ".
791 "<a href=\"index.php?me=$me&trump=no\">yes</a> <br />";
795 echo "it's not your turn yet to decide if you want to take the trump or not.";
798 * yes, display number of trump and user's hand, ask if he wants to take it
799 * no, set whom-to-ask to next player, email next player, cancle game if no next player
800 * yes -> link to new page:display all cards, ask for N return cards
806 /* check if noone wanted to take trump, in that case the gamesickness would be set to 5 or 50 */
807 $who=DB_get_sickness_by_gameid($gameid);
808 if($who==5 || $who==50)
810 $message = "Hello, \n\n".
811 "Game $gameid has been cancled since nobody wanted to take the trump.\n";
813 $userids = DB_get_all_userid_by_gameid($gameid);
814 foreach($userids as $user)
816 $To = DB_get_email_by_userid($user);
817 mymail($To,$EmailName."game cancled (poverty not resolved)",$message);
820 /* delete everything from the dB */
823 echo "<p style=\"background-color:red\";>Game $gameid has been cancled.<br /><br /></p>";
828 /* check if all players are ready to play */
830 foreach($userids as $user)
831 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='play')
836 /* only set this after all poverty, etc. are handled*/
837 DB_set_game_status_by_gameid($gameid,'play');
839 /* email startplayer */
840 $startplayer = DB_get_startplayer_by_gameid($gameid);
841 $email = DB_get_email_by_pos_and_gameid($startplayer,$gameid);
842 $hash = DB_get_hash_from_game_and_pos($gameid,$startplayer);
846 /* email startplayer) */
847 $message = "It's your turn now.\n".
848 "Use this link to play a card: ".$host."?me=".$hash."\n\n" ;
849 mymail($email,$EmailName."ready, set, go... ",$message);
852 echo " Please, <a href=\"$host?me=$me\">start</a> the game.<br />";
855 echo "You finished the setup, once everyone else has done the same you'll get an email when it is your turn..<br />";
860 /* both entries here, so that the tricks are visible for both.
861 * in case of 'play' there is a break later that skips the last part
864 /* figure out what kind of game we are playing,
865 * set the global variables $CARDS["trump"],$CARDS["diamonds"],$CARDS["hearts"],
866 * $CARDS["clubs"],$CARDS["spades"],$CARDS["foxes"]
870 $gametype = DB_get_gametype_by_gameid($gameid);
872 if($gametype=="solo")
874 $gametype = DB_get_solo_by_gameid($gameid);
875 $GT = $gametype." ".$GT;
880 set_gametype($gametype); /* this sets the $CARDS variable */
882 /* get some infos about the game */
883 $gamestatus = DB_get_game_status_by_gameid($gameid);
885 /* display useful things in divs */
887 /* display links to the users status page */
888 $result = mysql_query("SELECT email,password from User WHERE id='$myid'" );
889 $r = mysql_fetch_array($result,MYSQL_NUM);
891 display_links($r[0],$r[1]);
893 /* end display useful things*/
895 /* has the game started? No, then just wait here...*/
896 if($gamestatus == 'pre')
898 echo "You finished the setup, but not everyone else finished it...so you need to wait for the others... <br />";
899 break; /* not sure this works... the idea is that you can
900 * only play a card after everyone is ready to play */
903 /* display the table and the names */
904 $result = mysql_query("SELECT User.fullname as name,".
905 " Hand.position as position, ".
908 "LEFT JOIN User ON User.id=Hand.user_id ".
909 "WHERE Hand.game_id='".$gameid."' ".
910 "ORDER BY position ASC");
912 echo "<div class=\"table\">\n".
913 " <img src=\"pics/table.png\" alt=\"table\" />\n";
914 while($r = mysql_fetch_array($result,MYSQL_NUM))
920 $offset = DB_get_user_timezone($user);
921 $zone = return_timezone($offset);
922 date_default_timezone_set($zone);
924 echo " <span class=\"table".($pos-1)."\">\n";
925 echo " $name <br />\n";
926 echo " local time: ".date("Y-m-d H:i:s")."\n";
932 /* get everything relevant to display the tricks */
933 $result = mysql_query("SELECT Hand_Card.card_id as card,".
934 " Hand.position as position,".
935 " Play.sequence as sequence, ".
939 "LEFT JOIN Play ON Trick.id=Play.trick_id ".
940 "LEFT JOIN Hand_Card ON Play.hand_card_id=Hand_Card.id ".
941 "LEFT JOIN Hand ON Hand_Card.hand_id=Hand.id ".
942 "LEFT JOIN Comment ON Play.id=Comment.play_id ".
943 "WHERE Trick.game_id='".$gameid."' ".
944 "ORDER BY Trick.id,sequence ASC");
947 $lasttrick = DB_get_max_trickid($gameid);
949 $play = array(); /* needed to calculate winner later */
951 $pos = DB_get_startplayer_by_gameid($gameid)-1;
952 $firstcard = ""; /* first card in a trick */
954 echo "\n<ul class=\"tricks\">\n";
955 echo " <li class=\"nohighlight\"> Game $gameid: </li>\n";
957 while($r = mysql_fetch_array($result,MYSQL_NUM))
964 /* check if first schweinchen has been played */
965 if($r[0] == 19 || $r[0] == 20 )
966 $GAME["schweinchen"]++;
968 /* save card to be able to find the winner of the trick later */
969 $play[$seq] = array("card"=>$r[0],"pos"=>$pos);
973 /* first card in a trick, output some html */
974 if($trick!=$lasttrick)
976 /* start of an old trick? */
977 echo " <li onclick=\"hl('$trickNR');\" class=\"old\"><a href=\"#\">Trick $trickNR</a>\n".
978 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
979 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
981 else if($trick==$lasttrick)
983 /* start of a last trick? */
984 echo " <li onclick=\"hl('$trickNR');\" class=\"current\"><a href=\"#\">Trick $trickNR</a>\n".
985 " <div class=\"trick\" id=\"trick".$trickNR."\">\n".
986 " <img class=\"arrow\" src=\"pics/arrow".($pos-1).".png\" alt=\"table\" />\n";
989 /* remember first card, so that we are able to check, what cards can be played */
994 echo " <div class=\"card".($pos-1)."\">\n";
996 /* display comments */
998 echo " <span class=\"comment\">".$comment."</span>\n";
1001 display_card($r[0],$PREF["cardset"]);
1003 echo " </div>\n"; /* end div card */
1009 echo " </div>\n </li>\n"; /* end div table, end li table */
1013 if($seq!=4 && $trickNR>1)
1014 echo " </div>\n </li>\n"; /* end div table, end li table */
1018 /* whos turn is it? */
1021 $winner = get_winner($play,$gametype); /* returns the position */
1023 $firstcard = ""; /* new trick, no first card */
1028 if($next==5) $next=1;
1031 /* my turn?, display cards as links, ask for comments*/
1032 if(DB_get_pos_by_hash($me) == $next)
1037 /* do we want to play a card? */
1038 if(myisset("card") && $myturn)
1040 $card = $_REQUEST["card"];
1041 $handid = DB_get_handid_by_hash($me);
1043 /* check if we have card and that we haven't played it yet*/
1044 /* set played in hand_card to true where hand_id and card_id*/
1045 $result = mysql_query("SELECT id FROM Hand_Card WHERE played='false' and ".
1046 "hand_id='$handid' AND card_id=".DB_quote_smart($card));
1047 $r = mysql_fetch_array($result,MYSQL_NUM);
1048 $handcardid = $r[0];
1054 /* mark card as played */
1055 mysql_query("UPDATE Hand_Card SET played='true' WHERE hand_id='$handid' AND card_id=".
1056 DB_quote_smart($card));
1057 /* update Game timestamp */
1058 DB_update_game_timestamp($gameid);
1060 /* check for schweinchen */
1061 //echo "schweinchen = ".$GAME["schweinchen"]." --$card-<br />";
1062 if($card == 19 || $card == 20 )
1064 $GAME["schweinchen"]++;
1065 if($GAME["schweinchen"]==3 && $RULES["schweinchen"]=="second" )
1066 $comment="Schweinchen! ";
1067 if($RULES["schweinchen"]=="both" )
1068 $comment="Schweinchen! ";
1069 echo "schweinchen = ".$GAME["schweinchen"]." ---<br />";
1072 /* get trick id or start new trick */
1073 $a = DB_get_current_trickid($gameid);
1077 $playid = DB_play_card($trickid,$handcardid,$sequence);
1079 /* check for coment */
1080 if(myisset("comment"))
1082 $comment.=$_REQUEST["comment"];
1085 DB_insert_comment($comment,$playid,$myid);
1087 /* display played card */
1088 echo "<div class=\"card\">";
1089 echo " you played <br />";
1090 display_card($card,$PREF["cardset"]);
1093 /*check if we still have cards left, else set status to gameover */
1094 if(sizeof(DB_get_hand($me))==0)
1096 DB_set_hand_status_by_hash($me,'gameover');
1097 $mystatus='gameover';
1100 /* if all players are done, set game status to game over,
1101 * get the points of the last trick and send out an email
1104 $userids = DB_get_all_userid_by_gameid($gameid);
1107 foreach($userids as $user)
1108 if(DB_get_hand_status_by_userid_and_gameid($user,$gameid)!='gameover')
1113 DB_set_game_status_by_gameid($gameid,"gameover");
1114 /* get score for last trick
1115 * all other tricks are handled a few lines further down*/
1116 $play = DB_get_cards_by_trick($trickid);
1117 $winner = get_winner($play,$gametype); /* returns the position */
1118 /* get points of last trick and save it */
1120 foreach($play as $card)
1121 $points = $points + card_value($card["card"]);
1122 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1124 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1126 echo "ERROR during scoring";
1128 /* email all players */
1129 /* individual score */
1130 $result = mysql_query("SELECT fullname, SUM(score), Hand.party FROM Score".
1131 " LEFT JOIN Hand ON Hand.id=hand_id".
1132 " LEFT JOIN User ON Hand.user_id=User.id".
1133 " WHERE Hand.game_id=$gameid".
1134 " GROUP BY fullname" );
1135 $message = "The game is over. Thanks for playing :)\n";
1136 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1137 $message .= " FINAL SCORE: ".$r[0]."(".$r[2].") ".$r[1]."\n";
1138 $message .= "\nIf your not in the list above your score is zero...\n\n";
1140 $result = mysql_query("SELECT Hand.party, SUM(score) FROM Score".
1141 " LEFT JOIN Hand ON Hand.id=hand_id".
1142 " LEFT JOIN User ON Hand.user_id=User.id".
1143 " WHERE Hand.game_id=$gameid".
1144 " GROUP BY Hand.party" );
1146 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1147 $message .= " FINAL SCORE: ".$r[0]." ".$r[1]."\n";
1149 foreach($userids as $user)
1151 $To = DB_get_email_by_userid($user);
1152 mymail($To,$EmailName."game over",$message);
1157 /* email next player */
1158 if(DB_get_game_status_by_gameid($gameid)=='play')
1162 $play = DB_get_cards_by_trick($trickid);
1163 $winner = get_winner($play,$gametype); /* returns the position */
1165 /* get points of last trick and save it, last trick is handled
1166 * a few lines further up */
1168 foreach($play as $card)
1169 $points = $points + card_value($card["card"]);
1171 $winnerid = DB_get_handid_by_gameid_and_position($gameid,$winner);
1173 mysql_query("INSERT INTO Score VALUES (NULL, '$gameid', '$winnerid', '$points')");
1175 echo "ERROR during scoring";
1178 echo "DEBUG: $winner got $points <br />";
1180 /* who is the next player? */
1185 $next = DB_get_pos_by_hash($me)+1;
1187 if($next==5) $next=1;
1189 /* email next player */
1190 $next_hash = DB_get_hash_from_game_and_pos($gameid,$next);
1191 $email = DB_get_email_by_hash($next_hash);
1193 $message = "It's your turn now.\n".
1194 "Use this link to play a card: ".$host."?me=".$next_hash."\n\n" ;
1195 mymail($email,$EmailName."a card has been played",$message);
1198 echo "DEBUG:<a href=\"index.php?me=".DB_get_hash_from_game_and_pos($gameid,$next).
1199 "\"> next player </a> <br />\n";
1205 echo "can't find that card?! <br />\n";
1208 else if(myisset("card") && !$myturn )
1210 echo "please wait until it's your turn! <br />\n";
1213 $mycards = DB_get_hand($me);
1214 $mycards = mysort($mycards,$gametype);
1215 echo "<div class=\"mycards\">\n";
1217 if($myturn && !myisset("card"))
1219 echo "Hello ".$myname.", it's your turn! <br />\n";
1220 echo "Your cards are: <br />\n";
1221 echo "<form action=\"index.php?me=$me\" method=\"post\">\n";
1223 /* do we have to follow suite? */
1225 if(have_suit($mycards,$firstcard))
1228 foreach($mycards as $card)
1230 if($followsuit && !same_type($card,$firstcard))
1231 display_card($card,$PREF["cardset"]);
1233 display_link_card($card,$PREF["cardset"]);
1236 echo "<br />\nA short comments:<input name=\"comment\" type=\"text\" size=\"30\" maxlength=\"50\" />\n";
1237 echo "<input type=\"hidden\" name=\"me\" value=\"$me\" />\n";
1238 echo "<input type=\"submit\" value=\"move\" />\n";
1241 else if($mystatus=='play')
1243 echo "Your cards are: <br />\n";
1244 foreach($mycards as $card)
1245 display_card($card,$PREF["cardset"]);
1247 else if($mystatus=='gameover')
1249 $oldcards = DB_get_all_hand($me);
1250 $oldcards = mysort($oldcards,$gametype);
1251 echo "Your cards were: <br />\n";
1252 foreach($oldcards as $card)
1253 display_card($card,$PREF["cardset"]);
1257 /* check if we need to set status to 'gameover' is done during playing of the card */
1258 if($mystatus=='play')
1260 /* the following happens only when the gamestatus is 'gameover' */
1261 /* check if game is over, display results */
1262 if(DB_get_game_status_by_gameid($gameid)=='play')
1264 echo "the game is over for you.. other people still need to play though";
1268 echo "the game is over now...<br />\n";
1270 $result = mysql_query("SELECT fullname, SUM(score), Hand.party FROM Score".
1271 " LEFT JOIN Hand ON Hand.id=hand_id".
1272 " LEFT JOIN User ON Hand.user_id=User.id".
1273 " WHERE Hand.game_id=$gameid".
1274 " GROUP BY fullname" );
1275 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1276 echo " FINAL SCORE: ".$r[0]."(".$r[2].") ".$r[1]."<br />";
1278 $result = mysql_query("SELECT Hand.party, SUM(score) FROM Score".
1279 " LEFT JOIN Hand ON Hand.id=hand_id".
1280 " LEFT JOIN User ON Hand.user_id=User.id".
1281 " WHERE Hand.game_id=$gameid".
1282 " GROUP BY Hand.party" );
1283 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1284 echo " FINAL SCORE: ".$r[0]." ".$r[1]."<br />\n";
1287 $session = DB_get_session_by_gameid($gameid);
1288 $result = mysql_query("SELECT id,create_date FROM Game".
1289 " WHERE session=$session".
1290 " ORDER BY create_date DESC".
1294 $r = mysql_fetch_array($result,MYSQL_NUM);
1296 if(!$session || $gameid==$r[0])
1298 /* suggest a new game with the same people in it, just rotated once */
1299 $names = DB_get_all_names_by_gameid($gameid);
1300 output_ask_for_new_game($names[1],$names[2],$names[3],$names[0],$gameid);
1305 echo "error in testing the status";
1310 /* user status page */
1311 else if(myisset("email","password"))
1313 /* test id and password, should really be done in one step */
1314 $email = $_REQUEST["email"];
1315 $password = $_REQUEST["password"];
1317 if(myisset("forgot"))
1321 $uid = DB_get_userid_by_email($email);
1327 echo "Hmm, you forgot your passwort...nothing I can do at the moment:( ";
1328 echo " you need to email Arun for now... in the future it will be all automated and an ";
1329 echo "email with a new password will go to $email.";
1334 echo "you need to give me an email address!";
1336 echo "couldn't find a player with this email, please contact Arun, if you think this is a mistake";
1341 /* verify password and email */
1342 if(strlen($password)!=32)
1343 $password = md5($password);
1346 $uid = DB_get_userid_by_email_and_password($email,$password);
1352 if(myisset("setpref"))
1354 $setpref=$_REQUEST["setpref"];
1358 case "englishcards":
1359 $result = mysql_query("SELECT * from User_Prefs".
1360 " WHERE user_id='$uid' AND pref_key='cardset'" );
1361 if( mysql_fetch_array($result,MYSQL_NUM))
1362 $result = mysql_query("UPDATE User_Prefs SET value=".DB_quote_smart($setpref).
1363 " WHERE user_id='$uid' AND pref_key='cardset'" );
1365 $result = mysql_query("INSERT INTO User_Prefs VALUES(NULL,'$uid','cardset',".DB_quote_smart($setpref).")");
1366 echo "Ok, changed you preferences for the cards.\n";
1370 else /* output default user page */
1372 $time = DB_get_user_timestamp($uid);
1373 $unixtime =strtotime($time);
1375 $offset = DB_get_user_timezone($uid);
1376 $zone = return_timezone($offset);
1377 date_default_timezone_set($zone);
1379 /* display links to settings */
1380 output_user_settings($email,$password);
1382 echo "last login: ".date("r",$unixtime)."<br />";
1384 DB_update_user_timestamp($uid);
1386 echo "<p>these are the games you are playing in:<br />\n";
1387 $result = mysql_query("SELECT Hand.hash,Hand.game_id,Game.mod_date from Hand".
1388 " LEFT JOIN Game On Hand.game_id=Game.id".
1389 " WHERE Hand.user_id='$uid' AND Game.status<>'gameover'" );
1390 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1392 echo "<a href=\"".$host."?me=".$r[0]."\">game #".$r[1]." </a>";
1393 if(time()-strtotime($r[2]) > 60*60*24*30)
1394 echo " The game has been running for over a month.".
1395 " Do you want to cancel it? <a href=\"$host?cancle=1&me=".$r[0]."\">yes</a>".
1396 " (clicking here is final and can't be restored)";
1402 echo "<p>and these are your games that are already done:<br />Game: \n";
1403 $result = mysql_query("SELECT hash,game_id from Hand WHERE user_id='$uid' AND status='gameover'" );
1404 while( $r = mysql_fetch_array($result,MYSQL_NUM))
1405 echo "<a href=\"".$host."?me=".$r[0]."\">#".$r[1]." </a>, ";
1408 $names = DB_get_all_names();
1409 echo "<p>registered players:<br />\n";
1410 foreach ($names as $name)
1414 echo "<p>Want to start a new game? Visit <a href=\"".$host."?new\">this page.</a></p>";
1419 echo "sorry email and password don't match <br />";
1425 /* page for registration */
1426 else if(myisset("register") )
1430 /* new user wants to register */
1431 else if(myisset("Rfullname","Remail","Rpassword","Rtimezone") )
1434 if(DB_get_userid_by_name($_REQUEST["Rfullname"]))
1436 echo "please chose another name<br />";
1439 if(DB_get_userid_by_email($_REQUEST["Remail"]))
1441 echo "this email address is already used ?!<br />";
1446 $r=mysql_query("INSERT INTO User VALUES(NULL,".DB_quote_smart($_REQUEST["Rfullname"]).
1447 ",".DB_quote_smart($_REQUEST["Remail"]).
1448 ",".DB_quote_smart(md5($_REQUEST["Rpassword"])).
1449 ",".DB_quote_smart($_REQUEST["Rtimezone"]).",NULL)");
1452 echo " added you to the database";
1454 echo " something went wrong";
1457 /* default login page */
1460 $pre=0;$game=0;$done=0;
1461 $r=mysql_query("SELECT COUNT(id) FROM Game GROUP BY status");
1463 $pre = mysql_fetch_array($r,MYSQL_NUM);
1464 $game = mysql_fetch_array($r,MYSQL_NUM);
1465 $done = mysql_fetch_array($r,MYSQL_NUM);
1467 output_home_page($pre[0],$game[0],$done[0]);