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