+ $return[] = array( $pl['name'], round($pl['points']/$pl['nr'],3), $pl['points'],$pl['nr'],$pl['active'],
+ $pl['response'],$pl['solo'],$pl['soloavg']);
+ }
+
+ return $return;
+}
+
+function format_score_table_ascii($score)
+{
+ $output="";
+ if(sizeof($score)==0)
+ return "";
+
+ /* truncate table if we have too many games */
+ $max = sizeof($score);
+ if($max>6) $output.=" "._("(table truncated to last 6 games)")."\n";
+
+ /* output header */
+ foreach($score[0]['players'] as $id=>$points)
+ {
+ $name = DB_get_name('userid',$id); /*TODO*/
+ $output.= " ".substr($name,0,2)." |";
+ }
+ $output.=" P |\n";
+ $output.= "------+------+------+------+------+\n";
+
+ /* output score for each game */
+ $i=0;
+ foreach($score as $game)
+ {
+ $i++;
+ if($i-1<$max-6) continue;
+
+ foreach($game['players'] as $id=>$points)
+ $output.=str_pad($points,6," ",STR_PAD_LEFT)."|";
+ $output.=str_pad($game['points'],4," ",STR_PAD_LEFT);
+
+ /* check for solo */
+ if($game['solo'])
+ $output.= " S|";
+ else
+ $output.= " |";
+
+ $output.="\n";
+ }
+ return $output;
+}
+
+function format_score_table_html($score,$userid)
+{
+ global $INDEX;
+
+ if(sizeof($score)==0)
+ return "";
+
+ $output = "<div class=\"scoretable\">\n<table class=\"score\">\n";
+
+ /* output header */
+ $header = "";
+ $header.= " <thead>\n <tr>\n";
+ $header.= " <th> No </th>";
+ foreach($score[0]['players'] as $id=>$points)
+ {
+ $name = DB_get_name('userid',$id); /*TODO*/
+ $header.= "<th> ".substr($name,0,2)." </th>";
+ }
+ $header.="<th>P</th>\n </tr>\n </thead>\n";
+
+ /* use the same as footer */
+ $footer = "";
+ $footer.= " <tfoot>\n <tr>\n";
+ $footer.= " <td> No </td>";
+ foreach($score[0]['players'] as $id=>$points)
+ {
+ $name = DB_get_name('userid',$id); /*TODO*/
+ $footer.= "<td> ".substr($name,0,2)." </td>";
+ }
+ $footer.="<td>P</td>\n </tr>\n </tfoot>\n";
+
+ /* body */
+ $body = "";
+ $body.= " <tbody>\n";
+ $i=0;
+ foreach($score as $game)
+ {
+ $i++;
+ $body.=" <tr>";
+ $userhash = DB_get_hash_from_gameid_and_userid($game['gameid'],$userid);
+ /* create link to old games only if you are logged in and its your game*/
+ if(isset($_SESSION['id']) && $_SESSION['id']==$userid)
+ $body.=" <td> <a href=\"".$INDEX."?action=game&me=".$userhash."\">$i</a></td>";
+ else
+ $body.=" <td>$i</td>";
+
+ foreach($game['players'] as $id=>$points)
+ $body.="<td>".$points."</td>";
+ $body.="<td>".$game['points'];
+
+ /* check for solo */
+ if($game['solo'])
+ $body.= " S";
+ $body.="</td></tr>\n";
+ }
+
+ $output.=$header;
+ if($i>12)
+ $output.=$footer;
+ $output.=$body;
+
+ $output.=" </tbody>\n</table>\n</div>\n";
+
+ return $output;
+}
+
+function createCache($content, $cacheFile)
+{
+ $fp = fopen($cacheFile,"w");
+ if($fp)
+ {
+ fwrite($fp,$content);
+ fclose($fp);
+ }
+ else
+ echo "WARNING: couldn't create cache file";
+
+ return;
+}
+
+function getCache($cacheFile, $expireTime)
+{
+ if( file_exists($cacheFile) &&
+ filemtime($cacheFile )>( time() - $expireTime ) )
+ {
+ return file_get_contents($cacheFile);
+ }
+
+ return false;
+}
+
+function check_vacation($userid)
+{
+ /* get start date */
+ $result = DB_query_array("SELECT value FROM User_Prefs".
+ " WHERE user_id=".DB_quote_smart($userid)." AND pref_key='vacation start'" );
+ if($result)
+ $start = $result[0];
+ else
+ return NULL;
+
+ /* get end date */
+ $result = DB_query_array("SELECT value FROM User_Prefs".
+ " WHERE user_id=".DB_quote_smart($userid)." AND pref_key='vacation stop'" );
+ if($result)
+ $stop = $result[0];
+ else
+ return NULL;
+
+ /* get comment */
+ $result = DB_query_array("SELECT value FROM User_Prefs".
+ " WHERE user_id=".DB_quote_smart($userid)." AND pref_key='vacation comment'" );
+ if($result)
+ $comment = $result[0];
+ else
+ $comment = '';
+
+ /* check if user is on vacation. TODO: use user's timezone */
+ if( (time() - strtotime($start) >0) &&
+ (strtotime($stop) - time() >0))
+ return array ($start,$stop,$comment);
+ else
+ return NULL;
+}
+
+function cancel_game($why,$gameid)
+{
+ $gameid = DB_quote_smart($gameid);
+
+ /* update the game table */
+ switch($why)
+ {
+ case 'timedout':
+ DB_query("UPDATE Game SET status='cancel-timedout' WHERE id=$gameid");
+ break;
+ case 'nines':
+ DB_query("UPDATE Game SET status='cancel-nines' WHERE id=$gameid");
+ break;
+ case 'trump':
+ DB_query("UPDATE Game SET status='cancel-trump' WHERE id=$gameid");
+ break;
+ case 'noplay':
+ DB_query("UPDATE Game SET status='cancel-noplay' WHERE id=$gameid");
+ break;
+ case 'lowtrump':
+ DB_query("UPDATE Game SET status='cancel-lowtrump' WHERE id=$gameid");
+ break;