CLEANUP: started some database cleanup and added some more comments
[e-DoKo.git] / stats.php
1 <?php
2 error_reporting(E_ALL);
3
4 include_once("config.php");
5 include_once("output.php");      /* html output only */
6 include_once("db.php");          /* database only */
7 include_once("functions.php");   /* the rest */
8
9 config_check();
10
11 if(DB_open()<0)
12   {
13     output_header();
14     echo "Database error, can't connect... Please wait a while and try again. ".
15       "If the problem doesn't go away feel free to contact $ADMIN_NAME at $ADMIN_EMAIL.";
16     output_footer();
17     exit();
18   }
19
20 /* start a session, if it is not already running */
21 session_start();
22 /* done major error checking, output header of HTML page */
23 output_header();
24
25 /* check if we want to logout */
26 if(myisset("logout"))
27   {
28     session_unset();
29     session_destroy();
30     $_SESSION = array();
31     echo "<div class=\"message\"><span class=\"bigger\">You are now logged out!</span><br />\n".
32       "(<a href=\"$INDEX\">This will take you back to the home-page</a>)</div>";
33   }
34 /* user status page */
35 else if( isset($_SESSION["name"]) )
36    {
37      $name = $_SESSION["name"];
38      $email     = DB_get_email('name',$name);
39      $password  = DB_get_passwd_by_name($name);
40
41      /* verify password and email */
42      if(strlen($password)!=32)
43        $password = md5($password);
44
45      $ok  = 1;
46      $myid = DB_get_userid('email-password',$email,$password);
47      if(!$myid)
48        $ok = 0;
49
50      if($ok)
51        {
52          output_status();
53
54          DB_get_PREF($myid);
55
56          DB_update_user_timestamp($myid);
57
58          display_user_menu();
59
60          /* start statistics*/
61          echo "<div class=\"user\">\n";
62
63 /* always: if player logged in: add link to cards  */
64
65          /* total number of games */
66          echo "<p>The number of finished games on this server is: ";
67          $result = mysql_query("SELECT COUNT(*) from Game".
68                                " WHERE status='gameover'");
69          $r = mysql_fetch_array($result,MYSQL_NUM);
70          $GameN =  $r[0];
71          echo " $GameN </p>\n";
72
73          echo "<p>The contra party wins in ";
74          $result = mysql_query("SELECT COUNT(*) from Score".
75                                " LEFT JOIN Game ON Game.id=game_id".
76                                " WHERE score='againstqueens'".
77                                " AND Game.status='gameover'");
78          while( $r = mysql_fetch_array($result,MYSQL_NUM))
79            echo $r[0];
80          echo " games.</p>\n";
81
82
83          /* number of solos */
84          echo "<p>These kind of games have been played this often: <br />";
85          $result = mysql_query("SELECT COUNT(*) as c,type from Game".
86                                " WHERE status='gameover'".
87                                " GROUP BY type".
88                                " ORDER BY c DESC");
89          while( $r = mysql_fetch_array($result,MYSQL_NUM))
90            echo "".$r[1]." (".$r[0].") <br />";
91          echo " </p>\n";
92
93          /* break up solos in types */
94          echo "<p>These kind of solos have been played this often: <br />";
95          $result = mysql_query("SELECT COUNT(*) as c,solo from Game".
96                                " WHERE status='gameover'".
97                                " AND type='solo'".
98                                " GROUP BY solo".
99                                " ORDER BY c DESC");
100          while( $r = mysql_fetch_array($result,MYSQL_NUM))
101            echo "".$r[1]." (".$r[0].")<br />";
102          echo "</p>\n";
103
104          /*
105  2 top user mit maximaler quote an solo (min 10 games)
106
107  top scoring game: winning players
108
109  game with the same cards: show 3 at random:
110  player who won, points, what kind of game
111  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
112  select id from game where randomnumbers like "blablabl%"; the % is like .* in regexp
113  select id,type,solo,status from game where id in (select id from game where randomnumbers in (select randomnumbers from game where id=27));
114
115          */
116          echo "<p>Most extra points (doko, fox, karlchen) in a single game:<br />\n";
117          $result = mysql_query("SELECT COUNT(*) as c,fullname FROM Score".
118                                " LEFT JOIN User ON User.id=winner_id" .
119                                " WHERE score IN ('fox','doko','karlchen')".
120                                " GROUP BY game_id,fullname".
121                                " ORDER BY c DESC LIMIT 3" );
122          while( $r = mysql_fetch_array($result,MYSQL_NUM))
123            echo $r[1]." (".$r[0].") <br />\n";
124          echo "</p>\n";
125
126          /* longest and shortest game */
127          $r=mysql_query("SELECT timediff(mod_date,create_date) ,session,id".
128                         " FROM Game WHERE status='gameover'".
129                         " ORDER BY time_to_sec(timediff(mod_date,create_date)) ASC LIMIT 1");
130
131          if($r)
132            {
133              $short= mysql_fetch_array($r,MYSQL_NUM);
134              $names = DB_get_all_names_by_gameid($short[2]);
135              echo "<p> The shortest game took only ".$short[0]." hours and was played by  ".join(", ",$names).".<br />\n";
136            }
137
138          $r=mysql_query("SELECT datediff(mod_date,create_date) ,session,id".
139                         " FROM Game WHERE status='gameover'".
140                         " ORDER BY time_to_sec(timediff(mod_date,create_date)) DESC LIMIT 1");
141          if($r)
142            {
143              $long= mysql_fetch_array($r,MYSQL_NUM);
144              echo "The longest game took ".$long[0]." days.</p>\n";
145            }
146
147          $r=mysql_query("SELECT COUNT(*) as c, session, id FROM Game ".
148                         " GROUP BY session ORDER BY c DESC LIMIT 1");
149          if($r)
150            {
151              $long  = mysql_fetch_array($r,MYSQL_NUM);
152              $names = DB_get_all_names_by_gameid($long[2]);
153              echo "The longest session is session ".$long[1]." with ".$long[0].
154                " games played by ".join(", ",$names).".</p>\n";
155            }
156
157          /* most reminders */
158          echo "<p>These players got the most reminders per game:<br />\n";
159          $result = mysql_query("SELECT COUNT(*)  /" .
160                                "      (SELECT COUNT(*) FROM Hand".
161                                "       WHERE user_id=User.id) as c,".
162                                " fullname FROM Reminder".
163                                " LEFT JOIN User ON User.id=user_id".
164                                " GROUP BY user_id".
165                                " ORDER BY c DESC LIMIT 3" );
166          while( $r = mysql_fetch_array($result,MYSQL_NUM))
167            echo $r[1]." (".$r[0].") <br />\n";
168          echo "</p>\n";
169
170          /* fox */
171          echo "<p>These players caught the most foxes per game:<br />\n";
172          $result = mysql_query("SELECT COUNT(*) /" .
173                                "      (SELECT COUNT(*) FROM Hand".
174                                "       WHERE user_id=User.id) as c,".
175                                " fullname".
176                                " FROM Score".
177                                " LEFT JOIN User ON User.id=winner_id".
178                                " WHERE score='fox'".
179                                " GROUP BY winner_id".
180                                " ORDER BY c DESC LIMIT 5" );
181          while( $r = mysql_fetch_array($result,MYSQL_NUM))
182            echo $r[1]." (".$r[0].") <br />\n";
183          echo "</p>\n";
184
185          echo "<p>These players lost their fox most often per game:<br />\n";
186          $result = mysql_query("SELECT COUNT(*) /" .
187                                "      (SELECT COUNT(*) FROM Hand".
188                                "       WHERE user_id=User.id) as c,".
189                                " fullname".
190                                " FROM Score".
191                                " LEFT JOIN User ON User.id=looser_id".
192                                " WHERE score='fox'".
193                                " GROUP BY looser_id".
194                                " ORDER BY c DESC LIMIT 5" );
195          while( $r = mysql_fetch_array($result,MYSQL_NUM))
196            echo $r[1]." (".$r[0].") <br />\n";
197          echo "</p>\n";
198
199          /* which position wins the most tricks  */
200          echo "<p>Which positions at the table make the most tricks:<br />\n";
201          $result = mysql_query("SELECT COUNT(*) AS c,winner FROM Trick".
202                                " GROUP BY winner".
203                                " ORDER BY winner ASC " );
204          $r = mysql_fetch_array($result,MYSQL_NUM);
205          if($r[1]==NULL) /* ongoing games, no winner yet */
206            $r = mysql_fetch_array($result,MYSQL_NUM);
207          echo " left ".$r[0]." <br />\n";
208          $r = mysql_fetch_array($result,MYSQL_NUM);
209          echo " top ".$r[0]." <br />\n";
210          $r = mysql_fetch_array($result,MYSQL_NUM);
211          echo " right ".$r[0]." <br />\n";
212          $r = mysql_fetch_array($result,MYSQL_NUM);
213          echo " bottom ".$r[0]." <br />\n";
214          echo "</p>\n";
215
216          /*
217           does the party win more often if they start
218
219          echo "<p>The party playing first wins in";
220          $result = mysql_query("SELECT COUNT(*) from Score".
221                                " LEFT JOIN Game ON Game.id=game_id".
222                                " WHERE score='againstqueens'".
223                                " AND Game.status='gameover'".
224                                " AND Game.type<>'solo'");
225          while( $r = mysql_fetch_array($result,MYSQL_NUM))
226            echo $r[1]." (".$r[0].") <br />\n";
227          echo " games</p>\n";
228          */
229
230
231          /*
232           how often is the last trick a non-trump trick
233          */
234
235          echo "</div>\n"; /* end output */
236        }
237    }
238  else
239    {
240      /* send them back to the login page */
241     echo "<div class=\"message\"><span class=\"bigger\">You need to log in!</span><br />\n".
242       "(<a href=\"$INDEX\">This will take you back to the login-page</a>)</div>";
243    }
244
245 output_footer();
246 DB_close();
247
248 /*
249  *Local Variables:
250  *mode: php
251  *mode: hs-minor
252  *End:
253  */
254 ?>
255
256