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