updated copyright notice in footer
[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 $name  = $_SESSION["name"];
28 $email = DB_get_email('name',$name);
29
30 $myid = DB_get_userid('email',$email);
31 if(!$myid)
32   return;
33
34 $PREF = DB_get_PREF($myid);
35
36 DB_update_user_timestamp($myid);
37
38 display_user_menu($myid);
39
40 /* check if cached version exist */
41 if( !$content = getCache("cache/stats.html",60*60*24) )
42 {
43   /* start caching */
44   ob_start();
45
46   /* start statistics*/
47   echo "<div class=\"user wide\">\n";
48
49   echo "<p>Generated ".date("Y-m-d H:i:s")." (server time) </p>";
50
51   /* total number of games */
52   echo "<p>The number of finished games on this server is: ";
53   $r = DB_query_array("SELECT COUNT(*) from Game".
54                       " WHERE status='gameover'");
55   $GameN =  $r[0];
56   echo " $GameN </p>\n";
57
58   echo "<p>The contra party wins in ";
59   $result = DB_query("SELECT COUNT(*) from Score".
60                      " WHERE score='againstqueens'");
61   while( $r = DB_fetch_array($result))
62     echo $r[0];
63   echo " games.</p>\n";
64
65   /* longest and shortest game */
66   $r=DB_query("SELECT timediff(mod_date,create_date) ,session,id".
67             " FROM Game WHERE status='gameover'".
68               " ORDER BY time_to_sec(timediff(mod_date,create_date)) ASC LIMIT 1");
69
70   if($r)
71     {
72       $short= DB_fetch_array($r);
73       $names = DB_get_all_names_by_gameid($short[2]);
74       echo "<p> The shortest game took only ".$short[0]." hours and was played by  ".join(", ",$names).".<br />\n";
75     }
76
77   $r=DB_query("SELECT datediff(mod_date,create_date) ,session,id".
78               " FROM Game WHERE status='gameover'".
79               " ORDER BY time_to_sec(timediff(mod_date,create_date)) DESC LIMIT 1");
80   if($r)
81     {
82       $long= DB_fetch_array($r);
83       echo "The longest game took ".$long[0]." days.</p>\n";
84     }
85
86   $r=DB_query("SELECT COUNT(*) as c, session, id FROM Game ".
87               " GROUP BY session ORDER BY c DESC LIMIT 1");
88   if($r)
89     {
90       $long  = DB_fetch_array($r);
91       $names = DB_get_all_names_by_gameid($long[2]);
92       echo "<p>The longest session is session ".$long[1]." with ".$long[0].
93         " games played by ".join(", ",$names).".</p>\n";
94     }
95
96
97   /* number of solos */
98   $result = DB_query_array_all("SELECT type,COUNT(*) as c from Game".
99                                " WHERE status='gameover'".
100                                " GROUP BY type".
101                                " ORDER BY c DESC");
102   array_unshift($result,array("Type","Frequency"));
103   echo output_table($result,"Game types","stats");
104
105   /* break up solos in types */
106   $result = DB_query_array_all("SELECT solo,COUNT(*) as c from Game".
107                                " WHERE status='gameover'".
108                                " AND type='solo'".
109                                " GROUP BY solo".
110                                " ORDER BY c DESC");
111   array_unshift($result,array("Type","Frequency"));
112   echo output_table($result,"Kind of solos","stats");
113
114   /*
115  2 top user mit maximaler quote an solo (min 10 games)
116
117  top scoring game: winning players
118
119  game with the same cards: show 3 at random:
120  player who won, points, what kind of game
121  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
122  select id from game where randomnumbers like "blablabl%"; the % is like .* in regexp
123  select id,type,solo,status from game where id in (select id from game where randomnumbers in (select randomnumbers from game where id=27));
124   */
125
126   /* number of calls*/
127   $result = DB_query_array_all("SELECT CONCAT_WS(' ',party,IFNULL(point_call,'no call')),COUNT(*) from Hand".
128                                " LEFT JOIN Game on Game.id=Hand.game_id ".
129                                " WHERE Game.status='gameover'".
130                                " GROUP BY party,point_call");
131   array_unshift($result,array("call","Frequency"));
132   echo output_table($result,"Kind of call","stats");
133
134
135   /* most reminders */
136   $result = DB_query_array_all("SELECT fullname, COUNT(*)  /" .
137                                "      (SELECT COUNT(*) FROM Hand".
138                                "       WHERE user_id=User.id) as c".
139                                " FROM Reminder".
140                                " LEFT JOIN User ON User.id=user_id".
141                                " GROUP BY user_id".
142                                " ORDER BY c DESC LIMIT 5" );
143   array_unshift($result,array("Name","Reminders"));
144   echo output_table($result,"Most reminders per game","stats");
145
146   /* fox */
147   $result = DB_query_array_all("SELECT fullname, COUNT(*) /" .
148                                "      (SELECT COUNT(*) FROM Hand".
149                                "       WHERE user_id=User.id) as c".
150                                " FROM Score".
151                                " LEFT JOIN User ON User.id=winner_id".
152                                " WHERE score='fox'".
153                                " GROUP BY winner_id".
154                                " ORDER BY c DESC LIMIT 5" );
155   array_unshift($result,array("Name","Number of foxes caught"));
156   echo output_table($result,"Most caught foxes","stats");
157
158   $result = DB_query_array_all("SELECT fullname, COUNT(*) /" .
159                                "      (SELECT COUNT(*) FROM Hand".
160                                "       WHERE user_id=User.id) as c".
161                                " FROM Score".
162                                " LEFT JOIN User ON User.id=looser_id".
163                                " WHERE score='fox'".
164                                " GROUP BY looser_id".
165                                " ORDER BY c DESC LIMIT 5" );
166   array_unshift($result,array("Name","Number of foxes lost"));
167   echo output_table($result,"Lost foxes (most)","stats");
168
169   $result = DB_query_array_all("SELECT fullname, COUNT(*) /" .
170                                "      (SELECT COUNT(*) FROM Hand".
171                                "       WHERE user_id=User.id) as c".
172                                " FROM Score".
173                                " LEFT JOIN User ON User.id=looser_id".
174                                " WHERE score='fox'".
175                                " GROUP BY looser_id".
176                                " ORDER BY c ASC LIMIT 5" );
177   array_unshift($result,array("Name","Number of foxes lost"));
178   echo output_table($result,"Lost foxes (least)","stats");
179
180   /* which position wins the most tricks  */
181   $result = DB_query_array_all("SELECT CASE winner ".
182                                "   WHEN 1 THEN 'left' ".
183                                "   WHEN 2 THEN 'top' ".
184                                "   WHEN 3 THEN 'right' ".
185                                "   WHEN 4 THEN 'bottom' END,".
186                                " COUNT(*) AS c FROM Trick".
187                                " GROUP BY winner ".
188                                " HAVING LENGTH(winner)>0  ".
189                                " ORDER BY winner ASC " );
190   array_unshift($result,array("Position","Number of tricks"));
191   echo output_table($result,"Tricks at the table","stats");
192
193   /* most solos */
194   $result = DB_query_array_all("SELECT fullname as fname,".
195                                "       COUNT(*), ".
196                                "       COUNT(*)/(SELECT COUNT(*) FROM Hand LEFT JOIN User ON User.id=Hand.user_id WHERE fullname=fname) as c ".
197                                " FROM Game ".
198                                " LEFT JOIN Hand ON Hand.position=startplayer AND Game.id=Hand.game_id ".
199                                " LEFT JOIN User ON User.id=Hand.user_id ".
200                                " WHERE type='solo' AND Game.status='gameover' ".
201                                " GROUP BY user_id ".
202                                " ORDER BY c DESC;");
203   array_unshift($result,array("Name","Number of solos","Solos/game"));
204   echo output_table($result,"Most solos","stats");
205
206
207   /* most active games */
208   $result = DB_query_array_all("SELECT fullname, COUNT(*) as c  " .
209                                " FROM Hand".
210                                " LEFT JOIN User ON User.id=user_id".
211                                " LEFT JOIN Game ON Game.id=game_id".
212                                " WHERE Game.status IN ('pre','play')".
213                                " GROUP BY user_id".
214                                " ORDER BY c DESC LIMIT 7" );
215   array_unshift($result,array("Name","Number of active games"));
216   echo output_table($result,"Active games","stats");
217
218   /* response time of users*/
219   $result = DB_query_array_all("SELECT User.fullname,".
220                                "IFNULL(AVG(if(P1.sequence in (2,3,4),".
221                                "-timestampdiff(MINUTE,mod_date,(select mod_date from Play P2 where P1.trick_id=P2.trick_id  and P2.sequence=P1.sequence-1)),NULL )),1e9) as a, ".
222                                " COUNT(*) as na ".
223                                "FROM Play P1 ".
224                                "LEFT JOIN Hand_Card ON P1.hand_card_id=Hand_Card.id ".
225                                "LEFT JOIN Hand ON Hand.id=Hand_Card.hand_id ".
226                                "LEFT JOIN User ON Hand.user_id=User.id ".
227                                "GROUP BY user_id ".
228                                "HAVING na>8 ".
229                                "ORDER BY a " );
230   array_unshift($result,array("Name","Average minutes before response","trick count"));
231   echo output_table($result,"Response","stats");
232
233   /*
234  does the party win more often if they start
235
236  echo "<p>The party playing first wins in";
237  $result = mysql_query("SELECT COUNT(*) from Score".
238  " LEFT JOIN Game ON Game.id=game_id".
239  " WHERE score='againstqueens'".
240  " AND Game.status='gameover'".
241  " AND Game.type<>'solo'");
242  while( $r = mysql_fetch_array($result,MYSQL_NUM))
243  echo $r[1]." (".$r[0].") <br />\n";
244  echo " games</p>\n";
245   */
246   $result = generate_global_score_table();
247   array_unshift($result,array("Name","Average score per game","Total Points","Number of games"));
248   echo output_table($result,"Points per game (need more than 10 games)","stats","ScoreTable");
249
250   /*
251    * how often is the last trick a non-trump trick
252    */
253
254   /* needs this so that all tables are within the div and don't float around */
255   echo "<p style=\"clear:both;\">&nbsp;</p>\n";
256
257   echo "</div>\n"; /* end output */
258
259   /* write file to cache */
260   $content = ob_get_contents();
261   ob_end_clean();
262   createCache($content,"cache/stats.html");
263 }
264
265 echo $content;
266
267 ?>
268