BUGFIX: added indecis to tables for faster lookup
[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=\"$host\">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_by_name($name);
39      $password  = DB_get_passwd_by_name($name);
40
41
42      /* verify password and email */
43      if(strlen($password)!=32)
44        $password = md5($password);
45
46      $ok  = 1;
47      $myid = DB_get_userid_by_email_and_password($email,$password);
48      if(!$myid)
49        $ok = 0;
50
51      if($ok)
52        {
53          DB_get_PREF($myid);
54
55          DB_update_user_timestamp($myid);
56
57          display_user_menu();
58
59          /* start statistics*/
60          echo "<div class=\"user\">\n";
61
62 /* always: if player logged in: add link to cards  */
63
64          /* total number of games */
65          echo "<p>The number of finished games on this server is: ";
66          $result = mysql_query("SELECT COUNT(*) from Game".
67                                " WHERE status='gameover'");
68          $r = mysql_fetch_array($result,MYSQL_NUM);
69          $GameN =  $r[0];
70          echo " $GameN </p>\n";
71
72          echo "<p>The contra party wins in ";
73          $result = mysql_query("SELECT COUNT(*) from Score".
74                                " LEFT JOIN Game ON Game.id=game_id".
75                                " WHERE score='againstqueens'".
76                                " AND Game.status='gameover'");
77          while( $r = mysql_fetch_array($result,MYSQL_NUM))
78            echo $r[0];
79          echo " games.</p>\n";
80
81
82          /* number of solos */
83          echo "<p>These kind of games have been played this often: <br />";
84          $result = mysql_query("SELECT COUNT(*) as c,type from Game".
85                                " WHERE status='gameover'".
86                                " GROUP BY type".
87                                " ORDER BY c DESC");
88          while( $r = mysql_fetch_array($result,MYSQL_NUM))
89            echo "".$r[1]." (".$r[0].") <br />";
90          echo " </p>\n";
91
92          /* break up solos in types */
93          echo "<p>These kind of solos have been played this often: <br />";
94          $result = mysql_query("SELECT COUNT(*) as c,solo from Game".
95                                " WHERE status='gameover'".
96                                " AND type='solo'".
97                                " GROUP BY solo".
98                                " ORDER BY c DESC");
99          while( $r = mysql_fetch_array($result,MYSQL_NUM))
100            echo "".$r[1]." (".$r[0].")<br />";
101          echo "</p>\n";
102
103          /*
104  2 top user mit maximaler quote an solo (min 10 games)
105
106  top scoring game: winning players
107
108  game with the same cards: show 3 at random:
109  player who won, points, what kind of game
110  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
111  select id from game where randomnumbers like "blablabl%"; the % is like .* in regexp
112  select id,type,solo,status from game where id in (select id from game where randomnumbers in (select randomnumbers from game where id=27));
113
114          */
115          echo "<p>Most extra points (doko, fox, karlchen) in a single game:<br />\n";
116          $result = mysql_query("SELECT COUNT(*) as c,fullname FROM Score".
117                                " LEFT JOIN User ON User.id=winner_id" .
118                                " WHERE score IN ('fox','doko','karlchen')".
119                                " GROUP BY game_id,fullname".
120                                " ORDER BY c DESC LIMIT 3" );
121          while( $r = mysql_fetch_array($result,MYSQL_NUM))
122            echo $r[1]." (".$r[0].") <br />\n";
123          echo "</p>\n";
124
125          /* longest and shortest game */
126          $r=mysql_query("SELECT timediff(mod_date,create_date) ,session,id".
127                         " FROM Game WHERE status='gameover'".
128                         " ORDER BY time_to_sec(timediff(mod_date,create_date)) ASC LIMIT 1");
129
130          if($r)
131            {
132              $short= mysql_fetch_array($r,MYSQL_NUM);
133              $names = DB_get_all_names_by_gameid($short[2]);
134              echo "<p> The shortest game took only ".$short[0]." hours and was played by  ".join(", ",$names).".<br />\n";
135            }
136
137          $r=mysql_query("SELECT datediff(mod_date,create_date) ,session,id".
138                         " FROM Game WHERE status='gameover'".
139                         " ORDER BY time_to_sec(timediff(mod_date,create_date)) DESC LIMIT 1");
140          if($r)
141            {
142              $long= mysql_fetch_array($r,MYSQL_NUM);
143              echo "The longest game took ".$long[0]." days.</p>\n";
144            }
145
146          $r=mysql_query("SELECT COUNT(*) as c, session, id FROM Game ".
147                         " GROUP BY session ORDER BY c DESC LIMIT 1");
148          if($r)
149            {
150              $long  = mysql_fetch_array($r,MYSQL_NUM);
151              $names = DB_get_all_names_by_gameid($long[2]);
152              echo "The longest session is session ".$long[1]." with ".$long[0].
153                " games played by ".join(", ",$names).".</p>\n";
154            }
155
156          /* most reminders */
157          echo "<p>These players got the most reminders:<br />\n";
158          $result = mysql_query("SELECT COUNT(*) as c,fullname from Reminder".
159                                " LEFT JOIN User ON User.id=user_id".
160                                " GROUP BY user_id".
161                                " ORDER BY c DESC LIMIT 3" );
162          while( $r = mysql_fetch_array($result,MYSQL_NUM))
163            echo $r[1]." (".$r[0].") <br />\n";
164          echo "</p>\n";
165
166          /* fox */
167          echo "<p>These players caught the most foxes:<br />\n";
168          $result = mysql_query("SELECT COUNT(*) as c,fullname from Score".
169                                " LEFT JOIN User ON User.id=winner_id".
170                                " WHERE score='fox'".
171                                " GROUP BY winner_id".
172                                " ORDER BY c DESC LIMIT 2" );
173          while( $r = mysql_fetch_array($result,MYSQL_NUM))
174            echo $r[1]." (".$r[0].") <br />\n";
175          echo "</p>\n";
176
177          echo "<p>These players lost their fox most often:<br />\n";
178          $result = mysql_query("SELECT COUNT(*) as c,fullname from Score".
179                                " LEFT JOIN User ON User.id=looser_id".
180                                " WHERE score='fox'".
181                                " GROUP BY looser_id".
182                                " ORDER BY c DESC LIMIT 2" );
183          while( $r = mysql_fetch_array($result,MYSQL_NUM))
184            echo $r[1]." (".$r[0].") <br />\n";
185          echo "</p>\n";
186
187          /* which position wins the most tricks  */
188          echo "<p>Which positions at the table make the most tricks:<br />\n";
189          $result = mysql_query("SELECT COUNT(*) AS c,winner FROM Trick".
190                                " GROUP BY winner".
191                                " ORDER BY winner ASC " );
192          $r = mysql_fetch_array($result,MYSQL_NUM);
193          if($r[1]==NULL) /* ongoing games, no winner yet */
194            $r = mysql_fetch_array($result,MYSQL_NUM);
195          echo " left ".$r[0]." <br />\n";
196          $r = mysql_fetch_array($result,MYSQL_NUM);
197          echo " top ".$r[0]." <br />\n";
198          $r = mysql_fetch_array($result,MYSQL_NUM);
199          echo " right ".$r[0]." <br />\n";
200          $r = mysql_fetch_array($result,MYSQL_NUM);
201          echo " bottom ".$r[0]." <br />\n";
202          echo "</p>\n";
203
204          /*
205           does the party win more often if they start
206
207          echo "<p>The party playing first wins in";
208          $result = mysql_query("SELECT COUNT(*) from Score".
209                                " LEFT JOIN Game ON Game.id=game_id".
210                                " WHERE score='againstqueens'".
211                                " AND Game.status='gameover'".
212                                " AND Game.type<>'solo'");
213          while( $r = mysql_fetch_array($result,MYSQL_NUM))
214            echo $r[1]." (".$r[0].") <br />\n";
215          echo " games</p>\n";
216          */
217
218
219          /*
220           how often is the last trick a non-trump trick
221          */
222
223          echo "</div>\n"; /* end output */
224        }
225    }
226  else
227    {
228      /* send them back to the login page */
229     echo "<div class=\"message\"><span class=\"bigger\">You need to log in!</span><br />\n".
230       "(<a href=\"$host\">This will take you back to the login-page</a>)</div>";
231    }
232
233 output_footer();
234 DB_close();
235
236 /*
237  *Local Variables:
238  *mode: php
239  *mode: hs-minor
240  *End:
241  */
242 ?>
243
244