fixed link to gravatar home page
[e-DoKo.git] / include / stats.php
1 <?php
2 /* Copyright 2006, 2007, 2008, 2009, 2010 Arun Persaud <arun@nubati.net>
3  *
4  *   This file is part of e-DoKo.
5  *
6  *   e-DoKo is free software: you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, either version 3 of the License, or
9  *   (at your option) any later version.
10  *
11  *   e-DoKo is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 /* make sure that we are not called from outside the scripts,
22  * use a variable defined in config.php to check this
23  */
24 if(!isset($HOST))
25   exit;
26
27 /* turn warnings off (problem with two warnings in function.php */
28 error_reporting(!E_ALL & ~E_NOTICE);
29
30 $name  = $_SESSION["name"];
31 $email = DB_get_email('name',$name);
32
33 $myid = DB_get_userid('email',$email);
34 if(!$myid)
35   return;
36
37 $PREF = DB_get_PREF($myid);
38
39 DB_update_user_timestamp($myid);
40
41 display_user_menu($myid);
42
43 /* check if cached version exist */
44 if( !$content = getCache("cache/stats.html",60*60*24) )
45 {
46   /* start caching */
47   ob_start();
48
49   /* start statistics*/
50   echo "<div class=\"user wide\">\n";
51
52   echo "<p>Generated ".date("Y-m-d H:i:s")." (server time) </p>";
53
54   /* total number of games */
55   echo "<p>The number of finished games on this server is: ";
56   $r = DB_query_array("SELECT COUNT(*) from Game".
57                       " WHERE status='gameover'");
58   $GameN =  $r[0];
59   echo " $GameN </p>\n";
60
61   echo "<p>The contra party wins in ";
62   $result = DB_query("SELECT COUNT(*) from Score".
63                      " WHERE score='againstqueens'");
64   while( $r = DB_fetch_array($result))
65     echo $r[0];
66   echo " games.</p>\n";
67
68   /* longest and shortest game */
69   $r=DB_query("SELECT timediff(mod_date,create_date) ,session,id".
70             " FROM Game WHERE status='gameover'".
71               " ORDER BY time_to_sec(timediff(mod_date,create_date)) ASC LIMIT 1");
72
73   if($r)
74     {
75       $short= DB_fetch_array($r);
76       $names = DB_get_all_names_by_gameid($short[2]);
77       echo "<p> The shortest game took only ".$short[0]." hours and was played by  ".join(", ",$names).".<br />\n";
78     }
79
80   $r=DB_query("SELECT datediff(mod_date,create_date) ,session,id".
81               " FROM Game WHERE status='gameover'".
82               " ORDER BY time_to_sec(timediff(mod_date,create_date)) DESC LIMIT 1");
83   if($r)
84     {
85       $long= DB_fetch_array($r);
86       echo "The longest game took ".$long[0]." days.</p>\n";
87     }
88
89   $r=DB_query("SELECT COUNT(*) as c, session, id FROM Game ".
90               " GROUP BY session ORDER BY c DESC LIMIT 1");
91   if($r)
92     {
93       $long  = DB_fetch_array($r);
94       $names = DB_get_all_names_by_gameid($long[2]);
95       echo "<p>The longest session is session ".$long[1]." with ".$long[0].
96         " games played by ".join(", ",$names).".</p>\n";
97     }
98
99
100   /* number of solos */
101   $result = DB_query_array_all("SELECT type,COUNT(*) as c from Game".
102                                " WHERE status='gameover'".
103                                " GROUP BY type".
104                                " ORDER BY c DESC");
105   array_unshift($result,array("Type","Frequency"));
106   echo output_table($result,"Game types","stats");
107
108   /* break up solos in types */
109   $result = DB_query_array_all("SELECT solo,COUNT(*) as c from Game".
110                                " WHERE status='gameover'".
111                                " AND type='solo'".
112                                " GROUP BY solo".
113                                " ORDER BY c DESC");
114   array_unshift($result,array("Type","Frequency"));
115   echo output_table($result,"Kind of solos","stats");
116
117   /*
118  2 top user mit maximaler quote an solo (min 10 games)
119
120  top scoring game: winning players
121
122  game with the same cards: show 3 at random:
123  player who won, points, what kind of game
124  select g1.id, g2.id from game g1 left join game g2 on g1.randomnumbers=g2.randomnumbers where g1.id<g2.id order by g1.id
125  select id from game where randomnumbers like "blablabl%"; the % is like .* in regexp
126  select id,type,solo,status from game where id in (select id from game where randomnumbers in (select randomnumbers from game where id=27));
127   */
128
129   /* number of calls*/
130   $result = DB_query_array_all("SELECT CONCAT_WS(' ',party,IFNULL(point_call,'no call')),COUNT(*) from Hand".
131                                " LEFT JOIN Game on Game.id=Hand.game_id ".
132                                " WHERE Game.status='gameover'".
133                                " GROUP BY party,point_call");
134   array_unshift($result,array("call","Frequency"));
135   echo output_table($result,"Kind of call","stats");
136
137
138   /* most reminders */
139   $result = DB_query_array_all("SELECT fullname, COUNT(*)  /" .
140                                "      (SELECT COUNT(*) FROM Hand".
141                                "       WHERE user_id=User.id) as c".
142                                " FROM Reminder".
143                                " LEFT JOIN User ON User.id=user_id".
144                                " GROUP BY user_id".
145                                " ORDER BY c DESC LIMIT 5" );
146   array_unshift($result,array("Name","Reminders"));
147   echo output_table($result,"Most reminders per game","stats");
148
149   /* fox */
150   $result = DB_query_array_all("SELECT fullname, COUNT(*) /" .
151                                "      (SELECT COUNT(*) FROM Hand".
152                                "       WHERE user_id=User.id) as c".
153                                " FROM Score".
154                                " LEFT JOIN User ON User.id=winner_id".
155                                " WHERE score='fox'".
156                                " GROUP BY winner_id".
157                                " ORDER BY c DESC LIMIT 5" );
158   array_unshift($result,array("Name","Number of foxes caught"));
159   echo output_table($result,"Most caught foxes","stats");
160
161   $result = DB_query_array_all("SELECT fullname, COUNT(*) /" .
162                                "      (SELECT COUNT(*) FROM Hand".
163                                "       WHERE user_id=User.id) as c".
164                                " FROM Score".
165                                " LEFT JOIN User ON User.id=looser_id".
166                                " WHERE score='fox'".
167                                " GROUP BY looser_id".
168                                " ORDER BY c DESC LIMIT 5" );
169   array_unshift($result,array("Name","Number of foxes lost"));
170   echo output_table($result,"Lost foxes (most)","stats");
171
172   $result = DB_query_array_all("SELECT fullname, COUNT(*) /" .
173                                "      (SELECT COUNT(*) FROM Hand".
174                                "       WHERE user_id=User.id) as c".
175                                " FROM Score".
176                                " LEFT JOIN User ON User.id=looser_id".
177                                " WHERE score='fox'".
178                                " GROUP BY looser_id".
179                                " ORDER BY c ASC LIMIT 5" );
180   array_unshift($result,array("Name","Number of foxes lost"));
181   echo output_table($result,"Lost foxes (least)","stats");
182
183   /* which position wins the most tricks  */
184   $result = DB_query_array_all("SELECT CASE winner ".
185                                "   WHEN 1 THEN 'left' ".
186                                "   WHEN 2 THEN 'top' ".
187                                "   WHEN 3 THEN 'right' ".
188                                "   WHEN 4 THEN 'bottom' END,".
189                                " COUNT(*) AS c FROM Trick".
190                                " GROUP BY winner ".
191                                " HAVING LENGTH(winner)>0  ".
192                                " ORDER BY winner ASC " );
193   array_unshift($result,array("Position","Number of tricks"));
194   echo output_table($result,"Tricks at the table","stats");
195
196   /*
197  does the party win more often if they start
198
199  echo "<p>The party playing first wins in";
200  $result = mysql_query("SELECT COUNT(*) from Score".
201  " LEFT JOIN Game ON Game.id=game_id".
202  " WHERE score='againstqueens'".
203  " AND Game.status='gameover'".
204  " AND Game.type<>'solo'");
205  while( $r = mysql_fetch_array($result,MYSQL_NUM))
206  echo $r[1]." (".$r[0].") <br />\n";
207  echo " games</p>\n";
208   */
209   $result = generate_global_score_table();
210   array_unshift($result,array('Name','Average score per game','Total Points','Number of games', 'Active games',
211                               'Response Time [min]','Number of solos','Solos/game'));
212   echo output_table($result,'Players (need more than 10 games)','stats','ScoreTable');
213
214   /*
215    * how often is the last trick a non-trump trick
216    */
217
218   /* needs this so that all tables are within the div and don't float around */
219   echo "<p style=\"clear:both;\">&nbsp;</p>\n";
220
221   echo "</div>\n"; /* end output */
222
223   /* write file to cache */
224   $content = ob_get_contents();
225   ob_end_clean();
226   createCache($content,"cache/stats.html");
227 }
228
229 echo $content;
230
231 ?>
232